1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- 'use strict';
- const assert = require('assert');
- // const _ = require('lodash');
- // const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- // const { BusinessError, ErrorCode } = require('naf-core').Error;
- class PersonroomService extends CrudService {
- constructor(ctx) {
- super(ctx, 'personroom');
- this.model = this.ctx.model.Personroom;
- this.tmodel = this.ctx.model.Personroomtalk;
- }
- async create(query, { buyer_id, seller_id, buyer_name, seller_name }) {
- assert(buyer_id, '缺少买家信息');
- assert(seller_id, '缺少卖家信息');
- assert(buyer_name, '缺少买家信息');
- assert(seller_name, '缺少卖家信息');
- // 先查询是否有该房间
- let res = await this.model.findOne({ buyer_id, seller_id });
- if (!res) res = await this.model.create({ buyer_id, seller_id, buyer_name, seller_name });
- // TODO MQ
- const talk = await this.tmodel.findOne({ pr_id: res.id });
- if (!talk) {
- const newdata = { pr_id: res.id, type: '0' };
- await this.tmodel.create(newdata);
- }
- // 这个地方不应该有MQ;买家在请求完房间号后订阅MQ;卖家应该在自己进入中台管理时订阅MQ,且订阅的应该是这个人是否有未读消息
- const { mq } = this.ctx;
- if (mq) {
- const exchange = 'person_room';
- const parm = {
- durable: true,
- headers: {
- room_id: res.id,
- buyer_id,
- seller_id,
- } };
- mq.topic(exchange, seller_id, '您有买家进行对接请及时查收', parm);
- }
- return res;
- }
- }
- module.exports = PersonroomService;
|