Skip to content

Commit 82966b2

Browse files
committed
Dynamic Entities something
1 parent 2fbeae9 commit 82966b2

File tree

5 files changed

+271
-202
lines changed

5 files changed

+271
-202
lines changed

src/routes/(protected)/dynamic-entities/+page.server.ts

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,69 +17,42 @@ export const load: PageServerLoad = async ({ locals }) => {
1717
const sessionOAuth = SessionOAuthHelper.getSessionOAuth(session);
1818
const accessToken = sessionOAuth?.accessToken;
1919

20-
// Get user entitlements from session for role checking
21-
const userEntitlements = (session.data.user as any)?.entitlements?.list || [];
22-
23-
// Define required roles for viewing dynamic entities
24-
const requiredRoles = [
25-
{
26-
role: "CanGetDynamicEntity",
27-
description: "View dynamic entities",
28-
action: "view dynamic entities",
29-
},
30-
];
31-
3220
if (!accessToken) {
3321
logger.warn("No access token available for dynamic entities page");
34-
return {
35-
userEntitlements,
36-
requiredRoles,
37-
hasApiAccess: false,
38-
dynamicEntities: [],
39-
banks: [],
40-
error: "No API access token available",
41-
};
22+
throw error(401, "No API access token available");
4223
}
4324

44-
// Fetch available banks for filtering
45-
let banks = [];
4625
try {
47-
const banksResponse = await obp_requests.get(
48-
"/obp/v6.0.0/banks",
26+
// Fetch dynamic entities from OBP API
27+
logger.info("Fetching dynamic entities...");
28+
const entitiesResponse = await obp_requests.get(
29+
"/obp/v6.0.0/management/dynamic-entities",
4930
accessToken,
5031
);
51-
banks = banksResponse.banks || [];
52-
} catch (err) {
53-
logger.error("Error fetching banks:", err);
54-
}
32+
const entities = entitiesResponse.dynamic_entities || [];
33+
logger.info(`Found ${entities.length} dynamic entities`);
5534

56-
// Fetch dynamic entities
57-
let dynamicEntities = [];
58-
try {
59-
logger.info("Fetching dynamic entities...");
60-
const response = await obp_requests.get(
61-
"/obp/v6.0.0/management/dynamic-entities",
35+
// Fetch dynamic entity definitions from OBP API
36+
logger.info("Fetching dynamic entity definitions...");
37+
const definitionsResponse = await obp_requests.get(
38+
"/obp/v6.0.0/management/dynamic-entity-definitions",
6239
accessToken,
6340
);
64-
dynamicEntities = response.dynamic_entities || [];
65-
logger.info(`Found ${dynamicEntities.length} dynamic entities`);
41+
const definitions = definitionsResponse.dynamic_entity_definitions || [];
42+
logger.info(`Found ${definitions.length} dynamic entity definitions`);
43+
44+
return {
45+
entities,
46+
definitions,
47+
};
6648
} catch (err) {
67-
logger.error("Error fetching dynamic entities:", err);
49+
logger.error("Error fetching dynamic entities data:", err);
50+
// Return empty arrays instead of throwing error to allow page to render
6851
return {
69-
userEntitlements,
70-
requiredRoles,
71-
hasApiAccess: true,
72-
dynamicEntities: [],
73-
banks,
74-
error: err instanceof Error ? err.message : "Failed to fetch dynamic entities",
52+
entities: [],
53+
definitions: [],
54+
error:
55+
err instanceof Error ? err.message : "Failed to fetch dynamic entities",
7556
};
7657
}
77-
78-
return {
79-
userEntitlements,
80-
requiredRoles,
81-
hasApiAccess: true,
82-
dynamicEntities,
83-
banks,
84-
};
8558
};
Lines changed: 67 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,70 @@
1-
import type { PageServerLoad } from './$types';
2-
import { error } from '@sveltejs/kit';
1+
import type { PageServerLoad } from "./$types";
2+
import { error } from "@sveltejs/kit";
3+
import { createLogger } from "$lib/utils/logger";
4+
import { SessionOAuthHelper } from "$lib/oauth/sessionHelper";
5+
import { obp_requests } from "$lib/obp/requests";
6+
7+
const logger = createLogger("DynamicEntityViewPageServer");
38

49
export const load: PageServerLoad = async ({ locals, params }) => {
5-
const supabase = locals.supabase;
6-
const entityId = params.id;
7-
8-
try {
9-
// Fetch the dynamic entity
10-
const { data: entity, error: entityError } = await supabase
11-
.from('dynamic_entities')
12-
.select('*')
13-
.eq('id', entityId)
14-
.single();
15-
16-
if (entityError || !entity) {
17-
console.error('Error fetching entity:', entityError);
18-
throw error(404, 'Entity not found');
19-
}
20-
21-
// Fetch the entity's definition
22-
const { data: definition, error: definitionError } = await supabase
23-
.from('dynamic_entity_definitions')
24-
.select('*')
25-
.eq('id', entity.definition_id)
26-
.single();
27-
28-
if (definitionError || !definition) {
29-
console.error('Error fetching definition:', definitionError);
30-
throw error(500, 'Failed to load entity definition');
31-
}
32-
33-
return {
34-
entity,
35-
definition
36-
};
37-
} catch (err) {
38-
console.error('Load error:', err);
39-
if (err && typeof err === 'object' && 'status' in err) {
40-
throw err;
41-
}
42-
throw error(500, 'Failed to load entity data');
43-
}
10+
const session = locals.session;
11+
const entityId = params.id;
12+
13+
if (!session?.data?.user) {
14+
throw error(401, "Unauthorized");
15+
}
16+
17+
// Get the OAuth session data
18+
const sessionOAuth = SessionOAuthHelper.getSessionOAuth(session);
19+
const accessToken = sessionOAuth?.accessToken;
20+
21+
if (!accessToken) {
22+
logger.warn("No access token available for dynamic entity view page");
23+
throw error(401, "No API access token available");
24+
}
25+
26+
try {
27+
// Fetch the specific dynamic entity
28+
logger.info(`Fetching dynamic entity: ${entityId}`);
29+
const entityResponse = await obp_requests.get(
30+
`/obp/v6.0.0/management/dynamic-entities/${entityId}`,
31+
accessToken,
32+
);
33+
const entity = entityResponse;
34+
35+
if (!entity) {
36+
throw error(404, "Entity not found");
37+
}
38+
39+
// Get the definition ID from the entity
40+
const definitionId = entity.dynamic_entity_id || entity.definition_id;
41+
42+
if (!definitionId) {
43+
logger.error("Entity missing definition ID");
44+
throw error(500, "Entity is missing definition information");
45+
}
46+
47+
// Fetch the entity's definition
48+
logger.info(`Fetching entity definition: ${definitionId}`);
49+
const definitionResponse = await obp_requests.get(
50+
`/obp/v6.0.0/management/dynamic-entity-definitions/${definitionId}`,
51+
accessToken,
52+
);
53+
const definition = definitionResponse;
54+
55+
if (!definition) {
56+
throw error(404, "Entity definition not found");
57+
}
58+
59+
return {
60+
entity,
61+
definition,
62+
};
63+
} catch (err) {
64+
logger.error("Error fetching entity:", err);
65+
if (err && typeof err === "object" && "status" in err) {
66+
throw err;
67+
}
68+
throw error(500, "Failed to load entity data");
69+
}
4470
};
Lines changed: 67 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,70 @@
1-
import type { PageServerLoad } from './$types';
2-
import { error } from '@sveltejs/kit';
1+
import type { PageServerLoad } from "./$types";
2+
import { error } from "@sveltejs/kit";
3+
import { createLogger } from "$lib/utils/logger";
4+
import { SessionOAuthHelper } from "$lib/oauth/sessionHelper";
5+
import { obp_requests } from "$lib/obp/requests";
6+
7+
const logger = createLogger("DynamicEntityEditPageServer");
38

49
export const load: PageServerLoad = async ({ locals, params }) => {
5-
const supabase = locals.supabase;
6-
const entityId = params.id;
7-
8-
try {
9-
// Fetch the dynamic entity
10-
const { data: entity, error: entityError } = await supabase
11-
.from('dynamic_entities')
12-
.select('*')
13-
.eq('id', entityId)
14-
.single();
15-
16-
if (entityError || !entity) {
17-
console.error('Error fetching entity:', entityError);
18-
throw error(404, 'Entity not found');
19-
}
20-
21-
// Fetch the entity's definition
22-
const { data: definition, error: definitionError } = await supabase
23-
.from('dynamic_entity_definitions')
24-
.select('*')
25-
.eq('id', entity.definition_id)
26-
.single();
27-
28-
if (definitionError || !definition) {
29-
console.error('Error fetching definition:', definitionError);
30-
throw error(500, 'Failed to load entity definition');
31-
}
32-
33-
return {
34-
entity,
35-
definition
36-
};
37-
} catch (err) {
38-
console.error('Load error:', err);
39-
if (err && typeof err === 'object' && 'status' in err) {
40-
throw err;
41-
}
42-
throw error(500, 'Failed to load entity data');
43-
}
10+
const session = locals.session;
11+
const entityId = params.id;
12+
13+
if (!session?.data?.user) {
14+
throw error(401, "Unauthorized");
15+
}
16+
17+
// Get the OAuth session data
18+
const sessionOAuth = SessionOAuthHelper.getSessionOAuth(session);
19+
const accessToken = sessionOAuth?.accessToken;
20+
21+
if (!accessToken) {
22+
logger.warn("No access token available for dynamic entity edit page");
23+
throw error(401, "No API access token available");
24+
}
25+
26+
try {
27+
// Fetch the specific dynamic entity
28+
logger.info(`Fetching dynamic entity: ${entityId}`);
29+
const entityResponse = await obp_requests.get(
30+
`/obp/v6.0.0/management/dynamic-entities/${entityId}`,
31+
accessToken,
32+
);
33+
const entity = entityResponse;
34+
35+
if (!entity) {
36+
throw error(404, "Entity not found");
37+
}
38+
39+
// Get the definition ID from the entity
40+
const definitionId = entity.dynamic_entity_id || entity.definition_id;
41+
42+
if (!definitionId) {
43+
logger.error("Entity missing definition ID");
44+
throw error(500, "Entity is missing definition information");
45+
}
46+
47+
// Fetch the entity's definition
48+
logger.info(`Fetching entity definition: ${definitionId}`);
49+
const definitionResponse = await obp_requests.get(
50+
`/obp/v6.0.0/management/dynamic-entity-definitions/${definitionId}`,
51+
accessToken,
52+
);
53+
const definition = definitionResponse;
54+
55+
if (!definition) {
56+
throw error(404, "Entity definition not found");
57+
}
58+
59+
return {
60+
entity,
61+
definition,
62+
};
63+
} catch (err) {
64+
logger.error("Error fetching entity:", err);
65+
if (err && typeof err === "object" && "status" in err) {
66+
throw err;
67+
}
68+
throw error(500, "Failed to load entity data");
69+
}
4470
};
Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,48 @@
1-
import type { PageServerLoad } from './$types';
2-
import { error } from '@sveltejs/kit';
1+
import type { PageServerLoad } from "./$types";
2+
import { error } from "@sveltejs/kit";
3+
import { createLogger } from "$lib/utils/logger";
4+
import { SessionOAuthHelper } from "$lib/oauth/sessionHelper";
5+
import { obp_requests } from "$lib/obp/requests";
36

4-
export const load: PageServerLoad = async ({ locals, fetch }) => {
5-
const supabase = locals.supabase;
7+
const logger = createLogger("DynamicEntitiesCreatePageServer");
68

7-
try {
8-
// Fetch all dynamic entity definitions
9-
const { data: definitions, error: definitionsError } = await supabase
10-
.from('dynamic_entity_definitions')
11-
.select('*')
12-
.order('name');
9+
export const load: PageServerLoad = async ({ locals }) => {
10+
const session = locals.session;
1311

14-
if (definitionsError) {
15-
console.error('Error fetching definitions:', definitionsError);
16-
throw error(500, 'Failed to load entity definitions');
17-
}
12+
if (!session?.data?.user) {
13+
throw error(401, "Unauthorized");
14+
}
1815

19-
return {
20-
definitions: definitions || []
21-
};
22-
} catch (err) {
23-
console.error('Load error:', err);
24-
throw error(500, 'Failed to load page data');
25-
}
16+
// Get the OAuth session data
17+
const sessionOAuth = SessionOAuthHelper.getSessionOAuth(session);
18+
const accessToken = sessionOAuth?.accessToken;
19+
20+
if (!accessToken) {
21+
logger.warn("No access token available for dynamic entities create page");
22+
throw error(401, "No API access token available");
23+
}
24+
25+
try {
26+
// Fetch all dynamic entity definitions
27+
logger.info("Fetching dynamic entity definitions...");
28+
const response = await obp_requests.get(
29+
"/obp/v6.0.0/management/dynamic-entity-definitions",
30+
accessToken,
31+
);
32+
const definitions = response.dynamic_entity_definitions || [];
33+
logger.info(`Found ${definitions.length} entity definitions`);
34+
35+
return {
36+
definitions,
37+
};
38+
} catch (err) {
39+
logger.error("Error fetching entity definitions:", err);
40+
return {
41+
definitions: [],
42+
error:
43+
err instanceof Error
44+
? err.message
45+
: "Failed to fetch entity definitions",
46+
};
47+
}
2648
};

0 commit comments

Comments
 (0)