-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from brolag/feature/code-cleanup
Feature/code cleanup
- Loading branch information
Showing
10 changed files
with
416 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,7 @@ yarn-error.log* | |
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts | ||
|
||
# instructions | ||
instructions/ | ||
instructions/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import type { | ||
Escrow, | ||
EscrowPayload, | ||
BalanceItem, | ||
} from "@/@types/escrow.entity"; | ||
import { | ||
getAllEscrowsByUser, | ||
updateEscrow, | ||
addEscrow, | ||
} from "../server/escrow.firebase"; | ||
import { getBalance } from "./get-balance.service"; | ||
|
||
export const fetchAllEscrows = async ({ | ||
address, | ||
type = "approver", | ||
}: { | ||
address: string; | ||
type: string; | ||
}): Promise<Escrow[]> => { | ||
const escrowsByUser = await getAllEscrowsByUser({ address, type }); | ||
const contractIds = escrowsByUser.data.map( | ||
(escrow: Escrow) => escrow.contractId, | ||
); | ||
|
||
if (!Array.isArray(contractIds)) { | ||
throw new Error("contractIds is not a valid array."); | ||
} | ||
|
||
const response = await getBalance(address, contractIds); | ||
const balances = response.data as BalanceItem[]; | ||
|
||
return Promise.all( | ||
escrowsByUser.data.map(async (escrow: Escrow) => { | ||
const matchedBalance = balances.find( | ||
(item) => item.address === escrow.contractId, | ||
); | ||
|
||
const plainBalance = matchedBalance ? matchedBalance.balance : 0; | ||
const currentBalance = escrow.balance ? Number(escrow.balance) : 0; | ||
|
||
if (currentBalance !== plainBalance) { | ||
await updateEscrow({ | ||
escrowId: escrow.id, | ||
payload: { balance: String(plainBalance) }, | ||
}); | ||
escrow.balance = String(plainBalance); | ||
} | ||
|
||
return escrow; | ||
}), | ||
); | ||
}; | ||
|
||
export const addNewEscrow = async ( | ||
payload: EscrowPayload, | ||
address: string, | ||
contractId: string, | ||
): Promise<Escrow | undefined> => { | ||
const response = await addEscrow({ payload, address, contractId }); | ||
return response?.data; | ||
}; | ||
|
||
export const updateExistingEscrow = async ({ | ||
escrowId, | ||
payload, | ||
}: { | ||
escrowId: string; | ||
payload: Partial<EscrowPayload>; | ||
}): Promise<Escrow | undefined> => { | ||
const response = await updateEscrow({ | ||
escrowId, | ||
payload: { ...payload, balance: String(payload.balance || 0) }, | ||
}); | ||
return response?.data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.