From a60da6b2e661ed6801aeabcd3b145647e8c2b12c Mon Sep 17 00:00:00 2001 From: Bruno Perel Date: Sun, 17 Dec 2023 19:10:03 +0100 Subject: [PATCH] Fix top-level await --- .vscode/settings.json | 4 +- apps/duckguessr-old | 1 + packages/api/scripts/send-pending-emails.ts | 118 ++++++++++---------- packages/duckguessr-ml | 1 + 4 files changed, 64 insertions(+), 60 deletions(-) create mode 160000 apps/duckguessr-old create mode 160000 packages/duckguessr-ml diff --git a/.vscode/settings.json b/.vscode/settings.json index ae0ee58d6..82c61a488 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "editor.codeActionsOnSave": { - "source.fixAll": true + "source.fixAll": "explicit" }, "sqltools.connections": [ { @@ -17,5 +17,5 @@ "password": "changeme" } ], - "i18n-ally.localesPaths": ["translations"], + "i18n-ally.localesPaths": ["translations"] } diff --git a/apps/duckguessr-old b/apps/duckguessr-old new file mode 160000 index 000000000..709e012a6 --- /dev/null +++ b/apps/duckguessr-old @@ -0,0 +1 @@ +Subproject commit 709e012a6c783d5326347c89a7aaaf45996c179d diff --git a/packages/api/scripts/send-pending-emails.ts b/packages/api/scripts/send-pending-emails.ts index 7721a378b..fbc7261a2 100644 --- a/packages/api/scripts/send-pending-emails.ts +++ b/packages/api/scripts/send-pending-emails.ts @@ -16,69 +16,71 @@ const medalLevels = { } as const; -const emailsSent = []; -for (const contributionTypeStr in medalLevels) { - const contributionType = contributionTypeStr as userContributionType; - const pendingEmailContributionsForType = await prismaDmClient.userContribution.findMany({ - where: { - contribution: contributionType, - isEmailSent: false - }, orderBy: { - userId: 'asc', - totalPoints: 'asc' - }}); - if (!pendingEmailContributionsForType.length) { - console.info(`No email to send for contribution ${contributionType}`); - } else { - const pendingEmailContributionsByUser = pendingEmailContributionsForType.reduce>((acc, contribution) => ({ - ...acc, [contribution.userId]: [...((acc[contribution.userId]) || []), contribution] - }), {}); +(async () => { + const emailsSent = []; + for (const contributionTypeStr in medalLevels) { + const contributionType = contributionTypeStr as userContributionType; + const pendingEmailContributionsForType = await prismaDmClient.userContribution.findMany({ + where: { + contribution: contributionType, + isEmailSent: false + }, orderBy: { + userId: 'asc', + totalPoints: 'asc' + }}); + if (!pendingEmailContributionsForType.length) { + console.info(`No email to send for contribution ${contributionType}`); + } else { + const pendingEmailContributionsByUser = pendingEmailContributionsForType.reduce>((acc, contribution) => ({ + ...acc, [contribution.userId]: [...((acc[contribution.userId]) || []), contribution] + }), {}); - for (const [userId, pendingEmailContributionsForUser] of Object.entries(pendingEmailContributionsByUser)) { - console.info(`${pendingEmailContributionsForUser.length} contributions pending for user ${userId}`); - const initialPointsCount = pendingEmailContributionsForUser[0].totalPoints - pendingEmailContributionsForUser[0].newPoints; - const finalPointsCount = pendingEmailContributionsForUser[pendingEmailContributionsForUser.length - 1].totalPoints; - const pointsEarned = finalPointsCount - initialPointsCount; + for (const [userId, pendingEmailContributionsForUser] of Object.entries(pendingEmailContributionsByUser)) { + console.info(`${pendingEmailContributionsForUser.length} contributions pending for user ${userId}`); + const initialPointsCount = pendingEmailContributionsForUser[0].totalPoints - pendingEmailContributionsForUser[0].newPoints; + const finalPointsCount = pendingEmailContributionsForUser[pendingEmailContributionsForUser.length - 1].totalPoints; + const pointsEarned = finalPointsCount - initialPointsCount; - const medalReached = Object.entries(medalLevels[contributionType]).reduce((medalReached, [medal, medalThreshold]) => - (initialPointsCount < medalThreshold && finalPointsCount >= medalThreshold) ? parseInt(medal): medalReached, null as number|null) + const medalReached = Object.entries(medalLevels[contributionType]).reduce((medalReached, [medal, medalThreshold]) => + (initialPointsCount < medalThreshold && finalPointsCount >= medalThreshold) ? parseInt(medal): medalReached, null as number|null) - await prismaDmClient.$transaction( - pendingEmailContributionsForUser.map(({ id }) => - prismaDmClient.userContribution.update({ - where: { - id, - }, - data: { - isEmailSent: true - }, - }) - ) - ); + await prismaDmClient.$transaction( + pendingEmailContributionsForUser.map(({ id }) => + prismaDmClient.userContribution.update({ + where: { + id, + }, + data: { + isEmailSent: true + }, + }) + ) + ); - const user = await prismaDmClient.user.findUniqueOrThrow({ where: {id: parseInt(userId)}}); + const user = await prismaDmClient.user.findUniqueOrThrow({ where: {id: parseInt(userId)}}); - const locale = 'fr'; - let email; - switch (contributionType) { - case 'duckhunter': - email =(await new BookstoreApproved({ user, locale, newMedalLevel: medalReached })); - break; - case 'photographe': - email = (await new EdgesPublishedWithPhotographer({ user, locale, extraEdges: pendingEmailContributionsForUser.length, extraPhotographerPoints: pointsEarned, newMedalLevel: medalReached })); - break; - case 'createur': - email = (await new EdgesPublishedWithCreator({ user, locale, extraEdges: pendingEmailContributionsForUser.length, extraCreatorPoints: pointsEarned, newMedalLevel: medalReached })); - break; + const locale = 'fr'; + let email; + switch (contributionType) { + case 'duckhunter': + email =(await new BookstoreApproved({ user, locale, newMedalLevel: medalReached })); + break; + case 'photographe': + email = (await new EdgesPublishedWithPhotographer({ user, locale, extraEdges: pendingEmailContributionsForUser.length, extraPhotographerPoints: pointsEarned, newMedalLevel: medalReached })); + break; + case 'createur': + email = (await new EdgesPublishedWithCreator({ user, locale, extraEdges: pendingEmailContributionsForUser.length, extraCreatorPoints: pointsEarned, newMedalLevel: medalReached })); + break; + } + emailsSent.push(email) + email.send(); } - emailsSent.push(email) - email.send(); } } -} -console.log(JSON.stringify({ - 'emails_sent': emailsSent.map(emailHelper => ({ - 'to': emailHelper.getTo(), - 'subject': emailHelper.getSubject() - })) -})); + console.log(JSON.stringify({ + 'emails_sent': emailsSent.map(emailHelper => ({ + 'to': emailHelper.getTo(), + 'subject': emailHelper.getSubject() + })) + })); +})(); \ No newline at end of file diff --git a/packages/duckguessr-ml b/packages/duckguessr-ml new file mode 160000 index 000000000..eabeaf30c --- /dev/null +++ b/packages/duckguessr-ml @@ -0,0 +1 @@ +Subproject commit eabeaf30c7fa4af2282cf9b511c3935fc015b182