43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
|
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethod('freeAgencies', {
|
|
description: 'Returns a list of agencies without a supplier assigned',
|
|
accepts: [{
|
|
arg: 'filter',
|
|
type: 'object',
|
|
description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string`
|
|
}],
|
|
returns: {
|
|
type: ['object'],
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/freeAgencies`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.freeAgencies = async(filter, options) => {
|
|
const conn = Self.dataSource.connector;
|
|
const where = {'sat.supplierFk': null};
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
filter = mergeFilters(filter, {where});
|
|
|
|
let stmt = new ParameterizedSQL(
|
|
`SELECT a.name, a.id
|
|
FROM agency a
|
|
LEFT JOIN supplierAgencyTerm sat ON sat.agencyFk = a.id`,
|
|
null, myOptions);
|
|
|
|
stmt.merge(conn.makeSuffix(filter));
|
|
|
|
return conn.executeStmt(stmt);
|
|
};
|
|
};
|