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.

Handle cases where the sale reward is a one-time reward

Kiran Kc82fd2147139
diff --git a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
index 501754185d..ca0d366f17 100644
--- a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
+++ b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
@@ -123,56 +123,73 @@ export async function invoicePaid(event: Stripe.Event) {
});
if (reward) {
- const earnings = calculateSaleEarnings({
- reward,
- sale: {
- quantity: 1,
- amount: saleData.amount,
- },
- });
-
- await prisma.commission.create({
- data: {
- id: createId({ prefix: "cm_" }),
- programId: link.programId,
- linkId: link.id,
- partnerId: link.partnerId,
- eventId,
- customerId: customer.id,
- quantity: 1,
- type: "sale",
- amount: saleData.amount,
- earnings,
- invoiceId,
- },
- });
-
- waitUntil(
- (async () => {
- const program = await prisma.program.findUniqueOrThrow({
- where: {
- id: link.programId!,
- },
- select: {
- id: true,
- name: true,
- logo: true,
- },
- });
-
- await notifyPartnerSale({
- program,
- partner: {
- id: link.partnerId!,
- referralLink: link.shortLink,
- },
- sale: {
- amount: saleData.amount,
- earnings,
- },
- });
- })(),
- );
+ let eligibleForCommission = true;
+
+ if (reward.maxDuration === 0) {
+ const commissionCount = await prisma.commission.count({
+ where: {
+ type: "sale",
+ customerId: customer.id,
+ },
+ });
+
+ if (commissionCount > 0) {
+ eligibleForCommission = false;
+ }
+ }
+
+ if (eligibleForCommission) {
+ const earnings = calculateSaleEarnings({
+ reward,
+ sale: {
+ quantity: 1,
+ amount: saleData.amount,
+ },
+ });
+
+ await prisma.commission.create({
+ data: {
+ id: createId({ prefix: "cm_" }),
+ programId: link.programId,
+ linkId: link.id,
+ partnerId: link.partnerId,
+ eventId,
+ customerId: customer.id,
+ quantity: 1,
+ type: "sale",
+ amount: saleData.amount,
+ earnings,
+ invoiceId,
+ },
+ });
+
+ waitUntil(
+ (async () => {
+ const program = await prisma.program.findUniqueOrThrow({
+ where: {
+ id: link.programId!,
+ },
+ select: {
+ id: true,
+ name: true,
+ logo: true,
+ },
+ });
+
+ await notifyPartnerSale({
+ program,
+ partner: {
+ id: link.partnerId!,
+ referralLink: link.shortLink,
+ },
+ sale: {
+ amount: saleData.amount,
+ earnings,
+ },
+ });
+ })(),
+ );
+ }
}
}
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index e73fc2bb5f..5a7e21198d 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -155,52 +155,69 @@ export const POST = withWorkspaceEdge(
});
if (reward) {
- const earnings = calculateSaleEarnings({
- reward,
- sale: {
- quantity: 1,
- amount: saleData.amount,
- },
- });
-
- await prismaEdge.commission.create({
- data: {
- id: createId({ prefix: "cm_" }),
- programId: link.programId,
- linkId: link.id,
- partnerId: link.partnerId,
- eventId,
- customerId: customer.id,
- quantity: 1,
- type: "sale",
- amount: saleData.amount,
- earnings,
- invoiceId,
- },
- });
+ let eligibleForCommission = true;
- const program = await prismaEdge.program.findUniqueOrThrow({
- where: {
- id: link.programId!,
- },
- select: {
- id: true,
- name: true,
- logo: true,
- },
- });
+ if (reward.maxDuration === 0) {
+ const commissionCount = await prismaEdge.commission.count({
+ where: {
+ type: "sale",
+ customerId: customer.id,
+ },
+ });
- await notifyPartnerSale({
- program,
- partner: {
- id: link.partnerId!,
- referralLink: link.shortLink,
- },
- sale: {
- amount: saleData.amount,
- earnings,
- },
- });
+ if (commissionCount > 0) {
+ eligibleForCommission = false;
+ }
+ }
+
+ if (eligibleForCommission) {
+ const earnings = calculateSaleEarnings({
+ reward,
+ sale: {
+ quantity: 1,
+ amount: saleData.amount,
+ },
+ });
+
+ await prismaEdge.commission.create({
+ data: {
+ id: createId({ prefix: "cm_" }),
+ programId: link.programId,
+ linkId: link.id,
+ partnerId: link.partnerId,
+ eventId,
+ customerId: customer.id,
+ quantity: 1,
+ type: "sale",
+ amount: saleData.amount,
+ earnings,
+ invoiceId,
+ },
+ });
+
+ const program = await prismaEdge.program.findUniqueOrThrow({
+ where: {
+ id: link.programId!,
+ },
+ select: {
+ id: true,
+ name: true,
+ logo: true,
+ },
+ });
+
+ await notifyPartnerSale({
+ program,
+ partner: {
+ id: link.partnerId!,
+ referralLink: link.shortLink,
+ },
+ sale: {
+ amount: saleData.amount,
+ earnings,
+ },
+ });
+ }
}
}
diff --git a/apps/web/lib/integrations/shopify/create-sale.ts b/apps/web/lib/integrations/shopify/create-sale.ts
index 68286aed76..f3f544cb7c 100644
--- a/apps/web/lib/integrations/shopify/create-sale.ts
+++ b/apps/web/lib/integrations/shopify/create-sale.ts
@@ -130,57 +130,74 @@ export async function createShopifySale({
});
if (reward) {
- const earnings = calculateSaleEarnings({
- reward,
- sale: {
- quantity: 1,
- amount: saleData.amount,
- },
- });
-
- await prisma.commission.create({
- data: {
- id: createId({ prefix: "cm_" }),
- programId: link.programId,
- linkId: link.id,
- partnerId: link.partnerId,
- eventId: saleData.event_id,
- customerId: customer.id,
- quantity: 1,
- type: "sale",
- amount,
- earnings,
- invoiceId,
- currency,
- },
- });
-
- waitUntil(
- (async () => {
- const program = await prisma.program.findUniqueOrThrow({
- where: {
- id: link.programId!,
- },
- select: {
- id: true,
- name: true,
- logo: true,
- },
- });
-
- await notifyPartnerSale({
- program,
- partner: {
- id: link.partnerId!,
- referralLink: link.shortLink,
- },
- sale: {
- amount: saleData.amount,
- earnings,
- },
- });
- })(),
- );
+ let eligibleForCommission = true;
+
+ if (reward.maxDuration === 0) {
+ const commissionCount = await prisma.commission.count({
+ where: {
+ type: "sale",
+ customerId: customer.id,
+ },
+ });
+
+ if (commissionCount > 0) {
+ eligibleForCommission = false;
+ }
+ }
+
+ if (eligibleForCommission) {
+ const earnings = calculateSaleEarnings({
+ reward,
+ sale: {
+ quantity: 1,
+ amount: saleData.amount,
+ },
+ });
+
+ await prisma.commission.create({
+ data: {
+ id: createId({ prefix: "cm_" }),
+ programId: link.programId,
+ linkId: link.id,
+ partnerId: link.partnerId,
+ eventId: saleData.event_id,
+ customerId: customer.id,
+ quantity: 1,
+ type: "sale",
+ amount,
+ earnings,
+ invoiceId,
+ currency,
+ },
+ });
+
+ waitUntil(
+ (async () => {
+ const program = await prisma.program.findUniqueOrThrow({
+ where: {
+ id: link.programId!,
+ },
+ select: {
+ id: true,
+ name: true,
+ logo: true,
+ },
+ });
+
+ await notifyPartnerSale({
+ program,
+ partner: {
+ id: link.partnerId!,
+ referralLink: link.shortLink,
+ },
+ sale: {
+ amount: saleData.amount,
+ earnings,
+ },
+ });
+ })(),
+ );
+ }
}
}
}