Skip to content

Commit

Permalink
only cancel future reminders
Browse files Browse the repository at this point in the history
  • Loading branch information
tompretty committed Nov 17, 2021
1 parent 6c65598 commit 0654207
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
23 changes: 22 additions & 1 deletion src/cancel-reminders/lib/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const writeRecurringReminderAndGetCode = async (
};

describe('cancelPendingReminders', () => {
const now = new Date('2021-01-01');

it('cancels all pending one-off reminders', async () => {
expect.assertions(1);

Expand All @@ -53,19 +55,37 @@ describe('cancelPendingReminders', () => {
const cancelledReminders = await cancelPendingSignups(
reminderCode,
pool,
now,
);

expect(cancelledReminders).toBe(2);
});

it('doesnt cancel reminders that have already been sent', async () => {
expect.assertions(1);

const reminder = createOneOffReminder({
reminder_period: '2020-12-01',
});
const reminderCode = await writeOneOffReminderAndGetCode(reminder);

const cancelledReminders = await cancelPendingSignups(
reminderCode,
pool,
now,
);

expect(cancelledReminders).toBe(0);
});

it('doesnt cancel reminders that have already been cancelled', async () => {
expect.assertions(1);

const r1 = createOneOffReminder({
reminder_period: '2021-01-01',
});
const reminderCode = await writeOneOffReminderAndGetCode(r1);
await cancelPendingSignups(reminderCode, pool);
await cancelPendingSignups(reminderCode, pool, now);

const r2 = createOneOffReminder({
reminder_period: '2021-02-01',
Expand All @@ -75,6 +95,7 @@ describe('cancelPendingReminders', () => {
const cancelledReminders = await cancelPendingSignups(
reminderCode,
pool,
now,
);

expect(cancelledReminders).toBe(1);
Expand Down
8 changes: 5 additions & 3 deletions src/cancel-reminders/lib/db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Pool, QueryConfig } from 'pg';
import { runWithLogging } from '../../lib/db';
import { getCurrentReminderPeriod } from '../../lib/utils';

async function getIdentityIdForReminderCode(
reminderCode: string,
Expand Down Expand Up @@ -40,11 +41,11 @@ async function getIdentityIdForReminderCode(
export async function cancelPendingSignups(
reminderCode: string,
pool: Pool,
now: Date = new Date(),
): Promise<number> {
const now = new Date();

// Find the signup for the given reminder_code, then use the identity_id to cancel all reminders for that user
const identityId = await getIdentityIdForReminderCode(reminderCode, pool);
const reminderPeriod = getCurrentReminderPeriod(now);

if (identityId !== null) {
const oneOffQuery: QueryConfig = {
Expand All @@ -56,8 +57,9 @@ export async function cancelPendingSignups(
WHERE
identity_id = $2
AND reminder_cancelled_at IS NULL
AND DATE(reminder_period) >= Date($3)
`,
values: [now.toISOString(), identityId],
values: [now.toISOString(), identityId, reminderPeriod],
};

const recurringQuery: QueryConfig = {
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/next-reminders/lib/db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Pool, QueryConfig, QueryResult } from 'pg';
import { runWithLogging } from '../../lib/db';
import { getCurrentReminderPeriod } from './utils';
import { getCurrentReminderPeriod } from '../../lib/utils';

export function getNextReminders(
pool: Pool,
Expand Down

0 comments on commit 0654207

Please sign in to comment.