From 1862b16c8c5886eb457d59ab8ca6ac4b6167c7db Mon Sep 17 00:00:00 2001 From: Linus Flood Date: Mon, 16 Dec 2024 13:06:27 +0100 Subject: [PATCH] feat/membershipcard: utilize memoiozed function to fetch membership cards --- server/routers/user/query.ts | 49 ++++-------------------------------- 1 file changed, 5 insertions(+), 44 deletions(-) diff --git a/server/routers/user/query.ts b/server/routers/user/query.ts index 42d5bfa3b..8f646b947 100644 --- a/server/routers/user/query.ts +++ b/server/routers/user/query.ts @@ -778,51 +778,14 @@ export const userQueryRouter = router({ }), membershipCards: protectedProcedure.query(async function ({ ctx }) { - getProfileCounter.add(1) - console.info("api.profile start", JSON.stringify({})) - const apiResponse = await api.get(api.endpoints.v1.Profile.profile, { - cache: "no-store", - headers: { - Authorization: `Bearer ${ctx.session.token.access_token}`, - }, - }) + const userData = await getVerifiedUser({ session: ctx.session }) - if (!apiResponse.ok) { - // switch (apiResponse.status) { - // case 400: - // throw badRequestError() - // case 401: - // throw unauthorizedError() - // case 403: - // throw forbiddenError() - // default: - // throw internalServerError() - // } - const text = await apiResponse.text() - getProfileFailCounter.add(1, { - error_type: "http_error", - error: JSON.stringify({ - status: apiResponse.status, - statusText: apiResponse.statusText, - text, - }), - }) - console.error( - "api.profile error", - JSON.stringify({ - error: { - status: apiResponse.status, - statusText: apiResponse.statusText, - text, - }, - }) - ) + if (!userData || "error" in userData) { + return null } - const apiJson = await apiResponse.json() - const verifiedData = getMembershipCardsSchema.safeParse( - apiJson.data.attributes.memberships + userData.data.memberships ) if (!verifiedData.success) { @@ -837,9 +800,7 @@ export const userQueryRouter = router({ return null } getProfileSuccessCounter.add(1) - console.info("api.profile success", JSON.stringify({})) - const cards = getMembershipCards(verifiedData.data) - return cards + return getMembershipCards(verifiedData.data) }), })