cashOut.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const moment = require('moment');
  7. //
  8. class CashOutService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'cashout');
  11. this.model = this.ctx.model.User.CashOut;
  12. this.cashBackModel = this.ctx.model.User.CashBack;
  13. }
  14. async beforeCreate(data) {
  15. const { customer } = data;
  16. if (!customer) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '缺少提款人');
  17. const num = await this.model.count({ customer, status: '0' });
  18. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '您有未处理提现申请,请联系管理员先处理之前的申请');
  19. return data;
  20. }
  21. async afterUpdate(filter, body, data) {
  22. const { status } = body;
  23. if (status === '1') {
  24. const { customer: inviter } = data;
  25. const obj = { inviter, time: moment().format('YYYY-MM-DD HH:mm:ss'), status: '-1', source: '-1', source_id: data._id };
  26. await this.cashBackModel.create(obj);
  27. }
  28. return data;
  29. }
  30. }
  31. module.exports = CashOutService;