fix: refs #7184 fixed filter and test
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Jon Elias 2025-01-22 17:09:01 +01:00
parent 0eff155daf
commit 6d5a329b49
2 changed files with 72 additions and 23 deletions

View File

@ -90,17 +90,21 @@ module.exports = Self => {
}
});
Self.filter = async(ctx, filter) => {
let conn = Self.dataSource.connector;
const models = Self.app.models;
Self.filter = async(ctx, filter, options) => {
const userId = ctx.req.accessToken.userId;
const conn = Self.dataSource.connector;
const models = Self.app.models;
const args = ctx.args;
const myTeamIds = [];
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (args.myTeam != null)
myTeamIds.value = await models.Worker.myTeam(userId);
let where = buildFilter(ctx.args, (param, value) => {
const where = buildFilter(ctx.args, (param, value) => {
switch (param) {
case 'search':
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;
stmt = new ParameterizedSQL(
@ -162,11 +166,12 @@ module.exports = Self => {
LEFT JOIN account.emailUser eu ON eu.userFk = u.id`
);
stmt.merge(conn.makeSuffix(filter));
let itemsIndex = stmts.push(stmt) - 1;
stmt.merge(conn.makeWhere(filter.where));
stmts.push(stmt);
let sql = ParameterizedSQL.join(stmts, ';');
let result = await conn.executeStmt(sql);
return itemsIndex === 0 ? result : result[itemsIndex];
const itemsIndex = stmts.push(stmt) - 1;
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql, myOptions);
return result[itemsIndex];
};
};

View File

@ -1,25 +1,69 @@
const models = require('vn-loopback/server/server').models;
const app = require('vn-loopback/server/server');
describe('worker filter()', () => {
it('should return 1 result filtering by id', async() => {
let result = await app.models.Worker.filter({args: {filter: {}, search: 1}});
const ctx = beforeAll.getCtx();
expect(result.length).toEqual(1);
expect(result[0].id).toEqual(1);
it('should return 1 result filtering by id', async() => {
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() => {
let result = await app.models.Worker.filter({args: {filter: {}, search: 'administrativeNick'}});
const tx = await models.Worker.beginTransaction({});
expect(result.length).toEqual(1);
expect(result[0].id).toEqual(5);
try {
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() => {
let result = await app.models.Worker.filter({args: {filter: {}, firstName: 'agency'}});
it('should return 2 result filtering by name', async() => {
const tx = await models.Worker.beginTransaction({});
expect(result.length).toEqual(2);
expect(result[0].nickname).toEqual('agencyNick');
expect(result[1].nickname).toEqual('agencyBossNick');
try {
const options = {transaction: tx};
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;
}
});
});