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