teamApply.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. console.log('line 22 in function:');
  22. const conn = this.app.mongooseDB.get('base');
  23. const schema = _.get(this.ctx.model, 'Base.User.schema');
  24. const m = conn.model('User', schema);
  25. let list = await this.matchSignModel.find({ user_id: { $ne: user_id }, pay_status: '1', project_id }).populate({
  26. path: 'user_id',
  27. select: 'user_id',
  28. populate: { path: 'user_id', model: m, select: 'name' },
  29. });
  30. if (list.length > 0) {
  31. list = JSON.parse(JSON.stringify(list));
  32. list = list.map(i => {
  33. i.user_name = _.get(i, 'user_id.user_id.name');
  34. i.user_id = _.get(i, 'user_id._id');
  35. return i;
  36. });
  37. }
  38. return list;
  39. }
  40. }
  41. module.exports = TeamApplyService;