diff --git a/backend/src/utils/tenantFullExport.ts b/backend/src/utils/tenantFullExport.ts index 810023d..f032169 100644 --- a/backend/src/utils/tenantFullExport.ts +++ b/backend/src/utils/tenantFullExport.ts @@ -414,6 +414,45 @@ const prepareCommunicationRoomsForImport = (exportData: TenantFullExport) => { } } +const cleanupImportedCommunicationRooms = async (client: any, exportData: TenantFullExport) => { + const rows = exportData.tables.communication_rooms || [] + if (!rows.length) return 0 + + const tenantById = new Map((exportData.tables.tenants || []).map((tenant) => [ + Number(tenant.id), + { + id: Number(tenant.id), + name: tenant.name, + short: tenant.short, + }, + ])) + let cleaned = 0 + + for (const row of rows) { + const tenantId = Number(row.tenant_id) + const key = String(row.key || "") + const tenant = tenantById.get(tenantId) + if (!tenantId || !key || !tenant) continue + + const alias = tenantRoomAlias(tenant, key) + const result = await client.query( + ` + update communication_rooms + set matrix_room_id = null, + parent_space_room_id = null, + matrix_alias = $3, + updated_at = now() + where tenant_id = $1 and key = $2 + `, + [tenantId, key, alias] + ) + + cleaned += result.rowCount || 0 + } + + return cleaned +} + const prepareColumnValue = (value: any, isJsonColumn: boolean) => { if (!isJsonColumn || value === null || typeof value === "undefined") return value if (typeof value === "string") return value @@ -585,6 +624,11 @@ export const importTenantFullExport = async ( importedTables.push({ table, rows: count }) } + const cleanedCommunicationRooms = await cleanupImportedCommunicationRooms(client, exportData) + if (cleanedCommunicationRooms) { + importedTables.push({ table: "communication_rooms_matrix_reset", rows: cleanedCommunicationRooms }) + } + await refreshSequences(client, columnsByTable) await client.query("commit")