|
| 1 | +import { z } from "zod" |
| 2 | + |
| 3 | +import { withRouteSpec } from "lib/middleware/index.ts" |
| 4 | + |
| 5 | +import { |
| 6 | + bridge_connected_system, |
| 7 | + type BridgeConnectedSystem, |
| 8 | +} from "lib/zod/bridge_connected_system.ts" |
| 9 | + |
| 10 | +export default withRouteSpec({ |
| 11 | + description: ` |
| 12 | + --- |
| 13 | + title: List Bridge Connected Systems |
| 14 | + response_key: bridge_connected_systems |
| 15 | + undocumented: Seam Bridge Client only. |
| 16 | + --- |
| 17 | + Returns the bridge connected systems associated with the session token used. |
| 18 | + `, |
| 19 | + auth: ["bridge_client_session"], |
| 20 | + methods: ["GET", "POST"], |
| 21 | + jsonResponse: z.object({ |
| 22 | + bridge_connected_systems: z.array(bridge_connected_system), |
| 23 | + }), |
| 24 | +} as const)(async (req, res) => { |
| 25 | + const { db, auth } = req |
| 26 | + |
| 27 | + const bridges = db.bridges.filter( |
| 28 | + (bridge) => |
| 29 | + bridge.bridge_client_session_id === |
| 30 | + auth.bridge_client_session.bridge_client_session_id, |
| 31 | + ) |
| 32 | + |
| 33 | + const bridge_connected_systems: BridgeConnectedSystem[] = bridges.map( |
| 34 | + (bridge) => { |
| 35 | + const connected_account = db.connected_accounts.find( |
| 36 | + (connected_account) => connected_account.bridge_id === bridge.bridge_id, |
| 37 | + ) |
| 38 | + |
| 39 | + if (connected_account == null) { |
| 40 | + throw new Error("Could not find connected_account for bridge.") |
| 41 | + } |
| 42 | + |
| 43 | + const acs_system = db.acs_systems.find( |
| 44 | + (acs_system) => |
| 45 | + acs_system.connected_account_id === |
| 46 | + connected_account.connected_account_id, |
| 47 | + ) |
| 48 | + |
| 49 | + if (acs_system == null) { |
| 50 | + throw new Error("Could not find acs_system for third_party_account.") |
| 51 | + } |
| 52 | + |
| 53 | + const workspace = db.workspaces.find( |
| 54 | + (workspace) => workspace.workspace_id === bridge.workspace_id, |
| 55 | + ) |
| 56 | + |
| 57 | + if (workspace == null) { |
| 58 | + throw new Error("Could not find workspace for bridge.") |
| 59 | + } |
| 60 | + |
| 61 | + return { |
| 62 | + bridge_id: bridge.bridge_id, |
| 63 | + bridge_created_at: bridge.created_at, |
| 64 | + connected_account_id: connected_account.connected_account_id, |
| 65 | + connected_account_created_at: connected_account.created_at, |
| 66 | + acs_system_id: acs_system.acs_system_id, |
| 67 | + acs_system_display_name: acs_system.name, |
| 68 | + workspace_id: bridge.workspace_id, |
| 69 | + workspace_display_name: workspace.name, |
| 70 | + } |
| 71 | + }, |
| 72 | + ) |
| 73 | + |
| 74 | + res.status(200).json({ |
| 75 | + bridge_connected_systems, |
| 76 | + }) |
| 77 | +}) |
0 commit comments