diff --git a/db/routines/bs/procedures/clientNewBorn_recalc.sql b/db/routines/bs/procedures/clientNewBorn_recalc.sql index 1c89b5745..bb6b02aa7 100644 --- a/db/routines/bs/procedures/clientNewBorn_recalc.sql +++ b/db/routines/bs/procedures/clientNewBorn_recalc.sql @@ -6,25 +6,27 @@ BLOCK1: BEGIN DECLARE vShipped DATE; DECLARE vPreviousShipped DATE; DECLARE vDone boolean; - DECLARE cur cursor for - - SELECT clientFk, firstShipped - FROM bs.clientNewBorn; + + DECLARE cur CURSOR FOR + SELECT clientFk, firstShipped + FROM bs.clientNewBorn; DECLARE continue HANDLER FOR NOT FOUND SET vDone = TRUE; SET vDone := FALSE; DELETE FROM bs.clientNewBorn WHERE isModified = FALSE; - INSERT INTO clientNewBorn(clientFk, firstShipped, lastShipped) - SELECT c.id, MAX(t.shipped), MAX(t.shipped) - FROM vn.client c - JOIN vn.ticket t on t.clientFk = c.id - LEFT JOIN clientNewBorn cb on cb.clientFk = c.id - WHERE t.shipped BETWEEN TIMESTAMPADD(YEAR, -1, util.VN_CURDATE()) AND util.VN_CURDATE() AND cb.isModified is null - GROUP BY c.id; + INSERT INTO clientNewBorn(clientFk, firstShipped, lastShipped) + SELECT c.id, DATE(MAX(t.shipped)), DATE(MAX(t.shipped)) + FROM vn.client c + JOIN vn.ticket t ON t.clientFk = c.id + LEFT JOIN clientNewBorn cb ON cb.clientFk = c.id + WHERE t.shipped BETWEEN util.VN_CURDATE() - INTERVAL 1 YEAR + AND util.VN_CURDATE() + AND cb.isModified IS NULL + GROUP BY c.id; + OPEN cur; - LOOP1: LOOP SET vDone := FALSE; FETCH cur INTO vClientFk, vShipped; diff --git a/modules/claim/back/methods/claim/updateClaim.js b/modules/claim/back/methods/claim/updateClaim.js index 326192385..563b5b53d 100644 --- a/modules/claim/back/methods/claim/updateClaim.js +++ b/modules/claim/back/methods/claim/updateClaim.js @@ -85,16 +85,11 @@ module.exports = Self => { const updatedClaim = await claim.updateAttributes(args, myOptions); const salesPerson = claim.client().salesPersonUser(); - if (salesPerson) { - if (changedPickup && updatedClaim.pickup) - await notifyPickUp(ctx, salesPerson.id, claim); - - if (args.claimStateFk) { - const newState = await models.ClaimState.findById(args.claimStateFk, null, myOptions); - await notifyStateChange(ctx, salesPerson.id, claim, newState.description); - if (newState.code == 'canceled') - await notifyStateChange(ctx, claim.workerFk, claim, newState.description); - } + if (salesPerson && args.claimStateFk) { + const newState = await models.ClaimState.findById(args.claimStateFk, null, myOptions); + await notifyStateChange(ctx, salesPerson.id, claim, newState.description); + if (newState.code == 'canceled') + await notifyStateChange(ctx, claim.workerFk, claim, newState.description); } if (tx) await tx.commit(); @@ -119,18 +114,4 @@ module.exports = Self => { }); await models.Chat.sendCheckingPresence(ctx, workerId, message); } - - async function notifyPickUp(ctx, workerId, claim) { - const models = Self.app.models; - const url = await models.Url.getUrl(); - const $t = ctx.req.__; // $translate - - const message = $t('Claim will be picked', { - claimId: claim.id, - clientName: claim.client().name, - claimUrl: `${url}claim/${claim.id}/summary`, - claimPickup: $t(claim.pickup) - }); - await models.Chat.sendCheckingPresence(ctx, workerId, message); - } }; diff --git a/modules/client/back/methods/defaulter/filter.js b/modules/client/back/methods/defaulter/filter.js index 9f19dee0a..5359ce4a7 100644 --- a/modules/client/back/methods/defaulter/filter.js +++ b/modules/client/back/methods/defaulter/filter.js @@ -74,7 +74,8 @@ module.exports = Self => { pm.name payMethod, r.finished IS NULL hasRecovery, dp.id departmentFk, - dp.name departmentName + dp.name departmentName, + dp.notificationEmail departmentEmail FROM defaulter d JOIN client c ON c.id = d.clientFk JOIN country cn ON cn.id = c.countryFk diff --git a/modules/client/back/methods/defaulter/observationEmail.js b/modules/client/back/methods/defaulter/observationEmail.js index c06d1d3d0..797e88f74 100644 --- a/modules/client/back/methods/defaulter/observationEmail.js +++ b/modules/client/back/methods/defaulter/observationEmail.js @@ -47,7 +47,7 @@ module.exports = Self => { await models.Mail.create({ subject: $t('Comment added to client', {clientFk: defaulter.clientFk}), body: body, - receiver: `${defaulter.salesPersonName}@verdnatura.es`, + receiver: `${defaulter.departmentEmail}`, replyTo: `${user.name}@verdnatura.es` }, myOptions); } diff --git a/modules/ticket/back/methods/ticket-request/filter.js b/modules/ticket/back/methods/ticket-request/filter.js index 5364cef9a..53f90b98f 100644 --- a/modules/ticket/back/methods/ticket-request/filter.js +++ b/modules/ticket/back/methods/ticket-request/filter.js @@ -59,6 +59,11 @@ module.exports = Self => { arg: 'state', type: 'string', description: `Search request by request state` + }, + { + arg: 'myTeam', + type: 'boolean', + description: `Team partners` } ], returns: { @@ -75,6 +80,8 @@ module.exports = Self => { const conn = Self.dataSource.connector; const userId = ctx.req.accessToken.userId; const myOptions = {}; + const models = Self.app.models; + const args = ctx.args; if (typeof options == 'object') Object.assign(myOptions, options); @@ -82,6 +89,21 @@ module.exports = Self => { if (ctx.args.mine) ctx.args.attenderFk = userId; + const teamMembersId = []; + if (args.myTeam != null) { + const worker = await models.Worker.findById(userId, { + include: { + relation: 'collegues' + } + }, myOptions); + const collegues = worker.collegues() || []; + for (let collegue of collegues) + teamMembersId.push(collegue.collegueFk); + + if (teamMembersId.length == 0) + teamMembersId.push(userId); + } + let where = buildFilter(ctx.args, (param, value) => { switch (param) { case 'search': @@ -113,6 +135,11 @@ module.exports = Self => { return {'w.id': value}; case 'salesPersonFk': return {'c.salesPersonFk': value}; + case 'myTeam': + if (value) + return {'tr.requesterFk': {inq: teamMembersId}}; + else + return {'tr.requesterFk': {nin: teamMembersId}}; } }); diff --git a/modules/ticket/back/methods/ticket/setDeleted.js b/modules/ticket/back/methods/ticket/setDeleted.js index a684e1cbc..fcab95ee2 100644 --- a/modules/ticket/back/methods/ticket/setDeleted.js +++ b/modules/ticket/back/methods/ticket/setDeleted.js @@ -49,9 +49,12 @@ module.exports = Self => { where: {originalTicketFk: id} }, myOptions); + const hasRefund = !!ticketRefunds?.length; + const allDeleted = ticketRefunds.every(refund => refund.refundTicket().isDeleted); - if (ticketRefunds?.length && !allDeleted) { + if (!hasRefund) await models.TicketRefund.destroyAll({refundTicketFk: id}, myOptions); + if (hasRefund && !allDeleted) { const notDeleted = []; for (const refund of ticketRefunds) if (!refund.refundTicket().isDeleted) notDeleted.push(refund.refundTicket().id); diff --git a/modules/ticket/back/methods/ticket/specs/setDeleted.spec.js b/modules/ticket/back/methods/ticket/specs/setDeleted.spec.js index 782c31c02..b70c94cee 100644 --- a/modules/ticket/back/methods/ticket/specs/setDeleted.spec.js +++ b/modules/ticket/back/methods/ticket/specs/setDeleted.spec.js @@ -113,5 +113,27 @@ describe('ticket setDeleted()', () => { expect(error.message).not.toContain('Tickets with associated refunds'); }); + + it('should delete the refund - original ticket relation', async() => { + const tx = await models.Ticket.beginTransaction({}); + try { + const options = {transaction: tx}; + + const ticketId = 24; + const refundTicket = await models.TicketRefund.findOne({where: {refundTicketFk: ticketId}}, options); + + expect(refundTicket).toBeTruthy(); + + await models.Ticket.setDeleted(ctx, ticketId, options); + const removedRefundTicket = await models.TicketRefund.findOne({ + where: {refundTicketFk: ticketId}}, + options); + + expect(removedRefundTicket).toBeNull(); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + } + }); }); });