Files
web/server/routers/user/utils.ts
Michael Zetterberg 4a846540c3 feat: improve handling of deployment env vars
These are now defined in Netlify UI for dedicated environments (test, stage, production):

AUTH_URL
NEXTAUTH_URL
PUBLIC_URL

Code now falls back to incoming request host. Mainly used for
deployment previews which do not have Akamai in front, meaning
we do not need the above workaround as incoming request host
matches the actual public facing host. When Akamai is in front,
we lose the public facing host in Netlify's routing layer as they
internally use `x-forwarded-for` and we can't claim it for our usage.
2024-10-15 17:03:36 +02:00

114 lines
3.3 KiB
TypeScript

import { metrics } from "@opentelemetry/api"
import { Lang } from "@/constants/languages"
import { env } from "@/env/server"
import * as api from "@/lib/api"
import encryptValue from "../utils/encryptValue"
import type { FriendTransaction, Stay } from "./output"
const meter = metrics.getMeter("trpc.user")
const getProfileCounter = meter.createCounter("trpc.user.profile")
const getProfileSuccessCounter = meter.createCounter(
"trpc.user.profile-success"
)
const getProfileFailCounter = meter.createCounter("trpc.user.profile-fail")
async function updateStaysBookingUrl(
data: Stay[],
token: string,
lang: Lang
): Promise<Stay[]>
async function updateStaysBookingUrl(
data: FriendTransaction[],
token: string,
lang: Lang
): Promise<FriendTransaction[]>
async function updateStaysBookingUrl(
data: Stay[] | FriendTransaction[],
token: string,
lang: Lang
) {
// Temporary API call needed till we have user name in ctx session data
getProfileCounter.add(1)
console.info("api.user.profile updatebookingurl start", JSON.stringify({}))
const apiResponse = await api.get(api.endpoints.v1.profile, {
cache: "no-store",
headers: {
Authorization: `Bearer ${token}`,
},
})
// Temporary Url, domain and lang support for current web
const bookingUrl = new URL(
"/hotelreservation/my-booking",
env.PUBLIC_URL || "https://www.scandichotels.com" // fallback to production for ephemeral envs (like deploy previews)
)
switch (lang) {
case Lang.sv:
bookingUrl.host = bookingUrl.host.replace(".com", ".se")
bookingUrl.pathname = "/hotelreservation/din-bokning"
break
case Lang.no:
bookingUrl.host = bookingUrl.host.replace(".com", ".no")
bookingUrl.pathname = "/hotelreservation/my-booking"
break
case Lang.da:
bookingUrl.host = bookingUrl.host.replace(".com", ".dk")
bookingUrl.pathname = "/hotelreservation/min-booking"
break
case Lang.fi:
bookingUrl.host = bookingUrl.host.replace(".com", ".fi")
bookingUrl.pathname = "/varaa-hotelli/varauksesi"
break
case Lang.de:
bookingUrl.host = bookingUrl.host.replace(".com", ".de")
bookingUrl.pathname = "/hotelreservation/my-booking"
break
default:
break
}
if (!apiResponse.ok) {
getProfileFailCounter.add(1, { error: JSON.stringify(apiResponse) })
console.info(
"api.user.profile updatebookingurl error",
JSON.stringify({ error: apiResponse })
)
return data
}
const apiJson = await apiResponse.json()
if (!apiJson.data?.attributes) {
return data
}
getProfileSuccessCounter.add(1)
console.info("api.user.profile updatebookingurl success", JSON.stringify({}))
return data.map((d) => {
const originalString =
d.attributes.confirmationNumber.toString() +
"," +
apiJson.data.attributes.lastName
const encryptedBookingValue = encryptValue(originalString)
if (!!encryptedBookingValue) {
bookingUrl.searchParams.set("RefId", encryptedBookingValue)
} else {
bookingUrl.searchParams.set("lastName", apiJson.data.attributes.lastName)
bookingUrl.searchParams.set("bookingId", d.attributes.confirmationNumber)
}
return {
...d,
attributes: {
...d.attributes,
bookingUrl: bookingUrl.toString(),
},
}
})
}
export { updateStaysBookingUrl }