From 247b20f3ed76aacd493a25d6096fe31560eacf9e Mon Sep 17 00:00:00 2001 From: jeongmin Date: Thu, 7 Dec 2023 16:07:11 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EC=9C=A0=EC=A0=80=20=EB=B3=84?= =?UTF-8?q?=EA=B0=80=EB=A3=A8=20=EC=9E=94=EC=95=A1=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/purchase/purchase.controller.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/BE/src/purchase/purchase.controller.ts b/BE/src/purchase/purchase.controller.ts index fbddb35..795bd75 100644 --- a/BE/src/purchase/purchase.controller.ts +++ b/BE/src/purchase/purchase.controller.ts @@ -18,6 +18,11 @@ import { CreditDto } from "./dto/purchase.credit.dto"; export class PurchaseController { constructor(private purchaseService: PurchaseService) {} + @Get("/credit") + async getCreditBalanceByUser(@GetUser() user: User): Promise { + return new CreditDto(user.credit); + } + @Post("/design") async purchaseDesign( @GetUser() user: User, From 690070c858d2c978e84a20ff553d38eb2f2e316e Mon Sep 17 00:00:00 2001 From: jeongmin Date: Thu, 7 Dec 2023 17:14:19 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=EC=83=88=EB=A1=9C=EC=9A=B4=20?= =?UTF-8?q?=EC=9D=BC=EA=B8=B0=20=EC=9E=91=EC=84=B1=20=EC=8B=9C=20=EB=B3=84?= =?UTF-8?q?=EA=B0=80=EB=A3=A8=20=EC=A7=80=EA=B8=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - new Date는 UTC로 받아와지고, mysql Date도 UTC로 가져올때도 UTC로 가져옴 - 30 별가루 지급 --- BE/src/auth/users.repository.ts | 5 +++++ BE/src/diaries/diaries.module.ts | 2 ++ BE/src/diaries/diaries.repository.ts | 19 +++++++++++++++++++ BE/src/diaries/diaries.service.ts | 14 ++++++++++++++ 4 files changed, 40 insertions(+) diff --git a/BE/src/auth/users.repository.ts b/BE/src/auth/users.repository.ts index 2482edb..89cd62b 100644 --- a/BE/src/auth/users.repository.ts +++ b/BE/src/auth/users.repository.ts @@ -42,4 +42,9 @@ export class UsersRepository { async getUserByUserId(userId: string): Promise { return User.findOne({ where: { userId } }); } + + async accrueCredits(user: User, amount: number): Promise { + user.credit += amount; + await user.save(); + } } diff --git a/BE/src/diaries/diaries.module.ts b/BE/src/diaries/diaries.module.ts index 8a9dd4e..2f8df0e 100644 --- a/BE/src/diaries/diaries.module.ts +++ b/BE/src/diaries/diaries.module.ts @@ -11,6 +11,7 @@ import { ShapesRepository } from "src/shapes/shapes.repository"; import { TagsRepository } from "src/tags/tags.repository"; import { HttpModule } from "@nestjs/axios"; import { Line } from "src/lines/lines.entity"; +import { UsersRepository } from "src/auth/users.repository"; @Module({ imports: [ @@ -26,6 +27,7 @@ import { Line } from "src/lines/lines.entity"; DiariesRepository, TagsRepository, ShapesRepository, + UsersRepository, ], }) export class DiariesModule {} diff --git a/BE/src/diaries/diaries.repository.ts b/BE/src/diaries/diaries.repository.ts index 468bc62..dd68bf7 100644 --- a/BE/src/diaries/diaries.repository.ts +++ b/BE/src/diaries/diaries.repository.ts @@ -12,6 +12,7 @@ import { NotFoundException } from "@nestjs/common"; import { Tag } from "src/tags/tags.entity"; import { ReadDiaryDto } from "./dto/diaries.read.dto"; import { SentimentDto } from "./dto/diaries.sentiment.dto"; +import { Between } from "typeorm"; export class DiariesRepository { async createDiary( @@ -120,4 +121,22 @@ export class DiariesRepository { return found; } + + async getDiaryByToday(userId: string): Promise { + const currentDate = new Date(); + const [year, month, date] = [ + currentDate.getFullYear(), + currentDate.getMonth(), + currentDate.getDate(), + ]; + const startDate = new Date(year, month, date, 0, 0, 0, 0); + const endDate = new Date(year, month, date, 23, 59, 59, 999); + + const diaryList = await Diary.find({ + where: { user: { userId }, createdDate: Between(startDate, endDate) }, + withDeleted: true, + }); + + return diaryList; + } } diff --git a/BE/src/diaries/diaries.service.ts b/BE/src/diaries/diaries.service.ts index d973a64..988a5e9 100644 --- a/BE/src/diaries/diaries.service.ts +++ b/BE/src/diaries/diaries.service.ts @@ -16,10 +16,12 @@ import { ShapesRepository } from "src/shapes/shapes.repository"; import { HttpService } from "@nestjs/axios"; import { lastValueFrom } from "rxjs"; import { sentimentStatus } from "src/utils/enum"; +import { UsersRepository } from "src/auth/users.repository"; @Injectable() export class DiariesService { constructor( + private usersRepository: UsersRepository, private diariesRepository: DiariesRepository, private tagsRepository: TagsRepository, private shapesRepository: ShapesRepository, @@ -33,6 +35,12 @@ export class DiariesService { const encryptedContent = await this.getEncryptedContent(trimContent); const tagEntities = await this.getTags(tags); const sentimentResult: SentimentDto = await this.getSentiment(trimContent); + + const WRITE_REWARD_CREDIT = 30; + if (await this.isFirstDiaryForToday(user.userId)) { + await this.usersRepository.accrueCredits(user, WRITE_REWARD_CREDIT); + } + const diary = await this.diariesRepository.createDiary( createDiaryDto, encryptedContent, @@ -45,6 +53,12 @@ export class DiariesService { return diary; } + async isFirstDiaryForToday(userId: string): Promise { + const todayDiaries = await this.diariesRepository.getDiaryByToday(userId); + + return todayDiaries.length === 0; + } + async readDiary(readDiaryDto: ReadDiaryDto): Promise { let diary = await this.diariesRepository.readDiary(readDiaryDto);