e-commercee
Version:
This package contains a backend of what would be the logic of a e-commercee software, the architecture used is made in 3 layers
246 lines • 10.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LUser = void 0;
const FactoryData_1 = require("../../data/FactoryData");
const Administrator_1 = require("../../shared/entity/Administrator");
const Client_1 = require("../../shared/entity/Client");
const logicexception_1 = require("../../shared/exceptions/logicexception");
const crypto_1 = require("crypto");
class LUser {
constructor() {
//*************************************** */
//AUTH
this._loginuserobj = null;
}
static getInstance() {
if (!LUser.instancia) {
LUser.instancia = new LUser();
}
return LUser.instancia;
}
//Validations************************************
validateIdCard(idcard) {
var numbers = /^[0-9]+$/;
if (!idcard.trim().match(numbers)) {
throw new logicexception_1.LogicException("The identity card must have only numbers");
}
if (idcard.trim() === "") {
throw new logicexception_1.LogicException("The identity card cannot be empty");
}
}
validateLogin(username, password) {
this.validateUserName(username);
if (password.trim() === "") {
throw new logicexception_1.LogicException("The password cannot be empty");
}
}
validateUserName(username) {
if (username.trim() === "") {
throw new logicexception_1.LogicException("The username cannot be empty");
}
}
async validateAddUser(dtuser) {
if (dtuser == null) {
throw new logicexception_1.LogicException("The User is empty ");
}
if (dtuser.completename.trim() === "") {
throw new logicexception_1.LogicException("The complete name cannot be empty");
}
var passw = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{11,30}$/;
if (!dtuser.password.match(passw)) {
throw new logicexception_1.LogicException("The password must be between 11 to 30 characters which contain at least one numeric digit, one uppercase and one lowercase letter");
}
if (dtuser.password.trim() === "") {
throw new logicexception_1.LogicException("The password cannot be empty");
}
if (dtuser instanceof Client_1.Client) {
let client = dtuser;
var numbers = /^[0-9]+$/;
if (!client.creditcardnumber.trim().match(numbers)) {
throw new logicexception_1.LogicException("The credit card number must have only numbers");
}
if (client.creditcardnumber.trim() === "") {
throw new logicexception_1.LogicException("The credit card number cannot be empty");
}
if (client.shippingaddress.trim() === "") {
throw new logicexception_1.LogicException("The shipping address cannot be empty");
}
}
if (dtuser instanceof Administrator_1.Administrator) {
let admin = dtuser;
if (admin.position.trim() === "") {
throw new logicexception_1.LogicException("The position cannot be empty");
}
}
this.validateIdCard(dtuser.identitycard);
this.validateUserName(dtuser.username);
let idcardsearch = await this.getUser(dtuser.identitycard);
if (idcardsearch != null) {
throw new logicexception_1.LogicException("That User already exists in the system");
}
let usernamesearch = await this.getUserByusername(dtuser.username);
if (usernamesearch != null) {
throw new logicexception_1.LogicException("That User Name already exists in the system");
}
}
async validateUpdateUser(dtuser) {
if (dtuser == null) {
throw new logicexception_1.LogicException("The User is empty ");
}
if (dtuser.completename.trim() === "") {
throw new logicexception_1.LogicException("The complete name cannot be empty");
}
var passw = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{11,30}$/;
if (!dtuser.password.match(passw)) {
throw new logicexception_1.LogicException("The password must be between 11 to 30 characters which contain at least one numeric digit, one uppercase and one lowercase letter");
}
if (dtuser.password.trim() === "") {
throw new logicexception_1.LogicException("The password cannot be empty");
}
if (dtuser instanceof Client_1.Client) {
let client = dtuser;
var numbers = /^[0-9]+$/;
if (!client.creditcardnumber.trim().match(numbers)) {
throw new logicexception_1.LogicException("The credit card number must have only numbers");
}
if (client.creditcardnumber.trim() === "") {
throw new logicexception_1.LogicException("The credit card number cannot be empty");
}
if (client.shippingaddress.trim() === "") {
throw new logicexception_1.LogicException("The shipping address cannot be empty");
}
}
if (dtuser instanceof Administrator_1.Administrator) {
let admin = dtuser;
if (admin.position.trim() === "") {
throw new logicexception_1.LogicException("The position cannot be empty");
}
}
this.validateIdCard(dtuser.identitycard);
let idcardsearch = await this.getUser(dtuser.identitycard);
if (idcardsearch == null) {
throw new logicexception_1.LogicException("That User does not exists in the system");
}
}
async validateDeleteUser(dtuser) {
if (dtuser == null) {
throw new logicexception_1.LogicException("The User is empty ");
}
this.validateIdCard(dtuser.identitycard);
let sobjcli = await this.getUser(dtuser.identitycard);
if (sobjcli == null) {
throw new logicexception_1.LogicException("That User does not exists in the system");
}
}
//********************************************** */
//USER
async getUser(idcard) {
this.validateIdCard(idcard);
var suser;
suser = await FactoryData_1.FactoryData.getDClient().getClient(idcard);
if (suser == null) {
suser = await FactoryData_1.FactoryData.getDAdmin().getAdmin(idcard);
}
return suser;
}
async getUserByusername(username) {
this.validateUserName(username);
var suser;
suser = await FactoryData_1.FactoryData.getDClient().getClientbyusername(username);
if (suser == null) {
suser = await FactoryData_1.FactoryData.getDAdmin().getAdminbyusername(username);
}
return suser;
}
async addUser(dtuser) {
await this.validateAddUser(dtuser);
const passh = await this.hashPassword(dtuser.password);
dtuser.password = passh.hash;
dtuser.salt = passh.salt;
if (dtuser instanceof Client_1.Client) {
FactoryData_1.FactoryData.getDClient().addClient(dtuser);
}
if (dtuser instanceof Administrator_1.Administrator) {
FactoryData_1.FactoryData.getDAdmin().addAdmin(dtuser);
}
}
async updateUser(dtuser) {
await this.validateUpdateUser(dtuser);
const passh = await this.hashPassword(dtuser.password);
dtuser.password = passh.hash;
dtuser.salt = passh.salt;
if (dtuser instanceof Client_1.Client) {
FactoryData_1.FactoryData.getDClient().updateClient(dtuser);
}
if (dtuser instanceof Administrator_1.Administrator) {
FactoryData_1.FactoryData.getDAdmin().updateAdmin(dtuser);
}
}
async deleteUser(dtuser) {
await this.validateDeleteUser(dtuser);
if (dtuser instanceof Client_1.Client) {
FactoryData_1.FactoryData.getDClient().deleteClient(dtuser);
}
if (dtuser instanceof Administrator_1.Administrator) {
FactoryData_1.FactoryData.getDAdmin().deleteAdmin(dtuser);
}
}
async getClients() {
var list = await FactoryData_1.FactoryData.getDClient().getClients();
return list;
}
async getAdmins() {
var list = await FactoryData_1.FactoryData.getDAdmin().getAdmins();
return list;
}
//************************************************ */
hashPassword(password) {
let salt = crypto_1.randomBytes(16).toString('hex');
let hash = crypto_1.pbkdf2Sync(password, salt, 1000, 64, `sha512`).toString(`hex`);
return { hash, salt };
}
verifyPassword(password, hashPassword, salt) {
var hash = crypto_1.pbkdf2Sync(password, salt, 1000, 64, `sha512`).toString(`hex`);
return hashPassword === hash;
}
get loginuserobj() {
return this._loginuserobj;
}
set loginuserobj(value) {
this._loginuserobj = value;
}
async loginUser(username, password) {
this.validateLogin(username, password);
const getuseruname = await this.getUserByusername(username);
if (getuseruname == null) {
throw new logicexception_1.LogicException("That User does not exists in the system");
}
const verifyp = await this.verifyPassword(password, getuseruname.password, getuseruname.salt);
if (verifyp === false) {
throw new logicexception_1.LogicException("Wrong password");
}
this.loginuserobj = getuseruname;
return getuseruname;
}
getLoginUser() {
let lguser = this.loginuserobj;
if (lguser == null) {
return null;
}
else {
return lguser;
}
}
logout() {
let lguser = this.loginuserobj;
if (lguser != null) {
this.loginuserobj = null;
return true;
}
else {
return false;
}
}
}
exports.LUser = LUser;
//# sourceMappingURL=LUser.js.map