teamApply.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 TeamApplyService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'teamapply');
  10. this.model = this.ctx.model.Race.TeamApply;
  11. this.matchSignModel = this.ctx.model.Race.MatchSign;
  12. }
  13. /**
  14. * 查询项目下可以组队的人
  15. * @param {Object} query 地址栏查询条件
  16. * @property {String} project_id 项目地址
  17. * @property {String} user_id 项目地址
  18. */
  19. async findPartner({ project_id, user_id }) {
  20. // 多层嵌套populate
  21. const conn = this.app.mongooseDB.get('base');
  22. const schema = _.get(this.ctx.model, 'Base.User.schema');
  23. const m = conn.model('User', schema);
  24. let list = await this.matchSignModel.find({ user_id: { $ne: user_id }, pay_status: '1', project_id }).populate({
  25. path: 'user_id',
  26. select: 'user_id',
  27. populate: { path: 'user_id', model: m, select: 'name' },
  28. });
  29. if (list.length > 0) {
  30. list = JSON.parse(JSON.stringify(list));
  31. list = list.map(i => {
  32. i.user_name = _.get(i, 'user_id.user_id.name');
  33. i.user_id = _.get(i, 'user_id._id');
  34. return i;
  35. });
  36. }
  37. return list;
  38. }
  39. }
  40. module.exports = TeamApplyService;