Skip to content

Commit

Permalink
Merge pull request #20 from Apillon/dev
Browse files Browse the repository at this point in the history
Merge dev into staging
  • Loading branch information
vinkoS993 authored Feb 18, 2024
2 parents be6dd46 + ee94de8 commit b5923be
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions backend/src/cron.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { CronJob } from 'cron';
import { AirdropStatus } from './models/user';
import { dateToSqlString } from './lib/sql-utils';
import { SqlModelStatus } from './models/base-sql-model';
import { MysqlConnectionManager } from './lib/mysql-connection-manager';
import { SmtpSendTemplate } from './lib/node-mailer';
import { env } from './config/env';
import { generateEmailAirdropToken } from './lib/jwt';
import { LogType, writeLog } from './lib/logger';
import { CronJob } from "cron";
import { AirdropStatus } from "./models/user";
import { dateToSqlString } from "./lib/sql-utils";
import { SqlModelStatus } from "./models/base-sql-model";
import { MysqlConnectionManager } from "./lib/mysql-connection-manager";
import { SmtpSendTemplate } from "./lib/node-mailer";
import { env } from "./config/env";
import { generateEmailAirdropToken } from "./lib/jwt";
import { LogType, writeLog } from "./lib/logger";

export class Cron {
private cronJobs: CronJob[] = [];

constructor() {
this.cronJobs.push(new CronJob('* * * * *', this.sendEmail, null, false));
this.cronJobs.push(new CronJob("* * * * *", this.sendEmail, null, false));
if (env.MAX_SUPPLY > 0) {
this.cronJobs.push(
new CronJob('* * * * *', this.processExpiredClaims, null, false)
new CronJob("* * * * *", this.processExpiredClaims, null, false)
);
}
}
Expand Down Expand Up @@ -78,14 +78,14 @@ export class Cron {
const token = await generateEmailAirdropToken(users[i].email);
await SmtpSendTemplate(
[users[i].email],
'Claim your MENT token',
'en-airdrop-claim',
"Claim your MENT token",
"en-airdrop-claim",
{
appUrl: env.APP_URL,
link: `${env.APP_URL}/claim?token=${token}`,
claimExpiresIn: env.CLAIM_EXPIRES_IN,
},
'MENT',
"MENT"
);
updates.push(
`(${users[i].id}, '${users[i].email}', ${
Expand All @@ -96,12 +96,12 @@ export class Cron {
//Currently, waiting line for airdrop is full.Send info email and set appropriate status
await SmtpSendTemplate(
[users[i].email],
'You have been placed on a waitlist for MENT token',
'en-airdrop-waiting-line',
"You have been placed on a waitlist for MENT token",
"en-airdrop-waiting-line",
{
appUrl: env.APP_URL,
},
'MENT'
"MENT"
);
updates.push(
`(${users[i].id}, '${users[i].email}', ${
Expand All @@ -110,7 +110,7 @@ export class Cron {
);
}
} catch (e) {
writeLog(LogType.ERROR, e, 'cron.ts', 'sendEmail');
writeLog(LogType.ERROR, e, "cron.ts", "sendEmail");
updates.push(
`(${users[i].id}, '${users[i].email}', ${
AirdropStatus.EMAIL_ERROR
Expand All @@ -122,7 +122,7 @@ export class Cron {
if (updates.length > 0) {
const sql = `
INSERT INTO user (id, email, airdrop_status, email_sent_time)
VALUES ${updates.join(',')}
VALUES ${updates.join(",")}
ON DUPLICATE KEY UPDATE
airdrop_status = VALUES(airdrop_status),
email_sent_time = VALUES(email_sent_time)`;
Expand All @@ -132,7 +132,7 @@ export class Cron {

await mysql.commit(conn);
} catch (e) {
writeLog(LogType.ERROR, e, 'cron.ts', 'sendEmail');
writeLog(LogType.ERROR, e, "cron.ts", "sendEmail");
await mysql.rollback(conn);
}
}
Expand Down Expand Up @@ -161,15 +161,15 @@ export class Cron {
await mysql.paramExecute(
`UPDATE user
SET airdrop_status = ${AirdropStatus.AIRDROP_CLAIM_EXPIRED}
WHERE id IN (${usersWithExpiredClaim.join(',')})
WHERE id IN (${usersWithExpiredClaim.join(",")})
;
`,
null,
conn
);
console.info(
usersWithExpiredClaim.length +
' users updated to AIRDROP_CLAIM_EXPIRED'
" users updated to AIRDROP_CLAIM_EXPIRED"
);

//Get users in waiting line and set their airdrop status to PENDING, so that they will recieve email for claim
Expand All @@ -187,7 +187,7 @@ export class Cron {
);

console.info(
'Num of users in waiting line: ' + usersInWaitingLine.length
"Num of users in waiting line: " + usersInWaitingLine.length
);

if (usersInWaitingLine.length) {
Expand All @@ -196,29 +196,30 @@ export class Cron {
SET
airdrop_status = ${AirdropStatus.EMAIL_SENT},
email_sent_time = NOW()
WHERE id IN (${usersInWaitingLine.map((x) => x.id).join(',')})
WHERE id IN (${usersInWaitingLine.map((x) => x.id).join(",")})
;
`,
null,
conn
);
console.info(
usersInWaitingLine.map((x) => x.id).join(',') +
' should me moved from waiting line. Sending emails....'
usersInWaitingLine.map((x) => x.id).join(",") +
" should me moved from waiting line. Sending emails...."
);

for (const user of usersInWaitingLine) {
try {
const token = await generateEmailAirdropToken(user.email);
await SmtpSendTemplate(
[user.email],
'Claim your NFT',
'en-airdrop-claim',
"Claim your MENT token",
"en-airdrop-claim",
{
appUrl: env.APP_URL,
link: `${env.APP_URL}/claim?token=${token}`,
claimExpiresIn: env.CLAIM_EXPIRES_IN,
}
},
"MENT"
);
} catch (err) {
await mysql.paramExecute(
Expand All @@ -237,7 +238,7 @@ export class Cron {

await mysql.commit(conn);
} catch (e) {
writeLog(LogType.ERROR, e, 'cron.ts', 'processExpiredClaims');
writeLog(LogType.ERROR, e, "cron.ts", "processExpiredClaims");
await mysql.rollback(conn);
}
}
Expand Down

0 comments on commit b5923be

Please sign in to comment.