team.js 964 B

123456789101112131415161718192021222324252627282930
  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 TeamService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'team');
  10. this.model = this.ctx.model.Team;
  11. }
  12. // 用户退出团队
  13. async leaves({ team_id, user_id } = {}) {
  14. const res = await this.model.findById(team_id);
  15. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  16. const members = res.members.filter(i => i.user_id !== user_id);
  17. res.members = members;
  18. await res.save();
  19. }
  20. // 用户所在团队列表
  21. async userteams({ user_id } = {}) {
  22. const condition = { members: { $elemMatch: { user_id } } };
  23. const res = await this.model.find(condition);
  24. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  25. return res;
  26. }
  27. }
  28. module.exports = TeamService;