Skip to content

Commit

Permalink
fix tests, record leads & sales in planetscale
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed May 26, 2024
1 parent 24ce664 commit df85b8a
Show file tree
Hide file tree
Showing 6 changed files with 101 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

0 comments on commit df85b8a

Please sign in to comment.