feat: handle errors and autogenerate user and code
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2022-12-14 15:03:55 +01:00
parent b378598113
commit 156f2b9129
5 changed files with 69 additions and 37 deletions

View File

@ -247,7 +247,10 @@
"Empty data source": "Origen de datos vacio", "Empty data source": "Origen de datos vacio",
"Email verify": "Correo de verificación", "Email verify": "Correo de verificación",
"Landing cannot be lesser than shipment": "Landing cannot be lesser than shipment", "Landing cannot be lesser than shipment": "Landing cannot be lesser than shipment",
"Receipt's bank was not found": "No se encontró el banco del recibo", "Receipt's bank was not found": "No se encontró el banco del recibo",
"This receipt was not compensated": "Este recibo no ha sido compensado", "This receipt was not compensated": "Este recibo no ha sido compensado",
"Client's email was not found": "No se encontró el email del cliente" "Client's email was not found": "No se encontró el email del cliente",
"This worker code already exists": "Este codigo de trabajador ya existe",
"This personal mail already exists": "Este correo personal ya existe",
"This worker already exists": "Este trabajador ya existe"
} }

View File

@ -1,4 +1,5 @@
const md5 = require('md5'); const md5 = require('md5');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('new', { Self.remoteMethodCtx('new', {
@ -8,103 +9,103 @@ module.exports = Self => {
{ {
arg: 'fi', arg: 'fi',
type: 'string', type: 'string',
description: `The fi of worker`, description: `The worker fi`,
required: true, required: true,
}, },
{ {
arg: 'name', arg: 'name',
type: 'string', type: 'string',
description: `REPLACE!`, description: `The user name`,
required: true, required: true,
}, },
{ {
arg: 'firstName', arg: 'firstName',
type: 'string', type: 'string',
description: `REPLACE!`, description: `The worker firstname`,
required: true, required: true,
}, },
{ {
arg: 'lastNames', arg: 'lastNames',
type: 'string', type: 'string',
description: `REPLACE!`, description: `The worker lastnames`,
required: true, required: true,
}, },
{ {
arg: 'email', arg: 'email',
type: 'string', type: 'string',
description: `REPLACE!`, description: `The worker email`,
required: true, required: true,
}, },
{ {
arg: 'roleFk', arg: 'roleFk',
type: 'number', type: 'number',
description: `REPLACE!`, description: `The worker role`,
required: true, required: true,
}, },
{ {
arg: 'street', arg: 'street',
type: 'string', type: 'string',
description: `REPLACE!`, description: `The worker address`,
required: true, required: true,
}, },
{ {
arg: 'city', arg: 'city',
type: 'string', type: 'string',
description: `REPLACE!`, description: `The worker city`,
required: true, required: true,
}, },
{ {
arg: 'provinceFk', arg: 'provinceFk',
type: 'number', type: 'number',
description: `REPLACE!`, description: `The worker province`,
required: true, required: true,
}, },
{ {
arg: 'iban', arg: 'iban',
type: 'string', type: 'string',
description: `REPLACE!`, description: `The worker iban`,
required: true, required: true,
}, },
{ {
arg: 'bankEntityFk', arg: 'bankEntityFk',
type: 'number', type: 'number',
description: `REPLACE!`, description: `The worker bank entity`,
required: true, required: true,
}, },
{ {
arg: 'companyFk', arg: 'companyFk',
type: 'number', type: 'number',
description: `REPLACE!`, description: `The worker company`,
required: true, required: true,
}, },
{ {
arg: 'postcode', arg: 'postcode',
type: 'string', type: 'string',
description: `REPLACE!`, description: `The worker postcode`,
required: true, required: true,
}, },
{ {
arg: 'phone', arg: 'phone',
type: 'string', type: 'string',
description: `REPLACE!`, description: `The worker phone`,
required: true, required: true,
}, },
{ {
arg: 'code', arg: 'code',
type: 'string', type: 'string',
description: `REPLACE!`, description: `The worker code`,
required: true, required: true,
}, },
{ {
arg: 'bossFk', arg: 'bossFk',
type: 'number', type: 'number',
description: `REPLACE!`, description: `The worker boss`,
required: true, required: true,
}, },
{ {
arg: 'birth', arg: 'birth',
type: 'date', type: 'date',
description: `REPLACE!`, description: `The worker birth`,
required: true, required: true,
}, },
], ],
@ -152,7 +153,7 @@ module.exports = Self => {
nickname, nickname,
password: md5(randomPassword), password: md5(randomPassword),
email: args.email, email: args.email,
role: args.role, role: args.roleFk,
}, },
myOptions myOptions
); );
@ -222,10 +223,21 @@ module.exports = Self => {
); );
if (tx) await tx.commit(); if (tx) await tx.commit();
} catch (e) { } catch (err) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
console.log(e); const code = err.code;
throw e; const message = err.sqlMessage;
if (code === 'ER_DUP_ENTRY' && message.includes(`for key 'mail'`))
throw new UserError(`This personal mail already exists`);
if (code === 'ER_DUP_ENTRY' && message.includes(`CodigoTrabajador_UNIQUE`))
throw new UserError(`This worker code already exists`);
if (code === 'ER_DUP_ENTRY' && message.includes(`PRIMARY`))
throw new UserError(`This worker already exists`);
throw err;
} }
await models.user.resetPassword({ await models.user.resetPassword({

View File

@ -13,11 +13,13 @@
label="Firstname" label="Firstname"
ng-model="$ctrl.worker.firstName" ng-model="$ctrl.worker.firstName"
rule rule
on-change="$ctrl.generateCodeUser()"
vn-focus> vn-focus>
</vn-textfield> </vn-textfield>
<vn-textfield <vn-textfield
vn-one vn-one
label="Lastname" label="Lastname"
on-change="$ctrl.generateCodeUser()"
ng-model="$ctrl.worker.lastNames" ng-model="$ctrl.worker.lastNames"
rule> rule>
</vn-textfield> </vn-textfield>
@ -38,6 +40,8 @@
vn-one vn-one
label="Code" label="Code"
ng-model="$ctrl.worker.code" ng-model="$ctrl.worker.code"
maxLength="3"
on-change="$ctrl.worker.code = $ctrl.worker.code.toUpperCase()"
rule> rule>
</vn-textfield> </vn-textfield>
<vn-textfield <vn-textfield
@ -107,15 +111,14 @@
</vn-horizontal> </vn-horizontal>
<vn-horizontal> <vn-horizontal>
<vn-textfield <vn-textfield
label="User" label="Web user"
ng-model="$ctrl.worker.name" ng-model="$ctrl.worker.name"
rule> rule>
</vn-textfield> </vn-textfield>
<vn-textfield <vn-textfield
label="Personal email" label="Personal email"
ng-model="$ctrl.worker.email" ng-model="$ctrl.worker.email"
rule rule>
info="You can save multiple emails">
</vn-textfield> </vn-textfield>
<vn-autocomplete <vn-autocomplete
label="Access permission" label="Access permission"

View File

@ -33,6 +33,20 @@ export default class Controller extends Section {
}); });
} }
generateCodeUser() {
if (!this.worker.firstName || !this.worker.lastNames) return;
const totalName = this.worker.firstName.concat(' ' + this.worker.lastNames).toLowerCase();
const totalNameArray = totalName.split(' ');
let newCode = '';
for (let part of totalNameArray)
newCode += part.charAt(0);
this.worker.code = newCode.toUpperCase().slice(0, 3);
this.worker.name = totalNameArray[0] + newCode.slice(1);
}
get province() { get province() {
return this._province; return this._province;
} }

View File

@ -1,12 +1,12 @@
Name: Nombre Firstname: Nombre
Tax number: NIF/CIF Lastname: Apellidos
Business name: Razón social Fi: DNI/NIF/NIE
Birth: Fecha de nacimiento
Code: Código de trabajador
Province: Provincia
City: Población
ProfileType: Tipo de perfil
Street: Dirección
Postcode: Código postal
Web user: Usuario Web Web user: Usuario Web
Email: E-mail
Create and edit: Crear y editar
You can save multiple emails: >-
Puede guardar varios correos electrónicos encadenándolos mediante comas
sin espacios, ejemplo: user@dominio.com, user2@dominio.com siendo el primer
correo electrónico el principal
The type of business must be filled in basic data: El tipo de negocio debe estar rellenado en datos básicos
Access permission: Permiso de acceso Access permission: Permiso de acceso