fix(airports): use Bahrain time for Bahrain flights

Correct the airport data and repair affected flight endpoints and metadata during upgrade. Preserve stored local flight times and legacy metadata encoding.

Closes #3294
This commit is contained in:
Maurice
2026-12-02 09:45:20 +01:00
parent d6184559e9
commit 17c4d69e40
6 changed files with 141 additions and 6 deletions
@@ -0,0 +1,69 @@
import { forEachInOrder } from '../../utils/for-each-in-order';
import { Migration } from '@mikro-orm/migrations';
const WRONG_ZONE = 'Asia/Tehran';
const BAHRAIN_ZONE = 'Asia/Bahrain';
const BAHRAIN_CODES = ['BAH', 'OBBI'] as const;
function correctedMetadata(text: string): string | null {
let value: unknown;
let encodedAgain = false;
try {
value = JSON.parse(text);
// Older bookings could store a JSON string containing the metadata JSON.
if (typeof value === 'string') {
encodedAgain = true;
value = JSON.parse(value);
}
} catch {
return null;
}
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
const metadata = value as Record<string, unknown>;
let changed = false;
for (const side of ['departure', 'arrival'] as const) {
const airport = metadata[`${side}_airport`];
const timezone = `${side}_timezone`;
if (
typeof airport === 'string' &&
BAHRAIN_CODES.some((code) => code === airport.trim().toUpperCase()) &&
metadata[timezone] === WRONG_ZONE
) {
metadata[timezone] = BAHRAIN_ZONE;
changed = true;
}
}
if (!changed) return null;
const json = JSON.stringify(metadata);
return encodedAgain ? JSON.stringify(json) : json;
}
/**
* The airport dataset used the wrong zone for Bahrain. Repair its exact persisted
* airport/zone pair in flight endpoints and metadata, including legacy metadata-only
* bookings. The stored wall-clock times are already local and need no rewrite.
*/
export class Migration20200101046640_bahrain_flights_keep_bahrain_time extends Migration {
override name = 'Migration20200101046640_bahrain_flights_keep_bahrain_time';
override async up(): Promise<void> {
await this.execute(
`UPDATE reservation_endpoints SET timezone = ?
WHERE timezone = ? AND UPPER(TRIM(code)) IN (?, ?)
AND reservation_id IN (SELECT id FROM reservations WHERE type = 'flight')`,
[BAHRAIN_ZONE, WRONG_ZONE, ...BAHRAIN_CODES],
);
const rows = (await this.execute(
"SELECT id, metadata FROM reservations WHERE type = 'flight' AND metadata LIKE ?",
[`%${WRONG_ZONE}%`],
)) as { id: number; metadata: string }[];
await forEachInOrder(rows, async ({ id, metadata }) => {
const corrected = correctedMetadata(metadata);
if (corrected !== null) {
await this.execute('UPDATE reservations SET metadata = ? WHERE id = ?', [corrected, id]);
}
});
}
}