Skip to content

Commit

Permalink
Merge pull request #2 from Apillon/revert
Browse files Browse the repository at this point in the history
Revert
  • Loading branch information
MoMannn authored Feb 22, 2024
2 parents fee6b00 + 52e3a44 commit 5a99466
Show file tree
Hide file tree
Showing 49 changed files with 261 additions and 619 deletions.
75 changes: 41 additions & 34 deletions backend/src/cron.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
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));
this.cronJobs.push(
new CronJob("* * * * *", this.processExpiredClaims, null, false)
);
}
}

Expand Down Expand Up @@ -76,14 +78,14 @@ export class Cron {
const token = await generateEmailAirdropToken(users[i].email);
await SmtpSendTemplate(
[users[i].email],
'Claim your NFT',
'en-airdrop-claim',
"Claim your NFT",
"en-airdrop-claim",
{
appUrl: env.APP_URL,
link: `${env.APP_URL}/claim?token=${token}`,
claimExpiresIn: env.CLAIM_EXPIRES_IN,
},
'Apillon'
"Apillon"
);
updates.push(
`(${users[i].id}, '${users[i].email}', ${
Expand All @@ -94,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 NFT Airdrop token',
'en-airdrop-waiting-line',
"You have been placed on a waitlist for NFT Airdrop token",
"en-airdrop-waiting-line",
{
appUrl: env.APP_URL,
},
'Apillon'
"Apillon"
);
updates.push(
`(${users[i].id}, '${users[i].email}', ${
Expand All @@ -108,19 +110,19 @@ 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}, '${dateToSqlString(
new Date()
)}')`
`(${users[i].id}, '${users[i].email}', ${
AirdropStatus.EMAIL_ERROR
}, '${dateToSqlString(new Date())}')`
);
}
}

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 @@ -130,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 All @@ -152,20 +154,23 @@ export class Cron {
null,
conn
)
).map(x => x.id);
).map((x) => x.id);

if (usersWithExpiredClaim.length) {
//Update those users to claim expired
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');
console.info(
usersWithExpiredClaim.length +
" 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
const usersInWaitingLine = await mysql.paramExecute(
Expand All @@ -181,38 +186,40 @@ export class Cron {
conn
);

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

if (usersInWaitingLine.length) {
await mysql.paramExecute(
`UPDATE user
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 NFT",
"en-airdrop-claim",
{
appUrl: env.APP_URL,
link: `${env.APP_URL}/claim?token=${token}`,
claimExpiresIn: env.CLAIM_EXPIRES_IN,
},
'Apillon'
"Apillon"
);
} catch (err) {
await mysql.paramExecute(
Expand All @@ -231,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
14 changes: 7 additions & 7 deletions backend/src/routes/admin-login.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Application } from 'express';
import { NextFunction, Request, Response } from '../http';
import { RouteErrorCode } from '../config/values';
import { ResourceError } from '../lib/errors';
import { Identity } from '@apillon/sdk';
import { generateAdminAuthToken } from '../lib/jwt';
import { Application } from "express";
import { NextFunction, Request, Response } from "../http";
import { RouteErrorCode } from "../config/values";
import { ResourceError } from "../lib/errors";
import { Identity } from "@apillon/sdk";
import { generateAdminAuthToken } from "../lib/jwt";

/**
* Installs new route on the provided application.
* @param app ExpressJS application.
*/
export function inject(app: Application) {
app.post('/login', (req: Request, res: Response, next: NextFunction) => {
app.post("/login", (req: Request, res: Response, next: NextFunction) => {
resolve(req, res).catch(next);
});
}
Expand Down
37 changes: 23 additions & 14 deletions backend/src/routes/claim-airdrop.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { Application } from 'express';
import { NextFunction, Request, Response } from '../http';
import { RouteErrorCode, ValidatorErrorCode } from '../config/values';
import { ResourceError } from '../lib/errors';
import { readEmailAirdropToken } from '../lib/jwt';
import { AirdropStatus, User } from '../models/user';
import { Identity, LogLevel, Nft } from '@apillon/sdk';
import { LogType, writeLog } from '../lib/logger';
import { env } from '../config/env';
import { Application } from "express";
import { NextFunction, Request, Response } from "../http";
import { RouteErrorCode, ValidatorErrorCode } from "../config/values";
import { ResourceError } from "../lib/errors";
import { readEmailAirdropToken } from "../lib/jwt";
import { AirdropStatus, User } from "../models/user";
import { Identity, LogLevel, Nft } from "@apillon/sdk";
import { LogType, writeLog } from "../lib/logger";
import { env } from "../config/env";

/**∂
* Installs new route on the provided application.
* @param app ExpressJS application.
*/
export function inject(app: Application) {
app.post('/users/claim', (req: Request, res: Response, next: NextFunction) => {
resolve(req, res).catch(next);
});
app.post(
"/users/claim",
(req: Request, res: Response, next: NextFunction) => {
resolve(req, res).catch(next);
}
);
}

export async function resolve(req: Request, res: Response): Promise<void> {
Expand Down Expand Up @@ -83,14 +86,20 @@ export async function resolve(req: Request, res: Response): Promise<void> {
? AirdropStatus.AIRDROP_COMPLETED
: AirdropStatus.AIRDROP_ERROR;
} catch (e) {
writeLog(LogType.ERROR, 'Error creating airdrop', 'claim-airdrop.ts', 'resolve', e);
writeLog(
LogType.ERROR,
"Error creating airdrop",
"claim-airdrop.ts",
"resolve",
e
);
user.airdrop_status = AirdropStatus.AIRDROP_ERROR;
}

await user.update();
if (response && response.success) {
return res.respond(200, {
success: 'ok',
success: "ok",
transactionHash: response.transactionHash,
});
} else {
Expand Down
Loading

0 comments on commit 5a99466

Please sign in to comment.