Skip to content

Commit

Permalink
chore(voucher): adding fees to db
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhart1o1 committed May 17, 2024
1 parent 37fa7a4 commit e855dae
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
5 changes: 3 additions & 2 deletions apps/voucher/components/create/confirm-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ const ConfirmModal = ({
)

const platformFeesInPercentage = convertPpmToPercentage({ ppm: platformFeesInPpm })
const platformFeesAmount = amountCalculator.profitAmount({

const platformFeesAmount = amountCalculator.platformFeesAmount({
voucherPrice: Number(amount),
commissionPercentage: platformFeesInPercentage,
platformFeesInPpm: platformFeesInPpm,
})

const totalPaying = amountCalculator.voucherAmountAfterCommission({
Expand Down
7 changes: 6 additions & 1 deletion apps/voucher/graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ const resolvers = {
voucherAmount: voucherAmountInCents,
platformFeesInPpm,
})

const platformFeeInCents = amountCalculator.platformFeesAmount({
voucherPrice: salesAmountInCents,
platformFeesInPpm,
})
if (salesAmountInCents <= 0) return new Error("Invalid sales amount")

const userWalletDetails = getWalletDetailsFromWalletId({
Expand Down Expand Up @@ -140,6 +143,7 @@ const resolvers = {
voucherAmountInCents,
salesAmountInCents,
userId: userData.me.id,
platformFee: platformFeeInCents,
})

if (createWithdrawLinkResponse instanceof Error) return createWithdrawLinkResponse
Expand Down Expand Up @@ -181,6 +185,7 @@ const resolvers = {
voucherAmountInCents,
salesAmountInCents,
userId: userData.me.id,
platformFee: platformFeeInCents,
})

if (createWithdrawLinkResponse instanceof Error) return createWithdrawLinkResponse
Expand Down
10 changes: 10 additions & 0 deletions apps/voucher/lib/amount-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ export const amountCalculator = {
const result = voucherPrice - commissionAmount
return Math.max(result, 0)
},
platformFeesAmount({
voucherPrice,
platformFeesInPpm,
}: {
voucherPrice: number
platformFeesInPpm: number
}): number {
const platformFees = voucherPrice * (platformFeesInPpm / 1000000)
return Math.max(platformFees, 0)
},
profitAmount({
voucherPrice,
commissionPercentage,
Expand Down
1 change: 1 addition & 0 deletions apps/voucher/services/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export async function createWithdrawLinkMutation(input: {
voucherAmountInCents: number
salesAmountInCents: number
commissionPercentage: number
platformFee: number
}): Promise<WithdrawLink | Error> {
try {
let identifierCode = generateCode(6)
Expand Down
1 change: 1 addition & 0 deletions apps/voucher/services/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ export const createWithdrawLinksTable = async () => {
.unsigned()
.defaultTo(0)
.comment("min = 0 and max = 100")
table.decimal("platformFee").notNullable().unsigned().defaultTo(0)
})
}

0 comments on commit e855dae

Please sign in to comment.