lessonStudent.js 1.1 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. //
  7. class LessonStudentService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'lessonstudent');
  10. this.model = this.ctx.model.Business.LessonStudent;
  11. }
  12. async beforeCreate(body) {
  13. const { lesson_id, school_id, student_id } = body;
  14. // 检查是否已经有数据
  15. const data = await this.model.findOne({ lesson_id, school_id, student_id });
  16. // 数据已存在,检查是否是申请试听
  17. if (data) {
  18. const { is_try } = data;
  19. if (is_try === '0') throw new BusinessError(ErrorCode.DATA_EXISTED, '您已经报名');
  20. }
  21. // 数据不存在
  22. const { is_try } = body;
  23. // 检查是否使用试听名额
  24. if (is_try === '1') {
  25. const num = await this.model.count({ student_id, is_try: '1' });
  26. if (num > 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, '您已使用过免费试听的机会');
  27. }
  28. return body;
  29. }
  30. }
  31. module.exports = LessonStudentService;