lookuser.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. this.rmodel = this.ctx.model.Roomuser;
  12. }
  13. async create(data) {
  14. const { roomname, userid } = data;
  15. const lookuser = await this.model.findOne({ roomname, userid });
  16. if (!lookuser) {
  17. await this.model.create(data);
  18. }
  19. }
  20. async query({ skip, limit, ...info }) {
  21. const total = await this.model.count(info);
  22. const lookuser = await this.model
  23. .find(info)
  24. .skip(Number(skip))
  25. .limit(Number(limit));
  26. const data = [];
  27. for (const _lookuser of lookuser) {
  28. const lu = JSON.parse(JSON.stringify(_lookuser));
  29. const ruser = await this.rmodel.findById(_lookuser.userid);
  30. const _ruser = JSON.parse(JSON.stringify(ruser));
  31. const newdata = { ...lu, ..._ruser };
  32. data.push(newdata);
  33. }
  34. return { data, total };
  35. }
  36. }
  37. module.exports = LookuserService;