Skip to content

Commit

Permalink
Force timezone check during record creation (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
pushchris authored Jan 20, 2025
1 parent 617da44 commit 7293979
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
7 changes: 5 additions & 2 deletions apps/platform/src/campaigns/Campaign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Model, { ModelParams } from '../core/Model'
import List from '../lists/List'
import Template from '../render/Template'
import Subscription from '../subscriptions/Subscription'
import { crossTimezoneCopy } from '../utilities'
import { crossTimezoneCopy, isValidIANATimezone } from '../utilities'
import Project from '../projects/Project'
import { User } from '../users/User'

Expand Down Expand Up @@ -77,6 +77,9 @@ export class CampaignSend extends Model {
project: Pick<Project, 'timezone'>,
user: Pick<User, 'id' | 'timezone'>,
): CampaignSendParams {
const timezone = isValidIANATimezone(user.timezone)
? user.timezone
: project.timezone
return {
user_id: user.id,
campaign_id: campaign.id,
Expand All @@ -85,7 +88,7 @@ export class CampaignSend extends Model {
? crossTimezoneCopy(
campaign.send_at,
project.timezone,
user.timezone ?? project.timezone,
timezone ?? project.timezone,
)
: campaign.send_at,
}
Expand Down
13 changes: 13 additions & 0 deletions apps/platform/src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ export const partialMatchLocale = (locale1?: string, locale2?: string) => {
return locale1 === locale2 || locale1Root === locale2Root
}

export const isValidIANATimezone = (timezone?: string) => {
try {
if (!timezone || typeof timezone !== 'string') {
return false
}

Intl.DateTimeFormat(undefined, { timeZone: timezone })
return true
} catch (e) {
return false
}
}

export const crossTimezoneCopy = (
date: Date,
fromTimezone: string,
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/api/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ Create or update a user profile with associated traits.
- **anonymous_id** string (optional)
- **external_id** string
- **email** string (optional)
- **phone** string (optional)
- **timezone** string (optional)
- **locale** string (optional)
- **phone** string (optional) - Phone number in E.164 format
- **timezone** string (optional) - The users timezone provided in IANA format (i.e. America/Chicago)
- **locale** string (optional) - The locale of the user use for language and formatting (i.e `es` or `en`)
- **data** object (optional)

#### Responses
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/how-to/lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ You can create lists that contain fixed data that can be uploaded via CSV. When
- `first_name`: The first name of the user
- `last_name`: The last name of the user
- `email`: The users email
- `phone`: The users phone number with country code
- `timezone`: The users timezone provided in IANA format (America/Chicago)
- `locale`: The language
- `phone`: The users phone number in E.164 format
- `timezone`: The users timezone provided in IANA format (i.e. America/Chicago)
- `locale`: The locale of the user use for language and formatting (i.e `es` or `en`)
- `created_at`: When a user was created to override internal time setting. Must be in ISO 8601 format

0 comments on commit 7293979

Please sign in to comment.