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
69 lines
2.0 KiB
TypeScript
69 lines
2.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"
|
|
|
|
import type { LoginType } from "@scandic-hotels/common/constants/loginType"
|
|
|
|
import type { TrackingUserData } from "../../types"
|
|
|
|
export const userTrackingInfo = safeProtectedProcedure.query(async function ({
|
|
ctx,
|
|
}) {
|
|
const userTrackingInfoCounter = createCounter("user", "userTrackingInfo")
|
|
const metricsUserTrackingInfo = userTrackingInfoCounter.init()
|
|
|
|
metricsUserTrackingInfo.start()
|
|
|
|
const notLoggedInUserTrackingData: TrackingUserData = {
|
|
loginStatus: "Non-logged in",
|
|
}
|
|
|
|
if (!isValidSession(ctx.session)) {
|
|
metricsUserTrackingInfo.success({
|
|
reason: "invalid session",
|
|
data: notLoggedInUserTrackingData,
|
|
})
|
|
return notLoggedInUserTrackingData
|
|
}
|
|
|
|
try {
|
|
const verifiedUserData = await getVerifiedUser({ session: ctx.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: 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
|
|
}
|
|
})
|