Skip to content

Commit

Permalink
Fixes offsets for campaign sends not matching user timezone (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
pushchris authored Apr 22, 2023
1 parent 36e73f2 commit 80773c1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 12 deletions.
11 changes: 7 additions & 4 deletions apps/platform/src/campaigns/CampaignService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import App from '../app'
import { SearchParams } from '../core/searchParams'
import { allLists, listUserCount } from '../lists/ListService'
import { allTemplates, duplicateTemplate, validateTemplates } from '../render/TemplateService'
import { utcToZonedTime } from 'date-fns-tz'
import { getSubscription } from '../subscriptions/SubscriptionService'
import { pick } from '../utilities'
import { crossTimezoneCopy, pick } from '../utilities'
import { getProvider } from '../providers/ProviderRepository'
import { createTagSubquery, getTags, setTags } from '../tags/TagService'
import { getProject } from '../projects/ProjectService'
Expand Down Expand Up @@ -111,7 +110,7 @@ export const updateCampaign = async (id: number, projectId: number, { tags, ...p
}

// If we are rescheduling, abort sends to they are reset
if (data.send_at !== campaign.send_at) {
if (data.send_at && data.send_at !== campaign.send_at) {
data.state = 'pending'
await abortCampaign(campaign)
}
Expand Down Expand Up @@ -267,7 +266,11 @@ export const generateSendList = async (campaign: SentCampaign) => {
campaign_id: campaign.id,
state: 'pending',
send_at: campaign.send_in_user_timezone
? utcToZonedTime(campaign.send_at, timezone ?? project.timezone)
? crossTimezoneCopy(
campaign.send_at,
project.timezone,
timezone ?? project.timezone,
)
: campaign.send_at,
})
i++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ describe('CampaignService', () => {
last_name: uuid(),
email: `${uuid()}@test.com`,
})
const project = await createProject(adminId, { name: uuid() })
const project = await createProject(adminId, {
name: uuid(),
timezone: 'utc',
})
const subscription = await createSubscription(project.id, {
name: uuid(),
channel: 'email',
Expand All @@ -40,6 +43,7 @@ describe('CampaignService', () => {
data: {},
name: uuid(),
is_default: false,
rate_limit: 10,
})
return {
project_id: project.id,
Expand Down
2 changes: 1 addition & 1 deletion apps/platform/src/projects/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class Project extends Model {
description?: string
deleted_at?: Date
locale?: string
timezone?: string
timezone!: string
}

export type ProjectParams = Omit<Project, ModelParams | 'deleted_at'>
Expand Down
7 changes: 2 additions & 5 deletions apps/platform/src/projects/ProjectController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ router.get('/all', async ctx => {
const projectCreateParams: JSONSchemaType<ProjectParams> = {
$id: 'projectCreate',
type: 'object',
required: ['name'],
required: ['name', 'timezone'],
properties: {
name: {
type: 'string',
Expand All @@ -63,10 +63,7 @@ const projectCreateParams: JSONSchemaType<ProjectParams> = {
type: 'string',
nullable: true,
},
timezone: {
type: 'string',
nullable: true,
},
timezone: { type: 'string' },
},
additionalProperties: false,
}
Expand Down
2 changes: 1 addition & 1 deletion apps/platform/src/providers/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default class Provider extends Model {

export type ProviderMap<T extends Provider> = (record: any) => T

export type ProviderParams = Omit<Provider, ModelParams>
export type ProviderParams = Omit<Provider, ModelParams | 'setup'>

export type ExternalProviderParams = Omit<ProviderParams, 'group'>

Expand Down
16 changes: 16 additions & 0 deletions apps/platform/src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { validate } from '../core/validate'
import crypto from 'crypto'
import Hashids from 'hashids'
import { differenceInSeconds } from 'date-fns'
import { utcToZonedTime } from 'date-fns-tz'

export const pluralize = (noun: string, count = 2, suffix = 's') => `${noun}${count !== 1 ? suffix : ''}`

Expand Down Expand Up @@ -96,6 +97,21 @@ export const partialMatchLocale = (locale1?: string, locale2?: string) => {
return locale1 === locale2 || locale1Root === locale2Root
}

export const crossTimezoneCopy = (
date: Date,
fromTimezone: string,
toTimezone: string,
) => {
const baseDate = utcToZonedTime(date, fromTimezone)

const utcDate = new Date(baseDate.toLocaleString('en-US', { timeZone: 'UTC' }))
const tzDate = new Date(baseDate.toLocaleString('en-US', { timeZone: toTimezone }))
const offset = utcDate.getTime() - tzDate.getTime()

baseDate.setTime(baseDate.getTime() + offset)
return baseDate
}

export function extractQueryParams<T extends Record<string, any>>(search: URLSearchParams | Record<string, undefined | string | string[]>, schema: JSONSchemaType<T>) {
return validate(schema, Object.entries<JSONSchemaType<any>>(schema.properties).reduce((a, [name, def]) => {
let values!: string[]
Expand Down

0 comments on commit 80773c1

Please sign in to comment.