From aa5cfde1ae85e01be945fa6e5d92e44d42c6773f Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 2 Jan 2023 14:52:03 +0100 Subject: [PATCH] test(worker_new): add back tests --- CHANGELOG.md | 1 + db/dump/fixtures.sql | 14 +- .../components/reset-password/locale/es.yml | 2 +- modules/worker/back/methods/worker/new.js | 27 +++- .../back/methods/worker/specs/new.spec.js | 140 ++++++++++++++++++ modules/worker/front/create/index.js | 20 +++ .../email/worker-welcome/worker-welcome.js | 1 - 7 files changed, 188 insertions(+), 17 deletions(-) create mode 100644 modules/worker/back/methods/worker/specs/new.spec.js diff --git a/CHANGELOG.md b/CHANGELOG.md index bafedc760..897774716 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - [General](Inicio) Permite recuperar la contraseña +- [Trabajadores](Nuevo trabajador) Nueva sección ### Changed diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 03a0f8b50..933651f06 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -934,10 +934,10 @@ INSERT INTO `vn`.`expedition`(`id`, `agencyModeFk`, `ticketFk`, `freightItemFk`, (7, 2, 4, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -3 MONTH), 1, 18, NULL, 94, NULL,NULL), (8, 3, 5, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -4 MONTH), 1, 18, NULL, 94, 1, NULL), (9, 3, 6, 71, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1, 18, NULL, 94, 2, NULL), - (10, 7, 7, 71, NOW(), 1, 18, NULL, 94, 3, NULL), - (11, 7, 8, 71, NOW(), 1, 18, NULL, 94, 3, NULL), - (12, 7, 9, 71, NOW(), 1, 18, NULL, 94, 3, NULL), - (13, 1, 10,71, NOW(), 1, 18, NULL, 94, 3, NULL); + (10, 7, 7, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL), + (11, 7, 8, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL), + (12, 7, 9, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL), + (13, 1, 10,71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL); INSERT INTO `vn`.`expeditionState`(`id`, `created`, `expeditionFk`, `typeFk`, `userFk`) @@ -1910,7 +1910,7 @@ DROP TEMPORARY TABLE IF EXISTS tmp.worker; CREATE TEMPORARY TABLE tmp.worker (PRIMARY KEY (id)) ENGINE = MEMORY - SELECT w.id, w.id as `workerFk`, 'VNL', CONCAT(YEAR(DATE_ADD(CURDATE(), INTERVAL -1 YEAR)), '-12-25'), CONCAT(YEAR(DATE_ADD(CURDATE(), INTERVAL +1 YEAR)), '-12-25'), CONCAT('E-46-', RPAD(CONCAT(w.id, 9), 8, w.id)), NULL as `notes`, NULL as `departmentFk`, 23, 1 as `workerBusinessProfessionalCategoryFk`, 1 as `calendarTypeFk`, 1 as `isHourlyLabor`, 1 as `workerBusinessAgreementFk`, 1 as `workcenterFk` + SELECT w.id, w.id as `workerFk`, 'VNL', CONCAT(YEAR(DATE_ADD(util.VN_CURDATE(), INTERVAL -1 YEAR)), '-12-25'), CONCAT(YEAR(DATE_ADD(util.VN_CURDATE(), INTERVAL +1 YEAR)), '-12-25'), CONCAT('E-46-', RPAD(CONCAT(w.id, 9), 8, w.id)), NULL as `notes`, NULL as `departmentFk`, 23, 1 as `workerBusinessProfessionalCategoryFk`, 1 as `calendarTypeFk`, 1 as `isHourlyLabor`, 1 as `workerBusinessAgreementFk`, 1 as `workcenterFk` FROM `vn`.`worker` `w`; INSERT INTO `vn`.`business`(`id`, `workerFk`, `companyCodeFk`, `started`, `ended`, `workerBusiness`, `reasonEndFk`, `notes`, `departmentFk`, `workerBusinessProfessionalCategoryFk`, `calendarTypeFk`, `isHourlyLabor`, `workerBusinessAgreementFk`, `workcenterFk`) @@ -1920,7 +1920,7 @@ DROP TEMPORARY TABLE IF EXISTS tmp.worker; CREATE TEMPORARY TABLE tmp.worker (PRIMARY KEY (id)) ENGINE = MEMORY - SELECT '1111' as 'id', w.id as `workerFk`, 'VNL', CONCAT(YEAR(DATE_ADD(CURDATE(), INTERVAL -2 YEAR)), '-12-25'), CONCAT(YEAR(DATE_ADD(CURDATE(), INTERVAL -1 YEAR)), '-12-24'), CONCAT('E-46-', RPAD(CONCAT(w.id, 9), 8, w.id)), NULL as `notes`, NULL as `departmentFk`, 23, 1 as `workerBusinessProfessionalCategoryFk`, 1 as `calendarTypeFk`, 1 as `isHourlyLabor`, 1 as `workerBusinessAgreementFk`, 1 as `workcenterFk` + SELECT '1111' as 'id', w.id as `workerFk`, 'VNL', CONCAT(YEAR(DATE_ADD(util.VN_CURDATE(), INTERVAL -2 YEAR)), '-12-25'), CONCAT(YEAR(DATE_ADD(util.VN_CURDATE(), INTERVAL -1 YEAR)), '-12-24'), CONCAT('E-46-', RPAD(CONCAT(w.id, 9), 8, w.id)), NULL as `notes`, NULL as `departmentFk`, 23, 1 as `workerBusinessProfessionalCategoryFk`, 1 as `calendarTypeFk`, 1 as `isHourlyLabor`, 1 as `workerBusinessAgreementFk`, 1 as `workcenterFk` FROM `vn`.`worker` `w` WHERE `w`.`id` = 1109; @@ -2633,7 +2633,7 @@ INSERT INTO `vn`.`machineWorker` (`workerFk`, `machineFk`, `inTimed`, `outTimed` INSERT INTO `vn`.`zoneExclusion` (`id`, `zoneFk`, `dated`, `created`, `userFk`) VALUES - (1, 1, DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE())<=7, 7, 14) - DAYOFWEEK(util.VN_CURDATE())) DAY), util.VN_CURDATE(), 100), + (1, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL (IF(DAYOFWEEK(util.VN_CURDATE())<=7, 7, 14) - DAYOFWEEK(util.VN_CURDATE())) DAY), util.VN_CURDATE(), 100), (2, 1, DATE_ADD(util.VN_CURDATE(), INTERVAL (IF(DAYOFWEEK(util.VN_CURDATE())<=8, 8, 15) - DAYOFWEEK(util.VN_CURDATE())) DAY), util.VN_CURDATE(), 100); INSERT INTO `vn`.`zoneExclusionGeo` (`zoneExclusionFk`, `geoFk`) diff --git a/front/salix/components/reset-password/locale/es.yml b/front/salix/components/reset-password/locale/es.yml index 0771d5dc3..30893a152 100644 --- a/front/salix/components/reset-password/locale/es.yml +++ b/front/salix/components/reset-password/locale/es.yml @@ -1,6 +1,6 @@ Reset password: Restrablecer contraseña New password: Nueva contraseña -Repeat password: Repetir contraseñaç +Repeat password: Repetir contraseña Password changed!: ¡Contraseña cambiada! Password requirements: > La contraseña debe tener al menos {{ length }} caracteres de longitud, diff --git a/modules/worker/back/methods/worker/new.js b/modules/worker/back/methods/worker/new.js index 07a091693..b6da3301a 100644 --- a/modules/worker/back/methods/worker/new.js +++ b/modules/worker/back/methods/worker/new.js @@ -1,4 +1,3 @@ -const md5 = require('md5'); const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { @@ -108,6 +107,12 @@ module.exports = Self => { description: `The worker birth`, required: true, }, + { + arg: 'profileTypeFk', + type: 'number', + description: `The worker profile type ¿DELETE?`, + required: true, + } ], returns: { type: 'number', @@ -134,6 +139,7 @@ module.exports = Self => { } let client; + try { client = await models.Client.findOne( { @@ -144,14 +150,15 @@ module.exports = Self => { if (!client) { const nickname = args.firstName.concat(' ', args.lastNames); - const randomPassword = await models.Worker.rawSql( - 'SELECT account.passwordGenerate();' + const [randomPassword] = await models.Worker.rawSql( + 'SELECT account.passwordGenerate() as password;' ); + const user = await models.Account.create( { name: args.name, nickname, - password: md5(randomPassword), + password: randomPassword.password, email: args.email, role: args.roleFk, }, @@ -208,6 +215,10 @@ module.exports = Self => { myOptions ); } + + const user = await models.Account.findById(client.id, null, myOptions); + await user.updateAttribute('email', args.email, myOptions); + await models.Worker.rawSql( 'CALL vn.workerCreate(?, ?, ?, ?, ?, ?, ?)', [ @@ -223,10 +234,10 @@ module.exports = Self => { ); if (tx) await tx.commit(); - } catch (err) { + } catch (error) { if (tx) await tx.rollback(); - const code = err.code; - const message = err.sqlMessage; + const code = error.code; + const message = error.sqlMessage; if (code === 'ER_DUP_ENTRY' && message.includes(`for key 'mail'`)) throw new UserError(`This personal mail already exists`); @@ -237,7 +248,7 @@ module.exports = Self => { if (code === 'ER_DUP_ENTRY' && message.includes(`PRIMARY`)) throw new UserError(`This worker already exists`); - throw err; + throw error; } await models.user.resetPassword({ diff --git a/modules/worker/back/methods/worker/specs/new.spec.js b/modules/worker/back/methods/worker/specs/new.spec.js new file mode 100644 index 000000000..2cb8bebef --- /dev/null +++ b/modules/worker/back/methods/worker/specs/new.spec.js @@ -0,0 +1,140 @@ +const models = require('vn-loopback/server/server').models; +const LoopBackContext = require('loopback-context'); + +describe('Worker new', () => { + beforeAll(async() => { + const activeCtx = { + accessToken: {userId: 9}, + http: { + req: { + headers: {origin: 'http://localhost'} + } + } + }; + + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); + }); + + const employeeId = 1; + const defaultWorker = { + fi: '78457139E', + name: 'defaultWorker', + firstName: 'default', + lastNames: 'worker', + email: 'defaultWorker@mydomain.com', + roleFk: 1, + street: 'S/ defaultWorkerStreet', + city: 'defaultWorkerCity', + provinceFk: 1, + iban: 'ES8304879798578129532677', + bankEntityFk: 128, + companyFk: 442, + postcode: '46680', + phone: '633342693', + code: 'DWW', + bossFk: 9, + birth: '2022-12-11T23:00:00.000Z' + }; + + it('should return error if personal mail already exists', async() => { + const user = await models.Account.findById(employeeId, {fields: ['email']}); + + const tx = await models.Worker.beginTransaction({}); + + let error; + try { + const options = {transaction: tx}; + const ctx = { + args: Object.assign({}, defaultWorker, {email: user.email}) + }; + + await models.Worker.new(ctx, options); + + await tx.rollback(); + } catch (e) { + error = e; + await tx.rollback(); + } + + expect(error.message).toEqual('This personal mail already exists'); + }); + + it('should return error if worker code already exists', async() => { + const worker = await models.Worker.findById(employeeId, {fields: ['code']}); + + const tx = await models.Worker.beginTransaction({}); + + let error; + try { + const options = {transaction: tx}; + const ctx = { + args: Object.assign({}, defaultWorker, {code: worker.code}) + }; + + await models.Worker.new(ctx, options); + + await tx.rollback(); + } catch (e) { + error = e; + await tx.rollback(); + } + + expect(error.message).toEqual('This worker code already exists'); + }); + + it('should return error if worker already exists', async() => { + const worker = await models.Client.findById(employeeId, {fields: ['fi']}); + + const tx = await models.Worker.beginTransaction({}); + + let error; + try { + const options = {transaction: tx}; + const ctx = { + args: Object.assign({}, defaultWorker, {fi: worker.fi}) + }; + await models.Worker.new(ctx, options); + + await tx.rollback(); + } catch (e) { + error = e; + await tx.rollback(); + } + + expect(error.message).toEqual('This worker already exists'); + }); + + it('should create a new worker', async() => { + const newWorker = await models.Worker.new({args: defaultWorker}); + + await models.Worker.destroyById(newWorker.id); + await models.Address.destroyAll({clientFk: newWorker.id}); + await models.Mandate.destroyAll({clientFk: newWorker.id}); + await models.Client.destroyById(newWorker.id); + await models.Account.destroyById(newWorker.id); + + expect(newWorker.id).toBeDefined(); + }); + + it('should create a new worker in client', async() => { + const bruceWayneId = 1101; + const client = await models.Client.findById(bruceWayneId, {fields: ['fi', 'email']}); + + const newWorkerData = { + args: Object.assign( + {}, + defaultWorker, + { + fi: client.fi, + email: client.email + }) + }; + const newWorker = await models.Worker.new(newWorkerData); + + await models.Worker.destroyById(newWorker.id); + + expect(newWorker.id).toEqual(bruceWayneId); + }); +}); diff --git a/modules/worker/front/create/index.js b/modules/worker/front/create/index.js index 3314a4b10..b09fde94f 100644 --- a/modules/worker/front/create/index.js +++ b/modules/worker/front/create/index.js @@ -7,6 +7,26 @@ export default class Controller extends Section { this.$http.get('NewWorkerConfigs/findOne').then(res => { return this.worker = Object.assign({}, res.data); }); + // DELETE + this.worker = { + fi: '73226971P', + name: 'ASDASD', + firstName: 'Test', + lastNames: 'Test', + email: 'developer4@mydomain.com', + roleFk: 1, + street: 'S/ ', + city: 'asd', + provinceFk: 1, + iban: 'ES2420386579855387888566', + bankEntityFk: 128, + companyFk: 442, + postcode: 'asd', + phone: '633342693', + code: 'ASD', + bossFk: 34, + birth: '2022-12-11T23:00:00.000Z' + }; } onSubmit() { diff --git a/print/templates/email/worker-welcome/worker-welcome.js b/print/templates/email/worker-welcome/worker-welcome.js index 3fc9b4706..043a172a1 100755 --- a/print/templates/email/worker-welcome/worker-welcome.js +++ b/print/templates/email/worker-welcome/worker-welcome.js @@ -5,7 +5,6 @@ module.exports = { name: 'worker-welcome', async serverPrefetch() { this.worker = await this.fetchWorker(this.id); - console.log(this.url); }, methods: { fetchWorker(id) {