can't transfer claimed sales without claimManager role
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2022-01-12 10:17:51 +01:00
parent 51bf4e44ba
commit cc3e2fbc3a
3 changed files with 67 additions and 22 deletions

View File

@ -215,5 +215,6 @@
"You can't create a claim from a ticket delivered more than seven days ago": "No puedes crear una reclamación de un ticket entregado hace más de siete días",
"The worker has hours recorded that day": "El trabajador tiene horas fichadas ese día",
"The worker has a marked absence that day": "El trabajador tiene marcada una ausencia ese día",
"You can not modify is pay method checked": "No se puede modificar el campo método de pago validado"
}
"You can not modify is pay method checked": "No se puede modificar el campo método de pago validado",
"Can't transfer claimed sales": "No puedes transferir lineas reclamadas"
}

View File

@ -58,13 +58,63 @@ describe('sale transferSales()', () => {
expect(error.message).toEqual(`The sales of the receiver ticket can't be modified`);
});
it('should transfer the sales from one ticket to a new one then send them back and delete the created ticket', async() => {
it('should throw an error if any of the sales has a claim', async() => {
const tx = await models.Ticket.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const ticketId = 11;
const receiverTicketId = null;
const sales = await models.Ticket.getSales(ticketId, options);
await models.Ticket.transferSales(ctx, ticketId, receiverTicketId, sales, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.message).toEqual(`Can't transfer claimed sales`);
});
it('should be able to transfer claimed sales if the role is claimManager', async() => {
const tx = await models.Ticket.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const claimManagerId = 72;
const myActiveCtx = {
accessToken: {userId: claimManagerId},
};
const myCtx = {req: myActiveCtx};
const ticketId = 11;
const receiverTicketId = null;
const sales = await models.Ticket.getSales(ticketId, options);
await models.Ticket.transferSales(myCtx, ticketId, receiverTicketId, sales, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error).toBeUndefined();
});
it('should transfer the sales from a ticket to a new one', async() => {
const tx = await models.Ticket.beginTransaction({});
try {
const options = {transaction: tx};
const formerTicketId = 11;
const formerTicketId = 22;
let createdTicketId = undefined;
let formerTicketSales = await models.Ticket.getSales(formerTicketId, options);
@ -77,23 +127,11 @@ describe('sale transferSales()', () => {
createdTicketId = createdTicket.id;
formerTicketSales = await models.Ticket.getSales(formerTicketId, options);
createdTicketSales = await models.Ticket.getSales(createdTicketId, options);
let createdTicketSales = await models.Ticket.getSales(createdTicketId, options);
expect(formerTicketSales.length).toEqual(0);
expect(createdTicketSales.length).toEqual(2);
await models.Ticket.transferSales(
ctx, createdTicketId, formerTicketId, createdTicketSales, options);
formerTicketSales = await models.Ticket.getSales(formerTicketId, options);
createdTicketSales = await models.Ticket.getSales(createdTicketId, options);
createdTicket = await models.Ticket.findById(createdTicketId, null, options);
expect(createdTicket.isDeleted).toBeTruthy();
expect(formerTicketSales.length).toEqual(2);
expect(createdTicketSales.length).toEqual(0);
await tx.rollback();
} catch (e) {
await tx.rollback();
@ -109,10 +147,9 @@ describe('sale transferSales()', () => {
try {
const options = {transaction: tx};
const currentTicket = await models.Ticket.findById(11, null, options);
const currentTicketSales = await models.Ticket.getSales(currentTicket.id, options);
const currentTicketId = 22;
const currentTicketSales = await models.Ticket.getSales(currentTicketId, options);
const currentTicketId = currentTicket.id;
const receiverTicketId = undefined;
currentTicketSales[0].quantity = 99;
@ -135,7 +172,7 @@ describe('sale transferSales()', () => {
try {
const options = {transaction: tx};
const formerTicketId = 11;
const formerTicketId = 22;
let createdTicketId = undefined;
let formerTicketSales = await models.Ticket.getSales(formerTicketId, options);
@ -143,7 +180,7 @@ describe('sale transferSales()', () => {
const completeSaleId = formerTicketSales[1].id;
let partialSaleTotalQuantity = formerTicketSales[0].quantity;
expect(partialSaleTotalQuantity).toEqual(15);
expect(partialSaleTotalQuantity).toEqual(30);
formerTicketSales[0].quantity = 1;

View File

@ -75,6 +75,13 @@ module.exports = Self => {
for (const sale of originalSales)
map.set(sale.id, sale);
const saleIds = sales.map(sale => sale.id);
const isClaimManager = await models.Account.hasRole(userId, 'claimManager');
const hasClaimedSales = await models.ClaimBeginning.findOne({where: {saleFk: {inq: saleIds}}});
if (hasClaimedSales && !isClaimManager)
throw new UserError(`Can't transfer claimed sales`);
for (const sale of sales) {
const originalSale = map.get(sale.id);
const originalSaleData = { // <-- Loopback modifies original instance on save