Skip to content

Commit 17ed89e

Browse files
committed
feat: enhance environment handling in useEnvironments and HttpDispatcher with organization context
1 parent 269df04 commit 17ed89e

2 files changed

Lines changed: 38 additions & 13 deletions

File tree

apps/studio/src/hooks/useEnvironments.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import { useCallback, useEffect, useState } from 'react';
1515
import { useClient } from '@objectstack/client-react';
1616
import type { Environment, EnvironmentDatabase, EnvironmentMember } from '@objectstack/spec/cloud';
17+
import { useActiveOrganizationId } from '@/hooks/useSession';
1718

1819
export interface EnvironmentDetail {
1920
environment: Environment;
@@ -49,24 +50,29 @@ export function recallActiveEnvironment(): string | null {
4950
*/
5051
export function useEnvironments() {
5152
const client = useClient() as any; // ObjectStackClient — typed as any to avoid export shape coupling.
53+
const activeOrgId = useActiveOrganizationId();
5254
const [environments, setEnvironments] = useState<Environment[]>([]);
5355
const [loading, setLoading] = useState(false);
5456
const [error, setError] = useState<Error | null>(null);
5557

5658
const load = useCallback(async () => {
5759
if (!client?.environments) return;
60+
if (!activeOrgId) {
61+
setEnvironments([]);
62+
return;
63+
}
5864
setLoading(true);
5965
setError(null);
6066
try {
61-
const result = await client.environments.list();
67+
const result = await client.environments.list({ organizationId: activeOrgId });
6268
setEnvironments((result?.environments as Environment[]) ?? []);
6369
} catch (err) {
6470
setError(err as Error);
6571
setEnvironments([]);
6672
} finally {
6773
setLoading(false);
6874
}
69-
}, [client]);
75+
}, [client, activeOrgId]);
7076

7177
useEffect(() => {
7278
let alive = true;

packages/runtime/src/http-dispatcher.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,25 @@ export class HttpDispatcher {
10401040
return rows[0];
10411041
};
10421042

1043+
const toEnvironmentDto = (row: any): any => {
1044+
if (!row) return row;
1045+
return {
1046+
id: row.id,
1047+
organizationId: row.organization_id,
1048+
slug: row.slug,
1049+
displayName: row.display_name,
1050+
envType: row.env_type,
1051+
isDefault: row.is_default ?? false,
1052+
region: row.region,
1053+
plan: row.plan,
1054+
status: row.status,
1055+
createdBy: row.created_by,
1056+
metadata: row.metadata,
1057+
createdAt: row.created_at,
1058+
updatedAt: row.updated_at,
1059+
};
1060+
};
1061+
10431062
try {
10441063
// ----- /cloud/drivers ------------------------------------------
10451064
if (parts.length === 1 && parts[0] === 'drivers' && m === 'GET') {
@@ -1055,7 +1074,7 @@ export class HttpDispatcher {
10551074
if (query?.status) where.status = query.status;
10561075
let rows = await ql.find(ENV, Object.keys(where).length ? ({ where } as any) : undefined);
10571076
if (rows && (rows as any).value) rows = (rows as any).value;
1058-
const environments = Array.isArray(rows) ? rows : [];
1077+
const environments = (Array.isArray(rows) ? rows : []).map(toEnvironmentDto);
10591078
return { handled: true, response: this.success({ environments, total: environments.length }) };
10601079
}
10611080

@@ -1156,7 +1175,7 @@ export class HttpDispatcher {
11561175
updated_at: nowIso,
11571176
});
11581177

1159-
const environment = await findOne(ENV, { id: environmentId });
1178+
const environment = toEnvironmentDto(await findOne(ENV, { id: environmentId }));
11601179
const database = await findOne(DB, { id: environmentDatabaseId });
11611180
const res = this.success({ environment, database });
11621181
res.status = 201;
@@ -1168,8 +1187,8 @@ export class HttpDispatcher {
11681187
const id = decodeURIComponent(parts[1]);
11691188

11701189
if (m === 'GET') {
1171-
const environment = await findOne(ENV, { id });
1172-
if (!environment) return { handled: true, response: this.error(`Environment '${id}' not found`, 404) };
1190+
const envRow = await findOne(ENV, { id });
1191+
if (!envRow) return { handled: true, response: this.error(`Environment '${id}' not found`, 404) };
11731192
const database = await findOne(DB, { environment_id: id });
11741193
const credential = database
11751194
? await findOne(CRED, { environment_database_id: database.id, status: 'active' })
@@ -1187,7 +1206,7 @@ export class HttpDispatcher {
11871206
: undefined;
11881207
return {
11891208
handled: true,
1190-
response: this.success({ environment, database, credential: credMeta, membership }),
1209+
response: this.success({ environment: toEnvironmentDto(envRow), database, credential: credMeta, membership }),
11911210
};
11921211
}
11931212

@@ -1200,19 +1219,19 @@ export class HttpDispatcher {
12001219
if (body?.metadata !== undefined) patch.metadata = JSON.stringify(body.metadata);
12011220
patch.updated_at = new Date().toISOString();
12021221
await ql.update(ENV, patch, { where: { id } } as any);
1203-
const environment = await findOne(ENV, { id });
1204-
if (!environment) return { handled: true, response: this.error(`Environment '${id}' not found`, 404) };
1205-
return { handled: true, response: this.success({ environment }) };
1222+
const envRow = await findOne(ENV, { id });
1223+
if (!envRow) return { handled: true, response: this.error(`Environment '${id}' not found`, 404) };
1224+
return { handled: true, response: this.success({ environment: toEnvironmentDto(envRow) }) };
12061225
}
12071226
}
12081227

12091228
// ----- /cloud/environments/:id/activate -----
12101229
if (parts.length === 3 && parts[0] === 'environments' && parts[2] === 'activate' && m === 'POST') {
12111230
const id = decodeURIComponent(parts[1]);
1212-
const environment = await findOne(ENV, { id });
1213-
if (!environment) return { handled: true, response: this.error(`Environment '${id}' not found`, 404) };
1231+
const envRow = await findOne(ENV, { id });
1232+
if (!envRow) return { handled: true, response: this.error(`Environment '${id}' not found`, 404) };
12141233
// TODO: persist active_environment_id on the session once session service is wired.
1215-
return { handled: true, response: this.success({ environment, sessionUpdated: false }) };
1234+
return { handled: true, response: this.success({ environment: toEnvironmentDto(envRow), sessionUpdated: false }) };
12161235
}
12171236

12181237
// ----- /cloud/environments/:id/credentials/rotate -----

0 commit comments

Comments
 (0)