feat(SW-66, SW-348): search functionality and ui
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { env } from "@/env/server"
|
||||
import * as api from "@/lib/api"
|
||||
import {
|
||||
protectedProcedure,
|
||||
@@ -13,26 +11,24 @@ import { countries } from "@/components/TempDesignSystem/Form/Country/countries"
|
||||
import * as maskValue from "@/utils/maskValue"
|
||||
import { getMembership, getMembershipCards } from "@/utils/user"
|
||||
|
||||
import encryptValue from "../utils/encryptValue"
|
||||
import { friendTransactionsInput, staysInput } from "./input"
|
||||
import {
|
||||
creditCardsSchema,
|
||||
FriendTransaction,
|
||||
getFriendTransactionsSchema,
|
||||
getMembershipCardsSchema,
|
||||
getStaysSchema,
|
||||
getUserSchema,
|
||||
Stay,
|
||||
} from "./output"
|
||||
import { benefits, extendedUser, nextLevelPerks } from "./temp"
|
||||
import { updateStaysBookingUrl } from "./utils"
|
||||
|
||||
import type { Session } from "next-auth"
|
||||
|
||||
import { RewardTransactionTypes } from "@/types/components/myPages/myPage/enums"
|
||||
import type {
|
||||
LoginType,
|
||||
TrackingSDKUserData,
|
||||
} from "@/types/components/tracking"
|
||||
import { BlocksEnums } from "@/types/enums/blocks"
|
||||
import { Transactions } from "@/types/enums/transactions"
|
||||
import type { MembershipLevel } from "@/constants/membershipLevels"
|
||||
|
||||
// OpenTelemetry metrics: User
|
||||
@@ -93,7 +89,6 @@ export async function getVerifiedUser({ session }: { session: Session }) {
|
||||
getVerifiedUserCounter.add(1)
|
||||
console.info("api.user.profile getVerifiedUser start", JSON.stringify({}))
|
||||
const apiResponse = await api.get(api.endpoints.v1.profile, {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
Authorization: `Bearer ${session.token.access_token}`,
|
||||
},
|
||||
@@ -147,7 +142,7 @@ export async function getVerifiedUser({ session }: { session: Session }) {
|
||||
return null
|
||||
}
|
||||
|
||||
const verifiedData = getUserSchema.safeParse(apiJson.data.attributes)
|
||||
const verifiedData = getUserSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
getVerifiedUserFailCounter.add(1, {
|
||||
error_type: "validation_error",
|
||||
@@ -166,109 +161,6 @@ export async function getVerifiedUser({ session }: { session: Session }) {
|
||||
return verifiedData
|
||||
}
|
||||
|
||||
function fakingRequest<T>(payload: T): Promise<T> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve(payload)
|
||||
}, 1500)
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
) {
|
||||
// Tenporary 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
|
||||
let localeDomain = env.PUBLIC_URL
|
||||
let fullBookingUrl = localeDomain + "/hotelreservation/my-booking"
|
||||
switch (lang) {
|
||||
case Lang.sv:
|
||||
localeDomain = localeDomain?.replace(".com", ".se")
|
||||
fullBookingUrl = localeDomain + "/hotelreservation/din-bokning"
|
||||
break
|
||||
case Lang.no:
|
||||
localeDomain = localeDomain?.replace(".com", ".no")
|
||||
fullBookingUrl = localeDomain + "/hotelreservation/my-booking"
|
||||
break
|
||||
case Lang.da:
|
||||
localeDomain = localeDomain?.replace(".com", ".dk")
|
||||
fullBookingUrl = localeDomain + "/hotelreservation/min-booking"
|
||||
break
|
||||
case Lang.fi:
|
||||
localeDomain = localeDomain?.replace(".com", ".fi")
|
||||
fullBookingUrl = localeDomain + "/varaa-hotelli/varauksesi"
|
||||
break
|
||||
case Lang.de:
|
||||
localeDomain = localeDomain?.replace(".com", ".de")
|
||||
fullBookingUrl = localeDomain + "/hotelreservation/my-booking"
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
if (apiResponse.ok) {
|
||||
getProfileSuccessCounter.add(1)
|
||||
console.info(
|
||||
"api.user.profile updatebookingurl success",
|
||||
JSON.stringify({})
|
||||
)
|
||||
const apiJson = await apiResponse.json()
|
||||
if (apiJson.data?.attributes) {
|
||||
return data.map((d) => {
|
||||
const originalString =
|
||||
d.attributes.confirmationNumber.toString() +
|
||||
"," +
|
||||
apiJson.data.attributes.lastName
|
||||
const encryptedBookingValue = encryptValue(originalString)
|
||||
const bookingUrl = !!encryptedBookingValue
|
||||
? fullBookingUrl + "?RefId=" + encryptedBookingValue
|
||||
: fullBookingUrl +
|
||||
"?lastName=" +
|
||||
apiJson.data.attributes.lastName +
|
||||
"&bookingId=" +
|
||||
d.attributes.confirmationNumber
|
||||
return {
|
||||
...d,
|
||||
attributes: {
|
||||
...d.attributes,
|
||||
bookingUrl: bookingUrl,
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
getProfileFailCounter.add(1, { error: JSON.stringify(apiResponse) })
|
||||
console.info(
|
||||
"api.user.profile updatebookingurl error",
|
||||
JSON.stringify({ error: apiResponse })
|
||||
)
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
export const userQueryRouter = router({
|
||||
get: protectedProcedure
|
||||
.use(async function (opts) {
|
||||
@@ -299,7 +191,6 @@ export const userQueryRouter = router({
|
||||
)
|
||||
|
||||
const user = {
|
||||
...extendedUser,
|
||||
address: {
|
||||
city: verifiedData.data.address.city,
|
||||
country: country?.name ?? "",
|
||||
@@ -312,6 +203,7 @@ export const userQueryRouter = router({
|
||||
firstName: verifiedData.data.firstName,
|
||||
language: verifiedData.data.language,
|
||||
lastName: verifiedData.data.lastName,
|
||||
membership: getMembership(verifiedData.data.memberships),
|
||||
memberships: verifiedData.data.memberships,
|
||||
name: `${verifiedData.data.firstName} ${verifiedData.data.lastName}`,
|
||||
phoneNumber: verifiedData.data.phoneNumber,
|
||||
@@ -460,16 +352,6 @@ export const userQueryRouter = router({
|
||||
}
|
||||
return loggedInUserTrackingData
|
||||
}),
|
||||
benefits: router({
|
||||
current: protectedProcedure.query(async function (opts) {
|
||||
// TODO: Make request to get user data from Scandic API
|
||||
return await fakingRequest<typeof benefits>(benefits)
|
||||
}),
|
||||
nextLevel: protectedProcedure.query(async function (opts) {
|
||||
// TODO: Make request to get user data from Scandic API
|
||||
return await fakingRequest<typeof nextLevelPerks>(nextLevelPerks)
|
||||
}),
|
||||
}),
|
||||
|
||||
stays: router({
|
||||
previous: protectedProcedure
|
||||
@@ -735,7 +617,7 @@ export const userQueryRouter = router({
|
||||
)
|
||||
|
||||
const pageData = updatedData
|
||||
.filter((t) => t.type !== RewardTransactionTypes.expired)
|
||||
.filter((t) => t.type !== Transactions.rewardType.expired)
|
||||
.sort((a, b) => {
|
||||
// 'BALFWD' are transactions from Opera migration that happended in May 2021
|
||||
if (a.attributes.confirmationNumber === "BALFWD") return 1
|
||||
@@ -787,7 +669,6 @@ export const userQueryRouter = router({
|
||||
getCreditCardsCounter.add(1)
|
||||
console.info("api.profile.creditCards start", JSON.stringify({}))
|
||||
const apiResponse = await api.get(api.endpoints.v1.creditCards, {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user