feat(SW-353): dynamic rewards

This commit is contained in:
Christel Westerberg
2024-09-25 15:59:16 +02:00
parent 6a85cfd19c
commit 56cd02f90b
78 changed files with 1568 additions and 4587 deletions

View File

@@ -13,6 +13,7 @@ import {
import { type Context, createContext } from "./context"
import { fetchServiceToken } from "./tokenManager"
import { transformer } from "./transformer"
import { langInput } from "./utils"
import type { Session } from "next-auth"
@@ -39,7 +40,19 @@ export const { createCallerFactory, mergeRouters, router } = t
export const publicProcedure = t.procedure
export const contentstackBaseProcedure = t.procedure.use(async function (opts) {
if (!opts.ctx.lang) {
throw badRequestError("Missing Lang in tRPC context")
// When fetching data client side with TRPC we don't pass through middlewares and therefore do not get the lang through headers
// We can then pass lang as an input in the request and set it to the context in the procedure
const input = await opts.getRawInput()
const parsedInput = langInput.safeParse(input)
if (!parsedInput.success) {
throw badRequestError("Missing Lang in tRPC context")
}
return opts.next({
ctx: {
lang: parsedInput.data.lang,
},
})
}
return opts.next({
@@ -108,10 +121,10 @@ export const safeProtectedProcedure = t.procedure.use(async function (opts) {
})
})
export const serviceProcedure = t.procedure.use(async (opts) => {
const { access_token } = await fetchServiceToken()
export const profileServiceProcedure = t.procedure.use(async (opts) => {
const { access_token } = await fetchServiceToken(["profile"])
if (!access_token) {
throw internalServerError("Failed to obtain service token")
throw internalServerError("Failed to obtain profile service token")
}
return opts.next({
ctx: {
@@ -120,6 +133,17 @@ export const serviceProcedure = t.procedure.use(async (opts) => {
})
})
export const hotelServiceProcedure = t.procedure.use(async (opts) => {
const { access_token } = await fetchServiceToken(["hotel"])
if (!access_token) {
throw internalServerError("Failed to obtain hotel service token")
}
return opts.next({
ctx: {
serviceToken: access_token,
},
})
})
export const serverActionProcedure = t.procedure.experimental_caller(
experimental_nextAppDirCaller({
createContext,
@@ -149,5 +173,11 @@ export const protectedServerActionProcedure = serverActionProcedure.use(
// NOTE: This is actually save to use, just the implementation could change
// in minor version bumps. Please read: https://trpc.io/docs/faq#unstable
export const contentStackUidWithServiceProcedure =
contentstackExtendedProcedureUID.unstable_concat(serviceProcedure)
export const contentStackUidWithHotelServiceProcedure =
contentstackExtendedProcedureUID.unstable_concat(hotelServiceProcedure)
export const contentStackBaseWithProfileServiceProcedure =
contentstackBaseProcedure.unstable_concat(profileServiceProcedure)
export const contentStackBaseWithProtectedProcedure =
contentstackBaseProcedure.unstable_concat(protectedProcedure)