Skip to content

Commit 9cf155a

Browse files
[O2B-1107] Filter on environment id (#1326)
* feat: add like filtering for ids * feat: add exact filter for environment ids * fix: fix lint problems * refactor: implement comment on PR * test: add tests for feature * test: add test for filtering in API * test: add more tests for environment API
1 parent face45c commit 9cf155a

4 files changed

Lines changed: 90 additions & 2 deletions

File tree

lib/domain/dtos/GetAllEnvironmentsDto.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@
1414
const Joi = require('joi');
1515
const PaginationDto = require('./PaginationDto');
1616

17+
/**
18+
* Separate filter DTO for get all environments, because EnvironmentsFilterDto
19+
* is used for logs and requires different information
20+
*/
21+
const FilterDto = Joi.object({
22+
ids: Joi.string().trim().optional(),
23+
});
24+
1725
const QueryDto = Joi.object({
26+
filter: FilterDto,
1827
page: PaginationDto,
1928
token: Joi.string(),
2029
});

lib/usecases/environment/GetAllEnvironmentsUseCase.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
} = require('../../database');
2323
const { environmentAdapter } = require('../../database/adapters/index.js');
2424
const { ApiConfig } = require('../../config/index.js');
25+
const { Op } = require('sequelize');
2526

2627
/**
2728
* GetAllEnvironmentsUseCase
@@ -35,11 +36,29 @@ class GetAllEnvironmentsUseCase {
3536
*/
3637
async execute(dto = {}) {
3738
const { query = {} } = dto;
38-
const { page = {} } = query;
39+
const { filter, page = {} } = query;
3940
const { limit = ApiConfig.pagination.limit, offset = 0 } = page;
4041

42+
const queryBuilder = new QueryBuilder();
43+
44+
if (filter) {
45+
const { ids } = filter;
46+
if (ids) {
47+
const filters = ids.split(',').map((id) => id.trim());
48+
49+
// Filter should be like with only one filter
50+
if (filters.length === 1) {
51+
queryBuilder.where('id').substring(filters[0]);
52+
}
53+
54+
// Filters should be exact with more than one filter
55+
if (filters.length > 1) {
56+
queryBuilder.andWhere({ id: { [Op.in]: filters } });
57+
}
58+
}
59+
}
60+
4161
const { count, rows } = await TransactionHelper.provide(async () => {
42-
const queryBuilder = new QueryBuilder();
4362
queryBuilder.orderBy('updatedAt', 'desc');
4463
queryBuilder.limit(limit);
4564
queryBuilder.offset(offset);

test/api/environments.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@ module.exports = () => {
3535
done();
3636
});
3737
});
38+
39+
it('should successfully show all environments without filter', async () => {
40+
const response = await request(server).get('/api/environments');
41+
42+
expect(response.status).to.equal(200);
43+
const environments = response.body.data;
44+
expect(environments).to.lengthOf(9);
45+
// Check if the environment list is valid by checking the first one only
46+
expect(environments[0].id).to.equal('CmCvjNbg');
47+
});
48+
49+
it('should successfully apply filter for environments ids', async () => {
50+
const response = await request(server).get('/api/environments?filter[ids]=8E4aZTjY');
51+
52+
expect(response.status).to.equal(200);
53+
const environments = response.body.data;
54+
expect(environments).to.lengthOf(1);
55+
expect(environments[0].id).to.equal('8E4aZTjY');
56+
});
57+
58+
it('should successfully filter environments on a list of ids', async () => {
59+
const response = await request(server).get('/api/environments?filter[ids]=CmCvjNbg, TDI59So3d, EIDO13i3D');
60+
61+
expect(response.status).to.equal(200);
62+
const environments = response.body.data;
63+
expect(environments).to.lengthOf(3);
64+
expect(environments[0].id).to.equal('CmCvjNbg');
65+
expect(environments[1].id).to.equal('TDI59So3d');
66+
expect(environments[2].id).to.equal('EIDO13i3D');
67+
});
3868
});
3969
describe('POST /api/environments', () => {
4070
it('should return 201 if valid data is provided', (done) => {

test/lib/usecases/environment/GetAllEnvironmentsUseCase.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,34 @@ module.exports = () => {
2828
.execute(getAllEnvsDto);
2929
expect(result.environments).to.be.an('array');
3030
});
31+
32+
it('should successfully filter environments on one id', async () => {
33+
getAllEnvsDto.query = { filter: { ids: 'SomeId' } };
34+
const { environments } = await new GetAllEnvironmentsUseCase().execute(getAllEnvsDto);
35+
36+
expect(environments).to.be.an('array');
37+
expect(environments.length).to.be.equal(1);
38+
expect(environments[0].id).to.be.equal('SomeId');
39+
});
40+
41+
it('should successfully filter environments on a list of ids', async () => {
42+
getAllEnvsDto.query = { filter: { ids: 'SomeId, newId, CmCvjNbg' } };
43+
const { environments } = await new GetAllEnvironmentsUseCase().execute(getAllEnvsDto);
44+
45+
expect(environments).to.be.an('array');
46+
expect(environments.length).to.be.equal(3);
47+
expect(environments[0].id).to.be.equal('SomeId');
48+
expect(environments[1].id).to.be.equal('newId');
49+
expect(environments[2].id).to.be.equal('CmCvjNbg');
50+
});
51+
52+
it('should successfully filter environments on a list of ids with a non existing id', async () => {
53+
getAllEnvsDto.query = { filter: { ids: 'SomeId, nonExistingIdEnv, newId' } };
54+
const { environments } = await new GetAllEnvironmentsUseCase().execute(getAllEnvsDto);
55+
56+
expect(environments).to.be.an('array');
57+
expect(environments.length).to.be.equal(2);
58+
expect(environments[0].id).to.be.equal('SomeId');
59+
expect(environments[1].id).to.be.equal('newId');
60+
});
3161
};

0 commit comments

Comments
 (0)