|
| 1 | +import type { PageServerLoad } from "./$types"; |
| 2 | +import { error } from "@sveltejs/kit"; |
| 3 | +import { createLogger } from "$lib/utils/logger"; |
| 4 | +import { obp_requests } from "$lib/obp/requests"; |
| 5 | +import { SessionOAuthHelper } from "$lib/oauth/sessionHelper"; |
| 6 | + |
| 7 | +const logger = createLogger("CustomViewsPageServer"); |
| 8 | + |
| 9 | +interface CustomView { |
| 10 | + id: string; |
| 11 | + short_name: string; |
| 12 | + description: string; |
| 13 | + is_public: boolean; |
| 14 | + alias?: string; |
| 15 | + hide_metadata_if_alias_used?: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +interface ViewsResponse { |
| 19 | + views: CustomView[]; |
| 20 | +} |
| 21 | + |
| 22 | +export const load: PageServerLoad = async ({ locals }) => { |
| 23 | + const session = locals.session; |
| 24 | + |
| 25 | + if (!session?.data?.user) { |
| 26 | + throw error(401, "Unauthorized"); |
| 27 | + } |
| 28 | + |
| 29 | + // Get the OAuth session data |
| 30 | + const sessionOAuth = SessionOAuthHelper.getSessionOAuth(session); |
| 31 | + const accessToken = sessionOAuth?.accessToken; |
| 32 | + |
| 33 | + if (!accessToken) { |
| 34 | + logger.warn("No access token available for custom views page"); |
| 35 | + return { |
| 36 | + views: [], |
| 37 | + hasApiAccess: false, |
| 38 | + error: "No API access token available", |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + logger.info("=== FETCHING CUSTOM VIEWS ==="); |
| 44 | + const endpoint = `/obp/v6.0.0/management/custom-views`; |
| 45 | + logger.info(`Request: ${endpoint}`); |
| 46 | + |
| 47 | + const response: ViewsResponse = await obp_requests.get( |
| 48 | + endpoint, |
| 49 | + accessToken, |
| 50 | + ); |
| 51 | + |
| 52 | + logger.info(`Response received with ${response.views?.length || 0} views`); |
| 53 | + |
| 54 | + if (response.views) { |
| 55 | + logger.info(`First view sample: ${JSON.stringify(response.views[0])}`); |
| 56 | + } |
| 57 | + |
| 58 | + return { |
| 59 | + views: response.views || [], |
| 60 | + hasApiAccess: true, |
| 61 | + }; |
| 62 | + } catch (err) { |
| 63 | + logger.error("Error loading custom views:", err); |
| 64 | + logger.error( |
| 65 | + `Error message: ${err instanceof Error ? err.message : String(err)}`, |
| 66 | + ); |
| 67 | + |
| 68 | + return { |
| 69 | + views: [], |
| 70 | + hasApiAccess: true, |
| 71 | + error: err instanceof Error ? err.message : "Failed to load custom views", |
| 72 | + }; |
| 73 | + } |
| 74 | +}; |
0 commit comments