-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CMS-559: Add time zone to emergency-alert time fields (#1550)
* CMS-559: Add time zone to emergency-alert time fields * CMS-559: Format date with date-fns * CMS-559: Add small fix * CMS-559: Add timezone
- Loading branch information
Showing
1 changed file
with
14 additions
and
5 deletions.
There are no files selected for viewing
19 changes: 14 additions & 5 deletions
19
src/cms/src/api/emergency-alert/content-types/emergency-alert/lifecycles.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
"use strict"; | ||
|
||
const format = require('date-fns/format') | ||
const utcToZonedTime = require('date-fns-tz/utcToZonedTime') | ||
|
||
const formatDateToPacificTime = (dateString) => { | ||
// Strapi returns the date in ISO format e.g. 2025-01-01T00:00:00.000Z | ||
// Convert dateString to Pacific Time | ||
const pacificTime = utcToZonedTime(dateString, 'America/Los_Angeles') | ||
// Format the date in YYYY-MM-DD e.g. 2025-01-01 | ||
return format(pacificTime, 'yyyy-MM-dd') | ||
} | ||
|
||
module.exports = { | ||
beforeCreate(event) { | ||
const { data } = event.params | ||
const date = new Date(data.createdAt) | ||
const createdDate = date.toISOString().split('T')[0] | ||
const createdDate = formatDateToPacificTime(data.createdAt) | ||
if (data.isActive === true) { | ||
data.activeDate = createdDate | ||
} | ||
}, | ||
beforeUpdate(event) { | ||
const { data } = event.params | ||
const date = new Date(data.updatedAt) | ||
const updatedDate = date.toISOString().split('T')[0] | ||
const updatedDate = formatDateToPacificTime(data.updatedAt) | ||
if (data.isActive === true) { | ||
data.activeDate = updatedDate | ||
} | ||
if (data.isActive === false && data.activeDate) { | ||
data.inactiveDate = updatedDate | ||
} | ||
} | ||
} | ||
} |