The Pentacode API facilitates attendance tracking by allowing impromptu or scheduled work hour logging, requiring client-side effort to manage schedules and handle potential offline scenarios
The Pentacode API offers various endpoints for recording work hours either impromptu or based on a predefined schedule. Implementing a time logging flow requires multiple steps, and correctly enforcing schedules and time logging rules requires some work on the client side, especially if the time logging device is expected to operate with an unreliable internet connection. This guide will outline the various steps and techniques required to implement attendance recording using the Pentacode API.
Step 1: Register a new time logging device
In order to start recording time log events, you'll first have to create a new TimeLogDevice object. Time logging devices are used to make time log events more traceable by attributing them to a specific device. They can also be used to manage filters and other settings remotely without having to change these directly on the physical device. Lastly, we can use the TimeLogDevice object to connect a physical device to the Pentacode API. More about that in the next step.
To create a new TimeLogDevice, use the /time/log/devices endpoint:
POST /rest/v1/time/log/devices HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Bearer [auth token]
Host: api.pentacode.app
{
"filters": [ { "value":123, "type":"venue" } ],
"description": "Kitchen iPad"
}
{
"id": "4c4019bc-54d0-44c4-8970-f557c02c2235",
"description": "Kitchen iPad",
"filters": [
{
"value": 123,
"type": "venue"
}
],
"connectionCode": "K7NKFOI"
}
This will return the created device object, including the id and connectionCode properties, which will be important later.
Note that you'll need a valid auth token for this request. More about auth tokens and how to obtain one in the Authentication guide.
Step 2: Connect the client device
Now that you've created the TimeLogDevice object, it is time to connect the physical client device to the Pentacode API. Essentially the client will need two pieces of information to record time log events: the device id and a valid token for authenticating request. You can use the /time/log/connect endpoint with the value of the connectionCode property of the TimeLogDevice object (obtained in the previous step) to retrieve both:
POST /rest/v1/time/log/connect HTTP/1.1
Accept: application/json
Content-Type: application/json
Host: api.pentacode.app
{ "code": "K7NKFOI" }
{
"deviceId": "4c4019bc-54d0-44c4-8970-f557c02c2235",
"companyId": 123,
"authToken": "227d566831bcf31becee55f032bf3ff0fb9808f3053ae3fa42b78ae9eda23352"
}
The connection code is designed to be short and easily type-able, while still having enough entropy so serve as a secure single-use secret. The idea is that the end user can type in this code on the client device to easily connect to the API without having to go trough any additional steps authentication steps. The connection code is single-use and will be rotated after every successful connection request.
Alternatively, you can retrieve the auth token and device id on the server side and transmit it to the end device through other means. However, in this case you'll have to take your own steps to make sure the user is authenticated/authorised and the information transmitted securely.
Step 3: Fetch current time tracking options
Which time logging actions are available to a given employee at a certain time depends on the current shift schedule, as well as various time logging rules defined by the employer. Use the /time/log/options endpoint to retrieve information about the currently available time tracking options:
GET /rest/v1/time/log/options?timeFrom=2023-05-10T10%253A00%253A00&timeTo=2023-05-11T12%253A00%253A00&offset=0&limit=1&deviceId=4c4019bc-54d0-44c4-8970-f557c02c2235 HTTP/1.1
Accept: application/json
Authorization: Bearer [auth token]
Host: api.pentacode.app
{
"offset": 0,
"limit": 100,
"total": 376,
"data": [
{
"employee": {
"id": 39874,
"firstName": "Alvin",
"lastName": "Chipmunk",
"staffNumber": "48",
"avatar": "",
"timeLogPin": "3705"
},
"timeEntry": {
"id": "e2b1d676-0b2c-47e5-ac8f-98c5bad62b0a",
"date": "2023-05-03",
"status": "scheduled",
"startPlanned": "2023-05-03T05:00:00.000Z",
"endPlanned": "2023-05-03T13:00:00.000Z",
"startLogged": null,
"endLogged": null,
"startFinal": null,
"endFinal": null,
"currentBreakStart": null,
"type": "work",
"breaks": [],
"position": {
"id": 7541,
"name": "Koch"
}
},
"settings": {
"allowEarlyStart": 0.25,
"lockAfter": 12
},
"availableActions": [
"startShift"
]
},
{
"employee": {
"id": 39874,
"firstName": "Alvin",
"lastName": "Chipmunk",
"staffNumber": "48",
"avatar": "",,
"timeLogPin": "3705"
},
"settings": {
"allowEarlyStart": 0.25,
"lockAfter": 12
},
"position": {
"active": true,
"order": 2,
"id": 7541,
"name": "Spüler früh",
"departmentId": 4463,
"color": "#45aaf2"
},
"availableActions": [
"startUnscheduledShift"
]
}
]
}
Every TimeLogOption represents a scheduled shift or a position that is available for starting an unscheduled shift.
- The
employeeproperty contains basic information about the employee, including the staff number and a 4-digit PIN which is unique for each employee and intended to be used specifically as a simple method for authenticating employees. - The
settingsproperty is also present on every object and defines rules and constraints such as how much employees are allowed to deviate from their schedule. This information is essential if one wants to determine available time tracking option from an older list ofTimeLogOptions, as is necessary if the time logger is supposed to work offline. - The
availableActionsproperty indicates which time logging actions are available for a given shift at the time of the request. Please note that available actions may change based on the time and any changes to the settings or work schedule that may have occurred since the time of the request, so these are only reliable for a short time period after the request was made. If you can't ensure that you always have recent data (e.g. due to an unreliable internet connection), you can (and should) determine available actions based on a simple heuristic. More about the in the section working offline. - For scheduled shifts, The
timeEntryproperty contains information about the shifts status, start and end time, and area of work (position). This is useful for display purposes but can also be used to determine available actions in case theavailableActionsproperty is unreliable (as outlined above). - For employees that are allowed to start unscheduled shifts, the response will contain an entry for each position the employee is allowed to start an unscheduled shift for. In this, case the
positionproperty will contain the position name and id, the latter of which needs to be provided for allstartUnscheduledShiftactions.
Step 4: Post Time Log Event(s)
Finally, we'll want to actually record attendance using the API. We do this by posting TimeLogEvents to the /time/log/events endpoint:
POST /rest/v1/time/log/events HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Bearer [auth token]
Host: api.pentacode.app
{
"action": "startShift",
"time":"2023-05-05T05:55:00+02:00",
"timeEntryId":"e2b1d676-0b2c-47e5-ac8f-98c5bad62b0a",
"deviceId":"4c4019bc-54d0-44c4-8970-f557c02c2235",
}
{
"id": "f6a33790-31a9-4e89-ad9f-38523f708e1b",
"action": "startShift",
"time": "2023-05-05T05:55:00+02:00",
"timeEntryId": "e2b1d676-0b2c-47e5-ac8f-98c5bad62b0a",
"deviceId": "4c4019bc-54d0-44c4-8970-f557c02c2235",
"status": "accepted"
}
If the request is successful, the server will respond with the created TimeLogEvent object. Please note that receiving a successful response does not necessarily mean that the corresponding action was applied!. You can check the processing status of the event via the status property in the response. Time log events may be rejected for various reasons, like actions which are no longer valid (e.g. startShift for a shift that has already started) or other kinds of conflicts. In this case the status property will say rejected and the reason for the rejection can be found in the rejectedReason property.
How time log events are triggered will depend on the specific usage scenario. E.g. some employers will want to automatically start an employees shift when they enter the building using their key card, while others will want to apply a more deliberate approach where employees log their times manually. In either case the client application is responsible for identifying the employee (e.g. via fingerprint, key cards or using a pin code) and then choosing the appropriate action based on the information from the /time/log/options endpoint. In cases where more than one time log action is available (e.g.
startBreakandendShiftfor ongoing shifts) it may makes sense to prompt the employee to make a selection (if a suitable input device is available).
Adding Offline Support
Being able to function while being temporarily offline is an important prerequisite for any robust time logging system, as many workplaces have very slow and/or unreliable internet connections. While the Pentacode time logging API is designed specifically to allow for scenarios where devices may lose their connection for certain periods of time, properly handling these cases comes with some additional challenges. While implementing proper offline support is ultimately the responsibility of the API client, we'll outline some tips and techniques that may help with designing an offline-capable time logging system.
Cache time log options
One of the main challenges when dealing with an intermittent internet connection is that we might not be able to fetch fresh information about work schedules and other constraints at the moment a time log event is triggered. One way to deal with this is to fetch data from /time/log/options endpoint whenever possible and then store that data locally until we need it. Depending on how long you expect interruptions of the connection to last, you may want to adjust your timeTo parameter to fetch the work schedule for a longer time period in the future.
Determine available actions
Because we're relying on a locally cached data which may be several hours old, we might not have up-to-date information on which time logging actions are available at the moment. As outlined in Step 3, the availableActions property is only valid for a short period after the request was made. Without up-to-date information, we therefore have to determine which actions are available based on the known work schedule (which hopefully won't change too frequently). This can be done using a simple heuristic using the shift status, the scheduled start time and the constraints defined in the settings property of the TimeLogOption object:
- The shift is too far in the future (
currentTime < timeEntry.startPlanned - settings.allowEarlyStart) → No actions available. - The allowed time window for this shift has passed (
currentTime > timeEntry.startPlanned + settings.lockAfter) → No actions available - The shift status is
scheduled→ Available actions arestartShift - The shift status is
pending→ Available actions arestartBreakorendShift. - The shift status is
paused→ Available actions areendBreakorendShift. - The shift status is
completed→ No actions available
Keep a local event cue
Another challenge that comes with an unreliable connection is the fact that we may not always be able to receive an immediate response from the API when posting a TimeLogEvent. For obvious reasons, employees won't be able to wait for the internet connection to come back in order to start or end a shift. Therefore it will likely be necessary to keep a local record of which events need to be sent to the API. Once the connection is restored, this event cue can then be cleared by sending events to server in the order they were recorded.
Since available actions for a shift will change after recording an event, the status property of the locally stored shift should be adjusted accordingly so that the appropriate actions can be determined for subsequent events:
- When starting a shift (
startShift), the status should be changed fromscheduledtoongoing. - When starting a break (
startBreak), the status should be changed fromongoingtopaused. - When ending a break (
endBreak), the status should be changed frompausedtoongoing. - When ending a shift (
endShift), the status should be changed fromongoingorpausedtocompleted.
Recording unscheduled shifts
An additional challenge arises when recording unscheduled shifts while offline. First of all, after an unscheduled shift has been started, the startBreak, endBreak and endShift need to be made available even before the startUnscheduledShift event has been sent to the server. Therefore, the client is responsible for keeping track of these unscheduled shifts and update their status and available actions. Once the connection is restored, the startUnscheduledShift event for this shift needs to be processed first. Once the event is processed successfully, a new TimeEntry object will be created and the resulting timeEntryId will be returned in the response, which can then be referenced in any subsequent events.
