Skip to content

Commit

Permalink
Fix top-level await
Browse files Browse the repository at this point in the history
  • Loading branch information
bperel committed Dec 17, 2023
1 parent 5d4239e commit a60da6b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.fixAll": true
"source.fixAll": "explicit"
},
"sqltools.connections": [
{
Expand All @@ -17,5 +17,5 @@
"password": "changeme"
}
],
"i18n-ally.localesPaths": ["translations"],
"i18n-ally.localesPaths": ["translations"]
}
1 change: 1 addition & 0 deletions apps/duckguessr-old
Submodule duckguessr-old added at 709e01
118 changes: 60 additions & 58 deletions packages/api/scripts/send-pending-emails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<number, userContribution[]>>((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<Record<number, userContribution[]>>((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()
}))
}));
})();
1 change: 1 addition & 0 deletions packages/duckguessr-ml
Submodule duckguessr-ml added at eabeaf

0 comments on commit a60da6b

Please sign in to comment.