Skip to content

Commit

Permalink
Better enforces single run journey entrances (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
pushchris authored Apr 25, 2024
1 parent d5c0955 commit 1f813c4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
10 changes: 4 additions & 6 deletions apps/platform/src/journey/JourneyRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,10 @@ export const setJourneyStepMap = async (journeyId: number, stepMap: JourneyStepM
steps.push(step = new JourneyStep())
}
let next_scheduled_at: null | Date = null
if (type === JourneyEntrance.type) {
if (data.trigger === 'schedule') {
next_scheduled_at = JourneyEntrance.fromJson({ data }).nextDate(now)
} else {
next_scheduled_at = null
}
if (type === JourneyEntrance.type
&& data.trigger === 'schedule'
&& step.data?.schedule !== data.schedule) {
next_scheduled_at = JourneyEntrance.fromJson({ data }).nextDate(now)
}
const fields = { data, data_key, name, next_scheduled_at, x, y }
step.parseJson(step.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class ScheduledEntranceOrchestratorJob extends Job {
.whereNull('journeys.deleted_at')
.where('journey_steps.type', JourneyEntrance.type)
.whereJsonPath('journey_steps.data', '$.trigger', '=', 'schedule')
.whereJsonPath('journey_steps.data', '$.multiple', '=', true)
.whereNotNull('journey_steps.next_scheduled_at')
.where('journey_steps.next_scheduled_at', '<=', new Date()),
)
Expand Down
16 changes: 8 additions & 8 deletions apps/ui/src/ui/RRuleEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const dayOptions: FieldOption[] = [
},
]

interface RRuleEditorProps extends ControlledProps<string> {
interface RRuleEditorProps extends ControlledProps<[string, Partial<Options> | undefined]> {
label?: ReactNode
}

Expand All @@ -63,9 +63,10 @@ type RuleOptions = Omit<Options, 'freq'> & { freq: RuleFrequency }

export default function RRuleEditor({ label, onChange, value }: RRuleEditorProps) {
const options = useMemo<Partial<RuleOptions>>(() => {
if (value) {
const rule = value[0]
if (rule) {
try {
const options = RRule.fromString(value).origOptions as RuleOptions
const options = RRule.fromString(rule).origOptions as RuleOptions
if (options.freq === undefined) {
options.freq = 'once'
}
Expand All @@ -78,10 +79,11 @@ export default function RRuleEditor({ label, onChange, value }: RRuleEditorProps
}, [value])

const setValues = ({ freq, ...options }: Partial<RuleOptions>) => {
onChange(RRule.optionsToString({
const rule: Partial<Options> = {
...options,
freq: freq === 'once' ? undefined : freq,
}))
}
onChange([RRule.optionsToString(rule), rule])
}

return (
Expand Down Expand Up @@ -132,9 +134,7 @@ export default function RRuleEditor({ label, onChange, value }: RRuleEditorProps
}
return w
})}
onChange={byweekday => {
setValues({ ...options, byweekday: byweekday.map(n => Weekday.fromStr(n)) })
}}
onChange={byweekday => setValues({ ...options, byweekday: byweekday.map(n => Weekday.fromStr(n)) })}
label="Days"
/>
)
Expand Down
4 changes: 2 additions & 2 deletions apps/ui/src/views/journey/steps/Entrance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ export const entranceStep: JourneyStepType<EntranceConfig> = {
/>
<RRuleEditor
label={t('schedule')}
value={value.schedule ?? ''}
onChange={schedule => onChange({ ...value, schedule })}
value={[value.schedule ?? '', undefined]}
onChange={([schedule, rule]) => onChange({ ...value, schedule, multiple: !!rule?.freq })}
/>
</>
)
Expand Down

0 comments on commit 1f813c4

Please sign in to comment.