|
| 1 | +import { prisma } from "@dub/prisma"; |
| 2 | +import { getLeadEvents } from "../tinybird/get-lead-events"; |
| 3 | +import { redis } from "../upstash"; |
| 4 | +import { PartnerStackApi } from "./api"; |
| 5 | +import { createCommissionFromPS } from "./import-commissions"; |
| 6 | +import { MAX_BATCHES, partnerStackImporter } from "./importer"; |
| 7 | +import { PartnerStackImportPayload } from "./types"; |
| 8 | + |
| 9 | +export async function importScheduledCommissions( |
| 10 | + payload: PartnerStackImportPayload, |
| 11 | +) { |
| 12 | + const { importId, programId, startingAfter } = payload; |
| 13 | + |
| 14 | + const program = await prisma.program.findUniqueOrThrow({ |
| 15 | + where: { |
| 16 | + id: programId, |
| 17 | + }, |
| 18 | + }); |
| 19 | + |
| 20 | + const { publicKey, secretKey } = await partnerStackImporter.getCredentials( |
| 21 | + program.workspaceId, |
| 22 | + ); |
| 23 | + |
| 24 | + const partnerStackApi = new PartnerStackApi({ |
| 25 | + publicKey, |
| 26 | + secretKey, |
| 27 | + }); |
| 28 | + |
| 29 | + const fxRates = await redis.hgetall<Record<string, string>>("fxRates:usd"); |
| 30 | + |
| 31 | + let hasMore = true; |
| 32 | + let processedBatches = 0; |
| 33 | + let currentStartingAfter = startingAfter; |
| 34 | + |
| 35 | + while (hasMore && processedBatches < MAX_BATCHES) { |
| 36 | + const scheduledCommissions = await partnerStackApi.listCommissions({ |
| 37 | + status: "scheduled", |
| 38 | + startingAfter: currentStartingAfter, |
| 39 | + }); |
| 40 | + |
| 41 | + if (scheduledCommissions.length === 0) { |
| 42 | + hasMore = false; |
| 43 | + break; |
| 44 | + } |
| 45 | + |
| 46 | + const customersData = await prisma.customer.findMany({ |
| 47 | + where: { |
| 48 | + projectId: program.workspaceId, |
| 49 | + OR: [ |
| 50 | + { |
| 51 | + email: { |
| 52 | + in: scheduledCommissions |
| 53 | + .map((commission) => commission.customer?.email) |
| 54 | + .filter( |
| 55 | + (email): email is string => |
| 56 | + email !== null && email !== undefined, |
| 57 | + ), |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + externalId: { |
| 62 | + in: scheduledCommissions |
| 63 | + .map((commission) => commission.customer?.external_key) |
| 64 | + .filter( |
| 65 | + (externalKey): externalKey is string => |
| 66 | + externalKey !== null && externalKey !== undefined, |
| 67 | + ), |
| 68 | + }, |
| 69 | + }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + include: { |
| 73 | + link: true, |
| 74 | + }, |
| 75 | + orderBy: { |
| 76 | + createdAt: "asc", |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + const customerLeadEvents = await getLeadEvents({ |
| 81 | + customerIds: customersData.map((customer) => customer.id), |
| 82 | + }).then((res) => res.data); |
| 83 | + |
| 84 | + await Promise.allSettled( |
| 85 | + scheduledCommissions.map((commission) => |
| 86 | + createCommissionFromPS({ |
| 87 | + program, |
| 88 | + commission, |
| 89 | + fxRates, |
| 90 | + importId, |
| 91 | + customersData, |
| 92 | + customerLeadEvents, |
| 93 | + }), |
| 94 | + ), |
| 95 | + ); |
| 96 | + |
| 97 | + currentStartingAfter = |
| 98 | + scheduledCommissions[scheduledCommissions.length - 1].key; |
| 99 | + processedBatches++; |
| 100 | + } |
| 101 | + |
| 102 | + // If there are more scheduled commissions to import, sleep for 1 second and continue the loop |
| 103 | + if (hasMore) { |
| 104 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 105 | + } else { |
| 106 | + // else, delete the credentials and exit the loop |
| 107 | + await partnerStackImporter.deleteCredentials(program.workspaceId); |
| 108 | + } |
| 109 | + |
| 110 | + await partnerStackImporter.queue({ |
| 111 | + ...payload, |
| 112 | + startingAfter: hasMore ? currentStartingAfter : undefined, |
| 113 | + action: hasMore |
| 114 | + ? "import-scheduled-commissions" |
| 115 | + : "update-stripe-customers", |
| 116 | + }); |
| 117 | +} |
0 commit comments