small refactors correcting constants and adding transactions

This commit is contained in:
Carlos Jimenez Ruiz 2021-07-12 12:50:15 +02:00
parent 3b2b4e3819
commit dd3473b93d
16 changed files with 90 additions and 75 deletions

View File

@ -12,18 +12,22 @@ module.exports = function(Self) {
}
});
Self.getUserConfig = async ctx => {
Self.getUserConfig = async(ctx, options) => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
let userConfig = await models.UserConfig.findOne({
where: {userFk: ctx.req.accessToken.userId}
});
}, myOptions);
const companyFilter = {where: {code: 'VNL'}};
const company = await models.Company.findOne(companyFilter);
const company = await models.Company.findOne(companyFilter, myOptions);
const warehouseFilter = {where: {code: 'ALG'}};
const warehouse = await models.Warehouse.findOne(warehouseFilter);
const warehouse = await models.Warehouse.findOne(warehouseFilter, myOptions);
if (!userConfig) {
let newConfig = {
@ -32,7 +36,7 @@ module.exports = function(Self) {
userFk: ctx.req.accessToken.userId
};
userConfig = await models.UserConfig.create(newConfig);
userConfig = await models.UserConfig.create(newConfig, myOptions);
}
return userConfig;
};

View File

@ -1,10 +1,21 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('userConfig getUserConfig()', () => {
it(`should return the configuration data of a given user`, async() => {
const result = await app.models.UserConfig.getUserConfig({req: {accessToken: {userId: 9}}});
const tx = await models.Item.beginTransaction({});
const options = {transaction: tx};
expect(result.warehouseFk).toEqual(1);
expect(result.companyFk).toEqual(442);
try {
const ctx = {req: {accessToken: {userId: 9}}};
const result = await models.UserConfig.getUserConfig(ctx, options);
expect(result.warehouseFk).toEqual(1);
expect(result.companyFk).toEqual(442);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -133,7 +133,7 @@ module.exports = Self => {
};
promises.push(Self.app.models.ClaimEnd.find(filter, myOptions));
const res = await Promise.all(promises, myOptions);
const res = await Promise.all(promises);
[summary.claim] = res[0];
summary.salesClaimed = res[1];

View File

@ -79,7 +79,7 @@ module.exports = Self => {
promises.push(newClientDms);
});
const resolvedPromises = await Promise.all(promises, myOptions);
const resolvedPromises = await Promise.all(promises);
if (tx) await tx.commit();

View File

@ -66,7 +66,7 @@ module.exports = Self => {
const model = models[modelName];
try {
let promises = [];
const promises = [];
const targets = lines.map(line => {
return line[identifier];
@ -78,7 +78,7 @@ module.exports = Self => {
for (let target of targets)
promises.push(model.upsertWithWhere({id: target}, value, myOptions));
const result = await Promise.all(promises, myOptions);
const result = await Promise.all(promises);
if (tx) await tx.commit();

View File

@ -1,8 +1,8 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('InvoiceIn filter()', () => {
it('should return the invoice in matching supplier name', async() => {
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.InvoiceIn.beginTransaction({});
const options = {transaction: tx};
try {
@ -12,7 +12,7 @@ describe('InvoiceIn filter()', () => {
}
};
const result = await app.models.InvoiceIn.filter(ctx, {}, options);
const result = await models.InvoiceIn.filter(ctx, {}, options);
expect(result.length).toEqual(5);
expect(result[0].supplierName).toEqual('Plants SL');
@ -25,7 +25,7 @@ describe('InvoiceIn filter()', () => {
});
it('should return the invoice in matching supplier reference', async() => {
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.InvoiceIn.beginTransaction({});
const options = {transaction: tx};
try {
@ -35,7 +35,7 @@ describe('InvoiceIn filter()', () => {
}
};
const result = await app.models.InvoiceIn.filter(ctx, {}, options);
const result = await models.InvoiceIn.filter(ctx, {}, options);
expect(result.length).toEqual(1);
expect(result[0].supplierRef).toEqual('1241');
@ -48,7 +48,7 @@ describe('InvoiceIn filter()', () => {
});
it('should return the invoice in matching the serial number', async() => {
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.InvoiceIn.beginTransaction({});
const options = {transaction: tx};
try {
@ -58,7 +58,7 @@ describe('InvoiceIn filter()', () => {
}
};
const result = await app.models.InvoiceIn.filter(ctx, {}, options);
const result = await models.InvoiceIn.filter(ctx, {}, options);
expect(result.length).toEqual(1);
expect(result[0].serialNumber).toEqual(1002);
@ -71,7 +71,7 @@ describe('InvoiceIn filter()', () => {
});
it('should return the invoice in matching the account', async() => {
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.InvoiceIn.beginTransaction({});
const options = {transaction: tx};
try {
@ -81,7 +81,7 @@ describe('InvoiceIn filter()', () => {
}
};
const result = await app.models.InvoiceIn.filter(ctx, {}, options);
const result = await models.InvoiceIn.filter(ctx, {}, options);
expect(result.length).toEqual(5);
expect(result[0].account).toEqual('4000020002');
@ -94,7 +94,7 @@ describe('InvoiceIn filter()', () => {
});
it('should return the invoice in matching the awb code', async() => {
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.InvoiceIn.beginTransaction({});
const options = {transaction: tx};
try {
@ -104,7 +104,7 @@ describe('InvoiceIn filter()', () => {
}
};
const result = await app.models.InvoiceIn.filter(ctx, {}, options);
const result = await models.InvoiceIn.filter(ctx, {}, options);
const firstRow = result[0];
expect(result.length).toEqual(1);
@ -119,7 +119,7 @@ describe('InvoiceIn filter()', () => {
});
it('should return the invoice in matching the amount', async() => {
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.InvoiceIn.beginTransaction({});
const options = {transaction: tx};
try {
@ -129,7 +129,7 @@ describe('InvoiceIn filter()', () => {
}
};
const result = await app.models.InvoiceIn.filter(ctx, {}, options);
const result = await models.InvoiceIn.filter(ctx, {}, options);
expect(result.length).toEqual(1);
expect(result[0].amount).toEqual(64.23);
@ -142,7 +142,7 @@ describe('InvoiceIn filter()', () => {
});
it('should return the invoice in matching "from" and "to"', async() => {
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.InvoiceIn.beginTransaction({});
const options = {transaction: tx};
try {
@ -155,7 +155,7 @@ describe('InvoiceIn filter()', () => {
args: {from, to}
};
const result = await app.models.InvoiceIn.filter(ctx, {}, options);
const result = await models.InvoiceIn.filter(ctx, {}, options);
expect(result.length).toEqual(6);
@ -167,7 +167,7 @@ describe('InvoiceIn filter()', () => {
});
it('should return the booked invoice in', async() => {
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.InvoiceIn.beginTransaction({});
const options = {transaction: tx};
try {
@ -177,7 +177,7 @@ describe('InvoiceIn filter()', () => {
}
};
const result = await app.models.InvoiceIn.filter(ctx, {}, options);
const result = await models.InvoiceIn.filter(ctx, {}, options);
expect(result.length).toEqual(6);
expect(result[0].isBooked).toBeTruthy();

View File

@ -1,12 +1,12 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('invoiceIn summary()', () => {
it('should return a summary object containing data from one invoiceIn', async() => {
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.InvoiceIn.beginTransaction({});
const options = {transaction: tx};
try {
const summary = await app.models.InvoiceIn.summary(1, options);
const summary = await models.InvoiceIn.summary(1, options);
expect(summary.supplierRef).toEqual('1234');

View File

@ -22,7 +22,7 @@ module.exports = Self => {
Self.delete = async(id, options) => {
let tx;
let myOptions = {};
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
@ -42,7 +42,7 @@ module.exports = Self => {
promises.push(ticket.updateAttribute('refFk', null, myOptions));
});
await Promise.all(promises, myOptions);
await Promise.all(promises);
await invoiceOut.destroy(myOptions);

View File

@ -1,20 +1,20 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('invoiceOut book()', () => {
const invoiceOutId = 5;
it('should update the booked property', async() => {
const tx = await app.models.InvoiceOut.beginTransaction({});
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
const originalInvoiceOut = await app.models.InvoiceOut.findById(invoiceOutId, {}, options);
const originalInvoiceOut = await models.InvoiceOut.findById(invoiceOutId, {}, options);
const bookedDate = originalInvoiceOut.booked;
const invoiceOutRef = originalInvoiceOut.ref;
await app.models.InvoiceOut.book(invoiceOutRef, options);
await models.InvoiceOut.book(invoiceOutRef, options);
const updatedInvoiceOut = await app.models.InvoiceOut.findById(invoiceOutId, {}, options);
const updatedInvoiceOut = await models.InvoiceOut.findById(invoiceOutId, {}, options);
expect(updatedInvoiceOut.booked).not.toEqual(bookedDate);
expect(updatedInvoiceOut.hasPdf).toBeFalsy();

View File

@ -1,4 +1,4 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
const got = require('got');
describe('InvoiceOut createPdf()', () => {
@ -19,7 +19,7 @@ describe('InvoiceOut createPdf()', () => {
};
spyOn(got, 'stream').and.returnValue(response);
let result = await app.models.InvoiceOut.createPdf(ctx, invoiceId);
const result = await models.InvoiceOut.createPdf(ctx, invoiceId);
expect(result.hasPdf).toBe(true);
});

View File

@ -1,4 +1,4 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('invoiceOut delete()', () => {
@ -9,12 +9,12 @@ describe('invoiceOut delete()', () => {
};
it('should check that there is one ticket in the target invoiceOut', async() => {
const tx = await app.models.InvoiceOut.beginTransaction({});
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
const invoiceOut = await app.models.InvoiceOut.findById(invoiceOutId, {}, options);
const tickets = await app.models.Ticket.find({where: {refFk: invoiceOut.ref}}, options);
const invoiceOut = await models.InvoiceOut.findById(invoiceOutId, {}, options);
const tickets = await models.Ticket.find({where: {refFk: invoiceOut.ref}}, options);
expect(tickets.length).toEqual(1);
expect(tickets[0].id).toEqual(3);
@ -27,7 +27,7 @@ describe('invoiceOut delete()', () => {
});
it(`should delete the target invoiceOut then check the ticket doesn't have a refFk anymore`, async() => {
const tx = await app.models.InvoiceOut.beginTransaction({});
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
@ -35,11 +35,11 @@ describe('invoiceOut delete()', () => {
active: activeCtx
});
await app.models.InvoiceOut.delete(invoiceOutId, options);
await models.InvoiceOut.delete(invoiceOutId, options);
const originalTicket = await app.models.Ticket.findById(3, {}, options);
const originalTicket = await models.Ticket.findById(3, {}, options);
const deletedInvoiceOut = await app.models.InvoiceOut.findById(invoiceOutId, {}, options);
const deletedInvoiceOut = await models.InvoiceOut.findById(invoiceOutId, {}, options);
expect(deletedInvoiceOut).toBeNull();
expect(originalTicket.refFk).toBeNull();

View File

@ -1,8 +1,8 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('InvoiceOut download()', () => {
it('should return the downloaded fine name', async() => {
let result = await app.models.InvoiceOut.download(1);
const result = await models.InvoiceOut.download(1);
expect(result[1]).toEqual('text/plain');
expect(result[2]).toEqual('filename="README.md"');

View File

@ -1,11 +1,11 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('InvoiceOut filter()', () => {
let today = new Date();
today.setHours(2, 0, 0, 0);
it('should return the invoice out matching ref', async() => {
const tx = await app.models.InvoiceOut.beginTransaction({});
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
@ -15,7 +15,7 @@ describe('InvoiceOut filter()', () => {
}
};
const result = await app.models.InvoiceOut.filter(ctx, {}, options);
const result = await models.InvoiceOut.filter(ctx, {}, options);
expect(result.length).toEqual(1);
expect(result[0].ref).toEqual('T4444444');
@ -28,7 +28,7 @@ describe('InvoiceOut filter()', () => {
});
it('should return the invoice out matching clientFk', async() => {
const tx = await app.models.InvoiceOut.beginTransaction({});
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
@ -38,7 +38,7 @@ describe('InvoiceOut filter()', () => {
}
};
const result = await app.models.InvoiceOut.filter(ctx, {}, options);
const result = await models.InvoiceOut.filter(ctx, {}, options);
expect(result.length).toEqual(1);
expect(result[0].ref).toEqual('T2222222');
@ -51,7 +51,7 @@ describe('InvoiceOut filter()', () => {
});
it('should return the invoice out matching hasPdf', async() => {
const tx = await app.models.InvoiceOut.beginTransaction({});
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
@ -61,7 +61,7 @@ describe('InvoiceOut filter()', () => {
}
};
const result = await app.models.InvoiceOut.filter(ctx, {}, options);
const result = await models.InvoiceOut.filter(ctx, {}, options);
expect(result.length).toEqual(5);
@ -73,7 +73,7 @@ describe('InvoiceOut filter()', () => {
});
it('should return the invoice out matching amount', async() => {
const tx = await app.models.InvoiceOut.beginTransaction({});
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
@ -83,7 +83,7 @@ describe('InvoiceOut filter()', () => {
}
};
const result = await app.models.InvoiceOut.filter(ctx, {}, options);
const result = await models.InvoiceOut.filter(ctx, {}, options);
expect(result.length).toEqual(1);
expect(result[0].ref).toEqual('T2222222');
@ -96,7 +96,7 @@ describe('InvoiceOut filter()', () => {
});
it('should return the invoice out matching min and max', async() => {
const tx = await app.models.InvoiceOut.beginTransaction({});
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
@ -107,7 +107,7 @@ describe('InvoiceOut filter()', () => {
}
};
let result = await app.models.InvoiceOut.filter(ctx, {}, options);
let result = await models.InvoiceOut.filter(ctx, {}, options);
expect(result.length).toEqual(3);

View File

@ -1,13 +1,13 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('entry getTickets()', () => {
const invoiceOutId = 4;
it('should get the ticket of an invoiceOut', async() => {
const tx = await app.models.InvoiceOut.beginTransaction({});
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
const result = await app.models.InvoiceOut.getTickets(invoiceOutId, {}, options);
const result = await models.InvoiceOut.getTickets(invoiceOutId, {}, options);
expect(result.length).toEqual(1);

View File

@ -1,12 +1,12 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('invoiceOut summary()', () => {
it('should return a summary object containing data from one invoiceOut', async() => {
const tx = await app.models.InvoiceOut.beginTransaction({});
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
const result = await app.models.InvoiceOut.summary(1, options);
const result = await models.InvoiceOut.summary(1, options);
expect(result.invoiceOut.id).toEqual(1);
@ -18,11 +18,11 @@ describe('invoiceOut summary()', () => {
});
it(`should return a summary object containing it's supplier country`, async() => {
const tx = await app.models.InvoiceOut.beginTransaction({});
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
const summary = await app.models.InvoiceOut.summary(1, options);
const summary = await models.InvoiceOut.summary(1, options);
const supplier = summary.invoiceOut.supplier();
expect(summary.invoiceOut.ref).toEqual('T1111111');
@ -37,11 +37,11 @@ describe('invoiceOut summary()', () => {
});
it(`should return a summary object containing idata from it's tax types`, async() => {
const tx = await app.models.InvoiceOut.beginTransaction({});
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
const summary = await app.models.InvoiceOut.summary(1, options);
const summary = await models.InvoiceOut.summary(1, options);
expect(summary.invoiceOut.ref).toEqual('T1111111');
expect(summary.invoiceOut.taxesBreakdown.length).toEqual(2);

View File

@ -26,7 +26,7 @@ module.exports = Self => {
today.setHours(0, 0, 0, 0);
let tx;
let myOptions = {};
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
@ -68,7 +68,7 @@ module.exports = Self => {
}, myOptions));
}
}
await Promise.all(promises, myOptions);
await Promise.all(promises);
await models.Zone.destroyById(id, myOptions);
if (tx) await tx.commit();