Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
recordLead,
recordSale,
} from "@/lib/tinybird";
import { ClickEventTB, LeadEventTB } from "@/lib/types";
import { ClickEventTB, LeadEventTB, WebhookPartner } from "@/lib/types";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import {
Expand Down Expand Up @@ -350,14 +350,15 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
]);

// for program links
let webhookPartner: WebhookPartner | undefined;
if (link && link.programId && link.partnerId) {
const productId = await getSubscriptionProductId({
stripeSubscriptionId: charge.subscription as string,
stripeAccountId,
livemode: event.livemode,
});

await createPartnerCommission({
const createdCommission = await createPartnerCommission({
event: "sale",
programId: link.programId,
partnerId: link.partnerId,
Expand All @@ -377,6 +378,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
},
},
});
webhookPartner = createdCommission?.webhookPartner;

waitUntil(
Promise.allSettled([
Expand Down Expand Up @@ -423,6 +425,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
eventName: "Checkout session completed",
link: linkUpdated,
customer,
partner: webhookPartner,
metadata: null,
}),
});
Expand All @@ -437,6 +440,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
clickedAt: customer.clickedAt || customer.createdAt,
link: linkUpdated,
customer,
partner: webhookPartner,
metadata: null,
}),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { includeTags } from "@/lib/api/links/include-tags";
import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
import { WebhookPartner } from "@/lib/types";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { transformSaleEventData } from "@/lib/webhook/transform";
Expand Down Expand Up @@ -203,8 +204,9 @@ export async function invoicePaid(event: Stripe.Event) {
]);

// for program links
let webhookPartner: WebhookPartner | undefined;
if (link.programId && link.partnerId) {
await createPartnerCommission({
const createdCommission = await createPartnerCommission({
event: "sale",
programId: link.programId,
partnerId: link.partnerId,
Expand All @@ -224,6 +226,7 @@ export async function invoicePaid(event: Stripe.Event) {
},
},
});
webhookPartner = createdCommission?.webhookPartner;

waitUntil(
executeWorkflows({
Expand All @@ -250,6 +253,7 @@ export async function invoicePaid(event: Stripe.Event) {
clickedAt: customer.clickedAt || customer.createdAt,
link: linkUpdated,
customer,
partner: webhookPartner,
metadata: null,
}),
}),
Expand Down
6 changes: 5 additions & 1 deletion apps/web/app/(ee)/api/stripe/integration/webhook/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { generateRandomName } from "@/lib/names";
import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
import { stripeAppClient } from "@/lib/stripe";
import { getClickEvent, recordLead } from "@/lib/tinybird";
import { WebhookPartner } from "@/lib/types";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { transformLeadEventData } from "@/lib/webhook/transform";
import { prisma } from "@dub/prisma";
Expand Down Expand Up @@ -100,8 +101,9 @@ export async function createNewCustomer(event: Stripe.Event) {
}),
]);

let webhookPartner: WebhookPartner | undefined;
if (link.programId && link.partnerId) {
await createPartnerCommission({
const createdCommission = await createPartnerCommission({
event: "lead",
programId: link.programId,
partnerId: link.partnerId,
Expand All @@ -115,6 +117,7 @@ export async function createNewCustomer(event: Stripe.Event) {
},
},
});
webhookPartner = createdCommission?.webhookPartner;
}

waitUntil(
Expand All @@ -127,6 +130,7 @@ export async function createNewCustomer(event: Stripe.Event) {
eventName,
link: linkUpdated,
customer,
partner: webhookPartner,
metadata: null,
}),
}),
Expand Down
2 changes: 1 addition & 1 deletion apps/web/lib/actions/partners/approve-bounty-submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const approveBountySubmissionAction = authActionClient
);
}

