Merged in chore/refactor-hotel-trpc-routes (pull request #2891)
Chore/refactor hotel trpc routes * chore(SW-3519): refactor trpc hotel routers * chore(SW-3519): refactor trpc hotel routers * refactor * merge * Merge branch 'master' of bitbucket.org:scandic-swap/web into chore/refactor-hotel-trpc-routes Approved-by: Linus Flood
This commit is contained in:
46
packages/trpc/lib/routers/hotels/services/getCity.ts
Normal file
46
packages/trpc/lib/routers/hotels/services/getCity.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { getCacheClient } from "@scandic-hotels/common/dataCache"
|
||||
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
||||
|
||||
import * as api from "../../../api"
|
||||
import { type Cities, citiesSchema } from "../output"
|
||||
|
||||
import type { Endpoint } from "../../../api/endpoints"
|
||||
|
||||
const logger = createLogger("trpc:hotels:getCity")
|
||||
export async function getCity({
|
||||
cityUrl,
|
||||
serviceToken,
|
||||
}: {
|
||||
cityUrl: string
|
||||
serviceToken: string
|
||||
}): Promise<Cities> {
|
||||
const cacheClient = await getCacheClient()
|
||||
return await cacheClient.cacheOrGet(
|
||||
cityUrl,
|
||||
async () => {
|
||||
const url = new URL(cityUrl)
|
||||
const cityResponse = await api.get(
|
||||
url.pathname as Endpoint,
|
||||
{ headers: { Authorization: `Bearer ${serviceToken}` } },
|
||||
url.searchParams
|
||||
)
|
||||
|
||||
if (!cityResponse.ok) {
|
||||
return null
|
||||
}
|
||||
|
||||
const cityJson = await cityResponse.json()
|
||||
const city = citiesSchema.safeParse(cityJson)
|
||||
if (!city.success) {
|
||||
logger.error(`Validation of city failed`, {
|
||||
error: city.error,
|
||||
cityUrl,
|
||||
})
|
||||
return null
|
||||
}
|
||||
|
||||
return city.data
|
||||
},
|
||||
"1d"
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user