Skip to content

Commit

Permalink
Code format, e2e tests fix, added licence, ...
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkoS993 committed Apr 10, 2024
1 parent 1cc6037 commit a5db6b2
Show file tree
Hide file tree
Showing 52 changed files with 656 additions and 606 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Apillon

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions backend/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 80,
"singleQuote": true,
"trailingComma": "all",
"endOfLine": "auto",
"bracketSpacing": true
}
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"db-upgrade:ci": "node -r ts-node/register ./src/scripts/db/upgrade-db --F",
"db-downgrade": "node -r ts-node/register ./src/scripts/db/downgrade-db",
"db-rebuild": "node -r ts-node/register ./src/scripts/db/rebuild-db",
"db-drop": "node -r ts-node/register ./src/scripts/db/drop-db"
"db-drop": "node -r ts-node/register ./src/scripts/db/drop-db",
"format": "prettier --write \"**/*.{ts,tsx,md}\""
},
"dependencies": {
"@apillon/sdk": "2.0.0",
Expand Down
3 changes: 2 additions & 1 deletion backend/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export const env = {
/**
* Admin
*/
ADMIN_WALLET: process.env['ADMIN_WALLET']?.toLocaleLowerCase().split(/[,;]/) || [],
ADMIN_WALLET:
process.env['ADMIN_WALLET']?.toLocaleLowerCase().split(/[,;]/) || [],

/**
* Mysql URL.
Expand Down
6 changes: 3 additions & 3 deletions backend/src/config/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {
SystemErrorCode,
ValidatorErrorCode,
RouteErrorCode,
} from "./values";
} from './values';

/**
* Message codes.
*/
export default (code) => {
const customMessages = {
[RouteErrorCode.INVALID_REQUEST]: "INVALID REQUEST",
[RouteErrorCode.INVALID_REQUEST]: 'INVALID REQUEST',
};

return (
Expand All @@ -19,6 +19,6 @@ export default (code) => {
SystemErrorCode[code] ||
ValidatorErrorCode[code] ||
RouteErrorCode[code] ||
"UNKNOWN_ERROR"
'UNKNOWN_ERROR'
);
};
28 changes: 14 additions & 14 deletions backend/src/config/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
* Serialized strategy.
*/
export enum SerializedStrategy {
PROFILE = "profile",
DB = "db",
EXTENDED_DB = "extended_db",
ADMIN = "admin",
PROFILE = 'profile',
DB = 'db',
EXTENDED_DB = 'extended_db',
ADMIN = 'admin',
}

/**
* Populate strategy.
*/
export enum PopulateStrategy {
ADMIN = "admin",
PROFILE = "profile",
DB = "db",
ADMIN = 'admin',
PROFILE = 'profile',
DB = 'db',
}

/**
Expand All @@ -37,8 +37,8 @@ export enum PaginationValues {
* Request Token types.
*/
export enum RequestToken {
AUTH_ADMIN = "authAdmin",
AIRDROP_EMAIL = "airdropEmail",
AUTH_ADMIN = 'authAdmin',
AIRDROP_EMAIL = 'airdropEmail',
}

/**
Expand All @@ -51,11 +51,11 @@ export enum SystemErrorCode {
}

export enum AppEnvironment {
LOCAL_DEV = "local",
TEST = "testing",
DEV = "development",
STG = "staging",
PROD = "production",
LOCAL_DEV = 'local',
TEST = 'testing',
DEV = 'development',
STG = 'staging',
PROD = 'production',
}

/**
Expand Down
6 changes: 3 additions & 3 deletions backend/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IEnv } from "./config/env";
import { readAdminAuthToken } from "./lib/jwt";
import { MySql } from "./lib/mysql";
import { IEnv } from './config/env';
import { readAdminAuthToken } from './lib/jwt';
import { MySql } from './lib/mysql';

/**
* Request object context holds personalized request-based information.
Expand Down
65 changes: 41 additions & 24 deletions backend/src/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export class Cron {
constructor() {
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 @@ -48,7 +50,7 @@ export class Cron {
AND status = @status
;
`,
{ status: SqlModelStatus.ACTIVE }
{ status: SqlModelStatus.ACTIVE },
);
const numOfReservations = res[0].total;
availableNftLeft = env.MAX_SUPPLY - numOfReservations;
Expand All @@ -65,8 +67,12 @@ export class Cron {
FOR UPDATE
;
`,
{ airdrop_status: AirdropStatus.PENDING, status: SqlModelStatus.ACTIVE, date: new Date() },
conn
{
airdrop_status: AirdropStatus.PENDING,
status: SqlModelStatus.ACTIVE,
date: new Date(),
},
conn,
);

const updates = [];
Expand All @@ -84,12 +90,12 @@ export class Cron {
link: `${env.APP_URL}/claim?token=${token}`,
claimExpiresIn: env.CLAIM_EXPIRES_IN,
},
'Apillon'
'Apillon',
);
updates.push(
`(${users[i].id}, '${users[i].email}', ${
AirdropStatus.EMAIL_SENT
}, '${dateToSqlString(new Date())}')`
}, '${dateToSqlString(new Date())}')`,
);
} else {
//Currently, waiting line for airdrop is full.Send info email and set appropriate status
Expand All @@ -100,20 +106,20 @@ export class Cron {
{
appUrl: env.APP_URL,
},
'Apillon'
'Apillon',
);
updates.push(
`(${users[i].id}, '${users[i].email}', ${
AirdropStatus.IN_WAITING_LINE
}, '${dateToSqlString(new Date())}')`
}, '${dateToSqlString(new Date())}')`,
);
}
} catch (e) {
writeLog(LogType.ERROR, e, 'cron.ts', 'sendEmail');
updates.push(
`(${users[i].id}, '${users[i].email}', ${AirdropStatus.EMAIL_ERROR}, '${dateToSqlString(
new Date()
)}')`
new Date(),
)}')`,
);
}
}
Expand Down Expand Up @@ -151,10 +157,13 @@ export class Cron {
FOR UPDATE
;
`,
{ airdrop_status: AirdropStatus.EMAIL_SENT, status: SqlModelStatus.ACTIVE },
conn
{
airdrop_status: AirdropStatus.EMAIL_SENT,
status: SqlModelStatus.ACTIVE,
},
conn,
)
).map(x => x.id);
).map((x) => x.id);

if (usersWithExpiredClaim.length) {
//Update those users to claim expired
Expand All @@ -165,9 +174,12 @@ export class Cron {
;
`,
{ airdrop_status: AirdropStatus.AIRDROP_CLAIM_EXPIRED },
conn
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 @@ -179,27 +191,32 @@ export class Cron {
FOR UPDATE
;
`,
{ airdrop_status: AirdropStatus.IN_WAITING_LINE, status: SqlModelStatus.ACTIVE },
conn
{
airdrop_status: AirdropStatus.IN_WAITING_LINE,
status: SqlModelStatus.ACTIVE,
},
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 = @airdrop_status,
email_sent_time = NOW()
WHERE id IN (${usersInWaitingLine.map(x => x.id).join(',')})
WHERE id IN (${usersInWaitingLine.map((x) => x.id).join(',')})
;
`,
{ airdrop_status: AirdropStatus.EMAIL_SENT },
conn
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) {
Expand All @@ -214,7 +231,7 @@ export class Cron {
link: `${env.APP_URL}/claim?token=${token}`,
claimExpiresIn: env.CLAIM_EXPIRES_IN,
},
'Apillon'
'Apillon',
);
} catch (err) {
await mysql.paramExecute(
Expand All @@ -224,7 +241,7 @@ export class Cron {
;
`,
{ airdrop_status: AirdropStatus.EMAIL_ERROR, user_id: user.id },
conn
conn,
);
}
}
Expand Down
44 changes: 22 additions & 22 deletions backend/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import * as express from "express";
import * as cors from "cors";
import { Server } from "http";
import { IEnv } from "./config/env";
import { Context } from "./context";
import { MySql } from "./lib/mysql";
import { inject as injectContext } from "./middlewares/context";
import { inject as injectCors } from "./middlewares/cors";
import * as express from 'express';
import * as cors from 'cors';
import { Server } from 'http';
import { IEnv } from './config/env';
import { Context } from './context';
import { MySql } from './lib/mysql';
import { inject as injectContext } from './middlewares/context';
import { inject as injectCors } from './middlewares/cors';
// import { inject as injectCompression } from './middlewares/compression';
import { inject as injectErrors } from "./middlewares/errors";
import { inject as injectDataParser } from "./middlewares/parser";
import { inject as injectRenders } from "./middlewares/renders";
import { inject as injectGetRoot } from "./routes/get-root";
import { inject as injectCreateUserAdmin } from "./routes/create-user-admin";
import { inject as injectCreateUser } from "./routes/create-user";
import { inject as injectGetUser } from "./routes/get-user";
import { inject as injectGetStatistics } from "./routes/get-statistics";
import { inject as injectClaimAirdrop } from "./routes/claim-airdrop";
import { inject as injectAdminLogin } from "./routes/admin-login";
import { inject as injectErrors } from './middlewares/errors';
import { inject as injectDataParser } from './middlewares/parser';
import { inject as injectRenders } from './middlewares/renders';
import { inject as injectGetRoot } from './routes/get-root';
import { inject as injectCreateUserAdmin } from './routes/create-user-admin';
import { inject as injectCreateUser } from './routes/create-user';
import { inject as injectGetUser } from './routes/get-user';
import { inject as injectGetStatistics } from './routes/get-statistics';
import { inject as injectClaimAirdrop } from './routes/claim-airdrop';
import { inject as injectAdminLogin } from './routes/admin-login';

export interface Request extends express.Request {
context: Context;
Expand Down Expand Up @@ -92,7 +92,7 @@ export class HttpServer {
await new Promise((res) => {
this.server = this.app.listen(
this.config.env.API_PORT,
this.config.env.API_HOST
this.config.env.API_HOST,
);
res(null);
});
Expand All @@ -116,18 +116,18 @@ export class HttpServer {
* Returns an array of all available routes.
*/
public collectRoutes(): { method: string; path: string }[] {
return this.app.router["stack"]
return this.app.router['stack']
.map((middleware) => middleware.route)
.filter((route) => !!route)
.map((route) =>
Object.keys(route.methods).map((method) => ({
method: method.toUpperCase(),
path: route.path,
}))
})),
)
.reduce((a, b) => a.concat(b), [])
.sort((a, b) =>
`${a.path}@${a.method}`.localeCompare(`${b.path}@${b.method}`)
`${a.path}@${a.method}`.localeCompare(`${b.path}@${b.method}`),
);
}
}
8 changes: 4 additions & 4 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./lib/mysql";
export * from "./context";
export * from "./http";
export * from "./cron";
export * from './lib/mysql';
export * from './context';
export * from './http';
export * from './cron';
Loading

0 comments on commit a5db6b2

Please sign in to comment.