Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #61 from openkfw/api-datadate-patch
Browse files Browse the repository at this point in the history
api-datadate-patch
  • Loading branch information
MartinJurcoGlina authored Sep 6, 2021
2 parents d1aa03f + 52ff43d commit 4c66206
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
13 changes: 11 additions & 2 deletions api/src/models/attributeModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const createAttributesFilter = (attributeIds, attributeIdCategories, featureIds,
}
filter.date = { $lte: dateEnd };
}

return filter;
};

Expand Down Expand Up @@ -222,7 +223,10 @@ const getAvailableDates = async (attributeId) => {
},
])
.toArray();
return dates.map((date) => date._id);
return dates.map((date) => ({
dataDate: date.dataDate,
date: date._id,
}));
};

/**
Expand All @@ -236,4 +240,9 @@ const getAvailableDates = async (attributeId) => {
const countAttributes = (attributeIds, attributeIdCategories, featureIds, dateStart, dateEnd) =>
getDocumentsCount(createAttributesFilter(attributeIds, attributeIdCategories, featureIds, dateStart, dateEnd));

module.exports = { getFilteredAttributes, getLatestAttributes, getAvailableDates, countAttributes };
module.exports = {
getFilteredAttributes,
getLatestAttributes,
getAvailableDates,
countAttributes,
};
10 changes: 10 additions & 0 deletions api/src/testUtils/testData/attributesData.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
const casesAttrToDb = [
{
date: '2021-02-09T00:00:00.000Z',
dataDate: 'January 2021',
featureId: 'Province1',
attributeId: 'covid19Cases',
valueNumber: 272718,
},
{
date: '2021-02-10T00:00:00.000Z',
dataDate: 'February 2021',
featureId: 'Province1',
attributeId: 'covid19Cases',
valueNumber: 272718,
},
{
date: '2021-02-11T00:00:00.000Z',
dataDate: 'March 2021',
featureId: 'Province1',
attributeId: 'covid19Cases',
valueNumber: 272718,
},
{
date: '2021-02-12T00:00:00.000Z',
dataDate: 'April 2021',
featureId: 'Province1',
attributeId: 'covid19Cases',
valueNumber: 272718,
},
{
date: '2021-02-13T00:00:00.000Z',
dataDate: 'May 2021',
featureId: 'Province1',
attributeId: 'covid19Cases',
valueNumber: 272718,
Expand All @@ -34,30 +39,35 @@ const casesAttrToDb = [
const deathsAttrToDb = [
{
date: '2021-02-04T00:00:00.000Z',
dataDate: 'June 2021',
featureId: 'Province1',
attributeId: 'covid19Deaths',
valueNumber: 272718,
},
{
date: '2021-02-05T00:00:00.000Z',
dataDate: 'July 2021',
featureId: 'Province1',
attributeId: 'covid19Deaths',
valueNumber: 272718,
},
{
date: '2021-02-06T00:00:00.000Z',
dataDate: 'August 2021',
featureId: 'Province1',
attributeId: 'covid19Deaths',
valueNumber: 272718,
},
{
date: '2021-02-07T00:00:00.000Z',
dataDate: 'September 2021',
featureId: 'Province1',
attributeId: 'covid19Deaths',
valueNumber: 272718,
},
{
date: '2021-02-08T00:00:00.000Z',
dataDate: 'October 2021',
featureId: 'Province1',
attributeId: 'covid19Deaths',
valueNumber: 272718,
Expand Down
39 changes: 24 additions & 15 deletions api/src/tests/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,44 @@ describe('GET api/attributes/:attributeId/availableDates', () => {
await Attribute.insertMany(casesAttrToDb);
const res = await request(app).get(`/api/attributes/covid19Cases/availableDates`).expect(200);
expect(res.body).toEqual([
'2021-02-09T00:00:00.000Z',
'2021-02-10T00:00:00.000Z',
'2021-02-11T00:00:00.000Z',
'2021-02-12T00:00:00.000Z',
'2021-02-13T00:00:00.000Z',
{ dataDate: 'January 2021', date: '2021-02-09T00:00:00.000Z' },
{ dataDate: 'February 2021', date: '2021-02-10T00:00:00.000Z' },
{ dataDate: 'March 2021', date: '2021-02-11T00:00:00.000Z' },
{ dataDate: 'April 2021', date: '2021-02-12T00:00:00.000Z' },
{ dataDate: 'May 2021', date: '2021-02-13T00:00:00.000Z' },
]);
});

it('should return correct array with dates when different attributeIds are in the database', async () => {
await Attribute.insertMany([...casesAttrToDb, ...deathsAttrToDb]);
const res = await request(app).get(`/api/attributes/covid19Cases/availableDates`).expect(200);
expect(res.body).toEqual([
'2021-02-09T00:00:00.000Z',
'2021-02-10T00:00:00.000Z',
'2021-02-11T00:00:00.000Z',
'2021-02-12T00:00:00.000Z',
'2021-02-13T00:00:00.000Z',
{ dataDate: 'January 2021', date: '2021-02-09T00:00:00.000Z' },
{ dataDate: 'February 2021', date: '2021-02-10T00:00:00.000Z' },
{ dataDate: 'March 2021', date: '2021-02-11T00:00:00.000Z' },
{ dataDate: 'April 2021', date: '2021-02-12T00:00:00.000Z' },
{ dataDate: 'May 2021', date: '2021-02-13T00:00:00.000Z' },
]);
});

it('should return each date only once when different featureIds with same dates are in the database', async () => {
await Attribute.insertMany([...casesAttrToDb, { ...casesAttrToDb[0], featureId: 'Province2' }]);
const res = await request(app).get(`/api/attributes/covid19Cases/availableDates`).expect(200);
expect(res.body).toEqual([
'2021-02-09T00:00:00.000Z',
'2021-02-10T00:00:00.000Z',
'2021-02-11T00:00:00.000Z',
'2021-02-12T00:00:00.000Z',
'2021-02-13T00:00:00.000Z',
{ dataDate: 'January 2021', date: '2021-02-09T00:00:00.000Z' },
{ dataDate: 'February 2021', date: '2021-02-10T00:00:00.000Z' },
{ dataDate: 'March 2021', date: '2021-02-11T00:00:00.000Z' },
{ dataDate: 'April 2021', date: '2021-02-12T00:00:00.000Z' },
{ dataDate: 'May 2021', date: '2021-02-13T00:00:00.000Z' },
]);
});

it('should return null value for dataDate if it is not in the database', async () => {
await Attribute.insertMany(bedOccupancyWithoutDataDateAttrToDb);
const res = await request(app).get(`/api/attributes/Bed occupancy rate/availableDates`).expect(200);
expect(res.body).toEqual([
{ dataDate: null, date: '2021-03-28T00:00:0.000Z' },
{ dataDate: null, date: '2021-03-29T00:00:0.000Z' },
]);
});
});
Expand Down

0 comments on commit 4c66206

Please sign in to comment.