expert.js 2.3 KB

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