expert.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 { name, gender, id_number, phone, address, birthday, email, img_url, level, levelname, position, school, education, degree, major, profession, resume, project, academic, status } = data;
  16. const user = (await this.umodel.find({ userid: id }))[0];
  17. if (name) {
  18. user.name = name;
  19. expert.name = name;
  20. await user.save();
  21. }
  22. if (gender) {
  23. expert.gender = gender;
  24. }
  25. if (phone) {
  26. const _user = await this.umodel.find({ phone });
  27. if (_.isEqual(_user.length, 0) || (_.isEqual(_user.length, 1) && _.isEqual(_user[0].id, user.id))) {
  28. user.phone = phone;
  29. expert.phone = phone;
  30. await user.save();
  31. } else {
  32. throw new BusinessError(ErrorCode.DATA_EXISTED);
  33. }
  34. }
  35. if (address) {
  36. expert.address = address;
  37. }
  38. if (birthday) {
  39. expert.birthday = birthday;
  40. }
  41. if (id_number) {
  42. expert.id_number = id_number;
  43. }
  44. if (email) {
  45. expert.email = email;
  46. }
  47. if (img_url) {
  48. expert.img_url = img_url;
  49. }
  50. if (level) {
  51. expert.level = level;
  52. }
  53. if (levelname) {
  54. expert.levelname = levelname;
  55. }
  56. if (position) {
  57. expert.position = position;
  58. }
  59. if (school) {
  60. expert.school = school;
  61. }
  62. if (education) {
  63. expert.education = education;
  64. }
  65. if (degree) {
  66. expert.degree = degree;
  67. }
  68. if (major) {
  69. expert.major = major;
  70. }
  71. if (profession) {
  72. expert.profession = profession;
  73. }
  74. if (resume) {
  75. expert.resume = resume;
  76. }
  77. if (project) {
  78. expert.project = project;
  79. }
  80. if (academic) {
  81. expert.academic = academic;
  82. }
  83. if (status) {
  84. expert.status = status;
  85. }
  86. const res = await expert.save();
  87. return res;
  88. }
  89. }
  90. module.exports = ExpertService;