Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integrated fields endpoint for Greenhouse content #498

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _workers/prismic-if-greenhouse/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist/
20 changes: 20 additions & 0 deletions _workers/prismic-if-greenhouse/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "prismic-if-greenhouse",
"version": "0.0.0",
"private": true,
"type": "module",
"main": "./dist/worker.js",
"scripts": {
"wrangler": "wrangler"
},
"dependencies": {
"greenhouse-jobboard-js": "^0.3.0",
"worktop": "0.7.3"
},
"devDependencies": {
"@cloudflare/workers-types": "3.3.1",
"@cloudflare/wrangler": "1.19.8",
"esbuild": "0.14.22",
"typescript": "4.5.5"
}
}
103 changes: 103 additions & 0 deletions _workers/prismic-if-greenhouse/src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/// <reference types="@cloudflare/workers-types" />

import { Router } from 'worktop';
import * as CORS from 'worktop/cors';
import * as Cache from 'worktop/cache';
import { JobBoardClientV1 } from 'greenhouse-jobboard-js';

const API = new Router();

API.prepare = CORS.preflight();

const MAX_PER_PAGE = 50;

API.add('GET', '/if/boards/:boardToken/jobs', async (req, res) => {
const { boardToken } = req.params;
Comment on lines +14 to +15
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

글로벌 공고를 필드에서 활용하려면 알려진 보드 토큰을 다 긁도록 수정이 필요함

const page = +(req.query.get('page') || 1);
const offset = (page - 1) * MAX_PER_PAGE;

const jobBoardClient = new JobBoardClientV1({
boardToken,
client: jsonClient,
});

const jobs = await jobBoardClient.getJobListWithContent();
const jobsSlice = jobs.slice(offset, MAX_PER_PAGE);

const result: IntegrationFieldsFormat = {
results_size: jobs.length,
results: jobsSlice.map(job => ({
id: job.id.toString(),
title: job.title,
description: '',
image_url: '',
last_update: new Date(job.updated_at).getTime(),
blob: job,
})),
};

res.send(200, result);
});

API.add('GET', '/if/boards/:boardToken/departments', async (req, res) => {
const { boardToken } = req.params;
const page = +(req.query.get('page') || 1);
const offset = (page - 1) * MAX_PER_PAGE;

const jobBoardClient = new JobBoardClientV1({
boardToken,
client: jsonClient,
});

const departments = await jobBoardClient.getDepartmentList();
const leafDepartments = departments
.filter(department => department.id !== 0)
.filter(department => !department.child_ids.length);
const departmentsSlice = leafDepartments.slice(offset, MAX_PER_PAGE);

const result: IntegrationFieldsFormat = {
results_size: leafDepartments.length,
results: departmentsSlice.map(department => ({
id: department.id.toString(),
title: department.name,
description: '',
image_url: '',
last_update: Date.now(),
blob: department,
})),
};

res.send(200, result);
});

Cache.listen(API.run);

type IntegrationFieldsFormat = {
results_size: number,
results: IntegrationFieldsItem[],
};

type IntegrationFieldsItem = {
id: string,
title: string,
description: string,
image_url: string,
last_update: number,
blob: unknown,
};

const jsonClient = {
async get(url: URL) {
try {
const response = await fetch(url.toString());
const body = await response.json();
return body;
} catch (cause) {
throw new Error(
`Failed to fetch the resource from job board: ${url.toString()}`,
// @ts-ignore
{ cause },
);
}
},
};
8 changes: 8 additions & 0 deletions _workers/prismic-if-greenhouse/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"lib": ["DOM.Iterable", "WebWorker", "ES2020"],
"strict": true,
"esModuleInterop": true,
"downlevelIteration": true
}
}
12 changes: 12 additions & 0 deletions _workers/prismic-if-greenhouse/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name = 'prismic-if-greenhouse'
type = 'javascript'
compatibility_date = '2022-02-17'
account_id = 'aad5c82543cd1f267b89737d0f56405e'
usage_model = 'unbound'
workers_dev = true

[build]
command = 'yarn esbuild src/worker.ts --bundle --outfile=dist/worker.js --format=esm'

[build.upload]
format = 'service-worker'