1 | |
2 | type Personio = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Update document metadata. |
8 | * Updates the metadata of the Document with the provided Document ID. |
9 | */ |
10 | export async function main( |
11 | auth: Personio, |
12 | document_id: string, |
13 | body: { |
14 | id?: string |
15 | name?: string |
16 | date?: string |
17 | comment?: string |
18 | category?: { id?: string } |
19 | owner?: { id?: string } |
20 | document_type?: string |
21 | size?: number |
22 | created_at?: string |
23 | virus_scan?: { status?: 'unknown' | 'safe' | 'unsafe' } |
24 | esignature?: { |
25 | status?: |
26 | | 'status_unspecified' |
27 | | 'pending' |
28 | | 'signed' |
29 | | 'declined' |
30 | | 'canceled' |
31 | | 'expired' |
32 | | 'failed' |
33 | | 'email_bounced' |
34 | } |
35 | } & { owner?: { id?: {} } } |
36 | ) { |
37 | const url = new URL(`https://api.personio.de/v2/document-management/documents/${document_id}`) |
38 |
|
39 | const response = await fetch(url, { |
40 | method: 'PATCH', |
41 | headers: { |
42 | 'Content-Type': 'application/json', |
43 | Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token')) |
44 | }, |
45 | body: JSON.stringify(body) |
46 | }) |
47 | if (!response.ok) { |
48 | const text = await response.text() |
49 | throw new Error(`${response.status} ${text}`) |
50 | } |
51 | return await response.text() |
52 | } |
53 |
|
54 | async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> { |
55 | const params = new URLSearchParams({ |
56 | grant_type: 'client_credentials', |
57 | client_id: auth.clientId, |
58 | client_secret: auth.clientSecret |
59 | }) |
60 |
|
61 | const response = await fetch(tokenUrl, { |
62 | method: 'POST', |
63 | headers: { |
64 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
65 | 'Content-Type': 'application/x-www-form-urlencoded' |
66 | }, |
67 | body: params.toString() |
68 | }) |
69 |
|
70 | if (!response.ok) { |
71 | const text = await response.text() |
72 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
73 | } |
74 |
|
75 | const data = await response.json() |
76 | return data.access_token |
77 | } |
78 | } |
79 |
|