Files
web/apps/scandic-web/server/routers/user/utils.ts
Chuma Mcphoy (We Ahead) 56d5ad77d1 Merged in fix/SW-2068-correct-public-url-from-my-stays-overview-page (pull request #1694)
feat(SW-2068): Link to Correct Public URL from within My Stays Overview Page

* feat(SW-2068): Link to Correct Public URL from within My Stays Overview Page

- Added language parameter to previous and upcoming stays queries.
- Updated Client components to utilize the new language hook.
- Refactored EmptyUpcomingStays component to dynamically generate links based on the current language.
- Adjusted user input validation to include optional language parameter.

* refactor(SW-2068): Update EmptyUpcomingStays components to use PUBLIC_URL + add utility to handle TLD based on language

* chore(SW-2068): Clarify TLD

* feat(SW-2068): documentation for getTldForLanguage

* refactor(SW-2068): Simplify booking URL construction in updateStaysBookingUrl

* refactor(SW-2068): Remove incorrect TLD update logic from booking URL construction

* refactor(SW-2068): Centralize booking URL paths using myBookingPath constant

* refactor(SW-2068): Streamline search params in booking URL construction logic in updateStaysBookingUrl


Approved-by: Christian Andolf
2025-04-02 12:44:02 +00:00

103 lines
2.8 KiB
TypeScript

import { metrics } from "@opentelemetry/api"
import { myBookingPath } from "@/constants/myBooking"
import { myStay } from "@/constants/routes/myStay"
import { env } from "@/env/server"
import * as api from "@/lib/api"
import { getCurrentWebUrl } from "@/utils/url"
import { encrypt } from "../utils/encryption"
import type { Lang } from "@/constants/languages"
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.profile, {
cache: "no-store",
headers: {
Authorization: `Bearer ${token}`,
},
})
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 = encrypt(originalString)
// Get base URL with fallback for ephemeral environments (like deploy previews).
const baseUrl = env.PUBLIC_URL || "https://www.scandichotels.com"
// Construct Booking URL.
const bookingUrl = env.HIDE_FOR_NEXT_RELEASE
? new URL(getCurrentWebUrl(myBookingPath[lang], lang))
: new URL(myStay[lang], baseUrl)
// Add search parameters.
if (encryptedBookingValue) {
bookingUrl.searchParams.set("RefId", encryptedBookingValue)
} else {
bookingUrl.searchParams.set("lastName", apiJson.data.attributes.lastName)
bookingUrl.searchParams.set(
"bookingId",
d.attributes.confirmationNumber.toString()
)
}
return {
...d,
attributes: {
...d.attributes,
bookingUrl: bookingUrl.toString(),
},
}
})
}
export { updateStaysBookingUrl }