Skip to content

Commit

Permalink
Merge pull request #148 from parcelvoy/feat/delay-to-specific-time
Browse files Browse the repository at this point in the history
adds option to delay step to push to specific time in users tz
  • Loading branch information
chrishills authored May 4, 2023
2 parents 19889bc + 58799c2 commit d9eef0d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
37 changes: 33 additions & 4 deletions apps/platform/src/journey/JourneyStep.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { add, isFuture } from 'date-fns'
import { add, isBefore, isFuture } from 'date-fns'
import Model from '../core/Model'
import { User } from '../users/User'
import Rule from '../rules/Rule'
Expand All @@ -12,6 +12,8 @@ import JourneyProcessJob from './JourneyProcessJob'
import { Database } from '../config/database'
import { compileTemplate } from '../render'
import { logger } from '../config/logger'
import { getProject } from '../projects/ProjectService'
import { getTimezoneOffset } from 'date-fns-tz'

export class JourneyUserStep extends Model {
user_id!: number
Expand Down Expand Up @@ -121,13 +123,15 @@ export class JourneyDelay extends JourneyStep {
minutes = 0
hours = 0
days = 0
time?: string

parseJson(json: any) {
super.parseJson(json)

this.minutes = json?.data?.minutes
this.hours = json?.data?.hours
this.days = json?.data?.days
this.time = json?.data?.time
}

async condition(user: User): Promise<boolean> {
Expand All @@ -142,16 +146,41 @@ export class JourneyDelay extends JourneyStep {
}

// If event, check that it's in the past
if (isFuture(this.offset(userJourneyStep.created_at))) return false
if (isFuture(await this.offset(userJourneyStep.created_at, user))) return false
return true
}

private offset(date: Date): Date {
return add(date, {
private async offset(date: Date, user: User): Promise<Date> {

let nextDate = add(date, {
days: this.days,
hours: this.hours,
minutes: this.minutes,
})

const time = this.time?.trim() // hh:mm
if (time?.length === 5) {
const [hours, minutes] = time.split(':').map(s => parseInt(s, 10))
if (!isNaN(hours) && !isNaN(minutes)) {
let tz = user.timezone
if (!tz) {
const project = await getProject(user.project_id)
tz = project!.timezone
}
if (tz) {
const offset = getTimezoneOffset(tz, nextDate)
nextDate = add(nextDate, { hours: offset })
}
nextDate.setHours(hours)
nextDate.setMinutes(minutes)
// if resulting time is before the visit date, push out to next day same time
if (isBefore(nextDate, date)) {
nextDate = add(nextDate, { days: 1 })
}
}
}

return nextDate
}
}

Expand Down
9 changes: 9 additions & 0 deletions apps/ui/src/views/journey/steps/Delay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface DelayStepConfig {
minutes: number
hours: number
days: number
time?: string
}

export const delayStep: JourneyStepType<DelayStepConfig> = {
Expand Down Expand Up @@ -39,6 +40,14 @@ export const delayStep: JourneyStepType<DelayStepConfig> = {
/>
))
}
<TextInput
name="time"
label="Time"
type="time"
size="small"
value={value.time ?? ''}
onChange={time => onChange({ ...value, time })}
/>
</>
)
},
Expand Down

0 comments on commit d9eef0d

Please sign in to comment.