Skip to content

Commit 88f5bec

Browse files
authored
feat(server): support charge reward (#1261)
1 parent 698e136 commit 88f5bec

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

server/src/account/account.controller.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import {
1111
} from '@nestjs/common'
1212
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'
1313
import { IRequest, IResponse } from 'src/utils/interface'
14-
import { ApiResponseObject, ResponseUtil } from 'src/utils/response'
14+
import {
15+
ApiResponseArray,
16+
ApiResponseObject,
17+
ResponseUtil,
18+
} from 'src/utils/response'
1519
import { AccountService } from './account.service'
1620
import {
1721
CreateChargeOrderDto,
@@ -35,6 +39,7 @@ import { SystemDatabase } from 'src/system-database'
3539
import { Account } from './entities/account'
3640
import { AccountTransaction } from './entities/account-transaction'
3741
import { JwtAuthGuard } from 'src/authentication/jwt.auth.guard'
42+
import { AccountChargeReward } from './entities/account-charge-reward'
3843

3944
@ApiTags('Account')
4045
@Controller('accounts')
@@ -112,6 +117,18 @@ export class AccountController {
112117
})
113118
}
114119

120+
/**
121+
* Get charge reward list
122+
*/
123+
@ApiOperation({ summary: 'Get charge reward list' })
124+
@ApiResponseArray(AccountChargeReward)
125+
@UseGuards(JwtAuthGuard)
126+
@Get('charge-reward')
127+
async getChargeRewardList() {
128+
const rewards = await this.accountService.findAllChargeRewards()
129+
return ResponseUtil.ok(rewards)
130+
}
131+
115132
/**
116133
* WeChat payment notify
117134
*/
@@ -178,12 +195,23 @@ export class AccountController {
178195
return
179196
}
180197

181-
// get & update account balance
198+
// get max reward
199+
const reward = await db
200+
.collection<AccountChargeReward>('AccountChargeReward')
201+
.findOne(
202+
{
203+
amount: { $lte: order.amount },
204+
},
205+
{ sort: { amount: -1 } },
206+
)
207+
208+
const realAmount = reward ? reward.amount + order.amount : order.amount
209+
182210
const ret = await db
183211
.collection<Account>('Account')
184212
.findOneAndUpdate(
185213
{ _id: order.accountId },
186-
{ $inc: { balance: order.amount } },
214+
{ $inc: { balance: realAmount } },
187215
{ session, returnDocument: 'after' },
188216
)
189217

@@ -194,6 +222,7 @@ export class AccountController {
194222
{
195223
accountId: order.accountId,
196224
amount: order.amount,
225+
reward: reward?.reward,
197226
balance: ret.value.balance,
198227
message: 'Recharge by WeChat Pay',
199228
orderId: order._id,

server/src/account/account.service.ts

+17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
PaymentChannelType,
1414
} from './entities/account-charge-order'
1515
import { AccountTransaction } from './entities/account-transaction'
16+
import { AccountChargeReward } from './entities/account-charge-reward'
1617

1718
@Injectable()
1819
export class AccountService {
@@ -172,4 +173,20 @@ export class AccountService {
172173

173174
throw new Error('Unsupported payment channel')
174175
}
176+
177+
async findAllChargeRewards() {
178+
const rewards = await this.db
179+
.collection<AccountChargeReward>('AccountChargeReward')
180+
.find(
181+
{},
182+
{
183+
sort: {
184+
amount: 1,
185+
},
186+
},
187+
)
188+
.toArray()
189+
190+
return rewards
191+
}
175192
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ApiProperty } from '@nestjs/swagger'
2+
import { ObjectId } from 'mongodb'
3+
4+
export class AccountChargeReward {
5+
@ApiProperty({ type: String })
6+
_id?: ObjectId
7+
8+
@ApiProperty()
9+
amount: number
10+
11+
@ApiProperty({})
12+
reward: number
13+
14+
@ApiProperty()
15+
createdAt: Date
16+
17+
@ApiProperty()
18+
updatedAt: Date
19+
}

server/src/account/entities/account-transaction.ts

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export class AccountTransaction {
1414
@ApiProperty()
1515
balance: number
1616

17+
@ApiProperty()
18+
reward?: number
19+
1720
@ApiProperty()
1821
message: string
1922

0 commit comments

Comments
 (0)