Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional reminderCreatedAt field #22

Merged
merged 1 commit into from
Feb 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions src/create-reminder-signup/lambda/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,32 @@ function isValidEmail(email: string): boolean {
return re.test(email.toLowerCase());
}

type ReminderPeriod = string;
type DateString = string;

function isValidReminderPeriod(reminderPeriod: string): boolean {
const date = Date.parse(reminderPeriod);
function isValidDateString(dateString: string): boolean {
const date = Date.parse(dateString);
return !isNaN(date);
}

export interface BaseSignupRequest {
email: Email;
country?: string;
reminderCreatedAt?: DateString;
reminderPlatform: ReminderPlatform;
reminderComponent: ReminderComponent;
reminderStage: ReminderStage;
reminderOption?: string;
}

export interface OneOffSignupRequest extends BaseSignupRequest {
reminderPeriod: ReminderPeriod;
}
// typecheck.macro doesn't support extends
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😱 well spotted!

export type OneOffSignupRequest = BaseSignupRequest & {
reminderPeriod: DateString;
};

export interface RecurringSignupRequest extends BaseSignupRequest {
// typecheck.macro doesn't support extends
export type RecurringSignupRequest = BaseSignupRequest & {
reminderFrequencyMonths: number;
}
};

export interface APIGatewayEvent {
headers: Record<string, string | undefined>;
Expand All @@ -73,8 +76,8 @@ export const oneOffSignupValidator = createDetailedValidator<OneOffSignupRequest
constraints: {
Email: (email: string) =>
isValidEmail(email) ? null : 'Invalid email',
ReminderPeriod: (reminderPeriod: string) =>
isValidReminderPeriod(reminderPeriod) ? null : 'Invalid date',
DateString: (dateString: string) =>
isValidDateString(dateString) ? null : 'Invalid date',
},
},
);
Expand All @@ -86,6 +89,8 @@ export const recurringSignupValidator = createDetailedValidator<RecurringSignupR
constraints: {
Email: (email: string) =>
isValidEmail(email) ? null : 'Invalid email',
DateString: (dateString: string) =>
isValidDateString(dateString) ? null : 'Invalid date',
},
},
);
Expand All @@ -106,7 +111,10 @@ export const oneOffSignupFromRequest = (
identity_id: identityId,
country: request.country,
reminder_period: toDate(request.reminderPeriod),
reminder_created_at: new Date().toISOString(),
reminder_created_at: (request.reminderCreatedAt
? new Date(request.reminderCreatedAt)
: new Date()
).toISOString(),
reminder_platform: request.reminderPlatform,
reminder_component: request.reminderComponent,
reminder_stage: request.reminderStage,
Expand All @@ -120,7 +128,10 @@ export const recurringSignupFromRequest = (
identity_id: identityId,
country: request.country,
reminder_frequency_months: request.reminderFrequencyMonths,
reminder_created_at: new Date().toISOString(),
reminder_created_at: (request.reminderCreatedAt
? new Date(request.reminderCreatedAt)
: new Date()
).toISOString(),
reminder_platform: request.reminderPlatform,
reminder_component: request.reminderComponent,
reminder_stage: request.reminderStage,
Expand Down