//native
type Personio = {
clientId: string;
clientSecret: string;
};
/**
* Create a new Person and Employment.
* - This endpoint enables the creation of a Person resource and an associated Employment resource.
- The endpoint requires the personio:persons:write scope.
*/
export async function main(
auth: Personio,
body: {
email?: string;
first_name?: string;
preferred_name?: string;
last_name?: string;
gender?: "MALE" | "FEMALE" | "DIVERSE" | "UNKNOWN" | "UNDEFINED";
language_code?: "en" | "de" | "es" | "fr" | "nl" | "it" | "sv" | "fi";
custom_attributes?: { id?: string; value?: string | string[] }[];
} & {
employments?: {
supervisor?: { id: string };
office?: { id: string };
org_units?: { type: string; id: string }[];
subcompany?: { id: string };
legal_entity?: { id: string };
position?: { title: string };
status?: "ACTIVE" | "INACTIVE" | "ONBOARDING" | "LEAVE";
employment_start_date?: string;
type?: "INTERNAL" | "EXTERNAL";
contract_end_date?: string;
probation_end_date?: string;
probation_period_length?: number;
weekly_working_hours?: number;
full_time_weekly_working_hours?: number;
cost_centers?: { id: string; weight: number }[];
}[];
} & {},
) {
const url = new URL(`https://api.personio.de/v2/persons`);
const response = await fetch(url, {
method: "POST",
headers: {
"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