-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix acceptProgramInviteAction + backfillLinkData
- Loading branch information
1 parent
2a63789
commit b637959
Showing
7 changed files
with
151 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { getEvents } from "@/lib/analytics/get-events"; | ||
import { createSaleData } from "@/lib/api/sales/sale"; | ||
import { prisma } from "@/lib/prisma"; | ||
import { recordLink } from "@/lib/tinybird"; | ||
import z from "@/lib/zod"; | ||
import { saleEventResponseSchema } from "@/lib/zod/schemas/sales"; | ||
|
||
export const backfillLinkData = async (programEnrollmentId: string) => { | ||
const programEnrollment = await prisma.programEnrollment.findUniqueOrThrow({ | ||
where: { | ||
id: programEnrollmentId, | ||
}, | ||
include: { | ||
program: { | ||
include: { | ||
workspace: true, | ||
}, | ||
}, | ||
link: { | ||
include: { | ||
tags: true, | ||
}, | ||
}, | ||
partner: true, | ||
}, | ||
}); | ||
|
||
const { program, link, partner } = programEnrollment; | ||
const workspace = program.workspace; | ||
|
||
if (!link) { | ||
console.warn( | ||
`No link found for program enrollment ${programEnrollment.id}`, | ||
); | ||
return; | ||
} | ||
|
||
const saleEvents = await getEvents({ | ||
workspaceId: workspace.id, | ||
linkId: link.id, | ||
event: "sales", | ||
interval: "all", | ||
page: 1, | ||
limit: 5000, | ||
order: "desc", | ||
sortBy: "timestamp", | ||
}); | ||
|
||
const data = saleEvents.map((e: z.infer<typeof saleEventResponseSchema>) => ({ | ||
...createSaleData({ | ||
customerId: e.customer.id, | ||
linkId: e.link.id, | ||
clickId: e.click.id, | ||
invoiceId: e.invoice_id, | ||
eventId: e.eventId, | ||
paymentProcessor: e.payment_processor, | ||
amount: e.sale.amount, | ||
currency: "usd", | ||
partnerId: partner.id, | ||
program, | ||
metadata: e.click, | ||
}), | ||
createdAt: new Date(e.timestamp), | ||
})); | ||
|
||
return await Promise.all([ | ||
// Backfill sales for the partner's link | ||
data.length > 0 && | ||
prisma.sale.createMany({ | ||
data, | ||
skipDuplicates: true, | ||
}), | ||
// Add link's programId to dub_links_metadata in Tinybird | ||
recordLink({ | ||
domain: link.domain, | ||
key: link.key, | ||
link_id: link.id, | ||
created_at: link.createdAt, | ||
url: link.url, | ||
tag_ids: link.tags.map((t) => t.id) || [], | ||
program_id: program.id, | ||
workspace_id: workspace.id, | ||
deleted: false, | ||
}), | ||
]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { createDotsUser } from "@/lib/dots/create-dots-user"; | ||
import { retrieveDotsUser } from "@/lib/dots/retrieve-dots-user"; | ||
import { prisma } from "@/lib/prisma"; | ||
import { PartnerProps } from "@/lib/types"; | ||
|
||
export const enrollDotsUserApp = async ({ | ||
partner, | ||
dotsAppId, | ||
programEnrollmentId, | ||
}: { | ||
partner: PartnerProps; | ||
dotsAppId: string; | ||
programEnrollmentId: string; | ||
}) => { | ||
if (!partner.dotsUserId) { | ||
console.warn(`Partner ${partner.id} has no Dots user ID`); | ||
return; | ||
} | ||
|
||
const dotsUser = await retrieveDotsUser(partner); | ||
|
||
const newDotsUser = await createDotsUser({ | ||
dotsAppId, // we need to create a new Dots user under the Program's Dots App | ||
userInfo: { | ||
firstName: dotsUser.first_name, | ||
lastName: dotsUser.last_name, | ||
email: dotsUser.email, | ||
countryCode: dotsUser.phone_number.country_code, | ||
phoneNumber: dotsUser.phone_number.phone_number, | ||
}, | ||
}); | ||
|
||
return await prisma.programEnrollment.update({ | ||
where: { | ||
id: programEnrollmentId, | ||
}, | ||
data: { dotsUserId: newDotsUser.id }, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters