'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; class ExpertService extends CrudService { constructor(ctx) { super(ctx, 'expert'); this.model = this.ctx.model.Expert; this.umodel = this.ctx.model.User; } async update({ id }, data) { const expert = await this.model.findById(id); const { name, gender, id_number, phone, address, birthday, email, img_url, level, levelname, position, school, education, degree, major, profession, resume, project, academic, status } = data; const user = (await this.umodel.find({ userid: id }))[0]; if (name) { user.name = name; expert.name = name; await user.save(); } if (gender) { expert.gender = gender; } if (phone) { const _user = await this.umodel.find({ phone }); if (_.isEqual(_user.length, 0) || (_.isEqual(_user.length, 1) && _.isEqual(_user[0].id, user.id))) { user.phone = phone; expert.phone = phone; await user.save(); } else { throw new BusinessError(ErrorCode.DATA_EXISTED); } } if (address) { expert.address = address; } if (birthday) { expert.birthday = birthday; } if (id_number) { expert.id_number = id_number; } if (email) { expert.email = email; } if (img_url) { expert.img_url = img_url; } if (level) { expert.level = level; } if (levelname) { expert.levelname = levelname; } if (position) { expert.position = position; } if (school) { expert.school = school; } if (education) { expert.education = education; } if (degree) { expert.degree = degree; } if (major) { expert.major = major; } if (profession) { expert.profession = profession; } if (resume) { expert.resume = resume; } if (project) { expert.project = project; } if (academic) { expert.academic = academic; } if (status) { expert.status = status; } const res = await expert.save(); return res; } } module.exports = ExpertService;