Skip to content

Commit

Permalink
Updated process gems payout email (#4859)
Browse files Browse the repository at this point in the history
* Updated process gems payout email

* Fixed send gems payout emails

* Changes

* organize files

---------

Co-authored-by: mattcasey <[email protected]>
  • Loading branch information
Devorein and mattcasey authored Oct 21, 2024
1 parent 4ca7485 commit 46effc3
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 39 deletions.
4 changes: 2 additions & 2 deletions apps/scoutgame/app/api/session/claimable-points/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export async function GET() {
if (!user) {
return NextResponse.json({ points: 0 });
}
const result = await getClaimablePoints(user.id);
return NextResponse.json({ points: result.totalClaimablePoints });
const claimablePoints = await getClaimablePoints({ userId: user.id });
return NextResponse.json({ points: claimablePoints });
}
12 changes: 1 addition & 11 deletions apps/scoutgame/lib/session/loginWithFarcasterAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@ export const loginWithFarcasterAction = actionClient
const { fid } = await verifyFarcasterUser(parsedInput);

if (inviteCode && parsedInput.inviteCode === inviteCode) {
cookies().set(
'invite-code',
await sealData(
{
inviteCode
},
{
password: authSecret as string
}
)
);
cookies().set('invite-code', await sealData({ inviteCode }, { password: authSecret as string }));
log.info('Builder logged in with invite code', { fid });
} else {
// throws an error if user does not have access
Expand Down
5 changes: 0 additions & 5 deletions apps/scoutgamecron/cron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,3 @@ cron:
url: '/alert-low-wallet-gas-balance'
# Every 5 minutes
schedule: '*/5 * * * *'

- name: 'send-points-claim-emails'
url: '/send-points-claim-emails'
# Every Monday at 2am
schedule: '0 2 * * 1'
6 changes: 4 additions & 2 deletions apps/scoutgamecron/src/scripts/query.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { processNftMints } from '../tasks/processNftMints';
import { sendGemsPayoutEmails } from '../tasks/processGemsPayout/sendGemsPayoutEmails/sendGemsPayoutEmails';
import { prisma } from '@charmverse/core/prisma-client';

export async function query() {
await processNftMints();
await sendGemsPayoutEmails({
week: '2024-W42'
});
// const builderNft = await prisma.builderNft.findMany({
// where: {
// tokenId: {
Expand Down
5 changes: 4 additions & 1 deletion apps/scoutgamecron/src/tasks/processGemsPayout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Context } from 'koa';
import { DateTime } from 'luxon';

import { processScoutPointsPayout } from './processScoutPointsPayout';
import { sendGemsPayoutEmails } from './sendGemsPayoutEmails/sendGemsPayoutEmails';

export async function processGemsPayout(
ctx: Context,
Expand Down Expand Up @@ -62,5 +63,7 @@ export async function processGemsPayout(
}
}

log.info(`Processed ${topWeeklyBuilders.length} builders points payout`);
const emailsSent = await sendGemsPayoutEmails({ week });

log.info(`Processed ${topWeeklyBuilders.length} builders points payout`, { emailsSent });
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { render } from '@react-email/render';

import { ClaimPointsTemplate } from './templates/ClaimPointsTemplate';

export async function sendPointsClaimEmails() {
export async function sendGemsPayoutEmails({ week }: { week: string }) {
const scouts = await prisma.scout.findMany({
where: {
email: {
Expand All @@ -21,13 +21,14 @@ export async function sendPointsClaimEmails() {
}
});

let totalEmailsSent = 0;

for (const scout of scouts) {
try {
const pointsToClaim = await getClaimablePoints(scout.id);

if (pointsToClaim.totalClaimablePoints) {
const weeklyClaimablePoints = await getClaimablePoints({ userId: scout.id, week });
if (weeklyClaimablePoints) {
const html = await render(
ClaimPointsTemplate({ points: pointsToClaim.totalClaimablePoints, displayName: scout.displayName })
ClaimPointsTemplate({ points: weeklyClaimablePoints, displayName: scout.displayName })
);
await sendEmail({
to: {
Expand All @@ -39,9 +40,12 @@ export async function sendPointsClaimEmails() {
subject: 'Congratulations you just earned points in the Scout Game',
html
});
totalEmailsSent += 1;
}
} catch (error) {
log.error('Error sending points claim email', { error, scoutId: scout.id });
}
}

return totalEmailsSent;
}
3 changes: 0 additions & 3 deletions apps/scoutgamecron/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { processAllBuilderActivity } from './tasks/processBuilderActivity';
import { processGemsPayout } from './tasks/processGemsPayout';
import { processNftMints } from './tasks/processNftMints';
import { sendNotifications } from './tasks/pushNotifications/sendNotifications';
import { sendPointsClaimEmails } from './tasks/sendPointsClaimEmails/sendPointsClaimEmails';
import { updateMixpanelUserProfilesTask } from './tasks/updateMixpanelProfilesTask';

const app = new Koa();
Expand Down Expand Up @@ -58,8 +57,6 @@ addTask('/update-mixpanel-user-profiles', updateMixpanelUserProfilesTask);

addTask('/alert-low-wallet-gas-balance', alertLowWalletGasBalance);

addTask('/send-points-claim-emails', sendPointsClaimEmails);

// Standard health check used by Beanstalk
router.get('/api/health', middleware.healthCheck);

Expand Down
6 changes: 3 additions & 3 deletions packages/mailer/src/mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { log } from '@charmverse/core/log';
import { htmlToText } from 'html-to-text';
import type { IMailgunClient } from 'mailgun.js/Interfaces';

import mailgunClient, { DOMAIN, SENDER_ADDRESS } from './mailgunClient';
import mailgunClient, { DOMAIN } from './mailgunClient';

export interface EmailRecipient {
email: string;
Expand All @@ -15,7 +15,7 @@ interface EmailProps {
subject: string;
to: EmailRecipient;
attachment?: { data: Buffer; name: string };
senderAddress?: string;
senderAddress: string;
client?: IMailgunClient | null;
}

Expand All @@ -30,7 +30,7 @@ export async function sendEmail({ client, html, subject, to, attachment, senderA
}

return client?.messages.create(DOMAIN, {
from: senderAddress ?? SENDER_ADDRESS,
from: senderAddress,
to: [recipientAddress],
subject,
text: htmlToText(html),
Expand Down
1 change: 0 additions & 1 deletion packages/mailer/src/mailgunClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Mailgun from 'mailgun.js';

export const API_KEY = process.env.MAILGUN_API_KEY as string | undefined;
export const DOMAIN = process.env.MAILGUN_DOMAIN as string;
export const SENDER_ADDRESS = `CharmVerse <replies@${DOMAIN}>`;
const mailgun = new Mailgun(formData);
const client = API_KEY && API_KEY !== 'test-key' ? mailgun.client({ username: 'api', key: API_KEY }) : null;

Expand Down
9 changes: 3 additions & 6 deletions packages/scoutgame/src/points/getClaimablePoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { prisma } from '@charmverse/core/prisma-client';

import { currentSeason, getPreviousSeason } from '../dates';

export async function getClaimablePoints(userId: string): Promise<{ totalClaimablePoints: number }> {
export async function getClaimablePoints({ userId, week }: { week?: string; userId: string }): Promise<number> {
const previousSeason = getPreviousSeason(currentSeason);
const claimableSeasons = [previousSeason, currentSeason].filter(Boolean);
if (claimableSeasons.length === 0) {
Expand All @@ -13,6 +13,7 @@ export async function getClaimablePoints(userId: string): Promise<{ totalClaimab
recipientId: userId,
claimedAt: null,
event: {
week,
season: {
in: claimableSeasons
}
Expand All @@ -23,9 +24,5 @@ export async function getClaimablePoints(userId: string): Promise<{ totalClaimab
}
});

const totalClaimablePoints = pointsReceipts.reduce((acc, receipt) => acc + receipt.value, 0);

return {
totalClaimablePoints
};
return pointsReceipts.reduce((acc, receipt) => acc + receipt.value, 0);
}

0 comments on commit 46effc3

Please sign in to comment.