Merged in feat/SW-3494-noOfNightsStayed (pull request #2832)
feat(SW-3494): removed properties that we dont need in the tracking * feat(SW-3494): removed properties that we dont need in the tracking * Fixed waiting on user response before tracking * Refactor and cleanup * Cleanup Approved-by: Joakim Jäderberg
This commit is contained in:
@@ -35,8 +35,6 @@ export type TrackingUserData =
|
||||
memberId?: string
|
||||
membershipNumber?: string
|
||||
memberLevel?: MembershipLevel
|
||||
noOfNightsStayed?: number
|
||||
totalPointsAvailableToSpend?: number
|
||||
loginAction?: "login success"
|
||||
}
|
||||
| {
|
||||
|
||||
@@ -1,92 +1,68 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@scandic-hotels/common/constants/language"
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import { safeProtectedProcedure } from "../../../procedures"
|
||||
import { isValidSession } from "../../../utils/session"
|
||||
import { getFriendsMembership } from "../helpers"
|
||||
import { getPreviousStays, getVerifiedUser } from "../utils"
|
||||
import { getVerifiedUser } from "../utils"
|
||||
|
||||
import type { LoginType } from "@scandic-hotels/common/constants/loginType"
|
||||
|
||||
import type { TrackingUserData } from "../../types"
|
||||
|
||||
export const userTrackingInput = z.object({
|
||||
lang: z.nativeEnum(Lang).optional(),
|
||||
})
|
||||
export const userTrackingInfo = safeProtectedProcedure
|
||||
.input(userTrackingInput)
|
||||
.query(async function ({ ctx, input }) {
|
||||
const { lang } = input
|
||||
const language = lang || ctx.lang
|
||||
export const userTrackingInfo = safeProtectedProcedure.query(async function ({
|
||||
ctx,
|
||||
}) {
|
||||
const userTrackingInfoCounter = createCounter("user", "userTrackingInfo")
|
||||
const metricsUserTrackingInfo = userTrackingInfoCounter.init()
|
||||
|
||||
const userTrackingInfoCounter = createCounter("user", "userTrackingInfo")
|
||||
const metricsUserTrackingInfo = userTrackingInfoCounter.init()
|
||||
metricsUserTrackingInfo.start()
|
||||
|
||||
metricsUserTrackingInfo.start()
|
||||
const notLoggedInUserTrackingData: TrackingUserData = {
|
||||
loginStatus: "Non-logged in",
|
||||
}
|
||||
|
||||
const notLoggedInUserTrackingData: TrackingUserData = {
|
||||
loginStatus: "Non-logged in",
|
||||
}
|
||||
if (!isValidSession(ctx.session)) {
|
||||
metricsUserTrackingInfo.success({
|
||||
reason: "invalid session",
|
||||
data: notLoggedInUserTrackingData,
|
||||
})
|
||||
return notLoggedInUserTrackingData
|
||||
}
|
||||
|
||||
if (!isValidSession(ctx.session)) {
|
||||
try {
|
||||
const verifiedUserData = await getVerifiedUser({ session: ctx.session })
|
||||
|
||||
if (
|
||||
!verifiedUserData ||
|
||||
"error" in verifiedUserData ||
|
||||
!verifiedUserData.data.loyalty
|
||||
) {
|
||||
metricsUserTrackingInfo.success({
|
||||
reason: "invalid session",
|
||||
reason: "invalid user data",
|
||||
data: notLoggedInUserTrackingData,
|
||||
})
|
||||
return notLoggedInUserTrackingData
|
||||
}
|
||||
|
||||
try {
|
||||
const verifiedUserData = await getVerifiedUser({ session: ctx.session })
|
||||
const membership = getFriendsMembership(verifiedUserData.data.loyalty)
|
||||
|
||||
if (
|
||||
!verifiedUserData ||
|
||||
"error" in verifiedUserData ||
|
||||
!verifiedUserData.data.loyalty
|
||||
) {
|
||||
metricsUserTrackingInfo.success({
|
||||
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: TrackingUserData = {
|
||||
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
|
||||
const loggedInUserTrackingData: TrackingUserData = {
|
||||
loginStatus: "logged in",
|
||||
loginType: ctx.session.token.loginType as LoginType,
|
||||
memberId: verifiedUserData.data.profileId,
|
||||
membershipNumber: membership?.membershipNumber,
|
||||
memberLevel: membership?.membershipLevel,
|
||||
loginAction: "login success",
|
||||
}
|
||||
})
|
||||
|
||||
metricsUserTrackingInfo.success({
|
||||
reason: "valid logged in",
|
||||
data: loggedInUserTrackingData,
|
||||
})
|
||||
|
||||
return loggedInUserTrackingData
|
||||
} catch (error) {
|
||||
metricsUserTrackingInfo.fail(error)
|
||||
return notLoggedInUserTrackingData
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user