From e01f29a8e590acfe76f3e53f03fcaf8831ea4337 Mon Sep 17 00:00:00 2001 From: leGit-y Date: Sat, 16 Dec 2023 00:18:34 +0900 Subject: [PATCH] =?UTF-8?q?[FIX]=20=EC=B5=9C=EC=A2=85=EA=B2=B0=EC=A0=95=20?= =?UTF-8?q?=EC=99=84=EB=A3=8C=EC=8B=9C,=20used=20template=20=EC=97=85?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/interfaces/DAO/worryDAO.ts | 14 +++++- src/repository/worryRepository.ts | 74 ++++++++++++++++--------------- src/service/worryService.ts | 18 ++++---- 3 files changed, 60 insertions(+), 46 deletions(-) diff --git a/src/interfaces/DAO/worryDAO.ts b/src/interfaces/DAO/worryDAO.ts index 6e918c9..3572c4b 100644 --- a/src/interfaces/DAO/worryDAO.ts +++ b/src/interfaces/DAO/worryDAO.ts @@ -1,4 +1,4 @@ -import { worryCreateDTO } from "../DTO/worryDTO"; +import { finalAnswerCreateDTO, worryCreateDTO } from "../DTO/worryDTO"; interface worryCreateDAO extends worryCreateDTO{ deadlineDate: Date | null; @@ -11,8 +11,18 @@ interface deadlineUpdateDAO{ worryId: number; deadline: Date | null; } + +interface deadlineUpdateDAO{ + worryId: number; + deadline: Date | null; +} + +interface finalAnswerCreateDAO extends finalAnswerCreateDTO{ + templateId: number; +} export { worryCreateDAO, - deadlineUpdateDAO + deadlineUpdateDAO, + finalAnswerCreateDAO } \ No newline at end of file diff --git a/src/repository/worryRepository.ts b/src/repository/worryRepository.ts index 494a336..b008766 100644 --- a/src/repository/worryRepository.ts +++ b/src/repository/worryRepository.ts @@ -1,5 +1,5 @@ import prisma from "./prismaClient" -import { worryCreateDAO,deadlineUpdateDAO } from "../interfaces/DAO/worryDAO"; +import { worryCreateDAO,deadlineUpdateDAO, finalAnswerCreateDAO } from "../interfaces/DAO/worryDAO"; import { finalAnswerCreateDTO, worryUpdateDTO } from "../interfaces/DTO/worryDTO"; // created_at, updated_at 은 디비에 저장시 utc 값으로 저장 @@ -7,7 +7,7 @@ import { finalAnswerCreateDTO, worryUpdateDTO } from "../interfaces/DTO/worryDTO const createWorry = async(worryCreateDAO: worryCreateDAO) => { - const createdWorry = prisma.worry.create({ + return prisma.worry.create({ data: { template_id: worryCreateDAO.templateId, user_id: worryCreateDAO.userId, @@ -19,36 +19,6 @@ const createWorry = async(worryCreateDAO: worryCreateDAO) => { } }) - const user = await prisma.user.findUnique({ - select:{ - used_template: true - }, - where:{ - id: worryCreateDAO.userId - } - }) - if(!user){ - return null - } - - const usedTemplate = user.used_template - // 이미 usedTemplate에 해당 templateId가 저장되어 있는 경우 (usedTemplate update 필요 x) - if(usedTemplate != null && usedTemplate.includes(worryCreateDAO.templateId)){ - return await prisma.$transaction([createdWorry]) - } - - const updateUsedTemplate = prisma.user.update({ - where: { - id: worryCreateDAO.userId - }, - data: { - used_template: { - push: worryCreateDAO.templateId - } - } - }) - - return await prisma.$transaction([createdWorry,updateUsedTemplate]) } const updateWorry = async(worryUpdateDTO: worryUpdateDTO) => { @@ -105,17 +75,49 @@ const findWorryById = async(worryId:number) => { } -const createFinalAnswer = async(finalAnswerCreateDTO: finalAnswerCreateDTO) => { +const createFinalAnswer = async(finalAnswerCreateDAO: finalAnswerCreateDAO) => { - return await prisma.worry.update({ + const user = await prisma.user.findUnique({ + select:{ + used_template: true + }, + where:{ + id: finalAnswerCreateDAO.userId + } + }) + if(!user){ + return null; + } + + const createdFinalAnswer = prisma.worry.update({ where: { - id: finalAnswerCreateDTO.worryId + id: finalAnswerCreateDAO.worryId }, data: { updated_at: new Date(), - final_answer: finalAnswerCreateDTO.finalAnswer + final_answer: finalAnswerCreateDAO.finalAnswer } }) + + const usedTemplate = user.used_template + // 이미 usedTemplate에 해당 templateId가 저장되어 있는 경우 (usedTemplate update 필요 x) + if(usedTemplate.includes(finalAnswerCreateDAO.templateId)){ + return await prisma.$transaction([createdFinalAnswer]) + } + + const updateUsedTemplate = prisma.user.update({ + where: { + id: finalAnswerCreateDAO.userId + }, + data: { + used_template: { + push: finalAnswerCreateDAO.templateId + } + } + }) + + return await prisma.$transaction([createdFinalAnswer,updateUsedTemplate]) + } const updateDeadline = async(deadlineUpdateDAO: deadlineUpdateDAO) => { diff --git a/src/service/worryService.ts b/src/service/worryService.ts index a5fca22..ed28724 100644 --- a/src/service/worryService.ts +++ b/src/service/worryService.ts @@ -2,7 +2,7 @@ import { ClientException } from "../common/error/exceptions/customExceptions"; import { rm } from "../constants"; import worryRepository from "../repository/worryRepository" import { finalAnswerCreateDTO, worryCreateDTO, worryUpdateDTO, deadlineUpdateDTO } from "../interfaces/DTO/worryDTO"; -import { deadlineUpdateDAO, worryCreateDAO } from "../interfaces/DAO/worryDAO"; +import { deadlineUpdateDAO, finalAnswerCreateDAO, worryCreateDAO } from "../interfaces/DAO/worryDAO"; import templateRepository from "../repository/templateRepository"; import { calculate_d_day, calculate_random_num } from "../common/utils/calculate"; import reviewRepository from "../repository/reviewRepository"; @@ -30,13 +30,11 @@ const postWorry =async (worryCreateDTO: worryCreateDTO) => { } // console.log(worryCreateDAO) - const result = await worryRepository.createWorry(worryCreateDAO); - if (!result) { + const worry = await worryRepository.createWorry(worryCreateDAO); + if (!worry) { throw new ClientException(rm.CREATE_WORRY_FAIL); } - - // result[0]: createdWorry | result[1]: updatedUsedTemplate - const worry = result[0] + const data = { createdAt: moment(worry.created_at).utc().utcOffset(9).format('YYYY-MM-DD'), deadline: "데드라인이 없습니다." @@ -162,7 +160,11 @@ const patchFinalAnswer =async (finalAnswerCreateDTO: finalAnswerCreateDTO) => { throw new ClientException("한 번 내린 최종결정은 수정 불가합니다."); } - const updatedWorry = await worryRepository.createFinalAnswer(finalAnswerCreateDTO); + const finalAnswerCreateDAO: finalAnswerCreateDAO = { + ...finalAnswerCreateDTO, + templateId: worry.template_id + } + const updatedWorry = await worryRepository.createFinalAnswer(finalAnswerCreateDAO); if (!updatedWorry) { throw new ClientException(rm.MAKE_FINAL_ANSWER_FAIL); } @@ -175,7 +177,7 @@ const patchFinalAnswer =async (finalAnswerCreateDTO: finalAnswerCreateDTO) => { } const alarm_data = { - "templateId": updatedWorry.template_id + "templateId": worry.template_id } const data = {