Merged in feat/sw-2866-move-partners-router-to-trpc-package (pull request #2414)
feat(sw-2866): Move partners router to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Move booking router to trpc package * Move partners router to trpc package * Merge branch 'master' into feat/sw-2866-move-partners-router-to-trpc-package Approved-by: Linus Flood
This commit is contained in:
129
packages/trpc/lib/routers/partners/jobylon/output.ts
Normal file
129
packages/trpc/lib/routers/partners/jobylon/output.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { dt } from "@scandic-hotels/common/dt"
|
||||
|
||||
const categoriesSchema = z
|
||||
.array(
|
||||
z
|
||||
.object({ category: z.object({ id: z.number(), text: z.string() }) })
|
||||
.transform(({ category }) => {
|
||||
return {
|
||||
id: category.id,
|
||||
text: category.text,
|
||||
}
|
||||
})
|
||||
)
|
||||
.transform((categories) =>
|
||||
categories.filter(
|
||||
(category): category is NonNullable<typeof category> => !!category
|
||||
)
|
||||
)
|
||||
|
||||
const departmentsSchema = z
|
||||
.array(
|
||||
z
|
||||
.object({
|
||||
department: z.object({
|
||||
id: z.number(),
|
||||
name: z.string(),
|
||||
}),
|
||||
})
|
||||
.transform(({ department }) => {
|
||||
if (!department.id || !department.name) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
id: department.id,
|
||||
name: department.name,
|
||||
}
|
||||
})
|
||||
)
|
||||
.transform((departments) =>
|
||||
departments.filter(
|
||||
(department): department is NonNullable<typeof department> => !!department
|
||||
)
|
||||
)
|
||||
|
||||
const locationsSchema = z
|
||||
.array(
|
||||
z
|
||||
.object({
|
||||
location: z.object({
|
||||
city: z.string().nullish(),
|
||||
country: z.string().nullish(),
|
||||
place_id: z.string().nullish(),
|
||||
country_short: z.string().nullish(),
|
||||
}),
|
||||
})
|
||||
.transform(({ location }) => {
|
||||
if (!location.city || !location.country) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
city: location.city,
|
||||
country: location.country,
|
||||
countryShort: location.country_short ?? null,
|
||||
placeId: location.place_id ?? null,
|
||||
}
|
||||
})
|
||||
)
|
||||
.transform((locations) =>
|
||||
locations.filter(
|
||||
(location): location is NonNullable<typeof location> => !!location
|
||||
)
|
||||
)
|
||||
|
||||
const urlsSchema = z
|
||||
.object({
|
||||
apply: z.string(),
|
||||
ad: z.string(),
|
||||
})
|
||||
.transform(({ ad }) => ad)
|
||||
|
||||
export const jobylonItemSchema = z
|
||||
.object({
|
||||
id: z.number(),
|
||||
title: z.string(),
|
||||
from_date: z.string().nullish(),
|
||||
to_date: z.string().nullish(),
|
||||
categories: categoriesSchema,
|
||||
departments: departmentsSchema,
|
||||
locations: locationsSchema,
|
||||
urls: urlsSchema,
|
||||
})
|
||||
.transform(
|
||||
({
|
||||
id,
|
||||
from_date,
|
||||
to_date,
|
||||
title,
|
||||
categories,
|
||||
departments,
|
||||
locations,
|
||||
urls,
|
||||
}) => {
|
||||
const now = dt.utc()
|
||||
const fromDate = from_date ? dt(from_date) : null
|
||||
const toDate = to_date ? dt(to_date) : null
|
||||
// Transformed to string as Dayjs objects cannot be passed to client components
|
||||
const toDateAsString = toDate?.toString() ?? null
|
||||
|
||||
return {
|
||||
id,
|
||||
title,
|
||||
isActive:
|
||||
fromDate &&
|
||||
now.isSameOrAfter(fromDate) &&
|
||||
(!toDate || now.isSameOrBefore(toDate)),
|
||||
categories,
|
||||
departments,
|
||||
toDate: toDateAsString,
|
||||
locations,
|
||||
url: urls,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export const jobylonFeedSchema = z
|
||||
.array(jobylonItemSchema)
|
||||
.transform((jobs) => jobs.filter((job) => job.isActive))
|
||||
70
packages/trpc/lib/routers/partners/jobylon/query.ts
Normal file
70
packages/trpc/lib/routers/partners/jobylon/query.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { getCacheClient } from "@scandic-hotels/common/dataCache"
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import { router } from "../../.."
|
||||
import { publicProcedure } from "../../../procedures"
|
||||
import { jobylonFeedSchema } from "./output"
|
||||
|
||||
export const TWENTYFOUR_HOURS = 60 * 60 * 24
|
||||
|
||||
// The URL for the Jobylon feed including the hash for the specific feed.
|
||||
// The URL and hash are generated by Jobylon. Documentation: https://developer.jobylon.com/feed-api
|
||||
const feedUrl =
|
||||
"https://feed.jobylon.com/feeds/cc04ba19-f0bd-4412-8b9b-d1d1fcbf0800"
|
||||
|
||||
export const jobylonQueryRouter = router({
|
||||
feed: router({
|
||||
get: publicProcedure.query(async function () {
|
||||
const jobylonFeedGetCounter = createCounter("trpc.jobylon.feed", "get")
|
||||
const metricsJobylonFeedGet = jobylonFeedGetCounter.init()
|
||||
|
||||
metricsJobylonFeedGet.start()
|
||||
|
||||
const url = new URL(feedUrl)
|
||||
url.search = new URLSearchParams({
|
||||
format: "json",
|
||||
}).toString()
|
||||
|
||||
const cacheClient = await getCacheClient()
|
||||
|
||||
const result = cacheClient.cacheOrGet(
|
||||
"jobylon:feed",
|
||||
async () => {
|
||||
const response = await fetch(url, {
|
||||
cache: "no-cache",
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
await metricsJobylonFeedGet.httpError(response)
|
||||
const text = await response.text()
|
||||
throw new Error(
|
||||
`Failed to fetch Jobylon feed: ${JSON.stringify({
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
text,
|
||||
})}`
|
||||
)
|
||||
}
|
||||
|
||||
const responseJson = await response.json()
|
||||
const validatedResponse = jobylonFeedSchema.safeParse(responseJson)
|
||||
|
||||
if (!validatedResponse.success) {
|
||||
metricsJobylonFeedGet.validationError(validatedResponse.error)
|
||||
throw new Error(
|
||||
`Failed to parse Jobylon feed: ${JSON.stringify(validatedResponse.error)}`
|
||||
)
|
||||
}
|
||||
|
||||
return validatedResponse.data
|
||||
},
|
||||
"1d"
|
||||
)
|
||||
|
||||
metricsJobylonFeedGet.success()
|
||||
|
||||
return result
|
||||
}),
|
||||
}),
|
||||
})
|
||||
Reference in New Issue
Block a user