From 0e47f82d5b243bd34db65124c7c77edb301485d0 Mon Sep 17 00:00:00 2001 From: HUAHUAI23 Date: Fri, 14 Jul 2023 08:01:20 +0000 Subject: [PATCH 1/3] feat(server): add giftcode expired time --- server/src/account/account.controller.ts | 19 +++++++++++++++++++ server/src/account/account.service.ts | 16 +++++++++++++++- .../src/account/entities/account-gift-code.ts | 1 + 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/server/src/account/account.controller.ts b/server/src/account/account.controller.ts index 3b75c769ea..a44f4f09a3 100644 --- a/server/src/account/account.controller.ts +++ b/server/src/account/account.controller.ts @@ -332,10 +332,29 @@ export class AccountController { @UseGuards(JwtAuthGuard) @Post('gift-code') async giftCode(@Req() req: IRequest, @Body() dto: UseGiftCodeDto) { + const expiredCondition = true + const expired = await this.accountService.findOneGiftCode( + dto.code, + expiredCondition, + ) + if (expired) { + return ResponseUtil.error('gift code have expired') + } + + const usedCondition = true + const used = await this.accountService.findOneGiftCode( + dto.code, + usedCondition, + ) + if (used) { + return ResponseUtil.error('gift code have expired') + } + const found = await this.accountService.findOneGiftCode(dto.code) if (!found) { return ResponseUtil.error("gift code doesn't exist") } + const res = await this.accountService.useGiftCode(req.user._id, dto.code) return ResponseUtil.ok(res) } diff --git a/server/src/account/account.service.ts b/server/src/account/account.service.ts index 4cdc9162cb..195e355ce9 100644 --- a/server/src/account/account.service.ts +++ b/server/src/account/account.service.ts @@ -384,10 +384,24 @@ export class AccountService { } } - async findOneGiftCode(code: string, used = false): Promise { + async findOneGiftCode( + code: string, + used = false, + expired = false, + ): Promise { + if (expired) { + const giftCode = await this.db.collection('GiftCode').findOne({ + code: code, + used: used, + expiredAt: { $lte: new Date() }, + }) + return giftCode + } + const giftCode = await this.db.collection('GiftCode').findOne({ code: code, used: used, + expiredAt: { $gte: new Date() }, }) return giftCode diff --git a/server/src/account/entities/account-gift-code.ts b/server/src/account/entities/account-gift-code.ts index 0c99824c45..e2b821ca2e 100644 --- a/server/src/account/entities/account-gift-code.ts +++ b/server/src/account/entities/account-gift-code.ts @@ -8,5 +8,6 @@ export class GiftCode { usedBy?: ObjectId usedAt?: Date createdAt: Date + expiredAt?: Date transactionId?: ObjectId } From d0e239315309d9f5a752e8a9546ee0d3d11192d5 Mon Sep 17 00:00:00 2001 From: HUAHUAI23 Date: Mon, 17 Jul 2023 04:07:07 +0000 Subject: [PATCH 2/3] chore --- server/src/account/account.controller.ts | 24 ++++++++++++++---------- server/src/account/account.service.ts | 21 +++++++++++++++------ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/server/src/account/account.controller.ts b/server/src/account/account.controller.ts index a44f4f09a3..4cb249b8e8 100644 --- a/server/src/account/account.controller.ts +++ b/server/src/account/account.controller.ts @@ -332,25 +332,29 @@ export class AccountController { @UseGuards(JwtAuthGuard) @Post('gift-code') async giftCode(@Req() req: IRequest, @Body() dto: UseGiftCodeDto) { - const expiredCondition = true + const condition = { + expired: true, + used: null, + } const expired = await this.accountService.findOneGiftCode( dto.code, - expiredCondition, + condition, ) if (expired) { - return ResponseUtil.error('gift code have expired') + return ResponseUtil.error('gift code has expired') } - const usedCondition = true - const used = await this.accountService.findOneGiftCode( - dto.code, - usedCondition, - ) + condition.expired = false + condition.used = true + const used = await this.accountService.findOneGiftCode(dto.code, condition) if (used) { - return ResponseUtil.error('gift code have expired') + return ResponseUtil.error('gift code has been used') } - const found = await this.accountService.findOneGiftCode(dto.code) + condition.expired = false + condition.used = false + console.log(111) + const found = await this.accountService.findOneGiftCode(dto.code, condition) if (!found) { return ResponseUtil.error("gift code doesn't exist") } diff --git a/server/src/account/account.service.ts b/server/src/account/account.service.ts index 195e355ce9..532ea964f2 100644 --- a/server/src/account/account.service.ts +++ b/server/src/account/account.service.ts @@ -310,7 +310,10 @@ export class AccountService { const client = SystemDatabase.client const session = client.startSession() - const giftCode = await this.findOneGiftCode(code) + const giftCode = await this.findOneGiftCode(code, { + expired: false, + used: false, + }) const account = await this.findOne(userid) try { @@ -386,21 +389,27 @@ export class AccountService { async findOneGiftCode( code: string, - used = false, - expired = false, + condition?: any, ): Promise { - if (expired) { + if (condition.expired) { const giftCode = await this.db.collection('GiftCode').findOne({ code: code, - used: used, expiredAt: { $lte: new Date() }, }) return giftCode } + if (condition.used) { + const giftCode = await this.db.collection('GiftCode').findOne({ + code: code, + used: true, + }) + return giftCode + } + const giftCode = await this.db.collection('GiftCode').findOne({ code: code, - used: used, + used: false, expiredAt: { $gte: new Date() }, }) From d008f4c09c6438a9faa68d06e6088d34039bd090 Mon Sep 17 00:00:00 2001 From: HUAHUAI23 Date: Tue, 25 Jul 2023 10:46:05 +0000 Subject: [PATCH 3/3] chore(server): refactor --- server/src/account/account.controller.ts | 27 ++++------------------ server/src/account/account.service.ts | 29 ++---------------------- 2 files changed, 7 insertions(+), 49 deletions(-) diff --git a/server/src/account/account.controller.ts b/server/src/account/account.controller.ts index 4cb249b8e8..1364dae6e7 100644 --- a/server/src/account/account.controller.ts +++ b/server/src/account/account.controller.ts @@ -332,33 +332,16 @@ export class AccountController { @UseGuards(JwtAuthGuard) @Post('gift-code') async giftCode(@Req() req: IRequest, @Body() dto: UseGiftCodeDto) { - const condition = { - expired: true, - used: null, + const giftCode = await this.accountService.findOneGiftCode(dto.code) + if (!giftCode) { + return ResponseUtil.error("gift code doesn't exist") } - const expired = await this.accountService.findOneGiftCode( - dto.code, - condition, - ) - if (expired) { + if (giftCode.expiredAt < new Date()) { return ResponseUtil.error('gift code has expired') } - - condition.expired = false - condition.used = true - const used = await this.accountService.findOneGiftCode(dto.code, condition) - if (used) { + if (giftCode.used === true) { return ResponseUtil.error('gift code has been used') } - - condition.expired = false - condition.used = false - console.log(111) - const found = await this.accountService.findOneGiftCode(dto.code, condition) - if (!found) { - return ResponseUtil.error("gift code doesn't exist") - } - const res = await this.accountService.useGiftCode(req.user._id, dto.code) return ResponseUtil.ok(res) } diff --git a/server/src/account/account.service.ts b/server/src/account/account.service.ts index 532ea964f2..67b0a46abb 100644 --- a/server/src/account/account.service.ts +++ b/server/src/account/account.service.ts @@ -310,10 +310,7 @@ export class AccountService { const client = SystemDatabase.client const session = client.startSession() - const giftCode = await this.findOneGiftCode(code, { - expired: false, - used: false, - }) + const giftCode = await this.findOneGiftCode(code) const account = await this.findOne(userid) try { @@ -387,32 +384,10 @@ export class AccountService { } } - async findOneGiftCode( - code: string, - condition?: any, - ): Promise { - if (condition.expired) { - const giftCode = await this.db.collection('GiftCode').findOne({ - code: code, - expiredAt: { $lte: new Date() }, - }) - return giftCode - } - - if (condition.used) { - const giftCode = await this.db.collection('GiftCode').findOne({ - code: code, - used: true, - }) - return giftCode - } - + async findOneGiftCode(code: string): Promise { const giftCode = await this.db.collection('GiftCode').findOne({ code: code, - used: false, - expiredAt: { $gte: new Date() }, }) - return giftCode }