fix(SW-2659): pass input to userTrackingInfo since lang needs to be passed from client
This commit is contained in:
committed by
Michael Zetterberg
parent
8cb63c4947
commit
aaffcba94a
@@ -7,6 +7,7 @@ import { trpc } from "@/lib/trpc/client"
|
|||||||
import useRouterTransitionStore from "@/stores/router-transition"
|
import useRouterTransitionStore from "@/stores/router-transition"
|
||||||
import useTrackingStore from "@/stores/tracking"
|
import useTrackingStore from "@/stores/tracking"
|
||||||
|
|
||||||
|
import useLang from "@/hooks/useLang"
|
||||||
import { useSessionId } from "@/hooks/useSessionId"
|
import { useSessionId } from "@/hooks/useSessionId"
|
||||||
import { promiseWithTimeout } from "@/utils/promiseWithTimeout"
|
import { promiseWithTimeout } from "@/utils/promiseWithTimeout"
|
||||||
import { createSDKPageObject, trackPageView } from "@/utils/tracking"
|
import { createSDKPageObject, trackPageView } from "@/utils/tracking"
|
||||||
@@ -31,11 +32,12 @@ export const useTrackHardNavigation = ({
|
|||||||
paymentInfo,
|
paymentInfo,
|
||||||
ancillaries,
|
ancillaries,
|
||||||
}: TrackingSDKProps) => {
|
}: TrackingSDKProps) => {
|
||||||
|
const lang = useLang()
|
||||||
const {
|
const {
|
||||||
data: userTrackingData,
|
data: userTrackingData,
|
||||||
isPending,
|
isPending,
|
||||||
isError,
|
isError,
|
||||||
} = trpc.user.userTrackingInfo.useQuery()
|
} = trpc.user.userTrackingInfo.useQuery({ lang })
|
||||||
|
|
||||||
const sessionId = useSessionId()
|
const sessionId = useSessionId()
|
||||||
const pathName = usePathname()
|
const pathName = usePathname()
|
||||||
@@ -93,11 +95,12 @@ export const useTrackSoftNavigation = ({
|
|||||||
paymentInfo,
|
paymentInfo,
|
||||||
ancillaries,
|
ancillaries,
|
||||||
}: TrackingSDKProps) => {
|
}: TrackingSDKProps) => {
|
||||||
|
const lang = useLang()
|
||||||
const {
|
const {
|
||||||
data: userTrackingData,
|
data: userTrackingData,
|
||||||
isPending,
|
isPending,
|
||||||
isError,
|
isError,
|
||||||
} = trpc.user.userTrackingInfo.useQuery()
|
} = trpc.user.userTrackingInfo.useQuery({ lang })
|
||||||
|
|
||||||
const [status, setStatus] = useState<TransitionStatus>(
|
const [status, setStatus] = useState<TransitionStatus>(
|
||||||
TransitionStatusEnum.NotRun
|
TransitionStatusEnum.NotRun
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ import { Lang } from "@/constants/languages"
|
|||||||
import { signUpSchema } from "@/components/Forms/Signup/schema"
|
import { signUpSchema } from "@/components/Forms/Signup/schema"
|
||||||
|
|
||||||
// Query
|
// Query
|
||||||
|
export const userTrackingInput = z.object({
|
||||||
|
lang: z.nativeEnum(Lang).optional(),
|
||||||
|
})
|
||||||
export const staysInput = z
|
export const staysInput = z
|
||||||
.object({
|
.object({
|
||||||
cursor: z
|
cursor: z
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import {
|
|||||||
friendTransactionsInput,
|
friendTransactionsInput,
|
||||||
getSavedPaymentCardsInput,
|
getSavedPaymentCardsInput,
|
||||||
staysInput,
|
staysInput,
|
||||||
|
userTrackingInput,
|
||||||
} from "./input"
|
} from "./input"
|
||||||
import { getFriendTransactionsSchema } from "./output"
|
import { getFriendTransactionsSchema } from "./output"
|
||||||
import {
|
import {
|
||||||
@@ -124,72 +125,77 @@ export const userQueryRouter = router({
|
|||||||
const membershipLevel = getFriendsMembership(verifiedData.data.loyalty)
|
const membershipLevel = getFriendsMembership(verifiedData.data.loyalty)
|
||||||
return membershipLevel
|
return membershipLevel
|
||||||
}),
|
}),
|
||||||
userTrackingInfo: safeProtectedProcedure.query(async function ({ ctx }) {
|
userTrackingInfo: safeProtectedProcedure
|
||||||
const userTrackingInfoCounter = createCounter("user", "userTrackingInfo")
|
.input(userTrackingInput)
|
||||||
const metricsUserTrackingInfo = userTrackingInfoCounter.init()
|
.query(async function ({ ctx, input }) {
|
||||||
|
const { lang } = input
|
||||||
|
const language = lang || ctx.lang
|
||||||
|
|
||||||
metricsUserTrackingInfo.start()
|
const userTrackingInfoCounter = createCounter("user", "userTrackingInfo")
|
||||||
|
const metricsUserTrackingInfo = userTrackingInfoCounter.init()
|
||||||
|
|
||||||
const notLoggedInUserTrackingData: TrackingSDKUserData = {
|
metricsUserTrackingInfo.start()
|
||||||
loginStatus: "Non-logged in",
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isValidSession(ctx.session)) {
|
const notLoggedInUserTrackingData: TrackingSDKUserData = {
|
||||||
metricsUserTrackingInfo.success({
|
loginStatus: "Non-logged in",
|
||||||
reason: "invalid session",
|
}
|
||||||
data: notLoggedInUserTrackingData,
|
|
||||||
})
|
|
||||||
return notLoggedInUserTrackingData
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
if (!isValidSession(ctx.session)) {
|
||||||
const verifiedUserData = await getVerifiedUser({ session: ctx.session })
|
|
||||||
|
|
||||||
if (!verifiedUserData || "error" in verifiedUserData) {
|
|
||||||
metricsUserTrackingInfo.success({
|
metricsUserTrackingInfo.success({
|
||||||
reason: "invalid user data",
|
reason: "invalid session",
|
||||||
data: notLoggedInUserTrackingData,
|
data: notLoggedInUserTrackingData,
|
||||||
})
|
})
|
||||||
return notLoggedInUserTrackingData
|
return notLoggedInUserTrackingData
|
||||||
}
|
}
|
||||||
|
|
||||||
const previousStaysData = await getPreviousStays(
|
try {
|
||||||
ctx.session.token.access_token,
|
const verifiedUserData = await getVerifiedUser({ session: ctx.session })
|
||||||
1,
|
|
||||||
ctx.lang
|
if (!verifiedUserData || "error" in verifiedUserData) {
|
||||||
)
|
metricsUserTrackingInfo.success({
|
||||||
if (!previousStaysData) {
|
reason: "invalid user data",
|
||||||
|
data: notLoggedInUserTrackingData,
|
||||||
|
})
|
||||||
|
return notLoggedInUserTrackingData
|
||||||
|
}
|
||||||
|
|
||||||
|
const previousStaysData = await getPreviousStays(
|
||||||
|
ctx.session.token.access_token,
|
||||||
|
1,
|
||||||
|
language
|
||||||
|
)
|
||||||
|
if (!previousStaysData) {
|
||||||
|
metricsUserTrackingInfo.success({
|
||||||
|
reason: "no previous stays data",
|
||||||
|
data: notLoggedInUserTrackingData,
|
||||||
|
})
|
||||||
|
return notLoggedInUserTrackingData
|
||||||
|
}
|
||||||
|
|
||||||
|
const membership = getFriendsMembership(verifiedUserData.data.loyalty)
|
||||||
|
|
||||||
|
const loggedInUserTrackingData: TrackingSDKUserData = {
|
||||||
|
loginStatus: "logged in",
|
||||||
|
loginType: ctx.session.token.loginType as LoginType,
|
||||||
|
memberId: verifiedUserData.data.profileId,
|
||||||
|
membershipNumber: membership?.membershipNumber,
|
||||||
|
memberLevel: membership?.membershipLevel,
|
||||||
|
noOfNightsStayed: previousStaysData.links?.totalCount ?? 0,
|
||||||
|
totalPointsAvailableToSpend: membership?.currentPoints,
|
||||||
|
loginAction: "login success",
|
||||||
|
}
|
||||||
|
|
||||||
metricsUserTrackingInfo.success({
|
metricsUserTrackingInfo.success({
|
||||||
reason: "no previous stays data",
|
reason: "valid logged in",
|
||||||
data: notLoggedInUserTrackingData,
|
data: loggedInUserTrackingData,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return loggedInUserTrackingData
|
||||||
|
} catch (error) {
|
||||||
|
metricsUserTrackingInfo.fail(error)
|
||||||
return notLoggedInUserTrackingData
|
return notLoggedInUserTrackingData
|
||||||
}
|
}
|
||||||
|
}),
|
||||||
const membership = getFriendsMembership(verifiedUserData.data.loyalty)
|
|
||||||
|
|
||||||
const loggedInUserTrackingData: TrackingSDKUserData = {
|
|
||||||
loginStatus: "logged in",
|
|
||||||
loginType: ctx.session.token.loginType as LoginType,
|
|
||||||
memberId: verifiedUserData.data.profileId,
|
|
||||||
membershipNumber: membership?.membershipNumber,
|
|
||||||
memberLevel: membership?.membershipLevel,
|
|
||||||
noOfNightsStayed: previousStaysData.links?.totalCount ?? 0,
|
|
||||||
totalPointsAvailableToSpend: membership?.currentPoints,
|
|
||||||
loginAction: "login success",
|
|
||||||
}
|
|
||||||
|
|
||||||
metricsUserTrackingInfo.success({
|
|
||||||
reason: "valid logged in",
|
|
||||||
data: loggedInUserTrackingData,
|
|
||||||
})
|
|
||||||
|
|
||||||
return loggedInUserTrackingData
|
|
||||||
} catch (error) {
|
|
||||||
metricsUserTrackingInfo.fail(error)
|
|
||||||
return notLoggedInUserTrackingData
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
|
|
||||||
stays: router({
|
stays: router({
|
||||||
previous: languageProtectedProcedure
|
previous: languageProtectedProcedure
|
||||||
|
|||||||
Reference in New Issue
Block a user