feat(SW-3525): Set correct member type in tracking for partner-sas * Set correct member type in tracking for partner-sas Approved-by: Joakim Jäderberg
110 lines
3.0 KiB
TypeScript
110 lines
3.0 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { safeProtectedProcedure } from "../../../procedures"
|
|
import { isValidSession } from "../../../utils/session"
|
|
import { getFriendsMembership } from "../helpers"
|
|
import { getVerifiedUser } from "../utils/getVerifiedUser"
|
|
|
|
import type { LoginType } from "@scandic-hotels/common/constants/loginType"
|
|
import type { Session } from "next-auth"
|
|
|
|
import type { TrackingUserData } from "../../types"
|
|
|
|
export const userTrackingInfo = safeProtectedProcedure.query(async function ({
|
|
ctx,
|
|
}) {
|
|
if (ctx.app === "partner-sas") {
|
|
return getSasEurobonusUserTrackingData(ctx.session)
|
|
}
|
|
|
|
if (ctx.app === "scandic-web") {
|
|
return getScandicFriendsUserTrackingData(ctx.session)
|
|
}
|
|
|
|
return { loginStatus: "Error" } as const
|
|
})
|
|
|
|
async function getScandicFriendsUserTrackingData(session: Session | null) {
|
|
const userTrackingInfoCounter = createCounter("user", "userTrackingInfo")
|
|
const metricsUserTrackingInfo = userTrackingInfoCounter.init()
|
|
|
|
metricsUserTrackingInfo.start()
|
|
|
|
const memberType = "scandic-friends" as const
|
|
|
|
const notLoggedInUserTrackingData: TrackingUserData = {
|
|
loginStatus: "Non-logged in",
|
|
memberType,
|
|
}
|
|
|
|
if (!isValidSession(session)) {
|
|
metricsUserTrackingInfo.success({
|
|
reason: "invalid session",
|
|
data: notLoggedInUserTrackingData,
|
|
})
|
|
return notLoggedInUserTrackingData
|
|
}
|
|
|
|
try {
|
|
const verifiedUserData = await getVerifiedUser({ session: session })
|
|
|
|
if (
|
|
!verifiedUserData ||
|
|
"error" in verifiedUserData ||
|
|
!verifiedUserData.data.loyalty
|
|
) {
|
|
metricsUserTrackingInfo.success({
|
|
reason: "invalid user data",
|
|
data: notLoggedInUserTrackingData,
|
|
})
|
|
return notLoggedInUserTrackingData
|
|
}
|
|
|
|
const membership = getFriendsMembership(verifiedUserData.data.loyalty)
|
|
|
|
const loggedInUserTrackingData: TrackingUserData = {
|
|
loginStatus: "logged in",
|
|
loginType: session.token.loginType as LoginType,
|
|
memberId: verifiedUserData.data.profileId,
|
|
membershipNumber: membership?.membershipNumber,
|
|
memberLevel: membership?.membershipLevel,
|
|
loginAction: "login success",
|
|
memberType,
|
|
}
|
|
|
|
metricsUserTrackingInfo.success({
|
|
reason: "valid logged in",
|
|
data: loggedInUserTrackingData,
|
|
})
|
|
|
|
return loggedInUserTrackingData
|
|
} catch (error) {
|
|
metricsUserTrackingInfo.fail(error)
|
|
return notLoggedInUserTrackingData
|
|
}
|
|
}
|
|
|
|
async function getSasEurobonusUserTrackingData(session: Session | null) {
|
|
const notLoggedInUserTrackingData: TrackingUserData = {
|
|
loginStatus: "Non-logged in",
|
|
memberType: "sas-eurobonus",
|
|
}
|
|
|
|
if (!isValidSession(session)) {
|
|
return notLoggedInUserTrackingData
|
|
}
|
|
|
|
try {
|
|
const loggedInUserTrackingData: TrackingUserData = {
|
|
loginStatus: "logged in",
|
|
loginType: session.token.loginType,
|
|
loginAction: "login success",
|
|
memberType: "sas-eurobonus",
|
|
}
|
|
|
|
return loggedInUserTrackingData
|
|
} catch (_error) {
|
|
return notLoggedInUserTrackingData
|
|
}
|
|
}
|