//native
type Personio = {
clientId: string
clientSecret: string
}
/**
* Update Employee by ID
* Updates an existing employee. Note: Only the fields that are listed in the body example are updatable. Attributes that are not part of the sample request body but are present inside the request are ignored. It's not possible to update the Email field.
*/
export async function main(
auth: Personio,
employee_id: string,
body: {
employee?: {
first_name?: string
last_name?: string
preferred_name?: string
gender?: string
position?: string
subcompany?: string
department?: string
office?: string
hire_date?: string
weekly_working_hours?: number
status?: string
supervisor_id?: number
custom_attributes?: { 'dynamic_{{ field uid }}'?: string }
}
},
X_Personio_Partner_ID?: string,
X_Personio_App_ID?: string
) {
const url = new URL(`https://api.personio.de/v1/company/employees/${employee_id}`)
const response = await fetch(url, {
method: 'PATCH',
headers: {
...(X_Personio_Partner_ID ? { 'X-Personio-Partner-ID': X_Personio_Partner_ID } : {}),
...(X_Personio_App_ID ? { 'X-Personio-App-ID': X_Personio_App_ID } : {}),
'Content-Type': 'application/json',
Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token'))
},
body: JSON.stringify(body)
})
if (!response.ok) {
const text = await response.text()
throw new Error(`${response.status} ${text}`)
}
return await response.json()
}
async function getOAuthToken(auth: Personio, tokenUrl: string): Promise<string> {
const params = new URLSearchParams({
grant_type: 'client_credentials',
client_id: auth.clientId,
client_secret: auth.clientSecret
})
const response = await fetch(tokenUrl, {
method: 'POST',
headers: {
Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`),
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params.toString()
})
if (!response.ok) {
const text = await response.text()
throw new Error(`OAuth token request failed: ${response.status} ${text}`)
}
const data = await response.json()
return data.access_token
}
Submitted by hugo697 235 days ago