1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * List compensations. |
8 | * Returns a list of payroll compensations of people for an authorized company. Compensations listed include base salary (excluding proration), hourly, one time compensation, recurring compensation, and bonuses. |
9 |
|
10 | */ |
11 | export async function main( |
12 | auth: Personio, |
13 | start_date: string | undefined, |
14 | end_date: string | undefined, |
15 | person_id: string | undefined, |
16 | legal_entity_id: string | undefined, |
17 | limit: string | undefined, |
18 | cursor: string | undefined |
19 | ) { |
20 | const url = new URL(`https://api.personio.de/v2/compensations`) |
21 | for (const [k, v] of [ |
22 | ['start_date', start_date], |
23 | ['end_date', end_date], |
24 | ['person.id', person_id], |
25 | ['legal_entity.id', legal_entity_id], |
26 | ['limit', limit], |
27 | ['cursor', cursor] |
28 | ]) { |
29 | if (v !== undefined && v !== '' && k !== undefined) { |
30 | url.searchParams.append(k, v) |
31 | } |
32 | } |
33 | const response = await fetch(url, { |
34 | method: 'GET', |
35 | headers: { |
36 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
37 | }, |
38 | body: undefined |
39 | }) |
40 | if (!response.ok) { |
41 | const text = await response.text() |
42 | throw new Error(`${response.status} ${text}`) |
43 | } |
44 | return await response.json() |
45 | } |
46 |
|
47 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
48 | const params = new URLSearchParams({ |
49 | grant_type: 'client_credentials', |
50 | client_id: auth.clientId, |
51 | client_secret: auth.clientSecret |
52 | }) |
53 |
|
54 | const response = await fetch(tokenUrl, { |
55 | method: 'POST', |
56 | headers: { |
57 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
58 | 'Content-Type': 'application/x-www-form-urlencoded' |
59 | }, |
60 | body: params.toString() |
61 | }) |
62 |
|
63 | if (!response.ok) { |
64 | const text = await response.text() |
65 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
66 | } |
67 |
|
68 | const data = await response.json() |
69 | return data.access_token |
70 | } |
71 |
|