This commit is contained in:
parent
0eff155daf
commit
6d5a329b49
|
@ -90,17 +90,21 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.filter = async(ctx, filter) => {
|
Self.filter = async(ctx, filter, options) => {
|
||||||
let conn = Self.dataSource.connector;
|
|
||||||
const models = Self.app.models;
|
|
||||||
const userId = ctx.req.accessToken.userId;
|
const userId = ctx.req.accessToken.userId;
|
||||||
|
const conn = Self.dataSource.connector;
|
||||||
|
const models = Self.app.models;
|
||||||
const args = ctx.args;
|
const args = ctx.args;
|
||||||
const myTeamIds = [];
|
const myTeamIds = [];
|
||||||
|
const myOptions = {};
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
if (args.myTeam != null)
|
if (args.myTeam != null)
|
||||||
myTeamIds.value = await models.Worker.myTeam(userId);
|
myTeamIds.value = await models.Worker.myTeam(userId);
|
||||||
|
|
||||||
let where = buildFilter(ctx.args, (param, value) => {
|
const where = buildFilter(ctx.args, (param, value) => {
|
||||||
switch (param) {
|
switch (param) {
|
||||||
case 'search':
|
case 'search':
|
||||||
return /^\d+$/.test(value)
|
return /^\d+$/.test(value)
|
||||||
|
@ -137,9 +141,9 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
filter = mergeFilters(ctx.args.filter, {where});
|
filter = mergeFilters(filter, {where});
|
||||||
|
|
||||||
let stmts = [];
|
const stmts = [];
|
||||||
let stmt;
|
let stmt;
|
||||||
|
|
||||||
stmt = new ParameterizedSQL(
|
stmt = new ParameterizedSQL(
|
||||||
|
@ -162,11 +166,12 @@ module.exports = Self => {
|
||||||
LEFT JOIN account.emailUser eu ON eu.userFk = u.id`
|
LEFT JOIN account.emailUser eu ON eu.userFk = u.id`
|
||||||
);
|
);
|
||||||
|
|
||||||
stmt.merge(conn.makeSuffix(filter));
|
stmt.merge(conn.makeWhere(filter.where));
|
||||||
let itemsIndex = stmts.push(stmt) - 1;
|
stmts.push(stmt);
|
||||||
|
|
||||||
let sql = ParameterizedSQL.join(stmts, ';');
|
const itemsIndex = stmts.push(stmt) - 1;
|
||||||
let result = await conn.executeStmt(sql);
|
const sql = ParameterizedSQL.join(stmts, ';');
|
||||||
return itemsIndex === 0 ? result : result[itemsIndex];
|
const result = await conn.executeStmt(sql, myOptions);
|
||||||
|
return result[itemsIndex];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,25 +1,69 @@
|
||||||
|
const models = require('vn-loopback/server/server').models;
|
||||||
const app = require('vn-loopback/server/server');
|
const app = require('vn-loopback/server/server');
|
||||||
|
|
||||||
describe('worker filter()', () => {
|
describe('worker filter()', () => {
|
||||||
it('should return 1 result filtering by id', async() => {
|
const ctx = beforeAll.getCtx();
|
||||||
let result = await app.models.Worker.filter({args: {filter: {}, search: 1}});
|
|
||||||
|
|
||||||
expect(result.length).toEqual(1);
|
it('should return 1 result filtering by id', async() => {
|
||||||
expect(result[0].id).toEqual(1);
|
const tx = await models.Worker.beginTransaction({});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
const filter = {};
|
||||||
|
const args = {search: 1};
|
||||||
|
ctx.args = args;
|
||||||
|
|
||||||
|
let result = await app.models.Worker.filter(ctx, filter, options);
|
||||||
|
|
||||||
|
expect(result.length).toEqual(1);
|
||||||
|
expect(result[0].id).toEqual(1);
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return 1 result filtering by string', async() => {
|
it('should return 1 result filtering by string', async() => {
|
||||||
let result = await app.models.Worker.filter({args: {filter: {}, search: 'administrativeNick'}});
|
const tx = await models.Worker.beginTransaction({});
|
||||||
|
|
||||||
expect(result.length).toEqual(1);
|
try {
|
||||||
expect(result[0].id).toEqual(5);
|
const options = {transaction: tx};
|
||||||
|
const filter = {};
|
||||||
|
const args = {search: 'administrativeNick'};
|
||||||
|
ctx.args = args;
|
||||||
|
|
||||||
|
let result = await app.models.Worker.filter(ctx, filter, options);
|
||||||
|
|
||||||
|
expect(result.length).toEqual(1);
|
||||||
|
expect(result[0].id).toEqual(5);
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return 2 results filtering by name', async() => {
|
it('should return 2 result filtering by name', async() => {
|
||||||
let result = await app.models.Worker.filter({args: {filter: {}, firstName: 'agency'}});
|
const tx = await models.Worker.beginTransaction({});
|
||||||
|
|
||||||
expect(result.length).toEqual(2);
|
try {
|
||||||
expect(result[0].nickname).toEqual('agencyNick');
|
const options = {transaction: tx};
|
||||||
expect(result[1].nickname).toEqual('agencyBossNick');
|
const filter = {};
|
||||||
|
const args = {firstName: 'agency'};
|
||||||
|
ctx.args = args;
|
||||||
|
|
||||||
|
let result = await app.models.Worker.filter(ctx, filter, options);
|
||||||
|
|
||||||
|
expect(result[0].nickname).toEqual('agencyNick');
|
||||||
|
expect(result[1].nickname).toEqual('agencyBossNick');
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue