feat: refs #7119 add VehicleState model with data source configuration #3328

Open
jorgep wants to merge 36 commits from 7119-createVehicle into dev
7 changed files with 213 additions and 28 deletions
Showing only changes of commit 2346205072 - Show all commits

View File

@ -0,0 +1,10 @@
UPDATE vn.town t
LEFT JOIN vn.zoneGeo zg ON zg.id = t.geoFk
SET t.geoFk = NULL
WHERE zg.id IS NULL;
ALTER TABLE vn.town
ADD CONSTRAINT town_zoneGeo_FK FOREIGN KEY (geoFk)
REFERENCES vn.zoneGeo(id)
ON DELETE RESTRICT
ON UPDATE CASCADE;

View File

@ -0,0 +1,10 @@
UPDATE vn.postCode pc
LEFT JOIN vn.zoneGeo zg ON zg.id = pc.geoFk
SET pc.geoFk = NULL
WHERE zg.id IS NULL;
ALTER TABLE vn.postCode
ADD CONSTRAINT postCode_zoneGeo_FK FOREIGN KEY (geoFk)
REFERENCES vn.zoneGeo(id)
ON DELETE RESTRICT
ON UPDATE CASCADE;

View File

@ -0,0 +1,10 @@
UPDATE vn.province p
LEFT JOIN vn.zoneGeo zg ON zg.id = p.geoFk
SET p.geoFk = NULL
WHERE zg.id IS NULL;
ALTER TABLE vn.province
ADD CONSTRAINT province_zoneGeo_FK FOREIGN KEY (geoFk)
REFERENCES vn.zoneGeo(id)
ON DELETE RESTRICT
ON UPDATE CASCADE;

View File

@ -21,7 +21,7 @@ module.exports = Self => {
} }
], ],
returns: { returns: {
type: ['object'], type: 'object',
root: true root: true
}, },
http: { http: {
@ -41,23 +41,28 @@ module.exports = Self => {
switch (param) { switch (param) {
case 'search': case 'search':
return {or: [ return {or: [
{'d.clientFk': value}, {'c.id': value},
{'d.clientName': {like: `%${value}%`}} {'c.name': {like: `%${value}%`}}
]}; ]};
} }
}); });
filter = mergeFilters(ctx.args.filter, {where}); const date = Date.vnNew();
date.setHours(0, 0, 0, 0);
filter = mergeFilters({where: {'d.created': date, 'd.amount': {gt: 0}}}, ctx.args.filter);
filter = mergeFilters(filter, {where});
const stmts = []; const stmts = [];
const date = Date.vnNew(); let stmt = new ParameterizedSQL(
date.setHours(0, 0, 0, 0); `CREATE OR REPLACE TEMPORARY TABLE tmp.defaulters
const stmt = new ParameterizedSQL( WITH clientObservations AS
`SELECT * (SELECT clientFk,text, created, workerFk
FROM ( FROM vn.clientObservation
SELECT GROUP BY clientFk
DISTINCT c.id clientFk, ORDER BY created DESC
)SELECT c.id clientFk,
c.name clientName, c.name clientName,
c.salesPersonFk, c.salesPersonFk,
c.businessTypeFk = 'worker' isWorker, c.businessTypeFk = 'worker' isWorker,
@ -80,36 +85,43 @@ module.exports = Self => {
JOIN client c ON c.id = d.clientFk JOIN client c ON c.id = d.clientFk
JOIN country cn ON cn.id = c.countryFk JOIN country cn ON cn.id = c.countryFk
JOIN payMethod pm ON pm.id = c.payMethodFk JOIN payMethod pm ON pm.id = c.payMethodFk
LEFT JOIN clientObservation co ON co.clientFk = c.id LEFT JOIN clientObservations co ON co.clientFk = c.id
LEFT JOIN account.user u ON u.id = c.salesPersonFk LEFT JOIN account.user u ON u.id = c.salesPersonFk
LEFT JOIN account.user uw ON uw.id = co.workerFk LEFT JOIN account.user uw ON uw.id = co.workerFk
LEFT JOIN ( LEFT JOIN (
SELECT r1.started, r1.clientFk, r1.finished SELECT r1.started, r1.clientFk, r1.finished
FROM recovery r1 FROM recovery r1
JOIN ( JOIN (
SELECT MAX(started) AS maxStarted, clientFk SELECT MAX(started) maxStarted, clientFk
FROM recovery FROM recovery
GROUP BY clientFk GROUP BY clientFk
) r2 ON r1.clientFk = r2.clientFk ) r2 ON r1.clientFk = r2.clientFk
AND r1.started = r2.maxStarted AND r1.started = r2.maxStarted
WHERE r1.finished
GROUP BY r1.clientFk
) r ON r.clientFk = c.id ) r ON r.clientFk = c.id
LEFT JOIN workerDepartment wd ON wd.workerFk = u.id LEFT JOIN workerDepartment wd ON wd.workerFk = u.id
LEFT JOIN department dp ON dp.id = wd.departmentFk LEFT JOIN department dp ON dp.id = wd.departmentFk`);
WHERE
d.created = ?
AND d.amount > 0
ORDER BY co.created DESC) d`
, [date]);
stmt.merge(conn.makeWhere(filter.where)); stmt.merge(conn.makeWhere(filter.where));
stmt.merge(`GROUP BY d.clientFk`); stmts.push(stmt);
stmt = new ParameterizedSQL(`
SELECT SUM(amount) amount
FROM tmp.defaulters
`);
stmts.push(stmt);
stmt = new ParameterizedSQL(`
SELECT *
FROM tmp.defaulters
`);
stmt.merge(conn.makeOrderBy(filter.order)); stmt.merge(conn.makeOrderBy(filter.order));
stmt.merge(conn.makeLimit(filter));
const itemsIndex = stmts.push(stmt) - 1; const itemsIndex = stmts.push(stmt) - 1;
const sql = ParameterizedSQL.join(stmts, ';'); const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql, myOptions); const result = await conn.executeStmt(sql, myOptions);
return itemsIndex === 0 ? result : result[itemsIndex]; return {defaulters: result[itemsIndex], amount: result[itemsIndex - 1][0].amount};
}; };
}; };

View File

@ -11,10 +11,10 @@ describe('defaulter filter()', () => {
const ctx = {req: {accessToken: {userId: authUserId}}, args: {filter: filter}}; const ctx = {req: {accessToken: {userId: authUserId}}, args: {filter: filter}};
const result = await models.Defaulter.filter(ctx, null, options); const result = await models.Defaulter.filter(ctx, null, options);
const firstRow = result[0]; const firstRow = result.defaulters[0];
expect(firstRow.clientFk).toEqual(1101); expect(firstRow.clientFk).toEqual(1101);
expect(result.length).toEqual(5); expect(result.defaulters.length).toEqual(5);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
@ -31,7 +31,7 @@ describe('defaulter filter()', () => {
const ctx = {req: {accessToken: {userId: authUserId}}, args: {search: 1101}}; const ctx = {req: {accessToken: {userId: authUserId}}, args: {search: 1101}};
const result = await models.Defaulter.filter(ctx, null, options); const result = await models.Defaulter.filter(ctx, null, options);
const firstRow = result[0]; const firstRow = result.defaulters[0];
expect(firstRow.clientFk).toEqual(1101); expect(firstRow.clientFk).toEqual(1101);
@ -50,7 +50,7 @@ describe('defaulter filter()', () => {
const ctx = {req: {accessToken: {userId: authUserId}}, args: {search: 'Petter Parker'}}; const ctx = {req: {accessToken: {userId: authUserId}}, args: {search: 'Petter Parker'}};
const result = await models.Defaulter.filter(ctx, null, options); const result = await models.Defaulter.filter(ctx, null, options);
const firstRow = result[0]; const firstRow = result.defaulters[0];
expect(firstRow.clientName).toEqual('Petter Parker'); expect(firstRow.clientName).toEqual('Petter Parker');
@ -60,4 +60,23 @@ describe('defaulter filter()', () => {
throw e; throw e;
} }
}); });
it('should return the defaulter the sum of every defaulters', async() => {
const tx = await models.Defaulter.beginTransaction({});
try {
const options = {transaction: tx};
const ctx = {req: {accessToken: {userId: authUserId}}, args: {search: 1101}};
const {defaulters, amount} = await models.Defaulter.filter(ctx, null, options);
const total = defaulters.reduce((total, row) => total + row.amount, 0);
expect(total).toEqual(amount);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
}); });

View File

@ -68,7 +68,9 @@ module.exports = Self => {
const where = buildFilter(ctx.args, (param, value) => { const where = buildFilter(ctx.args, (param, value) => {
switch (param) { switch (param) {
case 'search': case 'search':
return {or: [{'v.id': value}, {numberPlate: {like: `%${value}%`}}]}; return /^\d+$/.test(value)
? {'v.id': value}
: {numberPlate: {like: `%${value}%`}};
case 'id': case 'id':
return {'v.id': value}; return {'v.id': value};
case 'description': case 'description':

View File

@ -0,0 +1,122 @@
const {models} = require('vn-loopback/server/server');
fdescribe('Vehicle filter()', () => {
const deliveryAssiId = 123;
const ctx = beforeAll.getCtx(deliveryAssiId);
let options;
let tx;
beforeEach(async() => {
ctx.args = {};
options = {};
tx = await models.Sale.beginTransaction({});
options.transaction = tx;
});
afterEach(async() => {
await tx.rollback();
});
it('should return the vehicles matching "search"', async() => {
const {id} = await models.Vehicle.findById(1, null, options);
const {numberPlate} = await models.Vehicle.findById(2, null, options);
ctx.args = {search: id};
const [searchResult] = await models.Vehicle.filter(ctx);
ctx.args = {search: numberPlate};
const [searchResult2] = await models.Vehicle.filter(ctx);
expect(searchResult.id).toEqual(id);
expect(searchResult2.numberPlate).toEqual(numberPlate);
});
it('should return the vehicles matching "company"', async() => {
const company = await models.Company.findOne({where: {code: 'VNL'}}, options);
ctx.args = {companyFk: company.id};
const searchResult = await models.Vehicle.filter(ctx, null, options);
searchResult.forEach(record => {
expect(record.company).toEqual(company.code);
});
});
it('should return the vehicles matching "tradeMark"', async() => {
const tradeMark = 'WAYNE INDUSTRIES';
ctx.args = {tradeMark};
const searchResult = await models.Vehicle.filter(ctx);
searchResult.forEach(record => {
expect(record.tradeMark).toEqual(tradeMark);
});
});
it('should return the vehicles matching "numberPlate"', async() => {
const {numberPlate} = await models.Vehicle.findById(1, null, options);
ctx.args = {numberPlate};
const searchResult = await models.Vehicle.filter(ctx);
searchResult.forEach(record => {
expect(record.numberPlate).toEqual(numberPlate);
});
});
it('should return the vehicles matching "warehouseFk"', async() => {
const warehouse = await models.Warehouse.findById(1, null, options);
ctx.args = {warehouseFk: warehouse.id};
const searchResult = await models.Vehicle.filter(ctx);
searchResult.forEach(record => {
expect(record.warehouseFk).toEqual(warehouse.id);
});
});
// it('should return the vehicles matching "chassis"', async() => {
// const ctx = {
// args: {
// chassis: 'CH12345',
// }
// };
// const result = await models.Vehicle.filter(ctx);
// expect(result.length).toBeGreaterThan(0);
// expect(result[0].chassis).toEqual('CH12345');
// });
// it('should return the vehicles matching "leasing"', async() => {
// const ctx = {
// args: {
// leasing: 'LeasingCompany',
// }
// };
// const result = await models.Vehicle.filter(ctx);
// expect(result.length).toBeGreaterThan(0);
// expect(result[0].leasing).toEqual('LeasingCompany');
// });
// it('should return the vehicles matching "countryCodeFk"', async() => {
// const ctx = {
// args: {
// countryCodeFk: 'US',
// }
// };
// const result = await models.Vehicle.filter(ctx);
// expect(result.length).toBeGreaterThan(0);
// expect(result[0].countryCodeFk).toEqual('US');
// });
// it('should return the vehicles matching "vehicleTypeFk"', async() => {
// const ctx = {
// args: {
// vehicleTypeFk: 3,
// }
// };
// const result = await models.Vehicle.filter(ctx);
// expect(result.length).toBeGreaterThan(0);
// expect(result[0].vehicleTypeFk).toEqual(3);
// });
});