personroom.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. this.tmodel = this.ctx.model.Personroomtalk;
  12. }
  13. async create(query, { buyer_id, seller_id, buyer_name, seller_name }) {
  14. assert(buyer_id, '缺少买家信息');
  15. assert(seller_id, '缺少卖家信息');
  16. assert(buyer_name, '缺少买家信息');
  17. assert(seller_name, '缺少卖家信息');
  18. // 先查询是否有该房间
  19. let res = await this.model.findOne({ buyer_id, seller_id });
  20. if (!res) res = await this.model.create({ buyer_id, seller_id, buyer_name, seller_name });
  21. // TODO MQ
  22. const talk = await this.tmodel.findOne({ pr_id: res.id });
  23. if (!talk) {
  24. const newdata = { pr_id: res.id, type: '0' };
  25. await this.tmodel.create(newdata);
  26. }
  27. // 这个地方不应该有MQ;买家在请求完房间号后订阅MQ;卖家应该在自己进入中台管理时订阅MQ,且订阅的应该是这个人是否有未读消息
  28. const { mq } = this.ctx;
  29. if (mq) {
  30. const exchange = 'person_room';
  31. const parm = {
  32. durable: true,
  33. headers: {
  34. room_id: res.id,
  35. buyer_id,
  36. seller_id,
  37. } };
  38. mq.topic(exchange, seller_id, '您有买家进行对接请及时查收', parm);
  39. }
  40. return res;
  41. }
  42. }
  43. module.exports = PersonroomService;