Skip to content

Commit

Permalink
Add: eligibility field into the response of globalState api
Browse files Browse the repository at this point in the history
  • Loading branch information
kmc7468 committed Feb 19, 2024
1 parent 8e21e39 commit 6509a41
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/lottery/routes/docs/globalState.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ globalStateDocs[`${apiPrefix}/`] = {
type: "object",
required: [
"isAgreeOnTermsOfEvent",
"eligibility",
"creditAmount",
"groupCreditAmount",
"completedQuests",
Expand All @@ -29,6 +30,11 @@ globalStateDocs[`${apiPrefix}/`] = {
description: "유저의 이벤트 참여 동의 여부",
example: true,
},
eligibility: {
type: "boolean",
description: "유저의 이벤트 참여 가능 여부",
example: true,
},
creditAmount: {
type: "number",
description: "재화 개수. 0 이상입니다.",
Expand Down
26 changes: 17 additions & 9 deletions src/lottery/services/globalState.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ const { eventConfig } = require("../../../loadenv");
const contracts = require("../modules/contracts");
const quests = Object.values(contracts.quests);

// 유저가 이벤트에 참여할 수 있는지 확인하는 함수입니다.
const checkUserEligibility = async (user) => {
// production 환경이 아닌 경우 테스트를 위해 참여 조건을 확인하지 않습니다.
if (nodeEnv !== "production") return true;

const kaistId = parseInt(user?.subinfo?.kaist || "0");
return 20240001 <= kaistId && kaistId <= 20241500;
};

const getUserGlobalStateHandler = async (req, res) => {
try {
const userId = isLogin(req) ? getLoginInfo(req).oid : null;
const user = userId && (await userModel.findOne({ _id: userId }).lean());

const eventStatus =
userId &&
(await eventStatusModel
Expand All @@ -19,6 +30,7 @@ const getUserGlobalStateHandler = async (req, res) => {
if (!eventStatus)
return res.json({
isAgreeOnTermsOfEvent: false,
eligibility: await checkUserEligibility(user),
completedQuests: [],
creditAmount: 0,
group: 0,
Expand Down Expand Up @@ -48,6 +60,7 @@ const getUserGlobalStateHandler = async (req, res) => {

return res.json({
isAgreeOnTermsOfEvent: true,
eligibility: true,
...eventStatus,
groupCreditAmount: groupCreditAmountReal,
quests,
Expand Down Expand Up @@ -82,17 +95,12 @@ const createUserGlobalStateHandler = async (req, res) => {
.status(500)
.json({ error: "GlobalState/Create : internal server error" });

// 24학번 학사 과정 학생이 아닌 경우 이벤트에 참여할 수 없습니다.
// 테스트를 위해, production 환경에서만 학번을 확인합니다.
const kaistId = parseInt(user.subinfo?.kaist || "0");
if (
nodeEnv === "production" &&
!(20240001 <= kaistId && kaistId <= 20241500)
) {
// 유저가 이벤트에 참여할 수 있는지 확인합니다.
const eligibility = await checkUserEligibility(user);
if (!eligibility)
return res.status(400).json({
error: "GlobalState/Create : not an undergraduate freshman",
error: "GlobalState/Create : not eligible to participate in the event",
});
}

// 수집한 전화번호를 User Document에 저장합니다.
// 다른 이벤트 참여 과정에서 문제가 생길 수 있으므로, 이벤트 참여 자격이 있는 경우에만 저장합니다.
Expand Down

0 comments on commit 6509a41

Please sign in to comment.