expert.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 ExpertService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'expert');
  10. this.model = this.ctx.model.Expert;
  11. this.umodel = this.ctx.model.User;
  12. }
  13. async update({ id }, data) {
  14. const expert = await this.model.findById(id);
  15. const {
  16. name,
  17. gender,
  18. id_number,
  19. phone,
  20. address,
  21. birthday,
  22. email,
  23. img_url,
  24. level,
  25. levelname,
  26. position,
  27. school,
  28. education,
  29. degree,
  30. major,
  31. profession,
  32. resume,
  33. project,
  34. academic,
  35. status,
  36. } = data;
  37. const user = (await this.umodel.find({ userid: id }))[0];
  38. if (name) {
  39. user.name = name;
  40. expert.name = name;
  41. await user.save();
  42. }
  43. if (gender) {
  44. expert.gender = gender;
  45. }
  46. if (phone) {
  47. const _user = await this.umodel.find({ phone });
  48. if (
  49. _.isEqual(_user.length, 0) ||
  50. (_.isEqual(_user.length, 1) && _.isEqual(_user[0].id, user.id))
  51. ) {
  52. user.phone = phone;
  53. expert.phone = phone;
  54. await user.save();
  55. } else {
  56. throw new BusinessError(ErrorCode.DATA_EXISTED);
  57. }
  58. }
  59. if (address) {
  60. expert.address = address;
  61. }
  62. if (birthday) {
  63. expert.birthday = birthday;
  64. }
  65. if (id_number) {
  66. expert.id_number = id_number;
  67. }
  68. if (email) {
  69. expert.email = email;
  70. }
  71. if (img_url) {
  72. expert.img_url = img_url;
  73. }
  74. if (level) {
  75. expert.level = level;
  76. }
  77. if (levelname) {
  78. expert.levelname = levelname;
  79. }
  80. if (position) {
  81. expert.position = position;
  82. }
  83. if (school) {
  84. expert.school = school;
  85. }
  86. if (education) {
  87. expert.education = education;
  88. }
  89. if (degree) {
  90. expert.degree = degree;
  91. }
  92. if (major) {
  93. expert.major = major;
  94. }
  95. if (profession) {
  96. expert.profession = profession;
  97. }
  98. if (resume) {
  99. expert.resume = resume;
  100. }
  101. if (project) {
  102. expert.project = project;
  103. }
  104. if (academic) {
  105. expert.academic = academic;
  106. }
  107. if (status) {
  108. expert.status = status;
  109. }
  110. const res = await expert.save();
  111. return res;
  112. }
  113. async delete({ id }) {
  114. await this.model.findByIdAndDelete(id);
  115. await this.umodel.findOneAndDelete({ userid: id });
  116. }
  117. }
  118. module.exports = ExpertService;