personroom.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. const assert = require('assert');
  3. // const _ = require('lodash');
  4. // const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. // const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class PersonroomService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'personroom');
  10. this.model = this.ctx.model.Personroom;
  11. }
  12. async create(query, { buyer_id, seller_id, buyer_name, seller_name }) {
  13. assert(buyer_id, '缺少买家信息');
  14. assert(seller_id, '缺少卖家信息');
  15. assert(buyer_name, '缺少买家信息');
  16. assert(seller_name, '缺少卖家信息');
  17. // 先查询是否有该房间
  18. let res = await this.model.findOne({ buyer_id, seller_id });
  19. if (!res) res = await this.model.create({ buyer_id, seller_id, buyer_name, seller_name });
  20. // TODO MQ
  21. const { mq } = this.ctx;
  22. if (mq) {
  23. const exchange = 'person_room';
  24. const parm = {
  25. durable: true,
  26. headers: {
  27. room_id: res.id,
  28. buyer_id,
  29. seller_id,
  30. } };
  31. mq.topic(exchange, seller_id, '您有买家进行对接请及时查收', parm);
  32. }
  33. return res;
  34. }
  35. }
  36. module.exports = PersonroomService;