1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Delete Absence Period by ID |
8 | * Deletes absence period data for absence types with **time unit** set to **hours**. |
9 | */ |
10 | export async function main( |
11 | auth: Personio, |
12 | id: string, |
13 | X_Personio_Partner_ID?: string, |
14 | X_Personio_App_ID?: string |
15 | ) { |
16 | const url = new URL(`https://api.personio.de/v1/company/absence-periods/${id}`) |
17 |
|
18 | const response = await fetch(url, { |
19 | method: 'DELETE', |
20 | headers: { |
21 | ...(X_Personio_Partner_ID ? { 'X-Personio-Partner-ID': X_Personio_Partner_ID } : {}), |
22 | ...(X_Personio_App_ID ? { 'X-Personio-App-ID': X_Personio_App_ID } : {}), |
23 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
24 | }, |
25 | body: undefined |
26 | }) |
27 | if (!response.ok) { |
28 | const text = await response.text() |
29 | throw new Error(`${response.status} ${text}`) |
30 | } |
31 | return await response.json() |
32 | } |
33 |
|
34 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
35 | const params = new URLSearchParams({ |
36 | grant_type: 'client_credentials', |
37 | client_id: auth.clientId, |
38 | client_secret: auth.clientSecret |
39 | }) |
40 |
|
41 | const response = await fetch(tokenUrl, { |
42 | method: 'POST', |
43 | headers: { |
44 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
45 | 'Content-Type': 'application/x-www-form-urlencoded' |
46 | }, |
47 | body: params.toString() |
48 | }) |
49 |
|
50 | if (!response.ok) { |
51 | const text = await response.text() |
52 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
53 | } |
54 |
|
55 | const data = await response.json() |
56 | return data.access_token |
57 | } |
58 |
|