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
50 changes: 35 additions & 15 deletions apps/web/lib/rewardful/import-commissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { sendEmail } from "@dub/email";
import ProgramImported from "@dub/email/templates/program-imported";
import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
import { CommissionStatus, Program } from "@prisma/client";
import { CommissionStatus, Customer, Link, Program } from "@prisma/client";
import { convertCurrencyWithFxRates } from "../analytics/convert-currency";
import { createId } from "../api/create-id";
import { syncTotalCommissions } from "../api/partners/sync-total-commissions";
import { getLeadEvent } from "../tinybird";
import { getLeadEvents } from "../tinybird/get-lead-events";
import { logImportError } from "../tinybird/log-import-error";
import { recordSaleWithTimestamp } from "../tinybird/record-sale";
import { LeadEventTB } from "../types";
import { redis } from "../upstash";
import { clickEventSchemaTB } from "../zod/schemas/clicks";
import { RewardfulApi } from "./api";
Expand Down Expand Up @@ -51,6 +52,23 @@ export async function importCommissions(payload: RewardfulImportPayload) {
break;
}

const customersData = await prisma.customer.findMany({
where: {
stripeCustomerId: {
in: commissions
.map((commission) => commission.sale.referral.stripe_customer_id)
.filter(Boolean),
},
},
include: {
link: true,
},
});

const customerLeadEvents = await getLeadEvents({
customerIds: customersData.map((customer) => customer.id),
}).then((res) => res.data);

await Promise.all(
commissions.map((commission) =>
createCommission({
Expand All @@ -59,6 +77,8 @@ export async function importCommissions(payload: RewardfulImportPayload) {
campaignId,
fxRates,
importId,
customersData,
customerLeadEvents,
}),
),
);
Expand Down Expand Up @@ -115,12 +135,16 @@ async function createCommission({
campaignId,
fxRates,
importId,
customersData,
customerLeadEvents,
}: {
commission: RewardfulCommission;
program: Program;
campaignId: string;
fxRates: Record<string, string> | null;
importId: string;
customersData: (Customer & { link: Link | null })[];
customerLeadEvents: LeadEventTB[];
}) {
const commonImportLogInputs = {
workspace_id: program.workspaceId,
Expand Down Expand Up @@ -223,14 +247,10 @@ async function createCommission({
return;
}

const customerFound = await prisma.customer.findUnique({
where: {
stripeCustomerId: sale.referral.stripe_customer_id,
},
include: {
link: true,
},
});
const customerFound = customersData.find(
(customer) =>
customer.stripeCustomerId === sale.referral.stripe_customer_id,
);

if (!customerFound) {
await logImportError({
Expand Down Expand Up @@ -280,11 +300,11 @@ async function createCommission({
return;
}

const leadEvent = await getLeadEvent({
customerId: customerFound.id,
});
const leadEvent = customerLeadEvents.find(
(event) => event.customer_id === customerFound.id,
);

if (!leadEvent || leadEvent.data.length === 0) {
if (!leadEvent) {
await logImportError({
...commonImportLogInputs,
entity: "commission",
Expand All @@ -298,7 +318,7 @@ async function createCommission({

const clickData = clickEventSchemaTB
.omit({ timestamp: true })
.parse(leadEvent.data[0]);
.parse(leadEvent);

const eventId = nanoid(16);

Expand Down
11 changes: 11 additions & 0 deletions apps/web/lib/tinybird/get-lead-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import z from "../zod";
import { leadEventSchemaTB } from "../zod/schemas/leads";
import { tb } from "./client";

export const getLeadEvents = tb.buildPipe({
pipe: "get_lead_events",
parameters: z.object({
customerIds: z.string().array(),
}),
data: leadEventSchemaTB,
});
9 changes: 1 addition & 8 deletions apps/web/scripts/backfill-customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@ import { prisma } from "@dub/prisma";
import { Prisma } from "@dub/prisma/client";
import "dotenv-flow/config";
import { tb } from "../lib/tinybird/client";
import { getLeadEvents } from "../lib/tinybird/get-lead-events";
import z from "../lib/zod";

export const getLeadEvents = tb.buildPipe({
pipe: "get_lead_events",
parameters: z.object({
customerIds: z.array(z.string()),
}),
data: z.any(),
});

export const getClickEvents = tb.buildPipe({
pipe: "get_click_events",
parameters: z.object({
Expand Down
2 changes: 1 addition & 1 deletion packages/email/src/templates/program-imported.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default function ProgramImported({
<Text className="mt-4 text-sm leading-6 text-black">
You can{" "}
<Link
href={`https://app.dub.co/api/${workspace.slug}/import/${importId}/download`}
href={`https://app.dub.co/api/workspaces/${workspace.slug}/import/${importId}/download`}
className="font-medium text-blue-600 no-underline"
target="_blank"
rel="noopener noreferrer"
Expand Down
16 changes: 16 additions & 0 deletions pipes/get_lead_events.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
TOKEN "get_lead_events_endpoint_read_0995" READ

NODE endpoint
SQL >

%
SELECT *
FROM dub_lead_events
WHERE
true
{% if defined(customerIds) %} AND customer_id IN {{ Array(customerIds, 'String') }} {% end %}
ORDER BY timestamp DESC

TYPE endpoint