relationCoachSchool.js 795 B

12345678910111213141516171819202122232425
  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 RelationCoachSchoolService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'relationcoachschool');
  10. this.model = this.ctx.model.Relation.RelationCoachSchool;
  11. }
  12. // 创建关系前,先查下有没有,有就别创建了
  13. async beforeCreate(data) {
  14. const { coach_id, school_id } = data;
  15. const num = await this.model.count({ coach_id, school_id });
  16. if (num <= 0) {
  17. return data;
  18. }
  19. throw new BusinessError(ErrorCode.DATA_EXISTED, '教练已在学校备案,无需重复添加');
  20. }
  21. }
  22. module.exports = RelationCoachSchoolService;