4658-createWorker #1236

Merged
alexm merged 35 commits from 4658-createWorker into dev 2023-01-23 07:30:52 +00:00
7 changed files with 188 additions and 17 deletions
Showing only changes of commit aa5cfde1ae - Show all commits

View File

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- [General](Inicio) Permite recuperar la contraseña - [General](Inicio) Permite recuperar la contraseña
- [Trabajadores](Nuevo trabajador) Nueva sección
### Changed ### Changed

View File

@ -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), (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), (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), (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), (10, 7, 7, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL),
(11, 7, 8, 71, NOW(), 1, 18, NULL, 94, 3, NULL), (11, 7, 8, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL),
(12, 7, 9, 71, NOW(), 1, 18, NULL, 94, 3, NULL), (12, 7, 9, 71, util.VN_NOW(), 1, 18, NULL, 94, 3, NULL),
(13, 1, 10,71, 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`) 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 CREATE TEMPORARY TABLE tmp.worker
(PRIMARY KEY (id)) (PRIMARY KEY (id))
ENGINE = MEMORY 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`; FROM `vn`.`worker` `w`;
INSERT INTO `vn`.`business`(`id`, `workerFk`, `companyCodeFk`, `started`, `ended`, `workerBusiness`, `reasonEndFk`, `notes`, `departmentFk`, `workerBusinessProfessionalCategoryFk`, `calendarTypeFk`, `isHourlyLabor`, `workerBusinessAgreementFk`, `workcenterFk`) 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 CREATE TEMPORARY TABLE tmp.worker
(PRIMARY KEY (id)) (PRIMARY KEY (id))
ENGINE = MEMORY 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` FROM `vn`.`worker` `w`
WHERE `w`.`id` = 1109; 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`) INSERT INTO `vn`.`zoneExclusion` (`id`, `zoneFk`, `dated`, `created`, `userFk`)
VALUES 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); (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`) INSERT INTO `vn`.`zoneExclusionGeo` (`zoneExclusionFk`, `geoFk`)

View File

@ -1,6 +1,6 @@
Reset password: Restrablecer contraseña Reset password: Restrablecer contraseña
New password: Nueva contraseña New password: Nueva contraseña
Repeat password: Repetir contraseñaç Repeat password: Repetir contraseña
Password changed!: ¡Contraseña cambiada! Password changed!: ¡Contraseña cambiada!
Password requirements: > Password requirements: >
La contraseña debe tener al menos {{ length }} caracteres de longitud, La contraseña debe tener al menos {{ length }} caracteres de longitud,

View File

@ -1,4 +1,3 @@
const md5 = require('md5');
const UserError = require('vn-loopback/util/user-error'); const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
@ -108,6 +107,12 @@ module.exports = Self => {
description: `The worker birth`, description: `The worker birth`,
required: true, required: true,
}, },
{
arg: 'profileTypeFk',
type: 'number',
description: `The worker profile type ¿DELETE?`,
required: true,
}
], ],
returns: { returns: {
type: 'number', type: 'number',
@ -134,6 +139,7 @@ module.exports = Self => {
} }
let client; let client;
try { try {
client = await models.Client.findOne( client = await models.Client.findOne(
{ {
@ -144,14 +150,15 @@ module.exports = Self => {
if (!client) { if (!client) {
const nickname = args.firstName.concat(' ', args.lastNames); const nickname = args.firstName.concat(' ', args.lastNames);
const randomPassword = await models.Worker.rawSql( const [randomPassword] = await models.Worker.rawSql(
'SELECT account.passwordGenerate();' 'SELECT account.passwordGenerate() as password;'
); );
const user = await models.Account.create( const user = await models.Account.create(
{ {
name: args.name, name: args.name,
nickname, nickname,
password: md5(randomPassword), password: randomPassword.password,
email: args.email, email: args.email,
role: args.roleFk, role: args.roleFk,
}, },
@ -208,6 +215,10 @@ module.exports = Self => {
myOptions myOptions
); );
} }
const user = await models.Account.findById(client.id, null, myOptions);
await user.updateAttribute('email', args.email, myOptions);
await models.Worker.rawSql( await models.Worker.rawSql(
'CALL vn.workerCreate(?, ?, ?, ?, ?, ?, ?)', 'CALL vn.workerCreate(?, ?, ?, ?, ?, ?, ?)',
[ [
@ -223,10 +234,10 @@ module.exports = Self => {
); );
if (tx) await tx.commit(); if (tx) await tx.commit();
} catch (err) { } catch (error) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
const code = err.code; const code = error.code;
const message = err.sqlMessage; const message = error.sqlMessage;
if (code === 'ER_DUP_ENTRY' && message.includes(`for key 'mail'`)) if (code === 'ER_DUP_ENTRY' && message.includes(`for key 'mail'`))
throw new UserError(`This personal mail already exists`); throw new UserError(`This personal mail already exists`);
@ -237,7 +248,7 @@ module.exports = Self => {
if (code === 'ER_DUP_ENTRY' && message.includes(`PRIMARY`)) if (code === 'ER_DUP_ENTRY' && message.includes(`PRIMARY`))
throw new UserError(`This worker already exists`); throw new UserError(`This worker already exists`);
throw err; throw error;
} }
await models.user.resetPassword({ await models.user.resetPassword({

View File

@ -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);
});
});

View File

@ -7,6 +7,26 @@ export default class Controller extends Section {
this.$http.get('NewWorkerConfigs/findOne').then(res => { this.$http.get('NewWorkerConfigs/findOne').then(res => {
return this.worker = Object.assign({}, res.data); 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() { onSubmit() {

View File

@ -5,7 +5,6 @@ module.exports = {
name: 'worker-welcome', name: 'worker-welcome',
async serverPrefetch() { async serverPrefetch() {
this.worker = await this.fetchWorker(this.id); this.worker = await this.fetchWorker(this.id);
console.log(this.url);
}, },
methods: { methods: {
fetchWorker(id) { fetchWorker(id) {