75 lines
3.5 KiB
JavaScript
75 lines
3.5 KiB
JavaScript
"use strict";
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.RechargeService = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const typeorm_1 = require("@nestjs/typeorm");
|
|
const typeorm_2 = require("typeorm");
|
|
const recharge_model_1 = require("../../../model/recharge.model");
|
|
const api_1 = require("../../common/api");
|
|
const user_id_context_1 = require("../../common/context/user_id.context");
|
|
const order_no_generator_1 = require("../../common/util/order_no_generator");
|
|
let RechargeService = class RechargeService {
|
|
rechargeModel;
|
|
constructor(rechargeModel) {
|
|
this.rechargeModel = rechargeModel;
|
|
}
|
|
async applyRecharge(amount) {
|
|
const userId = (0, user_id_context_1.getUserIdContext)();
|
|
const amountNum = parseFloat(amount);
|
|
if (isNaN(amountNum) || amountNum <= 0) {
|
|
return api_1.Api.error('无效的充值金额');
|
|
}
|
|
const pendingOrder = await this.rechargeModel.findOne({
|
|
where: {
|
|
userId,
|
|
status: recharge_model_1.RechargeStatus.Pending,
|
|
},
|
|
});
|
|
if (pendingOrder) {
|
|
return api_1.Api.error('您有一笔未支付的订单,请先完成支付或取消后再创建新订单');
|
|
}
|
|
const innerOrderNo = order_no_generator_1.OrderNoGenerator.generateWithPrefix('RC');
|
|
const recharge = this.rechargeModel.create({
|
|
userId,
|
|
amount,
|
|
actualAmount: amount,
|
|
innerOrderNo,
|
|
status: recharge_model_1.RechargeStatus.Pending,
|
|
});
|
|
await this.rechargeModel.save(recharge);
|
|
return api_1.Api.success(recharge);
|
|
}
|
|
async cancelOrder(orderId) {
|
|
const userId = (0, user_id_context_1.getUserIdContext)();
|
|
const order = await this.rechargeModel.findOne({
|
|
where: { id: orderId, userId },
|
|
});
|
|
if (!order) {
|
|
return api_1.Api.error('订单不存在');
|
|
}
|
|
if (order.status !== recharge_model_1.RechargeStatus.Pending) {
|
|
return api_1.Api.error('只有待支付订单才能取消');
|
|
}
|
|
await this.rechargeModel.update({ id: orderId }, { status: recharge_model_1.RechargeStatus.Closed });
|
|
return api_1.Api.success();
|
|
}
|
|
};
|
|
exports.RechargeService = RechargeService;
|
|
exports.RechargeService = RechargeService = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__param(0, (0, typeorm_1.InjectRepository)(recharge_model_1.RechargeModel)),
|
|
__metadata("design:paramtypes", [typeorm_2.Repository])
|
|
], RechargeService);
|
|
//# sourceMappingURL=recharge.service.js.map
|