1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Creating applications in Personio |
8 | * Allows you to create applications in Personio. |
9 | */ |
10 | export async function main( |
11 | auth: Personio, |
12 | X_Company_ID: string, |
13 | body: { |
14 | first_name: string |
15 | last_name: string |
16 | email: string |
17 | job_position_id: number |
18 | recruiting_channel_id?: number |
19 | external_posting_id?: string |
20 | message?: string |
21 | application_date?: string |
22 | phase?: { type: 'system' | 'custom'; id: string | number } |
23 | tags?: string[] |
24 | files?: { |
25 | uuid: string |
26 | original_filename: string |
27 | category: |
28 | | 'cv' |
29 | | 'cover-letter' |
30 | | 'employment-reference' |
31 | | 'certificate' |
32 | | 'work-sample' |
33 | | 'other' |
34 | }[] |
35 | attributes?: { id: string; value: string }[] |
36 | }, |
37 | X_Personio_Partner_ID?: string, |
38 | X_Personio_App_ID?: string |
39 | ) { |
40 | const url = new URL(`https://api.personio.de/v1/recruiting/applications`) |
41 |
|
42 | const response = await fetch(url, { |
43 | method: 'POST', |
44 | headers: { |
45 | ...(X_Personio_Partner_ID ? { 'X-Personio-Partner-ID': X_Personio_Partner_ID } : {}), |
46 | ...(X_Personio_App_ID ? { 'X-Personio-App-ID': X_Personio_App_ID } : {}), |
47 | 'X-Company-ID': X_Company_ID, |
48 | 'Content-Type': 'application/json', |
49 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
50 | }, |
51 | body: JSON.stringify(body) |
52 | }) |
53 | if (!response.ok) { |
54 | const text = await response.text() |
55 | throw new Error(`${response.status} ${text}`) |
56 | } |
57 | return await response.text() |
58 | } |
59 |
|
60 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
61 | const params = new URLSearchParams({ |
62 | grant_type: 'client_credentials', |
63 | client_id: auth.clientId, |
64 | client_secret: auth.clientSecret |
65 | }) |
66 |
|
67 | const response = await fetch(tokenUrl, { |
68 | method: 'POST', |
69 | headers: { |
70 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
71 | 'Content-Type': 'application/x-www-form-urlencoded' |
72 | }, |
73 | body: params.toString() |
74 | }) |
75 |
|
76 | if (!response.ok) { |
77 | const text = await response.text() |
78 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
79 | } |
80 |
|
81 | const data = await response.json() |
82 | return data.access_token |
83 | } |
84 | } |
85 |
|