1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- //
- class MatchGroupService extends CrudService {
- constructor(ctx) {
- super(ctx, 'matchgroup');
- this.model = this.ctx.model.Race.MatchGroup;
- this.matchModel = this.ctx.model.Race.Match;
- }
- async beforeCreate(data) {
- const r = await this.canOpera(data);
- if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
- return data;
- }
- async beforeUpdate(filter, update) {
- const r = await this.canOpera({ ...filter, ...update });
- if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
- return { filter, update };
- }
- async beforeDelete(filter) {
- const r = await this.canOpera(filter);
- if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
- return filter;
- }
- /**
- * 根据该赛事状态,判断是否可以操作
- * 赛事状态为 0 可以操作, 其余状态不能操作
- * @param {Object} data 数据
- * @return {Boolean} 是否可以操作
- */
- async canOpera(data) {
- let match_id = _.get(data, 'match_id');
- if (!match_id) {
- const id = data.id || data._id;
- if (!id) throw new BusinessError(ErrorCode.DATA_INVALID, '缺少检查可操作条件');
- const d = await this.model.findById(id);
- match_id = _.get(d, 'match_id');
- }
- const num = await this.matchModel.count({ _id: match_id, status: '0' });
- return num > 0;
- }
- }
- module.exports = MatchGroupService;
|