dock_user.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. }
  64. module.exports = Dock_userService;