dock_user.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { ObjectId } = require('mongoose').Types;
  5. const _ = require('lodash');
  6. const assert = require('assert');
  7. // 展会-用户
  8. class Dock_userService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'dock_user');
  11. this.model = this.ctx.model.DockUser;
  12. }
  13. async create(data) {
  14. const { goodsList = [] } = data;
  15. if (goodsList.length > 0) {
  16. data.goodsList = goodsList.map(i => {
  17. if (!i.status) i.status = '0';
  18. i._id = ObjectId();
  19. return i;
  20. });
  21. }
  22. return await this.model.create(data);
  23. }
  24. async update({ id }, data) {
  25. const { goodsList = [] } = data;
  26. if (goodsList.length > 0) {
  27. data.goodsList = goodsList.map(i => {
  28. if (!i.status) i.status = '0';
  29. if (!i._id)i._id = ObjectId();
  30. else i._id = ObjectId(i._id);
  31. return i;
  32. });
  33. }
  34. return this.model.updateOne({ _id: ObjectId(id) }, data);
  35. }
  36. /**
  37. * 申请参展的产品审核
  38. * @param {Object} {id} 申请参展的产品的_id
  39. * @param {Object} {good_id, status} 要改变的状态
  40. * 只要有一个产品通过审核,该用户就更变为可通过状态=>need_pass_user
  41. */
  42. async goodsCheck({ id }, { good_id, status }) {
  43. const object = await this.model.findOne({ _id: ObjectId(id), goodsList: { $elemMatch: { id: good_id } } });
  44. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到需要审核的产品信息');
  45. const product = object.goodsList.find(f => ObjectId(good_id).equals(f.id));
  46. if (!product) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未在需要审核的产品中找到指定的产品');
  47. product.status = status;
  48. await this.model.updateOne({ _id: ObjectId(id), goodsList: { $elemMatch: { id: good_id } } }, object);
  49. const need_pass_user = object.goodsList.some(e => e.status === '1');
  50. if (need_pass_user) this.userCheck({ id: object._id }, { status: '1' });
  51. }
  52. /**
  53. * 申请参展的用户审核
  54. * @param {Object} {id} 用户申请时生成的数据的id
  55. * @param {Object} {status} 要改变的状态
  56. */
  57. async userCheck({ id }, { status }) {
  58. const object = await this.model.findById(id);
  59. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的用户申请');
  60. object.status = status;
  61. await object.save();
  62. }
  63. async findUser({ dock_id, user_id }) {
  64. const res = await this.model.findOne({ dock_id: ObjectId(dock_id), user_id: ObjectId(user_id) });
  65. return res;
  66. }
  67. }
  68. module.exports = Dock_userService;