matchProject.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 MatchProjectService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'matchproject');
  10. this.model = this.ctx.model.Race.MatchProject;
  11. this.matchModel = this.ctx.model.Race.Match;
  12. }
  13. async beforeCreate(data) {
  14. const r = await this.canOpera(data);
  15. if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
  16. return data;
  17. }
  18. async beforeUpdate(filter, update) {
  19. const r = await this.canOpera({ ...filter, ...update });
  20. if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
  21. return { filter, update };
  22. }
  23. async beforeDelete(filter) {
  24. const r = await this.canOpera(filter);
  25. if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
  26. return filter;
  27. }
  28. /**
  29. * 根据该赛事状态,判断是否可以操作
  30. * 赛事状态为 0 可以操作, 其余状态不能操作
  31. * @param {Object} data 数据
  32. * @return {Boolean} 是否可以操作
  33. */
  34. async canOpera(data) {
  35. let match_id = _.get(data, 'match_id');
  36. if (!match_id) {
  37. const id = data.id || data._id;
  38. if (!id) throw new BusinessError(ErrorCode.DATA_INVALID, '缺少检查可操作条件');
  39. const d = await this.model.findById(id);
  40. match_id = _.get(d, 'match_id');
  41. }
  42. const num = await this.matchModel.count({ _id: match_id, status: '0' });
  43. return num > 0;
  44. }
  45. }
  46. module.exports = MatchProjectService;