team.js 940 B

1234567891011121314151617181920212223242526272829
  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 res = await this.model.find({ members: { $elemMatch: { user_id: user_id } } });
  23. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  24. return res;
  25. }
  26. }
  27. module.exports = TeamService;