answerchat.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const moment = require('moment');
  5. const { ObjectId } = require('mongoose').Types;
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. const { BusinessError, ErrorCode } = require('naf-core').Error;
  8. class AnswerchatService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'answerchat');
  11. this.model = this.ctx.model.Answerchat;
  12. }
  13. async create(data) {
  14. const { sender_id, sender_name, receiver_id, receiver_name, room, content } = data;
  15. assert(sender_id, '缺少发送人信息');
  16. assert(sender_name, '缺少发送人信息');
  17. assert(receiver_id, '缺少接收人信息');
  18. assert(receiver_name, '缺少接受人信息');
  19. assert(room, '缺少房间号');
  20. assert(content, '缺少发送内容');
  21. const send_time = moment().format('YYYY-MM-DD HH:mm:ss');
  22. const res = await this.model.create({ ...data, send_time });
  23. const { mq } = this.ctx;
  24. if (mq) {
  25. const exchange = 'answerchat';
  26. const parm = {
  27. durable: true,
  28. headers: {
  29. userid: 1,
  30. } };
  31. await mq.fanout(exchange, room, JSON.stringify(res), parm);
  32. }
  33. return res;
  34. }
  35. }
  36. module.exports = AnswerchatService;