company.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 CompanyService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'company');
  10. this.model = this.ctx.model.Company;
  11. }
  12. // 更新其他信息
  13. async updateotherinfo({ id }, data) {
  14. assert(id, 'id为必填项');
  15. const { _id, info_name, remark, url_info } = data;
  16. assert(_id, '_id为必填项');
  17. const company = await this.model.findById(id);
  18. if (!company) {
  19. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  20. }
  21. const otherinfo = company.other_info.id(_id);
  22. otherinfo.info_name = info_name;
  23. otherinfo.remark = remark;
  24. otherinfo.url_info = url_info;
  25. const res = await company.save();
  26. return res;
  27. }
  28. // 删除其他信息
  29. async deleteotherinfo({ id }, { _id }) {
  30. assert(id, 'id为必填项');
  31. assert(_id, '_id为必填项');
  32. const company = await this.model.findById(id);
  33. if (!company) {
  34. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  35. }
  36. company.other_info.id(_id).remove();
  37. const res = await company.save();
  38. return res;
  39. }
  40. }
  41. module.exports = CompanyService;