Skip to content

Commit

Permalink
Load employee data from personio api
Browse files Browse the repository at this point in the history
  • Loading branch information
malte-laukoetter committed Dec 20, 2023
1 parent 31c0674 commit f146a07
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
NOCODB_BASE_URL="https://metrics.ds4g.dev:38081"
NOCODB_AUTH_TOKEN="YOUR_NOCODB_AUTH_TOKEN"
SESSION_COOKIE_SECRET="Your secret"
PERSONIO_CLIENT_ID="your-personio-client-id"
PERSONIO_CLIENT_SECRET="your-personio-client-secret"
7 changes: 4 additions & 3 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ fileignoreconfig:
- filename: SECURITY.md
checksum: b1743150cdd537be3a66f5308f887d130f0f320ab21628b63713808090a84e3f
- filename: .env.example
checksum: d8f34bb85e049048eda804484e9928cc82d9524b639a23117b2faf94fcd9a8ab
checksum: 76b8d5ee9d1a3dcddb4efc166ff96c05ae787fab634b60993c43d827af7cad71
- filename: start.sh
checksum: 7546c3e95fbdb1c515680d6319ee31ee6c27aef9aa7415f132d63700f9692226
checksum: 8d64dc09bdde717b6b1057b2177e594d177fd1d5d80b12fa173ceacd6c8f1109
- filename: app/tasks/nocodbClient.server.ts
checksum: f4bcc85b1fbacd2a8dd8da7675849a40024fb1f7d909b4692954412aa2473319
- filename: app/mocks/stubs/calculationPositions.json
Expand All @@ -31,7 +31,8 @@ fileignoreconfig:
allowed_patterns: [password]
- filename: app/sessions.server.ts
allowed_patterns: [sessionCookieSecret, SESSION_COOKIE_SECRET]

- filename: app/personio/PersonioApiController.ts
allowed_patterns: [PERSONIO_CLIENT_SECRET, client_secret]
version: ""
scopeconfig:
- scope: node
Expand Down
92 changes: 92 additions & 0 deletions app/personio/PersonioApiController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { convertTimeStringToFloat } from "~/utils/timeConverter";
import type { PersonioApiEmployee } from "./PersonioApiEmployee";

const PERSONIO_BASE_URL = "https://api.personio.de/v1";
const PERSONIO_AUTH_URL = `${PERSONIO_BASE_URL}/auth`;
const PERSONIO_EMPLOYEES_URL = `${PERSONIO_BASE_URL}/company/employees`;

const PERSONIO_PARTNER_ID = "DIGITALSERVICE";

async function getAuthToken(): Promise<string> {
console.log("Loading new Personio auth token...");

const response = await fetch(PERSONIO_AUTH_URL, {
method: "POST",
headers: {
"X-Personio-Partner-ID": PERSONIO_PARTNER_ID,
},
body: JSON.stringify({
client_id: process.env.PERSONIO_CLIENT_ID,
client_secret: process.env.PERSONIO_CLIENT_SECRET,
}),
});

const { success, data } = (await response.json()) as {
success: true;
data: {
token: string;
expires_in: number;
scope: string;
};
};

if (!success) {
console.error(
"Couldn't load new Personio auth token! Is the client_id and client_secret still correct?",
);
throw new Error("Personio Api Auth Failed");
}

return data.token;
}

let authToken: string | undefined;
async function fetchWithPersonioAuth(
input: RequestInfo | URL,
init?: RequestInit | undefined,
): Promise<Response> {
if (authToken === undefined) {
authToken = await getAuthToken();
}

const fetchData = (authToken: string) => {
return fetch(input, {
...init,
headers: {
Authorization: `Bearer ${authToken}`,
"X-Personio-Partner-ID": PERSONIO_PARTNER_ID,
...init?.headers,
},
});
};

const response = await fetchData(authToken);
if (response.status === 401) {
authToken = await getAuthToken();
return fetchData(authToken);
}

return response;
}

export async function getEmployeeData(mailAddress: string) {
const url = new URL(PERSONIO_EMPLOYEES_URL);
url.searchParams.set("email", mailAddress);

const response = await fetchWithPersonioAuth(url);

const employeeData: PersonioApiEmployee = await response.json();

const schedule =
employeeData.data[0].attributes.work_schedule.value.attributes;
return {
id: employeeData.data[0].attributes.id.value,
workingHours: {
monday: convertTimeStringToFloat(schedule.monday),
tuesday: convertTimeStringToFloat(schedule.tuesday),
wednesday: convertTimeStringToFloat(schedule.wednesday),
thursday: convertTimeStringToFloat(schedule.thursday),
friday: convertTimeStringToFloat(schedule.friday),
},
};
}
24 changes: 24 additions & 0 deletions app/personio/PersonioApiEmployee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export interface PersonioApiEmployee {
success: true;
data: {
type: "Employee";
attributes: {
id: {
value: number;
};
work_schedule: {
value: {
attributes: {
monday: string;
tuesday: string;
wednesday: string;
thursday: string;
friday: string;
saturday: string;
sunday: string;
};
};
};
};
}[];
}
2 changes: 2 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ set -euf
export NOCODB_AUTH_TOKEN="${NOCODB_AUTH_TOKEN:=$(cat /etc/nocodb-credentials/authToken)}"
export NOCODB_BASE_URL="${NOCODB_BASE_URL:=$(cat /etc/nocodb-credentials/baseUrl)}"
export SESSION_COOKIE_SECRET="${SESSION_COOKIE_SECRET:=$(cat /etc/session-cookie-secret/secret)}"
export PERSONIO_CLIENT_ID="${PERSONIO_CLIENT_ID:=$(cat /etc/personio-credentials/client_id)}"
export PERSONIO_CLIENT_SECRET="${PERSONIO_CLIENT_SECRET:=$(cat /etc/personio-credentials/client_secret)}"

npm run start

0 comments on commit f146a07

Please sign in to comment.