diff --git a/back/methods/dms/uploadFile.js b/back/methods/dms/uploadFile.js index 8456cf2d3..9748b8225 100644 --- a/back/methods/dms/uploadFile.js +++ b/back/methods/dms/uploadFile.js @@ -88,7 +88,7 @@ module.exports = Self => { warehouseFk: args.warehouseId, reference: args.reference, description: args.description, - contentType: args.contentType, + contentType: uploadedFile.type, hasFile: args.hasFile }; const extension = await models.DmsContainer.getFileExtension(uploadedFile.name); diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index 43293c9ea..7c6a2db6b 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -3068,3 +3068,6 @@ INSERT INTO `vn`.`cmr` (id,truckPlate,observations,senderInstruccions,paymentIns VALUES (1,'123456A','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet',442,1,2,1,'Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet'), (2,'123456N','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet',69,3,4,2,'Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet'), (3,'123456B','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet',567,5,6,69,'Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet','Lorem ipsum dolor sit amet'); + +UPDATE vn.department + SET workerFk = null; diff --git a/db/routines/vn/procedures/bankPolicy_notifyExpired.sql b/db/routines/vn/procedures/bankPolicy_notifyExpired.sql index 6bdc6f759..52b747659 100644 --- a/db/routines/vn/procedures/bankPolicy_notifyExpired.sql +++ b/db/routines/vn/procedures/bankPolicy_notifyExpired.sql @@ -16,7 +16,7 @@ BEGIN LEFT JOIN vn.supplier s ON s.id = bp.supplierFk LEFT JOIN vn.bank b - ON b.id = bp.bankFk + ON b.id = bp.accountingFk WHERE bp.insuranceExpired = util.VN_CURDATE(); END$$ DELIMITER ; diff --git a/db/routines/vn/procedures/duaInvoiceInBooking.sql b/db/routines/vn/procedures/duaInvoiceInBooking.sql index b68d23f9d..26580907c 100644 --- a/db/routines/vn/procedures/duaInvoiceInBooking.sql +++ b/db/routines/vn/procedures/duaInvoiceInBooking.sql @@ -48,7 +48,7 @@ BEGIN IF vCounter > 0 OR vASIEN > 0 THEN - UPDATE vn2008.XDiario x + UPDATE XDiario x JOIN ledgerConfig lc ON lc.lastBookEntry = x.ASIEN SET x.ASIEN = vASIEN; diff --git a/db/routines/vn/procedures/workerTimeControl_clockIn.sql b/db/routines/vn/procedures/workerTimeControl_clockIn.sql index 23739a515..77a628d10 100644 --- a/db/routines/vn/procedures/workerTimeControl_clockIn.sql +++ b/db/routines/vn/procedures/workerTimeControl_clockIn.sql @@ -75,7 +75,7 @@ BEGIN SET vDated = DATE(vTimed); - SELECT IF(pc.name = 'Conductor +3500kg', + SELECT IF(pc.code = 'driveCE', wc.dayBreakDriver, wc.dayBreak), wc.shortWeekBreak, diff --git a/db/routines/vn/views/doc.sql b/db/routines/vn/views/doc.sql deleted file mode 100644 index 31ac20640..000000000 --- a/db/routines/vn/views/doc.sql +++ /dev/null @@ -1,14 +0,0 @@ -CREATE OR REPLACE DEFINER=`root`@`localhost` - SQL SECURITY DEFINER - VIEW `vn`.`doc` -AS SELECT `g`.`id` AS `id`, - `g`.`sref` AS `sref`, - `g`.`brief` AS `brief`, - `g`.`emp_id` AS `companyFk`, - `g`.`orden` AS `order`, - `g`.`file` AS `file`, - `g`.`original` AS `original`, - `g`.`trabajador_id` AS `workerFk`, - `g`.`odbc_date` AS `created`, - `g`.`warehouse_id` AS `warehouseFk` -FROM `vn2008`.`gestdoc` `g` diff --git a/db/routines/vn2008/views/credit.sql b/db/routines/vn2008/views/credit.sql index 4bd3cef39..0de60b967 100644 --- a/db/routines/vn2008/views/credit.sql +++ b/db/routines/vn2008/views/credit.sql @@ -1,9 +1,11 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` - SQL SECURITY DEFINER - VIEW `vn2008`.`credit` -AS SELECT `c`.`id` AS `id`, - `c`.`clientFk` AS `Id_Cliente`, - `c`.`workerFk` AS `Id_Trabajador`, - `c`.`amount` AS `amount`, - `c`.`created` AS `odbc_date` -FROM `vn`.`clientCredit` `c` \ No newline at end of file + SQL SECURITY DEFINER + VIEW `vn2008`.`credit` +AS SELECT + `c`.`id` AS `id`, + `c`.`clientFk` AS `Id_Cliente`, + `c`.`workerFk` AS `Id_Trabajador`, + `c`.`amount` AS `amount`, + `c`.`created` AS `odbc_date` +FROM + `vn`.`clientCredit` `c` diff --git a/db/routines/vn2008/views/versiones.sql b/db/routines/vn2008/views/versiones.sql new file mode 100644 index 000000000..3d27f4f92 --- /dev/null +++ b/db/routines/vn2008/views/versiones.sql @@ -0,0 +1,8 @@ +CREATE OR REPLACE DEFINER=`root`@`localhost` + SQL SECURITY DEFINER + VIEW `vn2008`.`versiones` +AS SELECT `m`.`app` AS `programa`, + `m`.`version` AS `version`, + 0 AS `critical` +FROM `vn`.`mdbVersion` `m` +WHERE `m`.`branchFk` = 'master' \ No newline at end of file diff --git a/db/versions/10888-brownBirch/00-firstScript.sql b/db/versions/10888-brownBirch/00-firstScript.sql new file mode 100644 index 000000000..7c8f96339 --- /dev/null +++ b/db/versions/10888-brownBirch/00-firstScript.sql @@ -0,0 +1,6 @@ +CREATE OR REPLACE DEFINER=`root`@`localhost` + SQL SECURITY DEFINER +VIEW `vn2008`.`credit`AS + SELECT 1; + +GRANT SELECT ON TABLE vn2008.credit TO financialBoss; diff --git a/db/versions/10896-salmonOrchid/03-gastos_resumen.sql b/db/versions/10896-salmonOrchid/03-gastos_resumen.sql index dbb71bca2..29152bdd8 100644 --- a/db/versions/10896-salmonOrchid/03-gastos_resumen.sql +++ b/db/versions/10896-salmonOrchid/03-gastos_resumen.sql @@ -1,24 +1,33 @@ ALTER TABLE IF EXISTS vn2008.gastos_resumen DROP FOREIGN KEY IF EXISTS gastos_resumen_expense_FK; ALTER TABLE IF EXISTS `vn2008`.`gastos_resumen` RENAME `vn`.`expenseManual`; -ALTER TABLE IF EXISTS `vn`.`expenseManual` +ALTER TABLE `vn`.`expenseManual` CHANGE COLUMN IF EXISTS `Id_Gasto` `expenseFk` varchar(10) NOT NULL, CHANGE COLUMN IF EXISTS `importe` `amount` decimal(10,2) DEFAULT NULL, CHANGE COLUMN IF EXISTS `empresa_id` `companyFk` int(11) NOT NULL; -ALTER TABLE IF EXISTS vn.expenseManual COLLATE=utf8mb3_general_ci; +ALTER TABLE vn.expenseManual COLLATE=utf8mb3_general_ci; -ALTER TABLE IF EXISTS vn.expenseManual MODIFY COLUMN IF EXISTS expenseFk varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL; +ALTER TABLE vn.expenseManual MODIFY COLUMN IF EXISTS expenseFk varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL; -ALTER TABLE IF EXISTS vn.expenseManual ADD CONSTRAINT expenseManual_expense_FK FOREIGN KEY IF NOT EXISTS (expenseFk) REFERENCES vn.expense(id) ON DELETE CASCADE ON UPDATE CASCADE; - -ALTER TABLE IF EXISTS vn.expenseManual MODIFY COLUMN IF EXISTS companyFk int(10) unsigned NOT NULL; +ALTER TABLE vn.expenseManual DROP PRIMARY KEY; ALTER TABLE vn.expenseManual MODIFY COLUMN companyFk int(10) unsigned NULL; +ALTER TABLE vn.expenseManual ADD CONSTRAINT expenseManual_expense_FK FOREIGN KEY IF NOT EXISTS (expenseFk) REFERENCES vn.expense(id) ON DELETE CASCADE ON UPDATE CASCADE; + +ALTER TABLE vn.expenseManual ADD CONSTRAINT expenseManual_company_FK FOREIGN KEY IF NOT EXISTS (companyFk) REFERENCES vn.company(id) ON DELETE CASCADE ON UPDATE CASCADE; + +ALTER TABLE vn.expenseManual ADD IF NOT EXISTS id INT unsigned NOT NULL FIRST; + +ALTER TABLE vn.expenseManual ADD CONSTRAINT expenseManual_pk PRIMARY KEY IF NOT EXISTS (id); + +ALTER TABLE vn.expenseManual ADD CONSTRAINT expenseManual_unique UNIQUE KEY IF NOT EXISTS (expenseFk,`year`,`month`,companyFk); + +ALTER TABLE vn.expenseManual MODIFY COLUMN IF EXISTS companyFk int(10) unsigned NOT NULL; + UPDATE vn.expenseManual SET companyFK= NULL WHERE companyFk= 0; -ALTER TABLE IF EXISTS vn.expenseManual ADD CONSTRAINT expenseManual_company_FK FOREIGN KEY IF NOT EXISTS (companyFk) REFERENCES vn.company(id) ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/db/versions/10903-pinkIvy/00-professionalCategoryAddCode.sql b/db/versions/10903-pinkIvy/00-professionalCategoryAddCode.sql new file mode 100644 index 000000000..7caa42f32 --- /dev/null +++ b/db/versions/10903-pinkIvy/00-professionalCategoryAddCode.sql @@ -0,0 +1,6 @@ +ALTER TABLE vn.professionalCategory DROP COLUMN IF EXISTS code; +ALTER TABLE IF EXISTS vn.professionalCategory ADD COLUMN code VARCHAR(25) DEFAULT NULL; + +UPDATE vn.professionalCategory + SET code = 'driverCE' + WHERE name = 'Conductor C + E'; \ No newline at end of file diff --git a/modules/client/back/methods/client/setRating.js b/modules/client/back/methods/client/setRating.js index 21ac0c914..e3f866683 100644 --- a/modules/client/back/methods/client/setRating.js +++ b/modules/client/back/methods/client/setRating.js @@ -28,7 +28,6 @@ module.exports = Self => { Self.setRating = async function(ctx, id, rating, recommendedCredit, options) { let tx; const myOptions = {}; - if (typeof options == 'object') Object.assign(myOptions, options); @@ -42,6 +41,7 @@ module.exports = Self => { const clientUpdated = await client.updateAttributes({ rating: rating, recommendedCredit: recommendedCredit + }, myOptions); if (tx) await tx.commit(); diff --git a/modules/client/back/methods/client/specs/setRating.spec.js b/modules/client/back/methods/client/specs/setRating.spec.js index a7d0fb03a..353095e31 100644 --- a/modules/client/back/methods/client/specs/setRating.spec.js +++ b/modules/client/back/methods/client/specs/setRating.spec.js @@ -40,4 +40,26 @@ describe('Client setRating()', () => { throw e; } }); + + it('should change rating and recommendedCredit to 0', async() => { + const tx = await models.Ticket.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const clientId = 1101; + const newRating = 0; + const newRecommendedCredit = 0; + + const updatedClient = await models.Client.setRating(ctx, clientId, newRating, newRecommendedCredit, options); + + expect(updatedClient.rating).toEqual(newRating); + expect(updatedClient.recommendedCredit).toEqual(newRecommendedCredit); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); }); diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index a9e14effa..0a8ebcae5 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -319,7 +319,8 @@ module.exports = Self => { await Self.changeCredit(ctx, finalState, changes); // Credit management changes - if (changes?.rating || changes?.recommendedCredit) + + if (changes?.rating >= 0 || changes?.recommendedCredit >= 0) await Self.changeCreditManagement(ctx, finalState, changes); const oldInstance = {}; diff --git a/modules/ticket/back/methods/sale/specs/usesMana.spec.js b/modules/ticket/back/methods/sale/specs/usesMana.spec.js index 777bdc8f0..74465ab27 100644 --- a/modules/ticket/back/methods/sale/specs/usesMana.spec.js +++ b/modules/ticket/back/methods/sale/specs/usesMana.spec.js @@ -1,11 +1,7 @@ const models = require('vn-loopback/server/server').models; describe('sale usesMana()', () => { - const ctx = { - req: { - accessToken: {userId: 18} - } - }; + const ctx = {req: { accessToken: {userId: 18}}}; it('should return that the worker uses mana', async() => { const tx = await models.Sale.beginTransaction({}); @@ -45,4 +41,27 @@ describe('sale usesMana()', () => { throw e; } }); + + it('should return that the worker does not use mana because it is excluded', async() => { + const tx = await models.Sale.beginTransaction({}); + const buyerId = 35; + const franceDepartmentId = 133; + const buyerCtx = {req: {accessToken: {userId: buyerId}}}; + + try { + const options = {transaction: tx} + + await models.WorkerManaExcluded.create({workerFk: buyerId}, options); + await models.Business.updateAll({workerFk: buyerId}, {departmentFk: franceDepartmentId}, options); + + const usesMana = await models.Sale.usesMana(buyerCtx, options); + + expect(usesMana).toBe(false); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); }); diff --git a/modules/ticket/back/methods/sale/usesMana.js b/modules/ticket/back/methods/sale/usesMana.js index 75d8cdda7..31beb3a4c 100644 --- a/modules/ticket/back/methods/sale/usesMana.js +++ b/modules/ticket/back/methods/sale/usesMana.js @@ -21,6 +21,9 @@ module.exports = Self => { if (typeof options == 'object') Object.assign(myOptions, options); + const isManaExcluded = await models.WorkerManaExcluded.findById(userId, null, myOptions); + if (isManaExcluded) return false; + const salesDepartment = await models.Department.findOne({where: {code: 'VT'}, fields: 'id'}, myOptions); const departments = await models.Department.getLeaves(ctx, salesDepartment.id, null, myOptions); const workerDepartment = await models.WorkerDepartment.findById(userId, null, myOptions); diff --git a/modules/ticket/back/methods/ticket/saveSign.js b/modules/ticket/back/methods/ticket/saveSign.js index 9c6e8181a..fd40c1c22 100644 --- a/modules/ticket/back/methods/ticket/saveSign.js +++ b/modules/ticket/back/methods/ticket/saveSign.js @@ -33,8 +33,8 @@ module.exports = Self => { const models = Self.app.models; const myOptions = {userId: ctx.req.accessToken.userId}; let tx; - let ticket; - let externalTickets = []; + let dms; + let gestDocCreated = false; if (typeof options == 'object') Object.assign(myOptions, options); @@ -44,11 +44,6 @@ module.exports = Self => { myOptions.transaction = tx; } - const dmsTypeTicket = await models.DmsType.findOne({ - where: {code: 'ticket'}, - fields: ['id'] - }, myOptions); - async function setLocation(ticketId) { await models.Delivery.create({ ticketFk: ticketId, @@ -58,106 +53,102 @@ module.exports = Self => { }, myOptions); } - async function hasSignDms(ticketId) { + async function gestDocExists(ticketId) { const ticketDms = await models.TicketDms.findOne({ where: {ticketFk: ticketId}, - include: [ - { - relation: 'dms', - fields: ['id'], - scope: { - where: {dmsTypeFk: dmsTypeTicket.id} - } - } - ] + fields: ['dmsFk'] }, myOptions); - if (ticketDms?.dms()?.id) return true; + + if (!ticketDms) return false; + + const ticket = await models.Ticket.findById(ticketId, {fields: ['isSigned']}, myOptions); + if (ticket.isSigned == true) + return true; + else + await models.Dms.destroyAll({where: {reference: ticketId}}, myOptions); + + return false; } - async function createGestDoc() { + async function createGestDoc(id) { + const ticket = await models.Ticket.findById(id, + { + include: [ + { + relation: 'warehouse', + scope: { + fields: ['id'] + } + }, { + relation: 'client', + scope: { + fields: ['name'] + } + }, { + relation: 'route', + scope: { + fields: ['id'] + } + } + ] + }, myOptions); + const dmsType = await models.DmsType.findOne({where: {code: 'Ticket'}, fields: ['id']}, myOptions); const ctxUploadFile = Object.assign({}, ctx); + if (ticket.route() === null) + throw new UserError('Ticket without route'); ctxUploadFile.args = { warehouseId: ticket.warehouseFk, companyId: ticket.companyFk, - dmsTypeId: dmsTypeTicket.id, - reference: ticket.id, + dmsTypeId: dmsType.id, + reference: '', description: `Firma del cliente - Ruta ${ticket.route().id}`, - contentType: 'image/png', - hasFile: true + hasFile: false }; - const dms = await models.Dms.uploadFile(ctxUploadFile, myOptions); - await models.TicketDms.create({ticketFk: ticket.id, dmsFk: dms[0].id}, myOptions); + dms = await models.Dms.uploadFile(ctxUploadFile, myOptions); + gestDocCreated = true; } try { for (const ticketId of tickets) { - ticket = await models.Ticket.findById(ticketId, { - include: [{ - relation: 'address', - scope: { - include: { - relation: 'province', - scope: { - include: { - relation: 'country', - scope: { - fields: ['code'] - } - } - } - } - } - }, { - relation: 'route', - scope: { - fields: ['id'] - } - }] - }, myOptions); + const ticketState = await models.TicketState.findOne( + {where: {ticketFk: ticketId}, + fields: ['alertLevel'] + }, myOptions); - const ticketState = await models.TicketState.findOne({ - where: {ticketFk: ticketId}, - fields: ['alertLevel'] - }, myOptions); - - const packedAlertLevel = await models.AlertLevel.findOne({ - where: {code: 'PACKED'}, + const packedAlertLevel = await models.AlertLevel.findOne({where: {code: 'PACKED'}, fields: ['id'] }, myOptions); if (!ticketState) throw new UserError('Ticket does not exist'); - if (!ticket.route()) - throw new UserError('Ticket without route'); if (ticketState.alertLevel < packedAlertLevel.id) throw new UserError('This ticket cannot be signed because it has not been boxed'); - if (await ticket.isSigned) + if (await gestDocExists(ticketId)) throw new UserError('Ticket is already signed'); if (location) await setLocation(ticketId); - if (!await hasSignDms(ticketId)) - await createGestDoc(ticketId); + if (!gestDocCreated) await createGestDoc(ticketId); + await models.TicketDms.create({ticketFk: ticketId, dmsFk: dms[0].id}, myOptions); + const ticket = await models.Ticket.findById(ticketId, null, myOptions); await ticket.updateAttribute('isSigned', true, myOptions); const deliveryState = await models.State.findOne({ - where: {code: 'DELIVERED'} + where: { + code: 'DELIVERED' + } }, myOptions); await models.Ticket.state(ctx, { ticketFk: ticketId, stateFk: deliveryState.id }, myOptions); - - if (ticket?.address()?.province()?.country()?.code != 'ES') { - await models.Ticket.saveCmr(ctx, [ticketId], myOptions); - externalTickets.push(ticketId); - } } + if (tx) await tx.commit(); + return; } catch (e) { if (tx) await tx.rollback(); throw e; } - await models.Route.cmrEmail(ctx, externalTickets); }; }; diff --git a/modules/worker/back/methods/worker-time-control/sendMail.js b/modules/worker/back/methods/worker-time-control/sendMail.js index 6f67bbea3..e43f4a8ab 100644 --- a/modules/worker/back/methods/worker-time-control/sendMail.js +++ b/modules/worker/back/methods/worker-time-control/sendMail.js @@ -137,7 +137,8 @@ module.exports = Self => { tb.type, tb.businessFk, tb.permissionRate, - d.isTeleworking + d.isTeleworking, + d.hasToRefill FROM tmp.timeBusinessCalculate tb JOIN account.user u ON u.id = tb.userFk JOIN department d ON d.id = tb.departmentFk @@ -174,7 +175,7 @@ module.exports = Self => { myOptions.transaction = tx; try { workerFk = day.workerFk; - if (day.timeWorkDecimal > 0 && day.timeWorkedDecimal == null + if (day.hasToRefill && day.timeWorkDecimal > 0 && day.timeWorkedDecimal == null && (day.permissionRate == null ? true : day.permissionRate)) { if (day.timeTable == null) { const timed = new Date(day.dated); diff --git a/modules/worker/back/methods/worker-time-control/specs/clockIn.spec.js b/modules/worker/back/methods/worker-time-control/specs/clockIn.spec.js index ff4cac7b7..0ee439941 100644 --- a/modules/worker/back/methods/worker-time-control/specs/clockIn.spec.js +++ b/modules/worker/back/methods/worker-time-control/specs/clockIn.spec.js @@ -46,6 +46,31 @@ describe('workerTimeControl clockIn()', () => { } }); + it('should throw an error trying to change a middle hour to out not resting 12h', async() => { + activeCtx.accessToken.userId = HHRRId; + const workerId = teamBossId; + + const tx = await models.WorkerTimeControl.beginTransaction({}); + try { + const options = {transaction: tx}; + + const entryTime = "2000-12-25T11:00:00.000Z"; + ctx.args = {timed: entryTime, direction: 'in'}; + await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); + + const middleTime ="2000-12-26T11:00:00.000Z"; + ctx.args = {timed: middleTime, direction: 'middle'}; + const middleEntryTime = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); + + const direction = 'out'; + await models.WorkerTimeControl.updateTimeEntry(ctx, middleEntryTime.id, direction, options); + await tx.rollback(); + } catch (e) { + expect(e.message).toBe('Superado el tiempo máximo entre entrada y salida'); + await tx.rollback(); + } + }); + describe('as Role errors', () => { it('should add if the current user is team boss and the target user is himself', async() => { activeCtx.accessToken.userId = teamBossId; @@ -110,16 +135,22 @@ describe('workerTimeControl clockIn()', () => { todayAtOne.setHours(1, 0, 0, 0); ctx.args = {timed: todayAtOne, direction: 'in'}; - const createdTimeEntry = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); + const entryTime = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); + expect(entryTime.id).toBeDefined(); + const todayAtTwo = Date.vnNew(); - expect(createdTimeEntry.id).toBeDefined(); - - ctx.args = {direction: 'out'}; - const updatedTimeEntry = await models.WorkerTimeControl.updateTimeEntry( - ctx, createdTimeEntry.id, options + todayAtTwo.setHours(2, 0, 0, 0); + ctx.args = {timed: todayAtTwo, direction: 'middle'}; + const middleTime = await models.WorkerTimeControl.addTimeEntry(ctx, workerId, options); + + const direction = 'out'; + const {id:outTimeEntryId} = await models.WorkerTimeControl.updateTimeEntry( + ctx, middleTime.id, direction, options ); - expect(updatedTimeEntry.direction).toEqual('out'); + const {direction: updatedDirection} = await models.WorkerTimeControl.findById(outTimeEntryId,{fields:['direction']},options); + expect(updatedDirection).toEqual('out'); + await tx.rollback(); } catch (e) { await tx.rollback(); diff --git a/modules/worker/back/methods/worker-time-control/updateTimeEntry.js b/modules/worker/back/methods/worker-time-control/updateTimeEntry.js index e9d35f880..7e4455447 100644 --- a/modules/worker/back/methods/worker-time-control/updateTimeEntry.js +++ b/modules/worker/back/methods/worker-time-control/updateTimeEntry.js @@ -26,32 +26,39 @@ module.exports = Self => { } }); - Self.updateTimeEntry = async(ctx, id, options) => { + Self.updateTimeEntry = async(ctx, timeEntryId, direction, options) => { const currentUserId = ctx.req.accessToken.userId; const models = Self.app.models; - const args = ctx.args; - const myOptions = {}; + let tx; if (typeof options == 'object') Object.assign(myOptions, options); - const targetTimeEntry = await Self.findById(id, null, myOptions); - const isSubordinate = await models.Worker.isSubordinate(ctx, targetTimeEntry.userFk, myOptions); - const isTeamBoss = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isTeamBoss', 'WRITE'); - const isHimself = currentUserId == targetTimeEntry.userFk; + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } - const notAllowed = isSubordinate === false || (isSubordinate && isHimself && !isTeamBoss); + try { + const {id, userFk, timed} = await Self.findById(timeEntryId, null, myOptions); + const isSubordinate = await models.Worker.isSubordinate(ctx, userFk, myOptions); + const isTeamBoss = await models.ACL.checkAccessAcl(ctx, 'Worker', 'isTeamBoss', 'WRITE'); + const isHimself = currentUserId == userFk; - if (notAllowed) - throw new UserError(`You don't have enough privileges`); + const notAllowed = isSubordinate === false || (isSubordinate && isHimself && !isTeamBoss); + if (notAllowed) throw new UserError(`You don't have enough privileges`); - const timeEntryUpdated = await targetTimeEntry.updateAttributes({ - direction: args.direction - }, myOptions); + await models.WorkerTimeControl.deleteById(id, myOptions); + const timeEntryUpdatedId = await Self.clockIn(userFk, timed, direction, null, myOptions); - await models.WorkerTimeControl.resendWeeklyHourEmail(ctx, targetTimeEntry.userFk, targetTimeEntry.timed, myOptions); + await models.WorkerTimeControl.resendWeeklyHourEmail(ctx, userFk, timed, myOptions); - return timeEntryUpdated; + if (tx) await tx.commit(); + return timeEntryUpdatedId; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; }; diff --git a/modules/worker/back/methods/worker/specs/new.spec.js b/modules/worker/back/methods/worker/specs/new.spec.js index 1f74e9b95..d3e9cb9d0 100644 --- a/modules/worker/back/methods/worker/specs/new.spec.js +++ b/modules/worker/back/methods/worker/specs/new.spec.js @@ -139,9 +139,12 @@ describe('Worker new', () => { }); it('should create a new worker', async() => { - const newWorker = await models.Worker.new({args: defaultWorker, req}); - - await removeWorker(newWorker.id); + let newWorker; + try { + newWorker = await models.Worker.new({args: defaultWorker, req}); + } finally { + await removeWorker(newWorker.id); + } expect(newWorker.id).toBeDefined(); }); @@ -173,9 +176,12 @@ describe('Worker new', () => { }), req }; - const newWorker = await models.Worker.new(newWorkerData); - - await models.Worker.destroyById(newWorker.id); + let newWorker; + try { + newWorker = await models.Worker.new(newWorkerData); + } finally { + await models.Worker.destroyById(newWorker.id); + } expect(newWorker.id).toEqual(bruceWayneId); }); diff --git a/modules/worker/back/model-config.json b/modules/worker/back/model-config.json index 8352eb070..e12ceada5 100644 --- a/modules/worker/back/model-config.json +++ b/modules/worker/back/model-config.json @@ -86,6 +86,9 @@ "WorkerMana": { "dataSource": "vn" }, + "WorkerManaExcluded": { + "dataSource": "vn" + }, "WorkerMistake": { "dataSource": "vn" }, diff --git a/modules/worker/back/models/worker-mana-excluded.json b/modules/worker/back/models/worker-mana-excluded.json new file mode 100644 index 000000000..6610b701b --- /dev/null +++ b/modules/worker/back/models/worker-mana-excluded.json @@ -0,0 +1,22 @@ +{ + "name": "WorkerManaExcluded", + "base": "VnModel", + "options": { + "mysql": { + "table": "workerManaExcluded" + } + }, + "properties": { + "workerFk": { + "id": true, + "type": "number" + } + }, + "relations": { + "worker": { + "type": "belongsTo", + "model": "Worker", + "foreignKey": "workerFk" + } + } +} diff --git a/modules/worker/front/time-control/index.js b/modules/worker/front/time-control/index.js index f6a6ed535..094e200a4 100644 --- a/modules/worker/front/time-control/index.js +++ b/modules/worker/front/time-control/index.js @@ -415,11 +415,13 @@ class Controller extends Section { throw new Error(`The entry type can't be empty`); const query = `WorkerTimeControls/${entry.id}/updateTimeEntry`; - this.$http.post(query, {direction: entry.direction}) - .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))) - .then(() => this.$.editEntry.hide()) - .then(() => this.fetchHours()) - .then(() => this.getMailStates(this.date)); + if (entry.direction !== entry.$orgRow.direction) { + this.$http.post(query, {direction: entry.direction}) + .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))) + .then(() => this.$.editEntry.hide()) + .then(() => this.fetchHours()) + .then(() => this.getMailStates(this.date)); + } } catch (e) { this.vnApp.showError(this.$t(e.message)); } diff --git a/modules/worker/front/time-control/index.spec.js b/modules/worker/front/time-control/index.spec.js index 10e8aba0d..8610da46e 100644 --- a/modules/worker/front/time-control/index.spec.js +++ b/modules/worker/front/time-control/index.spec.js @@ -130,7 +130,7 @@ describe('Component vnWorkerTimeControl', () => { controller.$.model = {applyFilter: jest.fn().mockReturnValue(Promise.resolve())}; controller.date = today; controller.fetchHours = jest.fn(); - controller.selectedRow = {id: 1, timed: Date.vnNew(), direction: 'in'}; + controller.selectedRow = {id: 1, timed: Date.vnNew(), direction: 'in', $orgRow: {direction: null}}; controller.$.editEntry = { hide: () => {} }; diff --git a/package.json b/package.json index 47b3a1564..302738524 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "@babel/plugin-syntax-dynamic-import": "^7.7.4", "@babel/preset-env": "^7.11.0", "@babel/register": "^7.7.7", - "@verdnatura/myt": "^1.6.7", + "@verdnatura/myt": "^1.6.8", "angular-mocks": "^1.7.9", "babel-jest": "^26.0.1", "babel-loader": "^8.2.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 025be234e..36bff2fe1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -131,8 +131,8 @@ devDependencies: specifier: ^7.7.7 version: 7.23.7(@babel/core@7.23.9) '@verdnatura/myt': - specifier: ^1.6.7 - version: 1.6.7 + specifier: ^1.6.8 + version: 1.6.8 angular-mocks: specifier: ^1.7.9 version: 1.8.3 @@ -2633,8 +2633,8 @@ packages: dev: false optional: true - /@verdnatura/myt@1.6.7: - resolution: {integrity: sha512-t/Q1T3QzHpZFdxwIyQL/CV5g+HJvWE6Q65VeA9k0svZdX/vezgnQ21nkI+wuvIurIl6BXqq2Arx7EWYkAhGNNA==} + /@verdnatura/myt@1.6.8: + resolution: {integrity: sha512-jpadr6yAR9TQXPv+has5yOYAolR/YEzsxbLgMR7BoDrpLyVFLHJEy4Dfe+Hy11r3AmxCB/8lWM+La1YGvXMWOA==} hasBin: true dependencies: '@sqltools/formatter': 1.2.5