Skip to content

Commit

Permalink
Merge pull request #31 from fga-eps-mds/206-filter
Browse files Browse the repository at this point in the history
Filtros dos gráficos
  • Loading branch information
EzequielDeOliveira authored May 1, 2021
2 parents 7c124f1 + 2a9b977 commit 97a8cae
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 26 deletions.
89 changes: 75 additions & 14 deletions src/Controllers/DemandController.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ const demandGet = async (req, res) => {
};

const demandsCategoriesStatistic = async (req, res) => {
const { id } = req.query;
const {
idSector, idCategory, initialDate, finalDate,
} = req.query;

const completeFinalDate = `${finalDate}T24:00:00`;

const aggregatorOpts = [
{ $unwind: '$categoryID' },
Expand All @@ -91,16 +95,63 @@ const demandsCategoriesStatistic = async (req, res) => {
},
];

if (id !== 'null' && id !== 'undefined') {
aggregatorOpts.unshift({ $match: { open: true, sectorID: id } });

aggregatorOpts.unshift({
$addFields: {
sectorID: { $arrayElemAt: ['$sectorHistory.sectorID', -1] },
},
});
} else {
aggregatorOpts.unshift({ $match: { open: true } });
try {
if (idSector && idSector !== 'null' && idSector !== 'undefined') {
if (idCategory && idCategory !== 'null' && idCategory !== 'undefined') {
const categoryId = mongoose.Types.ObjectId(idCategory);
aggregatorOpts.unshift({
$match: {
open: true,
sectorID: idSector,
categoryID: categoryId,
createdAt: {
$gte: new Date(initialDate),
$lte: new Date(completeFinalDate),
},
},
});
} else {
aggregatorOpts.unshift({
$match: {
open: true,
sectorID: idSector,
createdAt: {
$gte: new Date(initialDate),
$lte: new Date(completeFinalDate),
},
},
});
}
aggregatorOpts.unshift({
$addFields: {
sectorID: { $arrayElemAt: ['$sectorHistory.sectorID', -1] },
},
});
} else if (idCategory && idCategory !== 'null' && idCategory !== 'undefined') {
const categoryId = mongoose.Types.ObjectId(idCategory);
aggregatorOpts.unshift({
$match: {
open: true,
categoryID: categoryId,
createdAt: {
$gte: new Date(initialDate),
$lte: new Date(completeFinalDate),
},
},
});
} else {
aggregatorOpts.unshift({
$match: {
open: true,
createdAt: {
$gte: new Date(initialDate),
$lte: new Date(completeFinalDate),
},
},
});
}
} catch (err) {
console.error(err);
}

try {
Expand All @@ -112,7 +163,9 @@ const demandsCategoriesStatistic = async (req, res) => {
};

const demandsSectorsStatistic = async (req, res) => {
const { id } = req.query;
const { idCategory, initialDate, finalDate } = req.query;

const completeFinalDate = `${finalDate}T24:00:00`;

const aggregatorOpts = [
{
Expand All @@ -123,13 +176,17 @@ const demandsSectorsStatistic = async (req, res) => {
},
];

if (id !== 'null' && id !== 'undefined') {
if (idCategory && idCategory !== 'null' && idCategory !== 'undefined') {
try {
const objectID = mongoose.Types.ObjectId(id);
const objectID = mongoose.Types.ObjectId(idCategory);
aggregatorOpts.unshift({
$match: {
open: true,
categoryID: objectID,
createdAt: {
$gte: new Date(initialDate),
$lte: new Date(completeFinalDate),
},
},
});
} catch (err) {
Expand All @@ -139,6 +196,10 @@ const demandsSectorsStatistic = async (req, res) => {
aggregatorOpts.unshift({
$match: {
open: true,
createdAt: {
$gte: new Date(initialDate),
$lte: new Date(completeFinalDate),
},
},
});
}
Expand Down
74 changes: 62 additions & 12 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,43 +366,93 @@ describe('Sample Test', () => {

// statisticas tests
it('Get category statistics', async (done) => {
const res = await request(app).get('/statistic/category?id=null').set('x-access-token', token);
const res = await request(app).get(`/statistic/category?idCategory=null&idSector=null&initialDate=${'01-01-2021'}&finalDate=${moment().format('YYYY-MM-DD')}`).set('x-access-token', token);
const lastIdx = res.body.length - 1;
expect(res.statusCode).toBe(200);
expect(res.body[lastIdx].demandas).toBe(1);
done();
})

it('Get category statistics filtered by sector', async (done) => {
const res = await request(app).get('/statistic/category?id=6064ffa9942d5e008c0734dc').set('x-access-token', token);
it('Get sector statistics', async (done) => {
const res = await request(app).get(`/statistic/sector?idCategory=null&idSector=null&initialDate=${'01-01-2021'}&finalDate=${moment().format('YYYY-MM-DD')}`).set('x-access-token', token);
const lastIdx = res.body.length - 1;
expect(res.statusCode).toBe(200);
expect(res.body[lastIdx].total).toBe(1);
done();
})

it('Get category statistics filtered by category', async (done) => {
const statisticCategory = {
name: 'Categoria de estatistica',
description: 'Categoria sobre as estatistica',
color: '#000000',
}
const resCategory = await request(app).post('/category/create').set('x-access-token', token).send(statisticCategory);
const idSts = resCategory.body._id;
const statisticDemand = {
name: 'Statistic Demand',
description: 'Descrição da Demanda de estatistica',
process: '000000',
categoryID: [idSts],
sectorID: '606d094f9186b600486c5048',
clientID: '6054dacb934bd000d7ca623b',
userID: '6089c3538dfebe00555bc17e'
}
const resDemand = await request(app).post('/demand/create').set('x-access-token', token).send(statisticDemand);
const res = await request(app).get(`/statistic/category?idCategory=${idSts}&idSector=null&initialDate=${'01-01-2021'}&finalDate=${moment().format('YYYY-MM-DD')}`)
.set('x-access-token', token);
const lastIdx = res.body.length - 1;
expect(res.statusCode).toBe(200);
expect(res.body[lastIdx].demandas).toBe(1);
done();
})

it('Get sector statistics', async (done) => {
const res = await request(app).get('/statistic/sector?id=null').set('x-access-token', token);
it('Get category statistics filtered by category and sector', async (done) => {
const statisticCategory = {
name: 'Categoria de estatistica',
description: 'Categoria sobre as estatistica',
color: '#000000',
}
const resCategory = await request(app).post('/category/create').set('x-access-token', token).send(statisticCategory);
const idSts = resCategory.body._id;
const statisticDemand = {
name: 'Statistic Demand',
description: 'Descrição da Demanda de estatistica',
process: '000000',
categoryID: [idSts],
sectorID: '106d094f9186b600486c5048',
clientID: '6054dacb934bd000d7ca623b',
userID: '6089c3538dfebe00555bc17e'
}
const resDemand = await request(app).post('/demand/create').set('x-access-token', token).send(statisticDemand);
const res = await request(app).get(`/statistic/category?idCategory=${idSts}&idSector=106d094f9186b600486c5048&initialDate=${'01-01-2021'}&finalDate=${moment().format('YYYY-MM-DD')}`)
.set('x-access-token', token);
const lastIdx = res.body.length - 1;
expect(res.statusCode).toBe(200);
expect(res.body[lastIdx].total).toBe(1);
expect(res.body[lastIdx].demandas).toBe(1);
done();
})

it('Get sector statistics filtered by category', async (done) => {
const resCategory = await request(app).post('/category/create').set('x-access-token', token).send(category);
const categoryId3 = resCategory.body._id;
const statisticCategory = {
name: 'Categoria de estatistica',
description: 'Categoria sobre as estatistica',
color: '#000000',
}
const resCategory = await request(app).post('/category/create').set('x-access-token', token).send(statisticCategory);
const idSts = resCategory.body._id;
const statisticDemand = {
name: 'Statistic Demand',
description: 'Descrição da Demanda de estatistica',
process: '000000',
categoryID: [categoryId3],
sectorID: '6064ffa9942d5e008c0734dc',
categoryID: [idSts],
sectorID: '606d09569186b600486c5049',
clientID: '6054dacb934bd000d7ca623b',
userID: '6089c3538dfebe00555bc17e'
}
await request(app).post('/demand/create').set('x-access-token', token).send(statisticDemand);
const res = await request(app).get(`/statistic/sector?id=${categoryId3}`).set('x-access-token', token);
const resDemand = await request(app).post('/demand/create').set('x-access-token', token).send(statisticDemand);
const res = await request(app).get(`/statistic/sector?=idSector=null&idCategory${idSts}&initialDate=${'01-01-2021'}&finalDate=${moment().format('YYYY-MM-DD')}`)
.set('x-access-token', token);
const lastIdx = res.body.length - 1;
expect(res.statusCode).toBe(200);
expect(res.body[lastIdx].total).toBe(1);
Expand Down

0 comments on commit 97a8cae

Please sign in to comment.