tempLessonApply.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 TempLessonApplyService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'templessonapply');
  11. this.model = this.ctx.model.Apply.TempLessonApply;
  12. this.payOrderService = this.ctx.service.business.payOrder;
  13. }
  14. async beforeCreate(data) {
  15. const query = _.pick(data, [ 'school_id', 'lesson_id', 'student_id', 'coach_id' ]);
  16. const num = await this.model.count(query);
  17. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '学生已申请临时参加课程');
  18. return data;
  19. }
  20. // 去支付,生成支付单
  21. async toPay({ id, payer_id }) {
  22. const data = await this.model.findById(id);
  23. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到临时上课的申请,无法进行支付');
  24. const { student_id, coach_id, lesson_id, school_id, money } = data;
  25. const payer_role = payer_id === student_id ? 'Student' : payer_id === coach_id ? 'Coach' : null;
  26. if (!payer_role) throw new BusinessError(ErrorCode.DATA_INVALID, '该支付用户不是学生也不是教练');
  27. const obj = { payer_id, payer_role, pay_for: 'Lesson', from_id: lesson_id, school_id, money, time: moment().format('YYYY-MM-DD HH:mm:ss'), desc: '私教课' };
  28. // 找openid
  29. const user = await this.ctx.model.User[payer_role].findById(payer_id).populate('user_id');
  30. if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户信息');
  31. const openid = _.get(user, 'user_id.openid');
  32. if (!openid) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户微信信息');
  33. obj.openid = openid;
  34. const res = await this.payOrderService.create(obj);
  35. return res;
  36. }
  37. // 处理审核成功的情况
  38. async afterUpdate(filter, body, data) {
  39. return data;
  40. }
  41. }
  42. module.exports = TempLessonApplyService;