8355-testToMaster #3336
|
@ -9,9 +9,8 @@ describe('invoiceIn', () => {
|
|||
const companyId = 442;
|
||||
|
||||
beforeEach(async() => {
|
||||
options = {transaction: tx};
|
||||
tx = await models.InvoiceIn.beginTransaction({});
|
||||
options.transaction = tx;
|
||||
options = {transaction: tx};
|
||||
});
|
||||
|
||||
afterEach(async() => {
|
||||
|
@ -45,7 +44,6 @@ describe('invoiceIn', () => {
|
|||
error = err;
|
||||
}
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.message).toBe('InvoiceIn is already booked');
|
||||
});
|
||||
|
||||
|
@ -60,7 +58,6 @@ describe('invoiceIn', () => {
|
|||
error = err;
|
||||
}
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.sqlMessage).toBe('InvoiceIn is already booked');
|
||||
expect(error.message).toBe('InvoiceIn is already booked');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
const models = require('vn-loopback/server/server').models;
|
||||
|
||||
describe('invoiceInTax', () => {
|
||||
let options;
|
||||
let tx;
|
||||
const invoiceInId = 1;
|
||||
const invoiceInTaxId = 1;
|
||||
beforeEach(async() => {
|
||||
tx = await models.InvoiceInTax.beginTransaction({});
|
||||
options = {transaction: tx};
|
||||
});
|
||||
|
||||
afterEach(async() => {
|
||||
await tx.rollback();
|
||||
});
|
||||
|
||||
it('should throw an error if trying to save a tax from a booked invoice', async() => {
|
||||
const invoiceIn = await models.InvoiceIn.findById(invoiceInId, null, options);
|
||||
await invoiceIn.updateAttributes({isBooked: true}, options);
|
||||
const invoiceInTax = await models.InvoiceInTax.findById(invoiceInTaxId, null, options);
|
||||
let error;
|
||||
try {
|
||||
await invoiceInTax.updateAttribute('taxableBase', 100, options);
|
||||
} catch (err) {
|
||||
error = err;
|
||||
}
|
||||
|
||||
expect(error.message).toBe('InvoiceIn is already booked');
|
||||
});
|
||||
|
||||
it('should allow save if the invoice is not booked', async() => {
|
||||
const invoiceIn = await models.InvoiceIn.findById(invoiceInId, null, options);
|
||||
await invoiceIn.updateAttribute('isBooked', false, options);
|
||||
|
||||
const invoiceInTax = await models.InvoiceInTax.findById(invoiceInTaxId, null, options);
|
||||
await invoiceInTax.updateAttribute('taxableBase', 100, options);
|
||||
|
||||
const updatedInvoiceInTax = await models.InvoiceInTax.findById(invoiceInTaxId, null, options);
|
||||
|
||||
expect(updatedInvoiceInTax.taxableBase).toBe(100);
|
||||
});
|
||||
|
||||
it('should throw an error if trying to delete a tax from a booked invoice', async() => {
|
||||
const invoiceIn = await models.InvoiceIn.findById(invoiceInId, null, options);
|
||||
await invoiceIn.updateAttribute('isBooked', true, options);
|
||||
|
||||
let error;
|
||||
try {
|
||||
await models.InvoiceInTax.destroyById(invoiceInTaxId, options);
|
||||
} catch (err) {
|
||||
error = err;
|
||||
}
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.message).toBe('InvoiceIn is already booked');
|
||||
});
|
||||
|
||||
it('should allow delete if the invoice is not booked', async() => {
|
||||
const invoiceIn = await models.InvoiceIn.findById(invoiceInId, null, options);
|
||||
await invoiceIn.updateAttribute('isBooked', false, options);
|
||||
|
||||
let error;
|
||||
try {
|
||||
await models.InvoiceInTax.destroyById(invoiceInTaxId, options);
|
||||
} catch (err) {
|
||||
error = err;
|
||||
}
|
||||
|
||||
const deletedInvoiceInTax = await models.InvoiceInTax.findById(invoiceInTaxId, null, options);
|
||||
|
||||
expect(error).toBeUndefined();
|
||||
expect(deletedInvoiceInTax).toBeNull();
|
||||
});
|
||||
});
|
|
@ -5,14 +5,14 @@ module.exports = Self => {
|
|||
if (ctx.isNewInstance) return;
|
||||
|
||||
const models = Self.app.models;
|
||||
const invoiceIn = await models.InvoiceIn.findById(ctx.currentInstance.invoiceInFk);
|
||||
const invoiceIn = await models.InvoiceIn.findById(ctx.currentInstance.invoiceInFk, null, ctx.options);
|
||||
if (invoiceIn.isBooked) throw new UserError('InvoiceIn is already booked');
|
||||
});
|
||||
|
||||
Self.observe('before delete', async function(ctx) {
|
||||
const models = Self.app.models;
|
||||
const invoiceInTax = await models.InvoiceInTax.findById(ctx.where.id);
|
||||
const invoiceIn = await models.InvoiceIn.findById(invoiceInTax.invoiceInFk);
|
||||
const invoiceInTax = await Self.findById(ctx.where.id, null, ctx.options);
|
||||
const invoiceIn = await models.InvoiceIn.findById(invoiceInTax.invoiceInFk, null, ctx.options);
|
||||
if (invoiceIn.isBooked) throw new UserError('InvoiceIn is already booked');
|
||||
});
|
||||
};
|
||||
|
|
|
@ -36,7 +36,7 @@ module.exports = Self => {
|
|||
});
|
||||
|
||||
Self.observe('before delete', async function(ctx) {
|
||||
const invoiceIn = await Self.findById(ctx.where.id);
|
||||
const invoiceIn = await Self.findById(ctx.where.id, null, ctx.options);
|
||||
if (invoiceIn.isBooked) throw new UserError('InvoiceIn is already booked');
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue