From 156f2b9129c02b8ed928f72007b05d977625573a Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 14 Dec 2022 15:03:55 +0100 Subject: [PATCH] feat: handle errors and autogenerate user and code --- loopback/locale/es.json | 9 ++-- modules/worker/back/methods/worker/new.js | 54 ++++++++++++++--------- modules/worker/front/create/index.html | 9 ++-- modules/worker/front/create/index.js | 14 ++++++ modules/worker/front/create/locale/es.yml | 20 ++++----- 5 files changed, 69 insertions(+), 37 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 932dfe98f..41dfff73a 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -247,7 +247,10 @@ "Empty data source": "Origen de datos vacio", "Email verify": "Correo de verificación", "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", - "This receipt was not compensated": "Este recibo no ha sido compensado", - "Client's email was not found": "No se encontró el email del cliente" + "Receipt's bank was not found": "No se encontró el banco del recibo", + "This receipt was not compensated": "Este recibo no ha sido compensado", + "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" } diff --git a/modules/worker/back/methods/worker/new.js b/modules/worker/back/methods/worker/new.js index 97fdc1033..07a091693 100644 --- a/modules/worker/back/methods/worker/new.js +++ b/modules/worker/back/methods/worker/new.js @@ -1,4 +1,5 @@ const md5 = require('md5'); +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('new', { @@ -8,103 +9,103 @@ module.exports = Self => { { arg: 'fi', type: 'string', - description: `The fi of worker`, + description: `The worker fi`, required: true, }, { arg: 'name', type: 'string', - description: `REPLACE!`, + description: `The user name`, required: true, }, { arg: 'firstName', type: 'string', - description: `REPLACE!`, + description: `The worker firstname`, required: true, }, { arg: 'lastNames', type: 'string', - description: `REPLACE!`, + description: `The worker lastnames`, required: true, }, { arg: 'email', type: 'string', - description: `REPLACE!`, + description: `The worker email`, required: true, }, { arg: 'roleFk', type: 'number', - description: `REPLACE!`, + description: `The worker role`, required: true, }, { arg: 'street', type: 'string', - description: `REPLACE!`, + description: `The worker address`, required: true, }, { arg: 'city', type: 'string', - description: `REPLACE!`, + description: `The worker city`, required: true, }, { arg: 'provinceFk', type: 'number', - description: `REPLACE!`, + description: `The worker province`, required: true, }, { arg: 'iban', type: 'string', - description: `REPLACE!`, + description: `The worker iban`, required: true, }, { arg: 'bankEntityFk', type: 'number', - description: `REPLACE!`, + description: `The worker bank entity`, required: true, }, { arg: 'companyFk', type: 'number', - description: `REPLACE!`, + description: `The worker company`, required: true, }, { arg: 'postcode', type: 'string', - description: `REPLACE!`, + description: `The worker postcode`, required: true, }, { arg: 'phone', type: 'string', - description: `REPLACE!`, + description: `The worker phone`, required: true, }, { arg: 'code', type: 'string', - description: `REPLACE!`, + description: `The worker code`, required: true, }, { arg: 'bossFk', type: 'number', - description: `REPLACE!`, + description: `The worker boss`, required: true, }, { arg: 'birth', type: 'date', - description: `REPLACE!`, + description: `The worker birth`, required: true, }, ], @@ -152,7 +153,7 @@ module.exports = Self => { nickname, password: md5(randomPassword), email: args.email, - role: args.role, + role: args.roleFk, }, myOptions ); @@ -222,10 +223,21 @@ module.exports = Self => { ); if (tx) await tx.commit(); - } catch (e) { + } catch (err) { if (tx) await tx.rollback(); - console.log(e); - throw e; + const code = err.code; + 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({ diff --git a/modules/worker/front/create/index.html b/modules/worker/front/create/index.html index 58b4da060..a86a4e899 100644 --- a/modules/worker/front/create/index.html +++ b/modules/worker/front/create/index.html @@ -13,11 +13,13 @@ label="Firstname" ng-model="$ctrl.worker.firstName" rule + on-change="$ctrl.generateCodeUser()" vn-focus> @@ -38,6 +40,8 @@ vn-one label="Code" ng-model="$ctrl.worker.code" + maxLength="3" + on-change="$ctrl.worker.code = $ctrl.worker.code.toUpperCase()" rule> + rule> - - 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