Skip to content

Commit

Permalink
fix: change the data format to file creationDate and publishDate from…
Browse files Browse the repository at this point in the history
… Date to string
  • Loading branch information
ChengShi-1 committed Dec 6, 2024
1 parent 02b04e5 commit 35e5c95
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
20 changes: 12 additions & 8 deletions src/files/infrastructure/mappers/JSFileMetadataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Check failure on line 32 in src/files/infrastructure/mappers/JSFileMetadataMapper.ts

View workflow job for this annotation

GitHub Actions / lint

Argument of type 'Date | ""' is not assignable to parameter of type 'string'.
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),
Expand All @@ -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')
}
Expand Down
34 changes: 28 additions & 6 deletions src/shared/helpers/DateHelper.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 35e5c95

Please sign in to comment.