Skip to content

Commit

Permalink
Merge pull request #74 from TeamHARA/fix/usedTemplate
Browse files Browse the repository at this point in the history
[FEAT] 유저 used template 저장
  • Loading branch information
leGit-y authored Nov 12, 2023
2 parents 2a689a9 + 26c466d commit d589be0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
35 changes: 33 additions & 2 deletions src/repository/worryRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { finalAnswerCreateDTO, worryUpdateDTO } from "../interfaces/DTO/worryDTO
// deadline은 kst 값으로 저장

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

return await prisma.worry.create({
const createdWorry = prisma.worry.create({
data: {
template_id: worryCreateDAO.templateId,
user_id: worryCreateDAO.userId,
Expand All @@ -17,6 +17,37 @@ const createWorry = async(worryCreateDAO: worryCreateDAO) => {
deadline: worryCreateDAO.deadlineDate
}
})

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
6 changes: 4 additions & 2 deletions src/service/worryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ const postWorry =async (worryCreateDTO: worryCreateDTO) => {
}
// console.log(worryCreateDAO)

const worry = await worryRepository.createWorry(worryCreateDAO);
if (!worry) {
const result = await worryRepository.createWorry(worryCreateDAO);
if (!result) {
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

0 comments on commit d589be0

Please sign in to comment.