The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fix importer link not found errors
Steven Tey9d8af18895b4
diff --git a/apps/web/lib/rewardful/import-customers.ts b/apps/web/lib/rewardful/import-customers.tsindex 3528fcafe5..bb9860e5a5 100644--- a/apps/web/lib/rewardful/import-customers.ts+++ b/apps/web/lib/rewardful/import-customers.ts@@ -116,12 +116,12 @@ async function createCustomer({return;}- const link = await prisma.link.findUnique({+ // here we're using findFirst because for some reason findUnique uses a weird collation+ // that causes a bunch of LINK_NOT_FOUND errors (for links/coupons that actually exist)+ const link = await prisma.link.findFirst({where: {- domain_key: {- domain: program.domain!,- key: shortLinkToken,- },+ domain: program.domain!,+ key: shortLinkToken,},});diff --git a/apps/web/scripts/migrations/backfill-customer-first-sale.ts b/apps/web/scripts/migrations/backfill-customer-first-sale.tsindex bf50c2554e..9c62daa614 100644--- a/apps/web/scripts/migrations/backfill-customer-first-sale.ts+++ b/apps/web/scripts/migrations/backfill-customer-first-sale.ts@@ -16,45 +16,61 @@ export const getFirstSaleEvents = tb.buildPipe({});async function main() {- const customerWithSales = await prisma.customer.findMany({- where: {- sales: {- gt: 0,+ while (true) {+ const customerWithSales = await prisma.customer.findMany({+ where: {+ sales: {+ gt: 0,+ },+ firstSaleAt: null,},- firstSaleAt: null,- },- take: 5000,- });-- let updated = 0;-- const chunks = chunk(customerWithSales, 50);-- for (const chunk of chunks) {- const firstSaleEvents = await getFirstSaleEvents({- customerIds: chunk.map((customer) => customer.id),- }).then((res) => res.data);-- await Promise.all(- firstSaleEvents.map(async (event) => {- const res = await prisma.customer.update({- where: { id: event.customerId },- data: {- firstSaleAt: new Date(event.firstSaleAt),- },- });- console.log({- id: res.id,- firstSaleAt: res.firstSaleAt,- });- updated++;- }),- );- }+ take: 5000,+ });++ if (customerWithSales.length === 0) {+ console.log("No customers left to backfill");+ break;+ }++ let updated = 0;++ const chunks = chunk(customerWithSales, 100);- console.log(- `Updated ${updated}/${customerWithSales.length} customers (${(updated / customerWithSales.length) * 100}%)`,- );+ for (const chunk of chunks) {+ const firstSaleEvents = await getFirstSaleEvents({+ customerIds: chunk.map((customer) => customer.id),+ }).then((res) => res.data);++ await Promise.all(+ chunk.map(async (customer) => {+ const firstSaleEvent = firstSaleEvents+ .filter((event) => event.customerId === customer.id)+ .sort(+ (a, b) =>+ new Date(a.firstSaleAt).getTime() -+ new Date(b.firstSaleAt).getTime(),+ )[0];++ if (!firstSaleEvent) {+ return;+ }+ try {+ await prisma.customer.update({+ where: { id: customer.id },+ data: {+ firstSaleAt: new Date(firstSaleEvent.firstSaleAt),+ },+ });+ updated++;+ } catch (_e) {}+ }),+ );++ console.log(+ `Updated ${updated}/${customerWithSales.length} customers (${(updated / customerWithSales.length) * 100}%)`,+ );+ }+ }}main();