Merge pull request 'feat(ClaimBeginning): throw error when quantity claimed is greater than quantity of line' (!3031) from hotFix_claimBeginning_quantity into master
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
Reviewed-on: #3031 Reviewed-by: Javi Gallego <jgallego@verdnatura.es>
This commit is contained in:
commit
568a77aa19
|
@ -236,6 +236,6 @@
|
|||
"Cannot send mail": "Cannot send mail",
|
||||
"CONSTRAINT `chkParkingCodeFormat` failed for `vn`.`parking`": "CONSTRAINT `chkParkingCodeFormat` failed for `vn`.`parking`",
|
||||
"This postcode already exists": "This postcode already exists",
|
||||
"Original invoice not found": "Original invoice not found"
|
||||
|
||||
"Original invoice not found": "Original invoice not found",
|
||||
"The quantity claimed cannot be greater than the quantity of the line": "The quantity claimed cannot be greater than the quantity of the line"
|
||||
}
|
||||
|
|
|
@ -372,5 +372,6 @@
|
|||
"The entry not have stickers": "La entrada no tiene etiquetas",
|
||||
"Too many records": "Demasiados registros",
|
||||
"Original invoice not found": "Factura original no encontrada",
|
||||
"The entry has no lines or does not exist": "La entrada no tiene lineas o no existe"
|
||||
"The entry has no lines or does not exist": "La entrada no tiene lineas o no existe",
|
||||
"The quantity claimed cannot be greater than the quantity of the line": "La cantidad reclamada no puede ser mayor que la cantidad de la línea"
|
||||
}
|
||||
|
|
|
@ -361,6 +361,6 @@
|
|||
"The invoices have been created but the PDFs could not be generated": "La facture a été émise mais le PDF n'a pas pu être généré",
|
||||
"It has been invoiced but the PDF of refund not be generated": "Il a été facturé mais le PDF de remboursement n'a pas été généré",
|
||||
"Cannot send mail": "Impossible d'envoyer le mail",
|
||||
"Original invoice not found": "Facture originale introuvable"
|
||||
|
||||
"Original invoice not found": "Facture originale introuvable",
|
||||
"The quantity claimed cannot be greater than the quantity of the line": "Le montant réclamé ne peut pas être supérieur au montant de la ligne"
|
||||
}
|
||||
|
|
|
@ -361,5 +361,6 @@
|
|||
"The invoices have been created but the PDFs could not be generated": "Foi faturado, mas o PDF não pôde ser gerado",
|
||||
"It has been invoiced but the PDF of refund not be generated": "Foi faturado mas não foi gerado o PDF do reembolso",
|
||||
"Original invoice not found": "Fatura original não encontrada",
|
||||
"Cannot send mail": "Não é possível enviar o email"
|
||||
"Cannot send mail": "Não é possível enviar o email",
|
||||
"The quantity claimed cannot be greater than the quantity of the line": "O valor reclamado não pode ser superior ao valor da linha"
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
const models = require('vn-loopback/server/server').models;
|
||||
const LoopBackContext = require('loopback-context');
|
||||
|
||||
describe('ClaimBeginning model()', () => {
|
||||
const claimFk = 1;
|
||||
const activeCtx = {
|
||||
accessToken: {userId: 18},
|
||||
headers: {origin: 'localhost:5000'},
|
||||
__: () => {}
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
||||
active: activeCtx
|
||||
});
|
||||
});
|
||||
|
||||
it('should change quantity claimed', async() => {
|
||||
const tx = await models.ClaimBeginning.beginTransaction({});
|
||||
|
||||
let error;
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
const claim = await models.ClaimBeginning.findOne({where: {claimFk}}, options);
|
||||
const sale = await models.Sale.findById(claim.saleFk, {}, options);
|
||||
await claim.updateAttribute('quantity', sale.quantity - 1, options);
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
error = e;
|
||||
await tx.rollback();
|
||||
}
|
||||
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should throw error when quantity claimed is greater than quantity of the sale', async() => {
|
||||
const tx = await models.ClaimBeginning.beginTransaction({});
|
||||
|
||||
let error;
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
const claim = await models.ClaimBeginning.findOne({where: {claimFk}}, options);
|
||||
const sale = await models.Sale.findById(claim.saleFk, {}, options);
|
||||
await claim.updateAttribute('quantity', sale.quantity + 1, options);
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
error = e;
|
||||
await tx.rollback();
|
||||
}
|
||||
|
||||
expect(error.toString()).toContain('The quantity claimed cannot be greater than the quantity of the line');
|
||||
});
|
||||
});
|
|
@ -10,16 +10,21 @@ module.exports = Self => {
|
|||
});
|
||||
|
||||
Self.observe('before save', async ctx => {
|
||||
const options = ctx.options;
|
||||
const models = Self.app.models;
|
||||
const saleFk = ctx?.currentInstance?.saleFk || ctx?.instance?.saleFk;
|
||||
const sale = await models.Sale.findById(saleFk, {fields: ['ticketFk', 'quantity']}, options);
|
||||
|
||||
if (ctx.isNewInstance) {
|
||||
const models = Self.app.models;
|
||||
const options = ctx.options;
|
||||
const instance = ctx.instance;
|
||||
const ticket = await models.Sale.findById(instance.saleFk, {fields: ['ticketFk']}, options);
|
||||
const claim = await models.Claim.findById(instance.claimFk, {fields: ['ticketFk']}, options);
|
||||
if (ticket.ticketFk != claim.ticketFk)
|
||||
const claim = await models.Claim.findById(ctx.instance.claimFk, {fields: ['ticketFk']}, options);
|
||||
if (sale.ticketFk != claim.ticketFk)
|
||||
throw new UserError(`Cannot create a new claimBeginning from a different ticket`);
|
||||
}
|
||||
|
||||
await claimIsEditable(ctx);
|
||||
|
||||
if (sale?.quantity && ctx.data?.quantity && ctx.data.quantity > sale?.quantity)
|
||||
throw new UserError('The quantity claimed cannot be greater than the quantity of the line');
|
||||
});
|
||||
|
||||
Self.observe('before delete', async ctx => {
|
||||
|
|
Loading…
Reference in New Issue