Skip to content

Commit

Permalink
Merge pull request #241 from boostcampwm2023/feat/237-reward-credit-api
Browse files Browse the repository at this point in the history
[Feat] 새 일기 작성 시 별가루 적립 및 조회 API 구현
  • Loading branch information
mingxoxo authored Dec 7, 2023
2 parents b0d34ab + 690070c commit 2e3be3f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions BE/src/auth/users.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ export class UsersRepository {
async getUserByUserId(userId: string): Promise<User | null> {
return User.findOne({ where: { userId } });
}

async accrueCredits(user: User, amount: number): Promise<void> {
user.credit += amount;
await user.save();
}
}
2 changes: 2 additions & 0 deletions BE/src/diaries/diaries.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -26,6 +27,7 @@ import { Line } from "src/lines/lines.entity";
DiariesRepository,
TagsRepository,
ShapesRepository,
UsersRepository,
],
})
export class DiariesModule {}
19 changes: 19 additions & 0 deletions BE/src/diaries/diaries.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -120,4 +121,22 @@ export class DiariesRepository {

return found;
}

async getDiaryByToday(userId: string): Promise<Diary[]> {
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;
}
}
14 changes: 14 additions & 0 deletions BE/src/diaries/diaries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -45,6 +53,12 @@ export class DiariesService {
return diary;
}

async isFirstDiaryForToday(userId: string): Promise<boolean> {
const todayDiaries = await this.diariesRepository.getDiaryByToday(userId);

return todayDiaries.length === 0;
}

async readDiary(readDiaryDto: ReadDiaryDto): Promise<Diary> {
let diary = await this.diariesRepository.readDiary(readDiaryDto);

Expand Down
5 changes: 5 additions & 0 deletions BE/src/purchase/purchase.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreditDto> {
return new CreditDto(user.credit);
}

@Post("/design")
async purchaseDesign(
@GetUser() user: User,
Expand Down

0 comments on commit 2e3be3f

Please sign in to comment.