lookuser.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 LookuserService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'lookuser');
  10. this.model = this.ctx.model.Lookuser;
  11. }
  12. async create(data) {
  13. const { roomname, userid } = data;
  14. const lookuser = await this.model.findOne({ roomname, userid });
  15. if (!lookuser) {
  16. await this.model.create(data);
  17. }
  18. }
  19. async update({ id }, data) {
  20. const { switchrole } = data;
  21. const lookuser = await this.model.findById(id);
  22. if (!lookuser) {
  23. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  24. }
  25. lookuser.switchrole = switchrole;
  26. const res = await lookuser.save();
  27. if (res) {
  28. const { mq } = this.ctx;
  29. if (mq) {
  30. const exchange = 'switch_role_' + res.userid;
  31. const parm = {
  32. durable: true,
  33. headers: {
  34. userid: res.userid,
  35. } };
  36. await mq.fanout(exchange, res.userid, JSON.stringify(res), parm);
  37. }
  38. }
  39. return res;
  40. }
  41. async roomcount(data) {
  42. const { roomname } = data;
  43. const total = await this.model.count({ roomname });
  44. return { total };
  45. }
  46. async userswichrole(data) {
  47. const { roomid, userid } = data;
  48. const res = await this.model.findOne({ roomid, userid });
  49. return res;
  50. }
  51. }
  52. module.exports = LookuserService;