const commission = await createPartnerCommission({
const { commission } = await createPartnerCommission({
event: "custom",
partnerId: bountySubmission.partnerId,
programId: bountySubmission.programId,
Expand Down
8 changes: 6 additions & 2 deletions apps/web/lib/api/conversions/track-lead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createPartnerCommission } from "@/lib/partners/create-partner-commissio
import { isStored, storage } from "@/lib/storage";
import { getClickEvent, recordLead, recordLeadSync } from "@/lib/tinybird";
import { logConversionEvent } from "@/lib/tinybird/log-conversion-events";
import { ClickEventTB, WorkspaceProps } from "@/lib/types";
import { ClickEventTB, WebhookPartner, WorkspaceProps } from "@/lib/types";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { transformLeadEventData } from "@/lib/webhook/transform";
Expand Down Expand Up @@ -299,8 +299,10 @@ export const trackLead = async ({
]);
link = updatedLink; // update the link variable to the latest version

let webhookPartner: WebhookPartner | undefined;

if (link.programId && link.partnerId && customer) {
await createPartnerCommission({
const createdCommission = await createPartnerCommission({
event: "lead",
programId: link.programId,
partnerId: link.partnerId,
Expand All @@ -314,6 +316,7 @@ export const trackLead = async ({
},
},
});
webhookPartner = createdCommission?.webhookPartner;

await executeWorkflows({
trigger: WorkflowTrigger.leadRecorded,
Expand All @@ -334,6 +337,7 @@ export const trackLead = async ({
eventName,
link,
customer,
partner: webhookPartner,
metadata,
}),
workspace,
Expand Down
14 changes: 11 additions & 3 deletions apps/web/lib/api/conversions/track-sale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
recordSale,
} from "@/lib/tinybird";
import { logConversionEvent } from "@/lib/tinybird/log-conversion-events";
import { ClickEventTB, LeadEventTB, WorkspaceProps } from "@/lib/types";
import {
ClickEventTB,
LeadEventTB,
WebhookPartner,
WorkspaceProps,
} from "@/lib/types";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import {
Expand All @@ -30,7 +35,6 @@ import { waitUntil } from "@vercel/functions";
import { z } from "zod";
import { createId } from "../create-id";
import { executeWorkflows } from "../workflows/execute-workflows";

type TrackSaleParams = z.input<typeof trackSaleRequestSchema> & {
rawBody: any;
workspace: Pick<WorkspaceProps, "id" | "stripeConnectId" | "webhookEnabled">;
Expand Down Expand Up @@ -507,9 +511,10 @@ const _trackSale = async ({
}),
]);

let webhookPartner: WebhookPartner | undefined;
// Create partner commission and execute workflows
if (link.programId && link.partnerId) {
await createPartnerCommission({
const createdCommission = await createPartnerCommission({
event: "sale",
programId: link.programId,
partnerId: link.partnerId,
Expand All @@ -530,6 +535,8 @@ const _trackSale = async ({
},
});

webhookPartner = createdCommission?.webhookPartner;

await executeWorkflows({
trigger: WorkflowTrigger.saleRecorded,
context: {
Expand All @@ -549,6 +556,7 @@ const _trackSale = async ({
clickedAt: customer.clickedAt || customer.createdAt,
link,
customer,
partner: webhookPartner,
metadata,
});

Expand Down
2 changes: 1 addition & 1 deletion apps/web/lib/api/workflows/execute-award-bounty-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const executeAwardBountyAction = async ({
}

// Create the commission for the partner
const commission = await createPartnerCommission({
const { commission } = await createPartnerCommission({
event: "custom",
partnerId,
programId: bounty.programId,
Expand Down
35 changes: 19 additions & 16 deletions apps/web/lib/integrations/shopify/create-sale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { includeTags } from "@/lib/api/links/include-tags";
import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
import { recordSale } from "@/lib/tinybird";
import { LeadEventTB } from "@/lib/types";
import { LeadEventTB, WebhookPartner } from "@/lib/types";
import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { transformSaleEventData } from "@/lib/webhook/transform";
Expand Down Expand Up @@ -126,23 +126,10 @@ export async function createShopifySale({
redis.del(`shopify:checkout:${checkoutToken}`),
]);

waitUntil(
sendWorkspaceWebhook({
trigger: "sale.created",
workspace,
data: transformSaleEventData({
...saleData,
link,
clickedAt: customer.clickedAt || customer.createdAt,
customer,
metadata: null,
}),
}),
);

// for program links
let webhookPartner: WebhookPartner | undefined;
if (link.programId && link.partnerId) {
await createPartnerCommission({
const createdCommission = await createPartnerCommission({
event: "sale",
programId: link.programId,
partnerId: link.partnerId,
Expand All @@ -159,6 +146,7 @@ export async function createShopifySale({
},
},
});
webhookPartner = createdCommission?.webhookPartner;

waitUntil(
executeWorkflows({
Expand All @@ -174,4 +162,19 @@ export async function createShopifySale({
}),
);
}

waitUntil(
sendWorkspaceWebhook({
trigger: "sale.created",
workspace,
data: transformSaleEventData({
...saleData,
link,
clickedAt: customer.clickedAt || customer.createdAt,
customer,
partner: webhookPartner,
metadata: null,
}),
}),
);
}
Loading