Edits history of script submission #17758 for ' Passing applications to Personio (personio)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Personio = {
    	clientId: string
    	clientSecret: string
    }
    type Base64 = string
    /**
     * Passing applications to Personio
     *  DEPRECATED: This method of passing application to Personio is deprecated and will only receive bugfixes.
     */
    export async function main(
    	auth: Personio,
    	X_Company_ID: string,
    	body: {
    		company_id: number
    		access_token: string
    		job_position_id: number
    		first_name: string
    		last_name: string
    		email: string
    		gender?: 'undefined' | 'male' | 'female' | 'diverse'
    		recruiting_channel_id?: number
    		external_posting_id?: string
    		phone?: string
    		location?: string
    		salary_expectations?: string
    		available_from?: string
    		'categorised_documents[n][file]'?: {
    			base64: Base64
    			type:
    				| 'image/png'
    				| 'image/jpeg'
    				| 'image/gif'
    				| 'application/pdf'
    				| 'appication/json'
    				| 'text/csv'
    				| 'text/plain'
    				| 'audio/mpeg'
    				| 'audio/wav'
    				| 'video/mp4'
    			name: string
    		}
    		'categorised_documents[n][category]'?: string
    		'documents[n]'?: {
    			base64: Base64
    			type:
    				| 'image/png'
    				| 'image/jpeg'
    				| 'image/gif'
    				| 'application/pdf'
    				| 'appication/json'
    				| 'text/csv'
    				| 'text/plain'
    				| 'audio/mpeg'
    				| 'audio/wav'
    				| 'video/mp4'
    			name: string
    		}
    		'document{n}'?: { file?: string }
    		message?: string
    		tags?: {}
    		birthday?: string
    		'custom_attribute_{id}'?: number
    	},
    	X_Personio_Partner_ID?: string,
    	X_Personio_App_ID?: string
    ) {
    	const url = new URL(`https://api.personio.de/recruiting/applicant`)
    
    	const formData = new FormData()
    	for (const [k, v] of Object.entries(body)) {
    		if (v !== undefined && v !== '') {
    			if (['categorised_documents[n][file]', 'documents[n]'].includes(k)) {
    				const { base64, type, name } = v as {
    					base64: Base64
    					type: string
    					name: string
    				}
    				formData.append(
    					k,
    					new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
    						type
    					}),
    					name
    				)
    			} else {
    				formData.append(k, String(v))
    			}
    		}
    	}
    	const response = await fetch(url, {
    		method: 'POST',
    		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 } : {}),
    			'X-Company-ID': X_Company_ID,
    			Authorization: 'Bearer ' + (await getOAuthToken(auth, 'https://api.personio.de/oauth2/token'))
    		},
    		body: formData
    	})
    	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