Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Update backfill-application-groupId.ts

Kiran Kc0630b658e42
diff --git a/apps/web/scripts/partners/backfill-application-groupId.ts b/apps/web/scripts/partners/backfill-application-groupId.ts
index 85594b0ca3..a953f7ef8e 100644
--- a/apps/web/scripts/partners/backfill-application-groupId.ts
+++ b/apps/web/scripts/partners/backfill-application-groupId.ts
@@ -13,10 +13,14 @@ async function main() {
console.table(programs);
- const programApplications = await prisma.programApplication.findMany({
+ const programApplications = await prisma.programApplication.groupBy({
+ by: ["programId"],
where: {
groupId: null,
},
+ _count: {
+ _all: true,
+ },
});
if (programApplications.length === 0) {
@@ -28,28 +32,31 @@ async function main() {
`Found ${programApplications.length} program applications to update`,
);
+ console.log(programApplications);
+
// Create a map of programId -> defaultGroupId
const programMap = new Map(
programs.map(({ id, defaultGroupId }) => [id, defaultGroupId]),
);
- await Promise.allSettled(
- programApplications.map(({ id, programId }) => {
- if (!programMap.has(programId)) {
- console.error(`defaultGroupId not found for ${programId}.`);
- return;
- }
-
- return prisma.programApplication.update({
- where: {
- id,
- },
- data: {
- groupId: programMap.get(programId),
- },
- });
- }),
- );
+ const toUpdate = programApplications.map(({ programId, _count }) => ({
+ programId,
+ count: _count._all,
+ groupId: programMap.get(programId),
+ }));
+
+ console.table(toUpdate);
+
+ for (const { programId, groupId } of toUpdate) {
+ await prisma.programApplication.updateMany({
+ where: {
+ programId,
+ },
+ data: {
+ groupId,
+ },
+ });
+ }
}
main();