Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into Feat/#40
Browse files Browse the repository at this point in the history
  • Loading branch information
shb03323 committed Jan 7, 2023
2 parents 4643638 + 5ffb792 commit 3d98775
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"scripts": {
"dev": "nodemon",
"build": "tsc && node dist",
"build": "tsc && node dist/src",
"test": "mocha -r ts-node/register src/test/*.spec.ts -exit",
"db:pull": "npx prisma db pull",
"db:push": "npx prisma db push",
Expand Down
7 changes: 7 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ model not_todo {
title String? @db.VarChar(500)
missions mission[]
}

model banner {
id Int @id(map: "banner_pk") @unique(map: "banner_id_uindex") @default(autoincrement())
title String @db.VarChar(500)
image String @db.VarChar(500)
banner_category String @db.VarChar(500)
}
4 changes: 2 additions & 2 deletions src/DTO/missionDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export interface DailyMissionDTO {
actions: ActionNameDTO[];
}
export interface SituationStatDTO {
id: number;
id?: number;
count: string | null | number;
name: string;
missions?: NotTodoStatDTO[];
}

export interface NotTodoStatDTO {
id: number;
id?: number;
count: string | null | number;
title: string;
}
3 changes: 3 additions & 0 deletions src/constants/responseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@ export default {

// 상황
SITUATIONS_GET_SUCCESS: '상황 불러오기 성공',

// 배너
READ_BANNER_SUCCESS: '배너 불러오기 성공',
};
32 changes: 32 additions & 0 deletions src/controllers/bannerController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Request, Response } from 'express';
import { message, statusCode } from '../constants';
import { fail, success } from '../constants/response';

import { slackMessage } from '../modules/slackMessage';
import { sendMessageToSlack } from '../modules/slackAPI';
import bannerService from '../service/bannerService';

/**
* @route GET /mission/banner
* @desc Get banner
* @access Public
*/
const getBanner = async (req: Request, res: Response) => {
const userId = req.body.userId;
try {
const data = await bannerService.getBanner(userId);
return res.status(statusCode.OK).send(success(statusCode.OK, message.READ_BANNER_SUCCESS, data));
} catch (error) {
if (error === 400) {
res.status(statusCode.BAD_REQUEST).send(fail(statusCode.BAD_REQUEST, message.INVALID_DATE_TYPE));
return;
}
const errorMessage: string = slackMessage(req.method.toUpperCase(), req.originalUrl, error, req.body.user?.id);
sendMessageToSlack(errorMessage);
res.status(statusCode.INTERNAL_SERVER_ERROR).send(fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
}
};

export default {
getBanner,
}
1 change: 1 addition & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { default as authController } from './authController';
export { default as environmentController } from './environmentController';
export { default as missionController } from './missionController';
export { default as situationController } from './situationController';
export { default as bannerController } from './bannerController';
10 changes: 10 additions & 0 deletions src/modules/koreanTimeConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const KR_TIME_DIFF = 9 * 60 * 60 * 1000;

const getKoreanTime = async () => {
const curr = new Date();
const utc = curr.getTime() + curr.getTimezoneOffset() * 60 * 1000;
const kr_curr = new Date(utc + KR_TIME_DIFF);
return kr_curr;
};

export default getKoreanTime;
23 changes: 23 additions & 0 deletions src/modules/shuffleBanner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

const getRandomBanner = async (bannerCategory: string) => {
const banners = await prisma.banner.findMany({
where: {
banner_category: bannerCategory,
},
select: {
title: true,
image: true,
}
});

return shuffleAndSelect(banners);
};

const shuffleAndSelect = (array: Array<{ title: string; image: string }>) => {
const randomArray = array.sort(() => Math.random() - 0.5);
return randomArray[0];
};

export default getRandomBanner;
9 changes: 9 additions & 0 deletions src/router/bannerRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Router } from 'express';
import { auth } from '../middlewares';
import { bannerController } from '../controllers';

const router: Router = Router();

router.get('/', auth, bannerController.getBanner);

export default router;
2 changes: 2 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Router } from 'express';
import authRouter from './authRouter';
import bannerRouter from './bannerRouter';
import environmentRouter from './environmentRouter';
import missionRouter from './missionRouter';
import situationRouter from './situationRouter'
Expand All @@ -10,5 +11,6 @@ router.use('/auth', authRouter);
router.use('/environment', environmentRouter);
router.use('/mission', missionRouter);
router.use('/situation', situationRouter);
router.use('/banner', bannerRouter);

export default router;
26 changes: 26 additions & 0 deletions src/service/bannerService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PrismaClient } from '@prisma/client';
import getRandomBanner from '../modules/shuffleBanner';
import getKoreanTime from '../modules/koreanTimeConverter';
const prisma = new PrismaClient();

const getBanner = async (userId: number) => {
const dailyMissions = await prisma.mission.findMany({
where: {
user_id: userId,
action_date: await getKoreanTime(),
},
select: {
completion_status: true,
},
});

const notYetCount = dailyMissions.filter((x) => x.completion_status?.includes('NOTYET')).length;
if (notYetCount > 0 || dailyMissions.length === 0) {
return getRandomBanner('NOTYET');
}
return getRandomBanner('FINISH');
};

export default {
getBanner,
};
4 changes: 2 additions & 2 deletions src/service/missionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const getMissionCount = async (userId: number, startDate: Date, lastDate: Date)

const getDailyMission = async (userId: number, date: string) => {
const actionDate: Date = new Date(date);

const dailyMissions = await prisma.mission.findMany({
where: {
user_id: userId,
Expand Down Expand Up @@ -256,7 +256,7 @@ const getSituationStat = async (userId: number) => {

const notTodo: NotTodoStatDTO[] = await prisma.$queryRaw(
Prisma.sql`
SELECT not_todo.id, count(not_todo_id), title
SELECT count(not_todo_id), title
FROM mission, not_todo
WHERE user_id = ${userId} AND situation_id = ${x.id} AND mission.not_todo_id = not_todo.id
AND action_date > ${startDate} AND action_date <= ${lastDate}
Expand Down

0 comments on commit 3d98775

Please sign in to comment.