Skip to content

Commit

Permalink
Merge pull request dubinc#922 from dubinc/add-leads-sales
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey authored May 26, 2024
2 parents 8e15f29 + df85b8a commit 9d60701
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,28 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
}
}

// Record sale
await recordSale({
...leadEvent.data[0],
event_id: nanoid(16),
payment_processor: "stripe",
amount: charge.amount_total!,
currency: charge.currency!,
invoice_id: invoiceId || "",
metadata: JSON.stringify({
charge,
await Promise.all([
recordSale({
...leadEvent.data[0],
event_id: nanoid(16),
payment_processor: "stripe",
amount: charge.amount_total!,
currency: charge.currency!,
invoice_id: invoiceId || "",
metadata: JSON.stringify({
charge,
}),
}),
});
// update link sales count
prisma.link.update({
where: {
id: leadEvent.data[0].link_id,
},
data: {
sales: {
increment: 1,
},
},
}),
]);
}
12 changes: 12 additions & 0 deletions apps/web/app/api/stripe/connect/webhook/customer-created.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,17 @@ export async function customerCreated(event: Stripe.Event) {
event_name: "Customer created",
customer_id: customer.id,
}),

// update link leads count
prisma.link.update({
where: {
id: clickData.link_id,
},
data: {
leads: {
increment: 1,
},
},
}),
]);
}
34 changes: 23 additions & 11 deletions apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,28 @@ export async function invoicePaid(event: Stripe.Event) {
return;
}

// Record sale
await recordSale({
...leadEvent.data[0],
event_id: nanoid(16),
payment_processor: "stripe",
amount: invoice.amount_paid,
currency: invoice.currency,
invoice_id: invoiceId,
metadata: JSON.stringify({
invoice,
await Promise.all([
recordSale({
...leadEvent.data[0],
event_id: nanoid(16),
payment_processor: "stripe",
amount: invoice.amount_paid,
currency: invoice.currency,
invoice_id: invoiceId,
metadata: JSON.stringify({
invoice,
}),
}),
});
// update link sales count
prisma.link.update({
where: {
id: leadEvent.data[0].link_id,
},
data: {
sales: {
increment: 1,
},
},
}),
]);
}
12 changes: 12 additions & 0 deletions apps/web/app/api/track/lead/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ export const POST = withWorkspaceEdge(
customer_id: customer.id,
metadata: metadata ? JSON.stringify(metadata) : "",
}),

// update link leads count
prismaEdge.link.update({
where: {
id: clickData.link_id,
},
data: {
leads: {
increment: 1,
},
},
}),
]);

const response = trackLeadResponseSchema.parse({
Expand Down
33 changes: 23 additions & 10 deletions apps/web/app/api/track/sale/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,29 @@ export const POST = withWorkspaceEdge(
.omit({ timestamp: true })
.parse(leadEvent.data[0]);

await recordSale({
...clickData,
event_id: nanoid(16),
customer_id: customer.id,
payment_processor: paymentProcessor,
amount,
currency,
invoice_id: invoiceId || "",
metadata: metadata ? JSON.stringify(metadata) : "",
});
await Promise.all([
recordSale({
...clickData,
event_id: nanoid(16),
customer_id: customer.id,
payment_processor: paymentProcessor,
amount,
currency,
invoice_id: invoiceId || "",
metadata: metadata ? JSON.stringify(metadata) : "",
}),
// update link sales count
prismaEdge.link.update({
where: {
id: leadEvent.data[0].link_id,
},
data: {
sales: {
increment: 1,
},
},
}),
]);

const response = trackSaleResponseSchema.parse({
customerId: externalId,
Expand Down
8 changes: 8 additions & 0 deletions apps/web/lib/zod/schemas/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ export const LinkSchema = z
.string()
.nullable()
.describe("The date and time when the short link was last clicked."),
leads: z
.number()
.default(0)
.describe("[BETA]: The number of leads the short links has generated."),
sales: z
.number()
.default(0)
.describe("[BETA]: The number of sales the short links has generated."),
createdAt: z
.string()
.describe("The date and time when the short link was created."),
Expand Down
2 changes: 2 additions & 0 deletions apps/web/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ model Link {
publicStats Boolean @default(false) // whether to show public stats or not
clicks Int @default(0) // number of clicks
lastClicked DateTime? // when the link was last clicked
leads Int @default(0)
sales Int @default(0)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down
2 changes: 2 additions & 0 deletions apps/web/tests/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export const expectedLink: Partial<Link> & { tagId: string | null } = {
publicStats: false,
clicks: 0,
lastClicked: null,
leads: 0,
sales: 0,
tagId: null, // backwards compatibility
comments: null,
createdAt: expect.any(String),
Expand Down

0 comments on commit 9d60701

Please sign in to comment.