feat: refs #8167 update canBeInvoiced method to include active status check and improve test cases #3323
|
@ -2,7 +2,7 @@ const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
module.exports = function(Self) {
|
module.exports = function(Self) {
|
||||||
Self.remoteMethod('canBeInvoiced', {
|
Self.remoteMethod('canBeInvoiced', {
|
||||||
description: 'Change property isEqualizated in all client addresses',
|
description: 'Check if a client can be invoiced',
|
||||||
accessType: 'READ',
|
accessType: 'READ',
|
||||||
accepts: [
|
accepts: [
|
||||||
{
|
{
|
||||||
|
@ -38,7 +38,7 @@ module.exports = function(Self) {
|
||||||
Object.assign(myOptions, options);
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
const client = await models.Client.findById(id, {
|
const client = await models.Client.findById(id, {
|
||||||
fields: ['id', 'isTaxDataChecked', 'hasToInvoice', 'payMethodFk'],
|
fields: ['id', 'isTaxDataChecked', 'hasToInvoice', 'payMethodFk', 'isActive'],
|
||||||
include:
|
include:
|
||||||
{
|
{
|
||||||
relation: 'payMethod',
|
relation: 'payMethod',
|
||||||
|
@ -53,9 +53,6 @@ module.exports = function(Self) {
|
||||||
if (client.payMethod().code === 'wireTransfer' && !company.supplierAccountFk)
|
if (client.payMethod().code === 'wireTransfer' && !company.supplierAccountFk)
|
||||||
throw new UserError('The company has not informed the supplier account for bank transfers');
|
throw new UserError('The company has not informed the supplier account for bank transfers');
|
||||||
|
|
||||||
if (client.isTaxDataChecked && client.hasToInvoice)
|
return client.isTaxDataChecked && client.hasToInvoice && client.isActive;
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,6 +8,8 @@ describe('client canBeInvoiced()', () => {
|
||||||
const activeCtx = {
|
const activeCtx = {
|
||||||
accessToken: {userId: userId}
|
accessToken: {userId: userId}
|
||||||
};
|
};
|
||||||
|
let tx;
|
||||||
|
let options;
|
||||||
|
|
||||||
beforeAll(async() => {
|
beforeAll(async() => {
|
||||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
||||||
|
@ -15,60 +17,45 @@ describe('client canBeInvoiced()', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
beforeEach(async() => {
|
||||||
|
tx = await models.Client.beginTransaction({});
|
||||||
|
options = {transaction: tx};
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async() => {
|
||||||
|
await tx.rollback();
|
||||||
|
});
|
||||||
|
|
||||||
it('should return falsy for a client without the data checked', async() => {
|
it('should return falsy for a client without the data checked', async() => {
|
||||||
const tx = await models.Client.beginTransaction({});
|
const client = await models.Client.findById(clientId, null, options);
|
||||||
|
await client.updateAttribute('isTaxDataChecked', false, options);
|
||||||
|
|
||||||
try {
|
const canBeInvoiced = await models.Client.canBeInvoiced(clientId, companyId, options);
|
||||||
const options = {transaction: tx};
|
|
||||||
|
|
||||||
const client = await models.Client.findById(clientId, null, options);
|
expect(canBeInvoiced).toEqual(false);
|
||||||
await client.updateAttribute('isTaxDataChecked', false, options);
|
});
|
||||||
|
|
||||||
const canBeInvoiced = await models.Client.canBeInvoiced(clientId, companyId, options);
|
it('should return falsy for a client not active', async() => {
|
||||||
|
const client = await models.Client.findById(clientId, null, options);
|
||||||
|
await client.updateAttribute('isActive', false, options);
|
||||||
|
|
||||||
expect(canBeInvoiced).toEqual(false);
|
const canBeInvoiced = await models.Client.canBeInvoiced(clientId, companyId, options);
|
||||||
|
|
||||||
await tx.rollback();
|
expect(canBeInvoiced).toEqual(false);
|
||||||
} catch (e) {
|
|
||||||
await tx.rollback();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return falsy for a client with invoicing disabled', async() => {
|
it('should return falsy for a client with invoicing disabled', async() => {
|
||||||
const tx = await models.Client.beginTransaction({});
|
const client = await models.Client.findById(clientId, null, options);
|
||||||
|
await client.updateAttribute('hasToInvoice', false, options);
|
||||||
|
|
||||||
try {
|
const canBeInvoiced = await models.Client.canBeInvoiced(clientId, companyId, options);
|
||||||
const options = {transaction: tx};
|
|
||||||
|
|
||||||
const client = await models.Client.findById(clientId, null, options);
|
expect(canBeInvoiced).toEqual(false);
|
||||||
await client.updateAttribute('hasToInvoice', false, options);
|
|
||||||
|
|
||||||
const canBeInvoiced = await models.Client.canBeInvoiced(clientId, companyId, options);
|
|
||||||
|
|
||||||
expect(canBeInvoiced).toEqual(false);
|
|
||||||
|
|
||||||
await tx.rollback();
|
|
||||||
} catch (e) {
|
|
||||||
await tx.rollback();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return truthy for an invoiceable client', async() => {
|
it('should return truthy for an invoiceable client', async() => {
|
||||||
const tx = await models.Client.beginTransaction({});
|
const canBeInvoiced = await models.Client.canBeInvoiced(clientId, companyId, options);
|
||||||
|
|
||||||
try {
|
expect(canBeInvoiced).toEqual(true);
|
||||||
const options = {transaction: tx};
|
|
||||||
|
|
||||||
const canBeInvoiced = await models.Client.canBeInvoiced(clientId, companyId, options);
|
|
||||||
|
|
||||||
expect(canBeInvoiced).toEqual(true);
|
|
||||||
|
|
||||||
await tx.rollback();
|
|
||||||
} catch (e) {
|
|
||||||
await tx.rollback();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue