personroom.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. //这个地方不应该有MQ;买家在请求完房间号后订阅MQ;卖家应该在自己进入中台管理时订阅MQ,且订阅的应该是这个人是否有未读消息
  22. const { mq } = this.ctx;
  23. if (mq) {
  24. const exchange = 'person_room';
  25. const parm = {
  26. durable: true,
  27. headers: {
  28. room_id: res.id,
  29. buyer_id,
  30. seller_id,
  31. } };
  32. mq.topic(exchange, seller_id, '您有买家进行对接请及时查收', parm);
  33. }
  34. return res;
  35. }
  36. }
  37. module.exports = PersonroomService;