user.js 821 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const moment = require('moment');
  5. const { ObjectId } = require('mongoose').Types;
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. const { BusinessError, ErrorCode } = require('naf-core').Error;
  8. class UserService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'user');
  11. this.model = this.ctx.model.User;
  12. }
  13. async create(data) {
  14. const { password } = data;
  15. data.password = { secret: password };
  16. const res = await this.model.create(data);
  17. return res;
  18. }
  19. async update(data) {
  20. const { id, password } = data;
  21. if (password) data.passwd = { secret: password };
  22. const res = await this.model.update({ id: ObjectId(id) }, data);
  23. return res;
  24. }
  25. }
  26. module.exports = UserService;