From 35e5c957dfb00d0f2d95ce6dc6c2bc798a2226f4 Mon Sep 17 00:00:00 2001 From: Cheng Shi Date: Fri, 6 Dec 2024 17:44:21 -0500 Subject: [PATCH] fix: change the data format to file creationDate and publishDate from Date to string --- .../mappers/JSFileMetadataMapper.ts | 20 ++++++----- src/shared/helpers/DateHelper.ts | 34 +++++++++++++++---- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/files/infrastructure/mappers/JSFileMetadataMapper.ts b/src/files/infrastructure/mappers/JSFileMetadataMapper.ts index b7453ffdc..10419ab4b 100644 --- a/src/files/infrastructure/mappers/JSFileMetadataMapper.ts +++ b/src/files/infrastructure/mappers/JSFileMetadataMapper.ts @@ -29,13 +29,13 @@ export class JSFileMetadataMapper { return new FileMetadata( this.toFileType(jsFile.contentType, jsFile.originalFormatLabel), this.toFileSize(jsFile.sizeBytes), - this.toFileDate(jsFile.creationDate, jsFile.publicationDate, jsFile.embargo), + this.toFileDate(jsFile.creationDate ?? '', jsFile.publicationDate, jsFile.embargo), this.toFileDownloads(downloadsCount), this.toFileLabels(jsFile.categories, jsFile.tabularTags), this.toFileIsDeleted(jsFile.deleted), this.toFileOriginalFileDownloadUrl(jsFile.id), - jsFile.creationDate ?? new Date(), - jsFile.publicationDate, + jsFile.creationDate ? new Date(jsFile.creationDate) : new Date(), + jsFile.publicationDate ? new Date(jsFile.publicationDate) : new Date(), this.toFileThumbnail(thumbnail), this.toFileDirectory(jsFile.directoryLabel), this.toFileEmbargo(jsFile.embargo), @@ -55,18 +55,22 @@ export class JSFileMetadataMapper { } static toFileDate( - jsFileCreationDate?: Date, - jsFilePublicationDate?: Date, + jsFileCreationDate: string, + jsFilePublicationDate?: string, jsFileEmbargo?: JSFileEmbargo ): FileDate { + console.log('jsFileCreationDate', jsFileCreationDate) if (jsFilePublicationDate) { if (jsFileEmbargo) { - return { type: FileDateType.METADATA_RELEASED, date: jsFilePublicationDate } + return { + type: FileDateType.METADATA_RELEASED, + date: new Date(jsFilePublicationDate) + } } - return { type: FileDateType.PUBLISHED, date: jsFilePublicationDate } + return { type: FileDateType.PUBLISHED, date: new Date(jsFilePublicationDate) } } if (jsFileCreationDate) { - return { type: FileDateType.DEPOSITED, date: jsFileCreationDate } + return { type: FileDateType.DEPOSITED, date: new Date(jsFileCreationDate) } } throw new Error('File date not found') } diff --git a/src/shared/helpers/DateHelper.ts b/src/shared/helpers/DateHelper.ts index 690b83c91..6fee580f9 100644 --- a/src/shared/helpers/DateHelper.ts +++ b/src/shared/helpers/DateHelper.ts @@ -1,13 +1,35 @@ export class DateHelper { - static toDisplayFormat(date: Date | undefined): string { + static toDisplayFormat(date: Date | undefined | string): string { if (!date) { return '' } - return date.toLocaleDateString(Intl.DateTimeFormat().resolvedOptions().locale, { - year: 'numeric', - month: 'short', - day: 'numeric' - }) + + if (typeof date === 'string') { + const monthNames = [ + 'Jan', + 'Feb', + 'Mar', + 'Apr', + 'May', + 'Jun', + 'Jul', + 'Aug', + 'Sep', + 'Oct', + 'Nov', + 'Dec' + ] + const [year, month, day] = date.split('-') + const monthIndex = monthNames[parseInt(month) - 1] + const formattedDate = `${monthIndex} ${day}, ${year}` + return formattedDate + } else { + return date.toLocaleDateString(Intl.DateTimeFormat().resolvedOptions().locale, { + year: 'numeric', + month: 'short', + day: 'numeric' + }) + } } static toDisplayFormatYYYYMMDD(date: Date | undefined): string {