1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Update Attendance by ID |
8 | * This endpoint is responsible for updating attendance data for the company employees. Attributes are not required and if not specified, the current value will be used. It is not possible to change the employee id. |
9 | */ |
10 | export async function main( |
11 | auth: Personio, |
12 | id: string, |
13 | body: { |
14 | date?: string |
15 | start_time?: string |
16 | end_time?: string |
17 | break?: number |
18 | comment?: string |
19 | project_id?: number |
20 | skip_approval?: false | true |
21 | }, |
22 | X_Personio_Partner_ID?: string, |
23 | X_Personio_App_ID?: string |
24 | ) { |
25 | const url = new URL(`https://api.personio.de/v1/company/attendances/${id}`) |
26 |
|
27 | const response = await fetch(url, { |
28 | method: 'PATCH', |
29 | headers: { |
30 | ...(X_Personio_Partner_ID ? { 'X-Personio-Partner-ID': X_Personio_Partner_ID } : {}), |
31 | ...(X_Personio_App_ID ? { 'X-Personio-App-ID': X_Personio_App_ID } : {}), |
32 | 'Content-Type': 'application/json', |
33 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
34 | }, |
35 | body: JSON.stringify(body) |
36 | }) |
37 | if (!response.ok) { |
38 | const text = await response.text() |
39 | throw new Error(`${response.status} ${text}`) |
40 | } |
41 | return await response.json() |
42 | } |
43 |
|
44 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
45 | const params = new URLSearchParams({ |
46 | grant_type: 'client_credentials', |
47 | client_id: auth.clientId, |
48 | client_secret: auth.clientSecret |
49 | }) |
50 |
|
51 | const response = await fetch(tokenUrl, { |
52 | method: 'POST', |
53 | headers: { |
54 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
55 | 'Content-Type': 'application/x-www-form-urlencoded' |
56 | }, |
57 | body: params.toString() |
58 | }) |
59 |
|
60 | if (!response.ok) { |
61 | const text = await response.text() |
62 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
63 | } |
64 |
|
65 | const data = await response.json() |
66 | return data.access_token |
67 | } |
68 |
|