diff --git a/api/src/models/attributeModel.js b/api/src/models/attributeModel.js index 790e5fc..aea44fd 100644 --- a/api/src/models/attributeModel.js +++ b/api/src/models/attributeModel.js @@ -53,6 +53,7 @@ const createAttributesFilter = (attributeIds, attributeIdCategories, featureIds, } filter.date = { $lte: dateEnd }; } + return filter; }; @@ -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, + })); }; /** @@ -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, +}; diff --git a/api/src/testUtils/testData/attributesData.js b/api/src/testUtils/testData/attributesData.js index 874a7a5..cae393c 100644 --- a/api/src/testUtils/testData/attributesData.js +++ b/api/src/testUtils/testData/attributesData.js @@ -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, @@ -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, diff --git a/api/src/tests/attributes.js b/api/src/tests/attributes.js index 458b096..c06cc71 100644 --- a/api/src/tests/attributes.js +++ b/api/src/tests/attributes.js @@ -26,11 +26,11 @@ 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' }, ]); }); @@ -38,11 +38,11 @@ describe('GET api/attributes/:attributeId/availableDates', () => { 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' }, ]); }); @@ -50,11 +50,20 @@ describe('GET api/attributes/:attributeId/availableDates', () => { 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' }, ]); }); });