@lomray/microservice-users
Version:
Users microservice based on NodeJS & inverted json.
64 lines (60 loc) • 1.88 kB
JavaScript
;
var tslib = require('tslib');
var microserviceNodejsLib = require('@lomray/microservice-nodejs-lib');
var classValidator = require('class-validator');
/**
* Change user login: email or phone
*/
class ChangeLogin {
/**
* @constructor
*/
constructor({ userId, login, confirmBy, repository, isConfirmed }) {
this.userId = userId;
this.login = login;
this.confirmBy = confirmBy;
this.isConfirmed = isConfirmed;
this.repository = repository;
}
/**
* Init service
*/
static init(params) {
return new ChangeLogin(params);
}
/**
* Change login
*/
change() {
return tslib.__awaiter(this, void 0, void 0, function* () {
if (!(yield this.isConfirmed())) {
throw new microserviceNodejsLib.BaseException({
status: 422,
message: 'Invalid confirmation code.',
});
}
const user = yield this.repository.findOne({ id: this.userId });
if (!user) {
throw new microserviceNodejsLib.BaseException({
status: 404,
message: 'User not found.',
});
}
user[this.confirmBy] = this.login;
const errors = yield classValidator.validate(user, {
whitelist: true,
forbidNonWhitelisted: true,
validationError: { target: false },
});
if (errors.length > 0) {
throw new microserviceNodejsLib.BaseException({
status: 422,
message: 'Validation failed.',
payload: errors,
});
}
return this.repository.save(user);
});
}
}
module.exports = ChangeLogin;