fix: conditionally use user token or service token for booking
This commit is contained in:
@@ -2,7 +2,7 @@ import { metrics } from "@opentelemetry/api"
|
|||||||
|
|
||||||
import * as api from "@/lib/api"
|
import * as api from "@/lib/api"
|
||||||
import { getVerifiedUser } from "@/server/routers/user/query"
|
import { getVerifiedUser } from "@/server/routers/user/query"
|
||||||
import { router, serviceProcedure } from "@/server/trpc"
|
import { router, safeProtectedServiceProcedure } from "@/server/trpc"
|
||||||
|
|
||||||
import { getMembership } from "@/utils/user"
|
import { getMembership } from "@/utils/user"
|
||||||
|
|
||||||
@@ -35,95 +35,93 @@ async function getMembershipNumber(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const bookingMutationRouter = router({
|
export const bookingMutationRouter = router({
|
||||||
create: serviceProcedure.input(createBookingInput).mutation(async function ({
|
create: safeProtectedServiceProcedure
|
||||||
ctx,
|
.input(createBookingInput)
|
||||||
input,
|
.mutation(async function ({ ctx, input }) {
|
||||||
}) {
|
const accessToken = ctx.session?.token.access_token ?? ctx.serviceToken
|
||||||
const { checkInDate, checkOutDate, hotelId } = input
|
const { checkInDate, checkOutDate, hotelId } = input
|
||||||
|
|
||||||
// TODO: add support for user token OR service token in procedure
|
const loggingAttributes = {
|
||||||
// then we can fetch membership number if user token exists
|
membershipNumber: await getMembershipNumber(ctx.session),
|
||||||
const loggingAttributes = {
|
|
||||||
// membershipNumber: await getMembershipNumber(ctx.session),
|
|
||||||
checkInDate,
|
|
||||||
checkOutDate,
|
|
||||||
hotelId,
|
|
||||||
}
|
|
||||||
|
|
||||||
createBookingCounter.add(1, { hotelId, checkInDate, checkOutDate })
|
|
||||||
|
|
||||||
console.info(
|
|
||||||
"api.booking.create start",
|
|
||||||
JSON.stringify({
|
|
||||||
query: loggingAttributes,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
const headers = {
|
|
||||||
Authorization: `Bearer ${ctx.serviceToken}`,
|
|
||||||
}
|
|
||||||
|
|
||||||
const apiResponse = await api.post(api.endpoints.v1.Booking.bookings, {
|
|
||||||
headers,
|
|
||||||
body: input,
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!apiResponse.ok) {
|
|
||||||
const text = await apiResponse.text()
|
|
||||||
createBookingFailCounter.add(1, {
|
|
||||||
hotelId,
|
|
||||||
checkInDate,
|
checkInDate,
|
||||||
checkOutDate,
|
checkOutDate,
|
||||||
error_type: "http_error",
|
hotelId,
|
||||||
error: JSON.stringify({
|
}
|
||||||
status: apiResponse.status,
|
|
||||||
}),
|
createBookingCounter.add(1, { hotelId, checkInDate, checkOutDate })
|
||||||
})
|
|
||||||
console.error(
|
console.info(
|
||||||
"api.booking.create error",
|
"api.booking.create start",
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
query: loggingAttributes,
|
query: loggingAttributes,
|
||||||
error: {
|
})
|
||||||
|
)
|
||||||
|
const headers = {
|
||||||
|
Authorization: `Bearer ${accessToken}`,
|
||||||
|
}
|
||||||
|
|
||||||
|
const apiResponse = await api.post(api.endpoints.v1.Booking.bookings, {
|
||||||
|
headers,
|
||||||
|
body: input,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!apiResponse.ok) {
|
||||||
|
const text = await apiResponse.text()
|
||||||
|
createBookingFailCounter.add(1, {
|
||||||
|
hotelId,
|
||||||
|
checkInDate,
|
||||||
|
checkOutDate,
|
||||||
|
error_type: "http_error",
|
||||||
|
error: JSON.stringify({
|
||||||
status: apiResponse.status,
|
status: apiResponse.status,
|
||||||
statusText: apiResponse.statusText,
|
}),
|
||||||
error: text,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
)
|
console.error(
|
||||||
return null
|
"api.booking.create error",
|
||||||
}
|
JSON.stringify({
|
||||||
|
query: loggingAttributes,
|
||||||
|
error: {
|
||||||
|
status: apiResponse.status,
|
||||||
|
statusText: apiResponse.statusText,
|
||||||
|
error: text,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
const apiJson = await apiResponse.json()
|
const apiJson = await apiResponse.json()
|
||||||
const verifiedData = createBookingSchema.safeParse(apiJson)
|
const verifiedData = createBookingSchema.safeParse(apiJson)
|
||||||
if (!verifiedData.success) {
|
if (!verifiedData.success) {
|
||||||
createBookingFailCounter.add(1, {
|
createBookingFailCounter.add(1, {
|
||||||
|
hotelId,
|
||||||
|
checkInDate,
|
||||||
|
checkOutDate,
|
||||||
|
error_type: "validation_error",
|
||||||
|
})
|
||||||
|
|
||||||
|
console.error(
|
||||||
|
"api.booking.create validation error",
|
||||||
|
JSON.stringify({
|
||||||
|
query: loggingAttributes,
|
||||||
|
error: verifiedData.error,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
createBookingSuccessCounter.add(1, {
|
||||||
hotelId,
|
hotelId,
|
||||||
checkInDate,
|
checkInDate,
|
||||||
checkOutDate,
|
checkOutDate,
|
||||||
error_type: "validation_error",
|
|
||||||
})
|
})
|
||||||
|
|
||||||
console.error(
|
console.info(
|
||||||
"api.booking.create validation error",
|
"api.booking.create success",
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
query: loggingAttributes,
|
query: loggingAttributes,
|
||||||
error: verifiedData.error,
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
return null
|
return verifiedData.data
|
||||||
}
|
}),
|
||||||
|
|
||||||
createBookingSuccessCounter.add(1, {
|
|
||||||
hotelId,
|
|
||||||
checkInDate,
|
|
||||||
checkOutDate,
|
|
||||||
})
|
|
||||||
|
|
||||||
console.info(
|
|
||||||
"api.booking.create success",
|
|
||||||
JSON.stringify({
|
|
||||||
query: loggingAttributes,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
return verifiedData.data
|
|
||||||
}),
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user