trainchat.js 1018 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. const assert = require('assert');
  3. const { CrudService } = require('naf-framework-mongoose/lib/service');
  4. const moment = require('moment');
  5. // 培训问诊聊天
  6. class TrainchatService extends CrudService {
  7. constructor(ctx) {
  8. super(ctx, 'trainchat');
  9. this.model = this.ctx.model.Trainchat;
  10. }
  11. async create(query, { unit_id, sender_id, sender_name, content, role }) {
  12. assert(sender_name, '缺少发言人信息');
  13. assert(unit_id, '缺少会场id');
  14. assert(content, '缺少发言内容');
  15. const send_time = moment().format('YYYY-MM-DD HH:mm:ss');
  16. const res = await this.model.create({ sender_id, sender_name, content, send_time, role, unit_id });
  17. // TODO MQ
  18. const { mq } = this.ctx;
  19. if (mq) {
  20. const exchange = 'train_live';
  21. const parm = {
  22. durable: true,
  23. headers: {
  24. userid: 1,
  25. } };
  26. await mq.fanout(exchange, unit_id, JSON.stringify(res), parm);
  27. }
  28. return res;
  29. }
  30. }
  31. module.exports = TrainchatService;