Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update payout schema

Steven Teycf4ac148a88a
diff --git a/apps/web/lib/api/sales/payout.ts b/apps/web/lib/api/sales/payout.ts
index 1bb2814252..7073fcb1e4 100644
--- a/apps/web/lib/api/sales/payout.ts
+++ b/apps/web/lib/api/sales/payout.ts
@@ -57,7 +57,6 @@ export const createPartnerPayouts = async ({
select: { minimumPayout: true },
});
- const payoutFee = 0; // TODO: Implement payout fee
const currentDate = new Date();
const periodStart = new Date(
currentDate.getFullYear(),
@@ -86,15 +85,18 @@ export const createPartnerPayouts = async ({
return;
}
+ const amount = commissionEarnedTotal;
+ const fee = Math.min(amount * 0.02 + 100, 2000); // TODO: [payouts] tailor based on US / non-US
+
// Create the payout
const payout = await tx.payout.create({
data: {
id: createId({ prefix: "" }),
programId,
partnerId,
- payoutFee,
- total: commissionEarnedTotal,
- netTotal: commissionEarnedTotal - payoutFee,
+ amount,
+ fee,
+ total: amount + fee,
currency: "USD",
status: "pending",
periodStart,
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index 050481810d..c37e48dce9 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -87,9 +87,9 @@ export const payoutsQuerySchema = z
export const PayoutSchema = z.object({
id: z.string(),
+ amount: z.number(),
+ fee: z.number(),
total: z.number(),
- payoutFee: z.number().nullable(),
- netTotal: z.number(),
currency: z.string(),
status: z.nativeEnum(PayoutStatus),
periodStart: z.date(),
diff --git a/apps/web/prisma/schema/payout.prisma b/apps/web/prisma/schema/payout.prisma
index 29943ba94c..945fc1573c 100644
--- a/apps/web/prisma/schema/payout.prisma
+++ b/apps/web/prisma/schema/payout.prisma
@@ -17,9 +17,9 @@ model Payout {
id String @id @default(cuid())
programId String
partnerId String
- total Int
- payoutFee Int?
- netTotal Int // Total amount after fees (total - payoutFee)
+ amount Int @default(0)
+ fee Int @default(0)
+ total Int @default(0) // Total amount after fees (amount + fee)
currency String @default("USD")
status PayoutStatus @default(pending)
periodStart DateTime
diff --git a/apps/web/scripts/create-payouts.ts b/apps/web/scripts/create-payouts.ts
index c67c501479..3246edf5d4 100644
--- a/apps/web/scripts/create-payouts.ts
+++ b/apps/web/scripts/create-payouts.ts
@@ -1,10 +1,9 @@
+import { createId } from "@/lib/api/utils";
import { prisma } from "@/lib/prisma";
import "dotenv-flow/config";
async function main() {
- const userId = "cm1ypncqa0000tc44pfgxp6qs";
const programId = "prog_CYCu7IMAapjkRpTnr8F1azjN";
- const linkId = "cm2q86tm4000hgopgg0b54jfi";
const partnerId = "pn_DlsZeePb38RVcnrfbD0SrKzB";
// // Create a partner account
@@ -28,29 +27,32 @@ async function main() {
await prisma.payout.createMany({
data: [
{
+ id: createId({ prefix: "po_" }),
programId,
partnerId,
- total: 1000,
- payoutFee: 0,
- netTotal: 1000,
+ amount: 1000,
+ fee: 120,
+ total: 1120,
periodStart: new Date("2024-01-01"),
periodEnd: new Date("2024-01-31"),
},
{
+ id: createId({ prefix: "po_" }),
programId,
partnerId,
- total: 2000,
- payoutFee: 0,
- netTotal: 2000,
+ amount: 2000,
+ fee: 140,
+ total: 2140,
periodStart: new Date("2024-02-01"),
periodEnd: new Date("2024-02-28"),
},
{
+ id: createId({ prefix: "po_" }),
programId,
partnerId,
- total: 3000,
- payoutFee: 0,
- netTotal: 3000,
+ amount: 3000,
+ fee: 160,
+ total: 3160,
periodStart: new Date("2024-03-01"),
periodEnd: new Date("2024-03-31"),
},