Feat/SW-3549 pass scandic token * WIP pass scandic token * pass scandic token when booking * Merge branch 'master' of bitbucket.org:scandic-swap/web into feat/SW-3549-pass-scandic-token * pass user token when doing availability search * undo changes * merge * Merged in fix/sw-3551-rsc-bookingflowconfig (pull request #2988) fix(SW-3551): Fix issue with BookingConfigProvider in RSC * wip move config to pages * Move config providing to pages * Merged in fix/update-promo-error-modal-text (pull request #2990) fix: update promo error modal text * fix: update promo error modal text Approved-by: Emma Zettervall * Merged in fix/sw-3514-missing-membership-input-for-multiroom (pull request #2991) fix(SW-3514): Show join Scandic Friends card for SAS multiroom * Show join card for room 2+ Approved-by: Hrishikesh Vaipurkar * Merged in feat/lokalise-rebuild (pull request #2993) Feat/lokalise rebuild * chore(lokalise): update translation ids * chore(lokalise): easier to switch between projects * chore(lokalise): update translation ids * . * . * . * . * . * . * chore(lokalise): update translation ids * chore(lokalise): update translation ids * . * . * . * chore(lokalise): update translation ids * chore(lokalise): update translation ids * . * . * chore(lokalise): update translation ids * chore(lokalise): update translation ids * chore(lokalise): new translations * merge * switch to errors for missing id's * merge * sync translations Approved-by: Linus Flood * Merged in feat/SW-3552-logout-from-social-session-when- (pull request #2994) feat(SW-3552): Removed scandic session on logout Approved-by: Joakim Jäderberg * merge * replace getRedemptionTokenSafely() with context based instead * Refactor user verification and error handling across multiple components; implement safeTry utility for safer async calls * Refactor user verification and error handling across multiple components; implement safeTry utility for safer async calls * merge * Merge branch 'master' of bitbucket.org:scandic-swap/web into feat/SW-3549-pass-scandic-token * add booking scope remove unused getMembershipNumber() Approved-by: Anton Gunnarsson Approved-by: Hrishikesh Vaipurkar
115 lines
3.3 KiB
TypeScript
115 lines
3.3 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { safeProtectedProcedure } from "../../../procedures"
|
|
import { isValidSession } from "../../../utils/session"
|
|
import { getEuroBonusProfileData } from "../../partners/sas/getEuroBonusProfile"
|
|
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({ token: session.token })
|
|
|
|
if (!verifiedUserData || !verifiedUserData.loyalty) {
|
|
metricsUserTrackingInfo.success({
|
|
reason: "invalid user data",
|
|
data: notLoggedInUserTrackingData,
|
|
})
|
|
return notLoggedInUserTrackingData
|
|
}
|
|
|
|
const membership = getFriendsMembership(verifiedUserData.loyalty)
|
|
|
|
const loggedInUserTrackingData: TrackingUserData = {
|
|
loginStatus: "logged in",
|
|
loginType: session.token.loginType as LoginType,
|
|
memberId: verifiedUserData.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 eurobonusProfile = await getEuroBonusProfileData({
|
|
accessToken: session.token.access_token,
|
|
loginType: "eurobonus",
|
|
})
|
|
|
|
const loggedInUserTrackingData: TrackingUserData = {
|
|
loginStatus: "logged in",
|
|
loginType: session.token.loginType,
|
|
loginAction: "login success",
|
|
memberType: "sas-eurobonus",
|
|
eurobonusNumber: eurobonusProfile.eurobonusNumber,
|
|
tier: eurobonusProfile.tier,
|
|
linkStatus: eurobonusProfile.linkStatus,
|
|
}
|
|
|
|
return loggedInUserTrackingData
|
|
} catch (_error) {
|
|
return notLoggedInUserTrackingData
|
|
}
|
|
}
|