dock.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. const moment = require('moment');
  8. class ChatService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'dock');
  11. this.model = this.ctx.model.Dock;
  12. }
  13. // async create(query, { sender_id, sender_name, content }) {
  14. // assert(sender_name, '缺少发言人信息');
  15. // assert(content, '缺少发言内容');
  16. // const send_time = moment().format('YYYY-MM-DD HH:mm:ss');
  17. // const res = await this.model.create({
  18. // sender_id,
  19. // sender_name,
  20. // content,
  21. // send_time,
  22. // });
  23. // return res;
  24. // }
  25. async apply({ id }, body) {
  26. const dock = await this.model.findOne({ _id: ObjectId(id) });
  27. if (!dock) {
  28. throw new BusinessError('没有查询到该对接会');
  29. }
  30. dock.apply.push({
  31. ...body,
  32. apply_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  33. });
  34. const res = await dock.save();
  35. const info = _.last(res.apply);
  36. return info;
  37. }
  38. async check({ id, dock_id }, { status }) {
  39. assert(status, '请审核是否允许参加对接会');
  40. const dock = await this.model.findOne({ _id: ObjectId(dock_id) });
  41. if (!dock) {
  42. throw new BusinessError('没有查询到该对接会');
  43. }
  44. const user = dock.apply.id(id);
  45. if (!user) {
  46. throw new BusinessError('没有查询到用户的申请');
  47. }
  48. user.status = status;
  49. const res = dock.save();
  50. return res;
  51. }
  52. async dockCheck({ id }, { is_allowed, reason = '' }) {
  53. const dock = await this.model.findOne({ _id: ObjectId(id) });
  54. if (!dock) {
  55. throw new BusinessError('没有查询到该对接会');
  56. }
  57. assert(is_allowed, '请选择审核结果');
  58. dock.is_allowed = is_allowed;
  59. dock.reason = reason;
  60. const res = await dock.save();
  61. return res;
  62. }
  63. }
  64. module.exports = ChatService;