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); 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,