examine.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. const { ObjectId } = require('mongoose').Types;
  7. // 审批
  8. class ExamineService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'examine');
  11. this.model = this.ctx.model.Examine;
  12. }
  13. async getGoods({ examine_id, buy_id, mech_id, market_id, status }) {
  14. assert(examine_id, '缺少审批表信息');
  15. assert(buy_id, '缺少购买人信息');
  16. assert(mech_id, '缺少商铺信息');
  17. assert(market_id, '缺少商品信息');
  18. assert(status, '缺少货物状态');
  19. const query = {
  20. _id: ObjectId(examine_id),
  21. buy_id: ObjectId(buy_id),
  22. };
  23. let data = await this.model.findOne(query);
  24. if (!data) BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到信息');
  25. const i = data.order.findIndex(f => ObjectId(f.mech_id).equals(mech_id) && ObjectId(f.id).equals(market_id));
  26. if (i < 0)BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到货物信息');
  27. data.order[i].status = status;
  28. data = _.omit(data, [ 'meta' ]);
  29. data = JSON.parse(JSON.stringify(data));
  30. const res = await this.model.updateOne(query, data);
  31. return res;
  32. }
  33. }
  34. module.exports = ExamineService;