Merged in feat/SW-1555-jobylon-integration (pull request #1484)
Feat/SW-1555 jobylon integration * feat(SW-1555): Added jobylon feed query * feat(SW-1555): Added jobylon feed component Approved-by: Fredrik Thorsson Approved-by: Matilda Landström
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import { router } from "@/server/trpc"
|
||||
|
||||
import { jobylonQueryRouter } from "./jobylon/query"
|
||||
import { sasRouter } from "./sas"
|
||||
|
||||
export const partnerRouter = router({ sas: sasRouter })
|
||||
export const partnerRouter = router({
|
||||
sas: sasRouter,
|
||||
jobylon: jobylonQueryRouter,
|
||||
})
|
||||
|
||||
121
apps/scandic-web/server/routers/partners/jobylon/output.ts
Normal file
121
apps/scandic-web/server/routers/partners/jobylon/output.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { dt } from "@/lib/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,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
return {
|
||||
id,
|
||||
title,
|
||||
isActive:
|
||||
fromDate &&
|
||||
now.isSameOrAfter(fromDate) &&
|
||||
(!toDate || now.isSameOrBefore(toDate)),
|
||||
categories,
|
||||
departments,
|
||||
toDate,
|
||||
locations,
|
||||
url: urls,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export const jobylonFeedSchema = z
|
||||
.array(jobylonItemSchema)
|
||||
.transform((jobs) => jobs.filter((job) => job.isActive))
|
||||
94
apps/scandic-web/server/routers/partners/jobylon/query.ts
Normal file
94
apps/scandic-web/server/routers/partners/jobylon/query.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { publicProcedure, router } from "@/server/trpc"
|
||||
|
||||
import { jobylonFeedSchema } from "./output"
|
||||
import {
|
||||
getJobylonFeedCounter,
|
||||
getJobylonFeedFailCounter,
|
||||
getJobylonFeedSuccessCounter,
|
||||
} from "./telemetry"
|
||||
|
||||
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 url = new URL(feedUrl)
|
||||
url.search = new URLSearchParams({
|
||||
format: "json",
|
||||
}).toString()
|
||||
const urlString = url.toString()
|
||||
|
||||
getJobylonFeedCounter.add(1, { url: urlString })
|
||||
console.info(
|
||||
"jobylon.feed start",
|
||||
JSON.stringify({ query: { url: urlString } })
|
||||
)
|
||||
|
||||
const response = await fetch(url, {
|
||||
cache: "force-cache",
|
||||
next: {
|
||||
revalidate: TWENTYFOUR_HOURS,
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
const error = {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
text,
|
||||
}
|
||||
getJobylonFeedFailCounter.add(1, {
|
||||
url: urlString,
|
||||
error_type: "http_error",
|
||||
error: JSON.stringify(error),
|
||||
})
|
||||
console.error(
|
||||
"jobylon.feed error",
|
||||
JSON.stringify({
|
||||
query: { url: urlString },
|
||||
error,
|
||||
})
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const responseJson = await response.json()
|
||||
const validatedResponse = jobylonFeedSchema.safeParse(responseJson)
|
||||
|
||||
if (!validatedResponse.success) {
|
||||
getJobylonFeedFailCounter.add(1, {
|
||||
urlString,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedResponse.error),
|
||||
})
|
||||
|
||||
console.error(
|
||||
"jobylon.feed error",
|
||||
JSON.stringify({
|
||||
query: { url: urlString },
|
||||
error: validatedResponse.error,
|
||||
})
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
getJobylonFeedSuccessCounter.add(1, {
|
||||
url: urlString,
|
||||
})
|
||||
console.info(
|
||||
"jobylon.feed success",
|
||||
JSON.stringify({
|
||||
query: { url: urlString },
|
||||
})
|
||||
)
|
||||
|
||||
return validatedResponse.data
|
||||
}),
|
||||
}),
|
||||
})
|
||||
@@ -0,0 +1,10 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
const meter = metrics.getMeter("trpc.booking")
|
||||
export const getJobylonFeedCounter = meter.createCounter("trpc.jobylon-feed")
|
||||
export const getJobylonFeedSuccessCounter = meter.createCounter(
|
||||
"trpc.jobylon-feed-success"
|
||||
)
|
||||
export const getJobylonFeedFailCounter = meter.createCounter(
|
||||
"trpc.jobylon-feed-fail"
|
||||
)
|
||||
Reference in New Issue
Block a user