Skip to content

Commit

Permalink
Merge pull request #501 from sparcs-kaist/dev
Browse files Browse the repository at this point in the history
Main branch update from Dev branch
  • Loading branch information
kmc7468 authored Mar 19, 2024
2 parents 551e3bc + 2d01cfd commit 63acbe7
Show file tree
Hide file tree
Showing 26 changed files with 193 additions and 302 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
"@adminjs/express": "^5.1.0",
"@adminjs/mongoose": "^3.0.3",
"adminjs": "^6.8.7",
"ajv": "^8.12.0",
"ajv-errors": "^3.0.0",
"ajv-formats": "^2.1.1",
"aws-sdk": "^2.1386.0",
"axios": "^0.27.2",
"ci": "^2.2.0",
Expand Down Expand Up @@ -54,7 +51,9 @@
"swagger-ui-express": "^4.6.0",
"validator": "^13.7.0",
"winston": "^3.8.1",
"winston-daily-rotate-file": "^4.7.1"
"winston-daily-rotate-file": "^4.7.1",
"zod": "^3.22.4",
"zod-to-json-schema": "^3.22.4"
},
"devDependencies": {
"chai": "^4.3.10",
Expand Down
64 changes: 18 additions & 46 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions src/lottery/routes/docs/globalStateSchema.js

This file was deleted.

15 changes: 0 additions & 15 deletions src/lottery/routes/docs/inviteSchema.js

This file was deleted.

15 changes: 0 additions & 15 deletions src/lottery/routes/docs/questsSchema.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/lottery/routes/docs/schemas/globalStateSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { z } = require("zod");
const { zodToSchemaObject } = require("../../../../routes/docs/utils");
const { objectId, user } = require("../../../../modules/patterns");

const globalStateZod = {
createUserGlobalStateHandler: z
.object({
phoneNumber: z.string().regex(user.phoneNumber),
group: z.number().gte(1).lte(26),
inviter: z.string().regex(objectId),
})
.partial({ inviter: true }),
};

const globalStateSchema = zodToSchemaObject(globalStateZod);

module.exports = { globalStateZod, globalStateSchema };
13 changes: 13 additions & 0 deletions src/lottery/routes/docs/schemas/inviteSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { z } = require("zod");
const { zodToSchemaObject } = require("../../../../routes/docs/utils");
const { objectId } = require("../../../../modules/patterns");

const inviteZod = {
searchInviterHandler: z.object({
inviter: z.string().regex(objectId),
}),
};

const inviteSchema = zodToSchemaObject(inviteZod);

module.exports = { inviteSchema, inviteZod };
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/** Item에 대한 기본적인 프로퍼티를 갖고 있는 스키마입니다. */
/* Item에 대한 기본적인 프로퍼티를 갖고 있는 스키마입니다.
* TODO: 추후 코드 재사용시 상황에 맞춰 zod로 이전이 필요합니다.
*/
const itemBase = {
type: "object",
required: [
Expand Down
10 changes: 10 additions & 0 deletions src/lottery/routes/docs/schemas/questsSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { z } = require("zod");
const { zodToSchemaObject } = require("../../../../routes/docs/utils");

const questsZod = {
completeHandler: z.object({ questId: z.enum(["roomSharing"]) }),
};

const questsSchema = zodToSchemaObject(questsZod);

module.exports = { questsZod, questsSchema };
8 changes: 4 additions & 4 deletions src/lottery/routes/docs/swaggerDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const publicNoticeDocs = require("./publicNotice");
const questsDocs = require("./quests");
const transactionsDocs = require("./transactions");

const globalStateSchema = require("./globalStateSchema");
const inviteSchema = require("./inviteSchema");
const itemsSchema = require("./itemsSchema");
const questsSchema = require("./questsSchema");
const { globalStateSchema } = require("./schemas/globalStateSchema");
const { inviteSchema } = require("./schemas/inviteSchema");
const itemsSchema = require("./schemas/itemsSchema");
const { questsSchema } = require("./schemas/questsSchema");

const { eventConfig } = require("../../../../loadenv");
const apiPrefix = `/events/${eventConfig?.mode}`;
Expand Down
7 changes: 3 additions & 4 deletions src/lottery/routes/globalState.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const express = require("express");

const { validateBody } = require("../../middlewares/zod");
const { globalStateZod } = require("./docs/schemas/globalStateSchema");
const router = express.Router();
const globalStateHandlers = require("../services/globalState");
const { validateBody } = require("../../middlewares/ajv");
const globalStateSchema = require("./docs/globalStateSchema");

router.get("/", globalStateHandlers.getUserGlobalStateHandler);

Expand All @@ -13,7 +12,7 @@ router.use(require("../middlewares/timestampValidator"));

router.post(
"/create",
validateBody(globalStateSchema.createUserGlobalStateHandler),
validateBody(globalStateZod.createUserGlobalStateHandler),
globalStateHandlers.createUserGlobalStateHandler
);

Expand Down
8 changes: 3 additions & 5 deletions src/lottery/routes/invite.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const express = require("express");

const { validateParams } = require("../../middlewares/zod");
const { inviteZod } = require("./docs/schemas/inviteSchema");
const router = express.Router();
const inviteHandlers = require("../services/invite");

const { validateParams } = require("../../middlewares/ajv");
const inviteSchema = require("./docs/inviteSchema");

router.get(
"/search/:inviter",
validateParams(inviteSchema.searchInviterHandler),
validateParams(inviteZod.searchInviterHandler),
inviteHandlers.searchInviterHandler
);

Expand Down
5 changes: 2 additions & 3 deletions src/lottery/routes/items.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const express = require("express");

const router = express.Router();
// TODO: 추후 코드 재사용시 상황에 맞춰 zod로 이전이 필요합니다.
const itemsHandlers = require("../services/items");

const { validateParams } = require("../../middlewares/ajv");
const itemsSchema = require("./docs/itemsSchema");
const itemsSchema = require("./docs/schemas/itemsSchema");

// 아래의 Endpoint는 2024 봄학기 이벤트에서 사용되지 않습니다.
//
Expand Down
8 changes: 3 additions & 5 deletions src/lottery/routes/quests.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
const express = require("express");

const { validateParams } = require("../../middlewares/zod");
const { questsZod } = require("./docs/schemas/questsSchema");
const router = express.Router();
const questsHandlers = require("../services/quests");

const { validateParams } = require("../../middlewares/ajv");
const questsSchema = require("./docs/questsSchema");

// 아래의 Endpoint 접근 시 로그인, 차단 여부 체크 및 시각 체크 필요
router.use(require("../../middlewares/auth"));
router.use(require("../middlewares/checkBanned"));
router.use(require("../middlewares/timestampValidator"));

router.post(
"/complete/:questId",
validateParams(questsSchema.completeHandler),
validateParams(questsZod.completeHandler),
questsHandlers.completeHandler
);

Expand Down
27 changes: 13 additions & 14 deletions src/middlewares/ajv.js → src/middlewares/zod.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
const Ajv = require("ajv");
const ajvErrors = require("ajv-errors");
const { default: addFormats } = require("ajv-formats");
const logger = require("../modules/logger");

const ajv = new Ajv({ verbose: true, allErrors: true });
addFormats(ajv);
ajvErrors(ajv);

const parseAjvErrors = (errors, res) => {
const parseZodErrors = (statusCode, errors, res) => {
const error_message = errors;
res.status(400).send(error_message);
res.status(statusCode).send(error_message);
};

const validate = (schema, req, res) => {
const validate = ajv.compile(schema);
if (validate(req)) {
return true;
} else {
parseAjvErrors(validate.errors[0].message, res);
try {
const result = schema.safeParse(req);
if (result.success) {
return true;
} else {
parseZodErrors(400, result.error.issues[0].message, res);
}
} catch (err) {
logger.error(err);
parseZodErrors(400, err, res);
}
};

Expand Down
3 changes: 3 additions & 0 deletions src/modules/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class NodemailerTransport {
host: "smtp-relay.gmail.com",
secure: false,
port: 587,
tls: {
rejectUnauthorized: false,
},
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/modules/patterns.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module.exports = {
objectId: RegExp("^[a-fA-F\\d]{24}$"),
room: {
name: RegExp(
"^[A-Za-z0-9가-힣ㄱ-ㅎㅏ-ㅣ,.?! _~/#'\\\\@=\"\\-\\^()+*<>{}[\\]]{1,50}$" // ,.?/#'\@="-^()+*<>{}[] 허용
),
from: RegExp("^[A-Za-z0-9가-힣 -]{1,20}$"),
to: RegExp("^[A-Za-z0-9가-힣 -]{1,20}$"),
},
user: {
nickname: RegExp("^[A-Za-z가-힣ㄱ-ㅎㅏ-ㅣ0-9-_ ]{3,25}$"),
allowedEmployeeTypes: RegExp("^([PEUR]|[SAGC]|[PEUR][SAGC])$"),
profileImgType: RegExp("^(image/png|image/jpg|image/jpeg)$"),
account: RegExp("^[A-Za-z가-힣]{2,7} [0-9]{10,14}$|^$"),
phoneNumber: RegExp("^010-?([0-9]{3,4})-?([0-9]{4})$"),
},
chat: {
chatImgType: RegExp("^(image/png|image/jpg|image/jpeg)$"),
Expand Down
Loading

0 comments on commit 63acbe7

Please sign in to comment.