Feat/LOY-431: Switch to V2 of Profile endpoint * feat(LOY-431): switch to v2 of profile endpoint * feat(LOY-431): use CreditCard * feat(LOY-431): remove hotelinformation from friendTransaction schema * chore(LOY-431): add hotel data request to transactions * fix(LOY-431): use v1 of friendTransactions Approved-by: Linus Flood Approved-by: Erik Tiekstra Approved-by: Anton Gunnarsson
325 lines
9.4 KiB
TypeScript
325 lines
9.4 KiB
TypeScript
import { signupVerify } from "@scandic-hotels/common/constants/routes/signup"
|
|
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
|
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { env } from "../../../env/server"
|
|
import { router } from "../.."
|
|
import * as api from "../../api"
|
|
import { serverErrorByStatus } from "../../errors"
|
|
import { protectedProcedure, serviceProcedure } from "../../procedures"
|
|
import {
|
|
addCreditCardInput,
|
|
addPromoCampaignInput,
|
|
deleteCreditCardInput,
|
|
profilingConsentInput,
|
|
profilingConsentPromptDateInput,
|
|
saveCreditCardInput,
|
|
signupInput,
|
|
} from "./input"
|
|
import { initiateSaveCardSchema, subscriberIdSchema } from "./output"
|
|
|
|
const userMutationLogger = createLogger("userMutationRouter")
|
|
export const userMutationRouter = router({
|
|
creditCard: router({
|
|
add: protectedProcedure.input(addCreditCardInput).mutation(async function ({
|
|
ctx,
|
|
input,
|
|
}) {
|
|
userMutationLogger.info(
|
|
"api.user.creditCard.add start",
|
|
JSON.stringify({ query: { language: input.language } })
|
|
)
|
|
const apiResponse = await api.post(
|
|
api.endpoints.v2.Profile.CreditCard.initiateSaveCard,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
|
},
|
|
body: {
|
|
language: input.language,
|
|
mobileToken: false,
|
|
redirectUrl: `api/web/add-card-callback/${input.language}`,
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
const text = await apiResponse.text()
|
|
userMutationLogger.error(
|
|
"api.user.creditCard.add error",
|
|
JSON.stringify({
|
|
query: { language: input.language },
|
|
error: {
|
|
status: apiResponse.status,
|
|
statusText: apiResponse.statusText,
|
|
error: text,
|
|
},
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
const apiJson = await apiResponse.json()
|
|
const verifiedData = initiateSaveCardSchema.safeParse(apiJson)
|
|
if (!verifiedData.success) {
|
|
userMutationLogger.error(
|
|
"api.user.creditCard.add validation error",
|
|
JSON.stringify({
|
|
query: { language: input.language },
|
|
error: verifiedData.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
userMutationLogger.info(
|
|
"api.user.creditCard.add success",
|
|
JSON.stringify({ query: { language: input.language } })
|
|
)
|
|
return verifiedData.data.data
|
|
}),
|
|
save: protectedProcedure
|
|
.input(saveCreditCardInput)
|
|
.mutation(async function ({ ctx, input }) {
|
|
userMutationLogger.info(
|
|
"api.user.creditCard.save start",
|
|
JSON.stringify({})
|
|
)
|
|
const apiResponse = await api.post(
|
|
api.endpoints.v2.Profile.CreditCard.transaction(input.transactionId),
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
const text = await apiResponse.text()
|
|
userMutationLogger.error(
|
|
"api.user.creditCard.save error",
|
|
JSON.stringify({
|
|
error: {
|
|
status: apiResponse.status,
|
|
statusText: apiResponse.statusText,
|
|
text,
|
|
},
|
|
})
|
|
)
|
|
return false
|
|
}
|
|
userMutationLogger.info(
|
|
"api.user.creditCard.save success",
|
|
JSON.stringify({})
|
|
)
|
|
return true
|
|
}),
|
|
delete: protectedProcedure
|
|
.input(deleteCreditCardInput)
|
|
.mutation(async function ({ ctx, input }) {
|
|
userMutationLogger.info(
|
|
"api.user.creditCard.delete start",
|
|
JSON.stringify({ query: {} })
|
|
)
|
|
const apiResponse = await api.remove(
|
|
api.endpoints.v2.Profile.CreditCard.deleteCreditCard(
|
|
input.creditCardId
|
|
),
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
const text = await apiResponse.text()
|
|
userMutationLogger.error(
|
|
"api.user.creditCard.delete error",
|
|
JSON.stringify({
|
|
error: {
|
|
status: apiResponse.status,
|
|
statusText: apiResponse.statusText,
|
|
text,
|
|
},
|
|
query: {},
|
|
})
|
|
)
|
|
return false
|
|
}
|
|
userMutationLogger.info(
|
|
"api.user.creditCard.delete success",
|
|
JSON.stringify({})
|
|
)
|
|
return true
|
|
}),
|
|
}),
|
|
generatePreferencesLink: protectedProcedure.mutation(async function ({
|
|
ctx,
|
|
}) {
|
|
const generatePreferencesLinkCounter = createCounter(
|
|
"trpc.user",
|
|
"generatePreferencesLink"
|
|
)
|
|
|
|
const metricsGeneratePreferencesLink = generatePreferencesLinkCounter.init()
|
|
|
|
metricsGeneratePreferencesLink.start()
|
|
|
|
const apiResponse = await api.get(api.endpoints.v2.Profile.subscriberId, {
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
|
},
|
|
})
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsGeneratePreferencesLink.httpError(apiResponse)
|
|
return null
|
|
}
|
|
|
|
const data = await apiResponse.json()
|
|
|
|
const validatedData = subscriberIdSchema.safeParse(data)
|
|
|
|
if (!validatedData.success) {
|
|
metricsGeneratePreferencesLink.validationError(validatedData.error)
|
|
return null
|
|
}
|
|
|
|
const preferencesLink = new URL(env.SALESFORCE_PREFERENCE_BASE_URL)
|
|
preferencesLink.searchParams.set("subKey", validatedData.data.subscriberId)
|
|
|
|
metricsGeneratePreferencesLink.success()
|
|
|
|
return preferencesLink.toString()
|
|
}),
|
|
signup: serviceProcedure.input(signupInput).mutation(async function ({
|
|
ctx,
|
|
input,
|
|
}) {
|
|
const signupCounter = createCounter("trpc.user", "signup")
|
|
const metricsSignup = signupCounter.init()
|
|
|
|
const apiResponse = await api.post(api.endpoints.v2.Profile.profile, {
|
|
body: input,
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.serviceToken}`,
|
|
},
|
|
})
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsSignup.httpError(apiResponse)
|
|
const text = await apiResponse.text()
|
|
throw serverErrorByStatus(apiResponse.status, text)
|
|
}
|
|
|
|
metricsSignup.success()
|
|
|
|
return {
|
|
success: true,
|
|
redirectUrl: signupVerify[input.language],
|
|
}
|
|
}),
|
|
profilingConsent: router({
|
|
update: protectedProcedure
|
|
.input(profilingConsentInput)
|
|
.mutation(async function ({ ctx, input }) {
|
|
const profilingConsentCounter = createCounter(
|
|
"trpc.user",
|
|
"profilingConsent"
|
|
)
|
|
const metricsProfilingConsent = profilingConsentCounter.init()
|
|
|
|
const apiResponse = await api.patch(api.endpoints.v2.Profile.profile, {
|
|
body: input,
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
|
},
|
|
})
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsProfilingConsent.httpError(apiResponse)
|
|
const text = await apiResponse.text()
|
|
throw serverErrorByStatus(apiResponse.status, text)
|
|
}
|
|
|
|
metricsProfilingConsent.success()
|
|
|
|
return true
|
|
}),
|
|
}),
|
|
profilingConsentPromptDate: router({
|
|
update: protectedProcedure
|
|
.input(profilingConsentPromptDateInput)
|
|
.mutation(async function ({ ctx, input }) {
|
|
const profilingConsentPromptDateCounter = createCounter(
|
|
"trpc.user",
|
|
"profilingConsentPromptDate"
|
|
)
|
|
const metricsProfilingConsentPromptDate =
|
|
profilingConsentPromptDateCounter.init()
|
|
|
|
const apiResponse = await api.patch(api.endpoints.v2.Profile.profile, {
|
|
body: input,
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
|
},
|
|
})
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsProfilingConsentPromptDate.httpError(apiResponse)
|
|
const text = await apiResponse.text()
|
|
throw serverErrorByStatus(apiResponse.status, text)
|
|
}
|
|
|
|
metricsProfilingConsentPromptDate.success()
|
|
|
|
return true
|
|
}),
|
|
}),
|
|
promoCampaign: router({
|
|
add: protectedProcedure
|
|
.input(addPromoCampaignInput)
|
|
.mutation(async function ({ ctx, input }) {
|
|
userMutationLogger.info("api.user.promoCampaign.add start")
|
|
const apiResponse = await api.post(
|
|
api.endpoints.v2.Profile.promoCampaign,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
|
},
|
|
body: {
|
|
promotionId: input.promotionId,
|
|
language: input.language,
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
const text = await apiResponse.text()
|
|
userMutationLogger.error(
|
|
"api.user.promoCampaign.add error",
|
|
JSON.stringify({
|
|
query: {
|
|
promotionId: input.promotionId,
|
|
},
|
|
error: {
|
|
status: apiResponse.status,
|
|
statusText: apiResponse.statusText,
|
|
text,
|
|
},
|
|
})
|
|
)
|
|
return false
|
|
}
|
|
|
|
userMutationLogger.info(
|
|
"api.user.promoCampaign.add success",
|
|
JSON.stringify({
|
|
query: { promotionId: input.promotionId },
|
|
})
|
|
)
|
|
return true
|
|
}),
|
|
}),
|
|
})
|