cashOut.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 beforeUpdate(filter, update) {
  22. if (_.get(update, 'status') !== '0') {
  23. update.deal_person = _.get(this.ctx, 'admin._id');
  24. }
  25. return { filter, update };
  26. }
  27. async afterUpdate(filter, body, data) {
  28. const { status } = body;
  29. if (status === '1') {
  30. const { customer: inviter } = data;
  31. const obj = { inviter, time: moment().format('YYYY-MM-DD HH:mm:ss'), status: '-1', source: '-1', source_id: data._id };
  32. await this.cashBackModel.create(obj);
  33. }
  34. return data;
  35. }
  36. }
  37. module.exports = CashOutService;