feat(SW-3610): Get basic user from api * feat(SW-3610): Get basic user from api * . * Optional profileid Approved-by: Anton Gunnarsson
76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
import "server-only"
|
|
|
|
import { myStay } from "@scandic-hotels/common/constants/routes/myStay"
|
|
import { safeTry } from "@scandic-hotels/common/utils/safeTry"
|
|
|
|
import { env } from "../../../../env/server"
|
|
import { encrypt } from "../../../utils/encryption"
|
|
import { getBasicUser } from "./getBasicUser"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
import type { Session } from "next-auth"
|
|
|
|
import type { FriendTransaction, Stay } from "../output"
|
|
|
|
export async function updateStaysBookingUrl(
|
|
data: Stay[],
|
|
session: Session,
|
|
lang: Lang
|
|
): Promise<Stay[]>
|
|
|
|
export async function updateStaysBookingUrl(
|
|
data: FriendTransaction[],
|
|
session: Session,
|
|
lang: Lang
|
|
): Promise<FriendTransaction[]>
|
|
|
|
export async function updateStaysBookingUrl(
|
|
data: Stay[] | FriendTransaction[],
|
|
session: Session,
|
|
lang: Lang
|
|
) {
|
|
const [user, error] = await safeTry(
|
|
getBasicUser({
|
|
token: {
|
|
access_token: session.token.access_token,
|
|
expires_at: session.token.expires_at ?? 0,
|
|
},
|
|
})
|
|
)
|
|
|
|
if (error || !user) {
|
|
return data
|
|
}
|
|
|
|
return data.map((d) => {
|
|
const originalString =
|
|
d.attributes.confirmationNumber.toString() + "," + user.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 = new URL(myStay[lang], baseUrl)
|
|
|
|
// Add search parameters.
|
|
if (encryptedBookingValue) {
|
|
bookingUrl.searchParams.set("RefId", encryptedBookingValue)
|
|
} else {
|
|
bookingUrl.searchParams.set("lastName", user.lastName)
|
|
bookingUrl.searchParams.set(
|
|
"bookingId",
|
|
d.attributes.confirmationNumber.toString()
|
|
)
|
|
}
|
|
|
|
return {
|
|
...d,
|
|
attributes: {
|
|
...d.attributes,
|
|
bookingUrl: bookingUrl.toString(),
|
|
},
|
|
}
|
|
})
|
|
}
|