123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- '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 LookuserService extends CrudService {
- constructor(ctx) {
- super(ctx, 'lookuser');
- this.model = this.ctx.model.Lookuser;
- }
- async create(data) {
- const { roomname, userid } = data;
- const lookuser = await this.model.findOne({ roomname, userid });
- if (!lookuser) {
- await this.model.create(data);
- }
- }
- async update({ id }, data) {
- const { switchrole } = data;
- const lookuser = await this.model.findById(id);
- if (!lookuser) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
- }
- lookuser.switchrole = switchrole;
- const res = await lookuser.save();
- if (res) {
- const { mq } = this.ctx;
- if (mq) {
- const exchange = 'switch_role_' + res.userid;
- const parm = {
- durable: true,
- headers: {
- userid: res.userid,
- } };
- await mq.fanout(exchange, res.userid, JSON.stringify(res), parm);
- }
- }
- return res;
- }
- async roomcount(data) {
- const { roomname } = data;
- const total = await this.model.count({ roomname });
- return { total };
- }
- async userswichrole(data) {
- const { roomid, userid } = data;
- const res = await this.model.findOne({ roomid, userid });
- return res;
- }
- }
- module.exports = LookuserService;
|