relationStudentSchool.js 787 B

12345678910111213141516171819202122
  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 RelationStudentSchoolService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'relationstudentschool');
  10. this.model = this.ctx.model.Relation.RelationStudentSchool;
  11. }
  12. // 创建关系前,先查下有没有,有就别创建了
  13. async beforeCreate(data) {
  14. const { student_id, school_id } = data;
  15. const num = await this.model.count({ student_id, school_id });
  16. if (num <= 0) { return data; }
  17. throw new BusinessError(ErrorCode.DATA_EXISTED, '学员已入学,无需重复添加');
  18. }
  19. }
  20. module.exports = RelationStudentSchoolService;