fix(notifications): take today from the instance's TZ and catch up missed trip reminders

The reminder crons run at 09:00 in the instance's TZ but computed today
in UTC, so east of Greenwich a trip reminder arrived a day late. A day
the job did not run also lost that day's trip reminders for good: the
match was 'starts exactly N days from today'. Trips now record the start
date a reminder went out for, and the job sends every reminder that is
due and not yet sent. The active-trip redirect and the upcoming
reservations use the instance's date and clock as well.
This commit is contained in:
Maurice
2026-10-07 16:16:26 +02:00
parent e0aedbb2c6
commit a8fc93dfde
10 changed files with 119 additions and 31 deletions
+2
View File
@@ -52,6 +52,7 @@ export class Trips {
feed_token?: string | null;
created_at?: string | null;
updated_at?: string | null;
reminder_sent_for?: string | null;
budget_category_order_collection = new Collection<BudgetCategoryOrder>(this);
budget_items_collection = new Collection<BudgetItems>(this);
budget_settlements_collection = new Collection<BudgetSettlements>(this);
@@ -112,6 +113,7 @@ export const TripsSchema = defineEntity({
feed_token: p.text().nullable(),
created_at: p.type(DbTimestampType).nullable().defaultRaw(`CURRENT_TIMESTAMP`).index('idx_trips_created_at'),
updated_at: p.type(DbTimestampType).nullable().defaultRaw(`CURRENT_TIMESTAMP`),
reminder_sent_for: p.text().nullable(),
budget_category_order_collection: () => p.oneToMany(BudgetCategoryOrder).mappedBy('trip').hidden(),
budget_items_collection: () => p.oneToMany(BudgetItems).mappedBy('trip').hidden(),
budget_settlements_collection: () => p.oneToMany(BudgetSettlements).mappedBy('trip').hidden(),
@@ -0,0 +1,25 @@
import { addColumnIfMissing } from '../migration-utils';
import { Migration } from '@mikro-orm/migrations';
/**
* A trip reminder remembers the start date it was sent for. The daily job
* used to match only "starts exactly N days from today", so a day the server
* was down at the job's hour lost that day's reminders for good. With the
* date on record the job sends every reminder that is due and not yet sent,
* and a trip moved to a new start date is reminded again.
*
* Trips whose reminder day already passed are marked as sent, so the first
* run after the upgrade does not repeat reminders that went out before.
*/
export class Migration20200101042100_a_trip_reminder_remembers_it_was_sent extends Migration {
override name = 'Migration20200101042100_a_trip_reminder_remembers_it_was_sent';
override async up(): Promise<void> {
await addColumnIfMissing(this, 'trips', 'reminder_sent_for', 'reminder_sent_for TEXT');
this.addSql(`
UPDATE trips SET reminder_sent_for = start_date
WHERE start_date IS NOT NULL AND reminder_days > 0
AND date(start_date, '-' || reminder_days || ' days') < date('now')
`);
}
}
@@ -34,6 +34,7 @@ export interface TripRawRow {
feed_token: string | null;
created_at: string | null;
updated_at: string | null;
reminder_sent_for: string | null;
}
const _tripRawRowKeys: AssertRowKeys<TripRawRow, Trips> = true;
@@ -76,6 +77,7 @@ interface TripSelectKyselyDB {
feed_token: string | null;
created_at: string | null;
updated_at: string | null;
reminder_sent_for: string | null;
};
users: { id: number; username: string };
trip_members: { id: number; trip_id: number; user_id: number };
@@ -882,11 +884,16 @@ export class TripsRepository extends TrekRepository<Trips> {
*/
async listReminderCandidates(): Promise<TripReminderCandidateRow[]> {
return await this.qb('t')
.select(['t.id', 't.title', 't.user', 't.reminder_days', 't.start_date'])
.select(['t.id', 't.title', 't.user', 't.reminder_days', 't.start_date', 't.reminder_sent_for'])
.andWhere({ reminder_days: { $gt: 0 }, start_date: { $ne: null } })
.execute<TripReminderCandidateRow[]>('all', false);
}
/** `UPDATE trips SET reminder_sent_for = ? WHERE id = ?`: the start date the reminder went out for. */
async markReminderSent(id: number, startDate: string): Promise<void> {
await this.nativeUpdate({ id }, { reminder_sent_for: startDate });
}
// ---------------------------------------------------------------------------
// Plan 3f Task 6 (`SystemNoticesService`) — additive.
// ---------------------------------------------------------------------------
@@ -1076,6 +1083,7 @@ export interface TripReminderCandidateRow {
user_id: number;
reminder_days: number;
start_date: string;
reminder_sent_for: string | null;
}
/** `PublicApiService.listTrips`/`getTrip`'s 8-column trip projection. */