expert.js 2.3 KB

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