Skip to content

Commit

Permalink
Merge pull request #91 from TeamHARA/fix/usedTemplate
Browse files Browse the repository at this point in the history
[FIX] 최종결정 완료시, used template 업데이트
  • Loading branch information
leGit-y authored Dec 15, 2023
2 parents 18b68cd + e01f29a commit 0d3c69f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 46 deletions.
14 changes: 12 additions & 2 deletions src/interfaces/DAO/worryDAO.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { worryCreateDTO } from "../DTO/worryDTO";
import { finalAnswerCreateDTO, worryCreateDTO } from "../DTO/worryDTO";

interface worryCreateDAO extends worryCreateDTO{
deadlineDate: Date | null;
Expand All @@ -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
}
74 changes: 38 additions & 36 deletions src/repository/worryRepository.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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 값으로 저장
// deadline은 kst 값으로 저장

const createWorry = async(worryCreateDAO: worryCreateDAO) => {

const createdWorry = prisma.worry.create({
return prisma.worry.create({
data: {
template_id: worryCreateDAO.templateId,
user_id: worryCreateDAO.userId,
Expand All @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down
18 changes: 10 additions & 8 deletions src/service/worryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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: "데드라인이 없습니다."
Expand Down Expand Up @@ -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);
}
Expand All @@ -175,7 +177,7 @@ const patchFinalAnswer =async (finalAnswerCreateDTO: finalAnswerCreateDTO) => {
}

const alarm_data = {
"templateId": updatedWorry.template_id
"templateId": worry.template_id
}

const data = {
Expand Down

0 comments on commit 0d3c69f

Please sign in to comment.