Skip to content

Commit 5d4773b

Browse files
committed
Edit Consumer layout
1 parent caa059c commit 5d4773b

2 files changed

Lines changed: 391 additions & 8 deletions

File tree

src/routes/(protected)/consumers/[consumer_id]/edit/+page.server.ts

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ interface Consumer {
1919
company: string;
2020
enabled: boolean;
2121
created: string;
22+
logo_url?: string;
23+
certificate_pem?: string;
2224
created_by_user?: {
2325
user_id: string;
2426
email: string;
@@ -267,4 +269,184 @@ export const actions = {
267269
return fail(500, { error: message, action: "deleteScope" });
268270
}
269271
},
272+
273+
// Action to update consumer name
274+
updateName: async (event: RequestEvent) => {
275+
const session = event.locals.session;
276+
277+
if (!session?.data?.user) {
278+
return fail(401, { error: "Unauthorized" });
279+
}
280+
281+
const sessionOAuth = SessionOAuthHelper.getSessionOAuth(session);
282+
const token = sessionOAuth?.accessToken;
283+
284+
if (!token) {
285+
return fail(401, { error: "Unauthorized: No access token found in session." });
286+
}
287+
288+
const consumerId = event.params.consumer_id;
289+
290+
if (!consumerId) {
291+
return fail(400, { error: "Consumer ID is required." });
292+
}
293+
294+
const formData = await event.request.formData();
295+
const app_name = formData.get("app_name") as string;
296+
297+
if (!app_name?.trim()) {
298+
return fail(400, { error: "App name is required.", action: "updateName" });
299+
}
300+
301+
try {
302+
logger.info(`Updating consumer ${consumerId} name to: ${app_name}`);
303+
304+
await obp_requests.put(
305+
`/obp/v6.0.0/management/consumers/${consumerId}/consumer/name`,
306+
{ app_name },
307+
token,
308+
);
309+
310+
logger.info(`Successfully updated consumer ${consumerId} name`);
311+
312+
return { success: true, action: "updateName" };
313+
} catch (e: any) {
314+
logger.error("Error updating consumer name:", e);
315+
const { message } = extractErrorDetails(e);
316+
return fail(500, { error: message, action: "updateName" });
317+
}
318+
},
319+
320+
// Action to update consumer redirect URL
321+
updateRedirectUrl: async (event: RequestEvent) => {
322+
const session = event.locals.session;
323+
324+
if (!session?.data?.user) {
325+
return fail(401, { error: "Unauthorized" });
326+
}
327+
328+
const sessionOAuth = SessionOAuthHelper.getSessionOAuth(session);
329+
const token = sessionOAuth?.accessToken;
330+
331+
if (!token) {
332+
return fail(401, { error: "Unauthorized: No access token found in session." });
333+
}
334+
335+
const consumerId = event.params.consumer_id;
336+
337+
if (!consumerId) {
338+
return fail(400, { error: "Consumer ID is required." });
339+
}
340+
341+
const formData = await event.request.formData();
342+
const redirect_url = formData.get("redirect_url") as string;
343+
344+
if (!redirect_url?.trim()) {
345+
return fail(400, { error: "Redirect URL is required.", action: "updateRedirectUrl" });
346+
}
347+
348+
try {
349+
logger.info(`Updating consumer ${consumerId} redirect URL`);
350+
351+
await obp_requests.put(
352+
`/obp/v6.0.0/management/consumers/${consumerId}/consumer/redirect_url`,
353+
{ redirect_url },
354+
token,
355+
);
356+
357+
logger.info(`Successfully updated consumer ${consumerId} redirect URL`);
358+
359+
return { success: true, action: "updateRedirectUrl" };
360+
} catch (e: any) {
361+
logger.error("Error updating consumer redirect URL:", e);
362+
const { message } = extractErrorDetails(e);
363+
return fail(500, { error: message, action: "updateRedirectUrl" });
364+
}
365+
},
366+
367+
// Action to update consumer logo URL
368+
updateLogoUrl: async (event: RequestEvent) => {
369+
const session = event.locals.session;
370+
371+
if (!session?.data?.user) {
372+
return fail(401, { error: "Unauthorized" });
373+
}
374+
375+
const sessionOAuth = SessionOAuthHelper.getSessionOAuth(session);
376+
const token = sessionOAuth?.accessToken;
377+
378+
if (!token) {
379+
return fail(401, { error: "Unauthorized: No access token found in session." });
380+
}
381+
382+
const consumerId = event.params.consumer_id;
383+
384+
if (!consumerId) {
385+
return fail(400, { error: "Consumer ID is required." });
386+
}
387+
388+
const formData = await event.request.formData();
389+
const logo_url = formData.get("logo_url") as string || "";
390+
391+
try {
392+
logger.info(`Updating consumer ${consumerId} logo URL`);
393+
394+
await obp_requests.put(
395+
`/obp/v6.0.0/management/consumers/${consumerId}/consumer/logo_url`,
396+
{ logo_url },
397+
token,
398+
);
399+
400+
logger.info(`Successfully updated consumer ${consumerId} logo URL`);
401+
402+
return { success: true, action: "updateLogoUrl" };
403+
} catch (e: any) {
404+
logger.error("Error updating consumer logo URL:", e);
405+
const { message } = extractErrorDetails(e);
406+
return fail(500, { error: message, action: "updateLogoUrl" });
407+
}
408+
},
409+
410+
// Action to update consumer certificate
411+
updateCertificate: async (event: RequestEvent) => {
412+
const session = event.locals.session;
413+
414+
if (!session?.data?.user) {
415+
return fail(401, { error: "Unauthorized" });
416+
}
417+
418+
const sessionOAuth = SessionOAuthHelper.getSessionOAuth(session);
419+
const token = sessionOAuth?.accessToken;
420+
421+
if (!token) {
422+
return fail(401, { error: "Unauthorized: No access token found in session." });
423+
}
424+
425+
const consumerId = event.params.consumer_id;
426+
427+
if (!consumerId) {
428+
return fail(400, { error: "Consumer ID is required." });
429+
}
430+
431+
const formData = await event.request.formData();
432+
const certificate_pem = formData.get("certificate_pem") as string || "";
433+
434+
try {
435+
logger.info(`Updating consumer ${consumerId} certificate`);
436+
437+
await obp_requests.put(
438+
`/obp/v6.0.0/management/consumers/${consumerId}/consumer/certificate`,
439+
{ certificate_pem },
440+
token,
441+
);
442+
443+
logger.info(`Successfully updated consumer ${consumerId} certificate`);
444+
445+
return { success: true, action: "updateCertificate" };
446+
} catch (e: any) {
447+
logger.error("Error updating consumer certificate:", e);
448+
const { message } = extractErrorDetails(e);
449+
return fail(500, { error: message, action: "updateCertificate" });
450+
}
451+
},
270452
};

0 commit comments

Comments
 (0)