relationCoachSchool.js 924 B

1234567891011121314151617181920212223242526272829303132
  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. async afterQuery(filter, data) {
  22. // console.log(data);
  23. data = JSON.parse(JSON.stringify(data));
  24. return data;
  25. }
  26. }
  27. module.exports = RelationCoachSchoolService;