-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Session reminder and expiry events
- Loading branch information
1 parent
409d2ca
commit 3665eef
Showing
7 changed files
with
173 additions
and
93 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
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
21 changes: 21 additions & 0 deletions
21
api.planx.uk/modules/webhooks/lowcalSessionEvents/schema.ts
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { z } from "zod"; | ||
import { ValidatedRequestHandler } from "../../../shared/middleware/validate"; | ||
import { ScheduledEventResponse } from "../../../hasura/metadata"; | ||
|
||
export const createSessionEventSchema = z.object({ | ||
body: z.object({ | ||
createdAt: z.string().transform((val) => new Date(val)), | ||
payload: z.object({ | ||
sessionId: z.string(), | ||
}), | ||
}), | ||
}); | ||
|
||
export type CreateSessionEvent = z.infer< | ||
typeof createSessionEventSchema | ||
>["body"]; | ||
|
||
export type CreateSessionEventController = ValidatedRequestHandler< | ||
typeof createSessionEventSchema, | ||
ScheduledEventResponse[] | ||
>; |
44 changes: 44 additions & 0 deletions
44
api.planx.uk/modules/webhooks/lowcalSessionEvents/service.ts
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { addDays } from "date-fns"; | ||
|
||
import { createScheduledEvent } from "../../../hasura/metadata"; | ||
import { | ||
DAYS_UNTIL_EXPIRY, | ||
REMINDER_DAYS_FROM_EXPIRY, | ||
} from "../../../saveAndReturn/utils"; | ||
import { CreateSessionEvent } from "./schema"; | ||
|
||
/** | ||
* Create "reminder" events for a lowcal_session record | ||
*/ | ||
export const createSessionReminderEvent = async ({ | ||
createdAt, | ||
payload, | ||
}: CreateSessionEvent) => { | ||
const response = await Promise.all( | ||
REMINDER_DAYS_FROM_EXPIRY.map((day: number) => | ||
createScheduledEvent({ | ||
webhook: "{{HASURA_PLANX_API_URL}}/send-email/reminder", | ||
schedule_at: addDays(createdAt, DAYS_UNTIL_EXPIRY - day), | ||
payload: payload, | ||
comment: `reminder_${payload.sessionId}_${day}day`, | ||
}), | ||
), | ||
); | ||
return response; | ||
}; | ||
|
||
/** | ||
* Create an "expiry" event for a lowcal_session record | ||
*/ | ||
export const createSessionExpiryEvent = async ({ | ||
createdAt, | ||
payload, | ||
}: CreateSessionEvent) => { | ||
const response = await createScheduledEvent({ | ||
webhook: "{{HASURA_PLANX_API_URL}}/send-email/expiry", | ||
schedule_at: addDays(createdAt, DAYS_UNTIL_EXPIRY), | ||
payload: payload, | ||
comment: `expiry_${payload.sessionId}`, | ||
}); | ||
return [response]; | ||
}; |
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