Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(server): add expired time for gift code #1412

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion server/src/account/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,33 @@ export class AccountController {
@UseGuards(JwtAuthGuard)
@Post('gift-code')
async giftCode(@Req() req: IRequest, @Body() dto: UseGiftCodeDto) {
const found = await this.accountService.findOneGiftCode(dto.code)
const condition = {
expired: true,
used: null,
}
const expired = await this.accountService.findOneGiftCode(
dto.code,
condition,
)
if (expired) {
return ResponseUtil.error('gift code has expired')
}

condition.expired = false
condition.used = true
const used = await this.accountService.findOneGiftCode(dto.code, condition)
if (used) {
return ResponseUtil.error('gift code has been used')
}

condition.expired = false
condition.used = false
console.log(111)
0fatal marked this conversation as resolved.
Show resolved Hide resolved
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)
}
Expand Down
29 changes: 26 additions & 3 deletions server/src/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -384,10 +387,30 @@ export class AccountService {
}
}

async findOneGiftCode(code: string, used = false): Promise<GiftCode | null> {
async findOneGiftCode(
0fatal marked this conversation as resolved.
Show resolved Hide resolved
code: string,
condition?: any,
): Promise<GiftCode | null> {
if (condition.expired) {
const giftCode = await this.db.collection<GiftCode>('GiftCode').findOne({
code: code,
expiredAt: { $lte: new Date() },
})
return giftCode
}

if (condition.used) {
const giftCode = await this.db.collection<GiftCode>('GiftCode').findOne({
code: code,
used: true,
})
return giftCode
}

const giftCode = await this.db.collection<GiftCode>('GiftCode').findOne({
code: code,
used: used,
used: false,
expiredAt: { $gte: new Date() },
})

return giftCode
Expand Down
1 change: 1 addition & 0 deletions server/src/account/entities/account-gift-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export class GiftCode {
usedBy?: ObjectId
usedAt?: Date
createdAt: Date
expiredAt?: Date
transactionId?: ObjectId
}