Merge pull request 'can't transfer claimed sales without claimManager role' (#845) from 3506-ticket_sale_transfer into dev
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
Reviewed-on: #845 Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
commit
e7b2dc718a
|
@ -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",
|
"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 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",
|
"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"
|
||||||
|
}
|
|
@ -58,13 +58,63 @@ describe('sale transferSales()', () => {
|
||||||
expect(error.message).toEqual(`The sales of the receiver ticket can't be modified`);
|
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({});
|
const tx = await models.Ticket.beginTransaction({});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const options = {transaction: tx};
|
const options = {transaction: tx};
|
||||||
|
|
||||||
const formerTicketId = 11;
|
const formerTicketId = 22;
|
||||||
let createdTicketId = undefined;
|
let createdTicketId = undefined;
|
||||||
|
|
||||||
let formerTicketSales = await models.Ticket.getSales(formerTicketId, options);
|
let formerTicketSales = await models.Ticket.getSales(formerTicketId, options);
|
||||||
|
@ -77,23 +127,11 @@ describe('sale transferSales()', () => {
|
||||||
createdTicketId = createdTicket.id;
|
createdTicketId = createdTicket.id;
|
||||||
|
|
||||||
formerTicketSales = await models.Ticket.getSales(formerTicketId, options);
|
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(formerTicketSales.length).toEqual(0);
|
||||||
expect(createdTicketSales.length).toEqual(2);
|
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();
|
await tx.rollback();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await tx.rollback();
|
await tx.rollback();
|
||||||
|
@ -109,10 +147,9 @@ describe('sale transferSales()', () => {
|
||||||
try {
|
try {
|
||||||
const options = {transaction: tx};
|
const options = {transaction: tx};
|
||||||
|
|
||||||
const currentTicket = await models.Ticket.findById(11, null, options);
|
const currentTicketId = 22;
|
||||||
const currentTicketSales = await models.Ticket.getSales(currentTicket.id, options);
|
const currentTicketSales = await models.Ticket.getSales(currentTicketId, options);
|
||||||
|
|
||||||
const currentTicketId = currentTicket.id;
|
|
||||||
const receiverTicketId = undefined;
|
const receiverTicketId = undefined;
|
||||||
|
|
||||||
currentTicketSales[0].quantity = 99;
|
currentTicketSales[0].quantity = 99;
|
||||||
|
@ -135,7 +172,7 @@ describe('sale transferSales()', () => {
|
||||||
try {
|
try {
|
||||||
const options = {transaction: tx};
|
const options = {transaction: tx};
|
||||||
|
|
||||||
const formerTicketId = 11;
|
const formerTicketId = 22;
|
||||||
let createdTicketId = undefined;
|
let createdTicketId = undefined;
|
||||||
|
|
||||||
let formerTicketSales = await models.Ticket.getSales(formerTicketId, options);
|
let formerTicketSales = await models.Ticket.getSales(formerTicketId, options);
|
||||||
|
@ -143,7 +180,7 @@ describe('sale transferSales()', () => {
|
||||||
const completeSaleId = formerTicketSales[1].id;
|
const completeSaleId = formerTicketSales[1].id;
|
||||||
let partialSaleTotalQuantity = formerTicketSales[0].quantity;
|
let partialSaleTotalQuantity = formerTicketSales[0].quantity;
|
||||||
|
|
||||||
expect(partialSaleTotalQuantity).toEqual(15);
|
expect(partialSaleTotalQuantity).toEqual(30);
|
||||||
|
|
||||||
formerTicketSales[0].quantity = 1;
|
formerTicketSales[0].quantity = 1;
|
||||||
|
|
||||||
|
|
|
@ -75,6 +75,13 @@ module.exports = Self => {
|
||||||
for (const sale of originalSales)
|
for (const sale of originalSales)
|
||||||
map.set(sale.id, sale);
|
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) {
|
for (const sale of sales) {
|
||||||
const originalSale = map.get(sale.id);
|
const originalSale = map.get(sale.id);
|
||||||
const originalSaleData = { // <-- Loopback modifies original instance on save
|
const originalSaleData = { // <-- Loopback modifies original instance on save
|
||||||
|
|
Loading…
Reference in New Issue