From c43b6ccf57b2c41479ef12d527c88faaee4e5ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Gonz=C3=A1lez=20Rubio?= Date: Thu, 23 Jan 2025 22:28:43 +0100 Subject: [PATCH] fix: removed invalid connection from null hotels - fix #263 (#264) * fix: accountId is undefined on connections - fix #263 * fix: removed invalid connection from null hotels - fix #263 * fix: removed invalid connection from null hotels - fix #263 --- ...22-00-remove-invalid-connections.migration.ts | 16 ++++++++++++++++ app/server/src/modules/migrations/main.ts | 1 + app/server/src/modules/system/hotels.ts | 12 ++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 app/server/src/modules/migrations/2025-01-23--22-00-remove-invalid-connections.migration.ts diff --git a/app/server/src/modules/migrations/2025-01-23--22-00-remove-invalid-connections.migration.ts b/app/server/src/modules/migrations/2025-01-23--22-00-remove-invalid-connections.migration.ts new file mode 100644 index 0000000..26e2252 --- /dev/null +++ b/app/server/src/modules/migrations/2025-01-23--22-00-remove-invalid-connections.migration.ts @@ -0,0 +1,16 @@ +import { Migration, DbMutable } from "@oh/utils"; + +export default { + id: "2025-01-23--22-00-remove-invalid-connections", + description: `remove invalid 'connections' from null hotels`, + up: async (db: DbMutable) => { + const connections = await db.list({ + prefix: ["integrationsByAccountId"], + }); + for await (const connection of connections) { + const hotel = await db.get(["hotels", connection.value.hotelId]); + if (!hotel) await db.delete(connection.key); + } + }, + down: async (db: DbMutable) => {}, +} as Migration; diff --git a/app/server/src/modules/migrations/main.ts b/app/server/src/modules/migrations/main.ts index bdf6373..c3b3852 100644 --- a/app/server/src/modules/migrations/main.ts +++ b/app/server/src/modules/migrations/main.ts @@ -12,6 +12,7 @@ const MIGRATION_LIST = [ await import( "./2025-01-23--15-15-remove-integrations-by-hotels-by-account-id.migration.ts" ), + await import("./2025-01-23--22-00-remove-invalid-connections.migration.ts"), ]; export const Migrations = (() => { diff --git a/app/server/src/modules/system/hotels.ts b/app/server/src/modules/system/hotels.ts index 50f015e..10aac43 100644 --- a/app/server/src/modules/system/hotels.ts +++ b/app/server/src/modules/system/hotels.ts @@ -14,7 +14,7 @@ export const hotels = () => { ) => { return ( await System.db.list({ - prefix: ["integrationsByHotelsByAccountId"], + prefix: ["integrationsByAccountId"], }) ).filter(({ key }) => key[2] === hotelId && key[3] === integrationId); }; @@ -100,8 +100,16 @@ export const hotels = () => { hotelsIdList.filter(($hotelId: string) => hotelId !== $hotelId), ); - for (const { integrationId } of hotel.integrations) + for (const { integrationId } of hotel.integrations) { + const accounts = await getAccountsByIntegrationId(hotelId, integrationId); + + for (const { + value: { accountId }, + } of accounts) + await System.connections.remove(accountId, hotelId, integrationId); + await System.db.delete(["hotelsByConnectId", integrationId]); + } await System.db.delete(["hotels", hotelId]); };