1234567891011121314151617181920212223242526272829303132333435363738394041 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const moment = require('moment');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- class AnswerchatService extends CrudService {
- constructor(ctx) {
- super(ctx, 'answerchat');
- this.model = this.ctx.model.Answerchat;
- }
- async create(data) {
- const { sender_id, sender_name, receiver_id, receiver_name, room, content } = data;
- assert(sender_id, '缺少发送人信息');
- assert(sender_name, '缺少发送人信息');
- assert(receiver_id, '缺少接收人信息');
- assert(receiver_name, '缺少接受人信息');
- assert(room, '缺少房间号');
- assert(content, '缺少发送内容');
- const send_time = moment().format('YYYY-MM-DD HH:mm:ss');
- const res = await this.model.create({ ...data, send_time });
- const { mq } = this.ctx;
- if (mq) {
- const exchange = 'answerchat';
- const parm = {
- durable: true,
- headers: {
- userid: 1,
- } };
- await mq.fanout(exchange, room, JSON.stringify(res), parm);
- }
- return res;
- }
- }
- module.exports = AnswerchatService;
|