Skip to content

Commit

Permalink
[104] change date format (#155)
Browse files Browse the repository at this point in the history
* Format date properly for csv, tsv and json download

* Update tests

* Use dedicated function for date formatting

* Remove only from tests
  • Loading branch information
stanislaw-zakrzewski authored Nov 20, 2024
1 parent 881e39e commit 8db28d3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 33 deletions.
8 changes: 8 additions & 0 deletions data-serving/data-service/src/controllers/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,14 @@ export class CasesController {
header: false,
columns: this.csvHeaders,
delimiter: delimiter,
cast: {
date: (value: Date) => {
if (value) {
return new Date(value).toISOString().split('T')[0];
}
return value;
},
}
});
res.write(stringifiedCase);
doc = await cursor.next();
Expand Down
65 changes: 39 additions & 26 deletions data-serving/data-service/src/util/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ export const removeBlankHeader = (headers: string[]): string[] => {
return headers;
};

export const formatDateWithoutTime = (date: Date | undefined): string => {
if (!date) return '';
return date.toISOString().split('T')[0];
};

export const denormalizeFields = async (
doc: CaseDocument,
): Promise<Partial<CaseDocument>> => {
Expand Down Expand Up @@ -178,9 +183,8 @@ function denormalizeCaseReferenceFields(
additionalSources.push(source.sourceUrl);
}
}
denormalizedData[
'caseReference.additionalSources'
] = additionalSources.join(',');
denormalizedData['caseReference.additionalSources'] =
additionalSources.join(',');
denormalizedData['caseReference.sourceEntryId'] = doc.sourceEntryId || '';
denormalizedData['caseReference.sourceId'] = doc.sourceId || '';
denormalizedData['caseReference.sourceUrl'] = doc.sourceUrl || '';
Expand Down Expand Up @@ -211,38 +215,46 @@ export const denormalizeEventsFields = (
const denormalizedData: Record<string, string> = {};

denormalizedData['events.dateEntry'] = doc.dateEntry
? doc.dateEntry.toDateString()
? formatDateWithoutTime(doc.dateEntry)
: undefined || '';
denormalizedData['events.dateReported'] = doc.dateReported
? doc.dateReported.toDateString()
? formatDateWithoutTime(doc.dateReported)
: undefined || '';
denormalizedData['events.dateOnset'] = doc.dateOnset?.toDateString() || '';
denormalizedData['events.dateConfirmation'] =
doc.dateConfirmation?.toDateString() || '';
denormalizedData['events.dateOnset'] = formatDateWithoutTime(doc.dateOnset);
denormalizedData['events.dateConfirmation'] = formatDateWithoutTime(
doc.dateConfirmation,
);
denormalizedData['events.confirmationMethod'] =
doc.confirmationMethod || '';
denormalizedData['events.dateOfFirstConsult'] =
doc.dateOfFirstConsult?.toDateString() || '';
denormalizedData['events.dateOfFirstConsult'] = formatDateWithoutTime(
doc.dateOfFirstConsult,
);
denormalizedData['events.hospitalized'] = doc.hospitalized || '';
denormalizedData['events.reasonForHospitalization'] =
doc.reasonForHospitalization || '';
denormalizedData['events.dateHospitalization'] =
doc.dateHospitalization?.toDateString() || '';
denormalizedData['events.dateDischargeHospital'] =
doc.dateDischargeHospital?.toDateString() || '';
denormalizedData['events.dateHospitalization'] = formatDateWithoutTime(
doc.dateHospitalization,
);
denormalizedData['events.dateDischargeHospital'] = formatDateWithoutTime(
doc.dateDischargeHospital,
);
denormalizedData['events.intensiveCare'] = doc.intensiveCare || '';
denormalizedData['events.dateAdmissionICU'] =
doc.dateAdmissionICU?.toDateString() || '';
denormalizedData['events.dateDischargeICU'] =
doc.dateDischargeICU?.toDateString() || '';
denormalizedData['events.dateAdmissionICU'] = formatDateWithoutTime(
doc.dateAdmissionICU,
);
denormalizedData['events.dateDischargeICU'] = formatDateWithoutTime(
doc.dateDischargeICU,
);
denormalizedData['events.homeMonitoring'] = doc.homeMonitoring || '';
denormalizedData['events.isolated'] = doc.isolated || '';
denormalizedData['events.dateIsolation'] =
doc.dateIsolation?.toDateString() || '';
denormalizedData['events.dateIsolation'] = formatDateWithoutTime(
doc.dateIsolation,
);
denormalizedData['events.outcome'] = doc.outcome || '';
denormalizedData['events.dateDeath'] = doc.dateDeath?.toDateString() || '';
denormalizedData['events.dateRecovered'] =
doc.dateRecovered?.toDateString() || '';
denormalizedData['events.dateDeath'] = formatDateWithoutTime(doc.dateDeath);
denormalizedData['events.dateRecovered'] = formatDateWithoutTime(
doc.dateRecovered,
);

return denormalizedData;
};
Expand Down Expand Up @@ -334,7 +346,7 @@ function denormalizeTravelHistoryFields(

denormalizedData['travelHistory.travelHistory'] = doc?.travelHistory || '';
denormalizedData['travelHistory.travelHistoryEntry'] =
doc?.travelHistoryEntry?.toDateString() || '';
formatDateWithoutTime(doc?.travelHistoryEntry);
denormalizedData['travelHistory.travelHistoryStart'] =
doc?.travelHistoryStart || '';
denormalizedData['travelHistory.travelHistoryLocation'] =
Expand All @@ -352,8 +364,9 @@ function denormalizeVaccineFields(

denormalizedData['vaccination.vaccination'] = doc?.vaccination || '';
denormalizedData['vaccination.vaccineName'] = doc?.vaccineName || '';
denormalizedData['vaccination.vaccineDate'] =
doc?.vaccineDate?.toDateString() || '';
denormalizedData['vaccination.vaccineDate'] = formatDateWithoutTime(
doc?.vaccineDate,
);
denormalizedData['vaccination.vaccineSideEffects'] =
doc?.vaccineSideEffects || '';

Expand Down
27 changes: 20 additions & 7 deletions data-serving/data-service/test/util/case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { RevisionMetadataDocument } from '../model/revision-metadata';
import { TransmissionDocument } from '../model/transmission';
import { TravelHistoryDocument } from '../model/travel-history';
import { VaccineDocument } from '../model/vaccine';
import { removeBlankHeader, denormalizeFields } from '../../src/util/case';
import {
removeBlankHeader,
denormalizeFields,
formatDateWithoutTime,
} from '../../src/util/case';
import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { GenomeSequenceDocument } from '../../src/model/genome-sequence';
Expand Down Expand Up @@ -421,16 +425,16 @@ describe('Case', () => {
const denormalizedCase = await denormalizeFields(caseDoc);

expect(denormalizedCase['events.dateEntry']).toEqual(
eventsDoc.dateEntry.toDateString(),
formatDateWithoutTime(eventsDoc.dateEntry),
);
expect(denormalizedCase['events.dateReported']).toEqual(
eventsDoc.dateReported.toDateString(),
formatDateWithoutTime(eventsDoc.dateReported),
);
expect(denormalizedCase['events.dateOnset']).toEqual(
eventsDoc.dateOnset?.toDateString(),
formatDateWithoutTime(eventsDoc.dateOnset),
);
expect(denormalizedCase['events.dateConfirmation']).toEqual(
eventsDoc.dateConfirmation?.toDateString(),
formatDateWithoutTime(eventsDoc.dateConfirmation),
);
expect(denormalizedCase['events.confirmationMethod']).toEqual('');
expect(denormalizedCase['events.dateOfFirstConsult']).toEqual('');
Expand Down Expand Up @@ -612,7 +616,7 @@ describe('Case', () => {

expect(denormalizedCase['travelHistory.travelHistory']).toEqual('Y');
expect(denormalizedCase['travelHistory.travelHistoryEntry']).toEqual(
travelHistoryDoc.travelHistoryEntry.toDateString(),
formatDateWithoutTime(travelHistoryDoc.travelHistoryEntry),
);
expect(denormalizedCase['travelHistory.travelHistoryStart']).toEqual(
'start',
Expand Down Expand Up @@ -652,7 +656,7 @@ describe('Case', () => {
expect(denormalizedCase['vaccination.vaccination']).toEqual('Y');
expect(denormalizedCase['vaccination.vaccineName']).toEqual('Pfizer');
expect(denormalizedCase['vaccination.vaccineDate']).toEqual(
vaccinationDoc.vaccineDate.toDateString(),
formatDateWithoutTime(vaccinationDoc.vaccineDate),
);
expect(denormalizedCase['vaccination.vaccineSideEffects']).toEqual(
'cough',
Expand Down Expand Up @@ -689,4 +693,13 @@ describe('Case', () => {
'1234',
);
});

it('formatsDateWithoutTimeCorrectly', async () => {
const correctDateString = '2024-01-01';
const correctDate = new Date(`${correctDateString}T03:24:00`);
const missingDate = undefined;

expect(formatDateWithoutTime(correctDate)).toEqual(correctDateString);
expect(formatDateWithoutTime(missingDate)).toEqual('');
});
});

0 comments on commit 8db28d3

Please sign in to comment.