The employee with external ID 1042 clocks in at 09:02:11. The message reaches the server five minutes later. The app should show the original attendance time under the correct employee, and keep a single record if the device sends the event again.
The awkward case comes when 1042 has no matching employee yet. Discard the message and the attendance record is lost. Keep it in a durable inbox and you can finish processing once the mapping exists.
We’ll build that path around an existing backend, employee database and API-driven interface. The identifiers and device protocol are fictional; the TypeScript snippets show the integration points you would implement for your own device.
1. Establish how the data arrives#
Start with the protocol supported by the device and its firmware version. Some devices can send events to a server configured in their settings. Other integrations require reading records through an SDK or an intermediary application's API.
| Integration method | When does it fit? |
|---|---|
| The device sends events to your service | It supports a custom destination and can reach the service |
| Your service periodically requests records | A read interface is available and receiving data in batches is sufficient |
| You integrate with device management software | That software provides the available integration point for the system |
We will use device-initiated delivery as our example. Its message might be tab-separated text, JSON or another format defined by the protocol. We need a person identifier and an event time, along with trusted context identifying the sending device.
In this setup, the device handles fingerprint recognition and sends the resulting attendance event.
2. Keep protocol details in a separate layer#
Design an adapter that reads the device format and converts it into a clear internal representation. The adapter understands the integration's field names, delimiters and time formats, and returns data the rest of the service can use.
For the adapter example, use this fictional device message:
{
"event": "evt-7301",
"person": "1042",
"time": "2026-10-19 09:02:11"
}In this example, we assume the event ID stays the same when retransmitted and is unique throughout the device's history. After validating the fields, the adapter converts the message to:
type AttendanceEvent = {
externalPersonId: string;
occurredAtLocal: string;
sourceEventId?: string;
};
const event: AttendanceEvent = {
externalPersonId: "1042",
occurredAtLocal: "2026-10-19T09:02:11",
sourceEventId: "evt-7301",
};The adapter gives the rest of the application a stable field name, externalPersonId, even if another device calls it something else.
Validate incoming fields at runtime before creating this object. Keep sourceEventId optional for protocols that omit an event identifier.
Define the required fields, accepted time formats, request size and record count limits in this layer. If a batch contains an incomplete row, choose an explicit policy: reject the batch, or accept valid records while recording an outcome for each excluded record.
Store the validated event and its source context in a durable inbox before processing. That gives the service somewhere to resume after a failed attempt; section six covers when to acknowledge receipt.
3. Separate device, organization and employee identities#
A person's number on a device is not always enough to identify them in the application. Two independent devices might use 1042 for different people. An organization might instead use the same number for the same person across all its devices.
Define the identifier's scope before designing the mapping table:
| Numbering scheme | Mapping key for the example |
|---|---|
| Numbers are consistent across the organization | Organization plus person number |
| Each device has its own numbering | Device plus person number |
Resolve the employee within the configured identity scope:
const employee = await identities.resolve({
organizationId: context.organizationId,
scopeId: context.identityScopeId,
externalPersonId: event.externalPersonId,
});
if (!employee) {
return {
status: "unmapped",
externalPersonId: event.externalPersonId,
};
}identities.resolve represents the mapping lookup you need to implement. Build context from the verified connection and device configuration, keeping organization and scope selection under server control.
For this example, the connection gives us organizationId = org-demo, deviceId = device-demo and identityScopeId = roster-demo. The lookup resolves 1042 to employee-demo within that scope.
Authenticate the sender using the mechanisms available to the device and network; a device ID can be copied. If a reverse proxy supplies connection information, configure which sources the service trusts.
4. Preserve what the time means#
Store two timestamps: occurredAt for the attendance event and receivedAt for its arrival at the service.
For this example, assume the device is configured with a fixed offset of +03:00. The service places that value in context.utcOffset and captures the arrival time in context.receivedAt. After validating the local time and offset, the conversion in this example is:
const occurredAt = new Date(
`${event.occurredAtLocal}${context.utcOffset}`,
).toISOString();The result is 2026-10-19T06:02:11.000Z. The request arrived at 2026-10-19T06:07:11.000Z, but the attendance time remains 09:02:11 when displayed using the same offset.
We prepare the record for storage with its context and source identifiers:
const record = {
organizationId: context.organizationId,
employeeId: employee.id,
deviceId: context.deviceId,
externalPersonId: event.externalPersonId,
sourceEventId: event.sourceEventId ?? null,
sourceLocalTime: event.occurredAtLocal,
sourceUtcOffset: context.utcOffset,
occurredAt,
receivedAt: context.receivedAt,
};Capture reception time when the message arrives, even if processing is delayed. When an event is retransmitted, you can record the new attempt's arrival without replacing the event's first reception time.
Do not append Z to a local time just to format it; Z means the value is in UTC. Similarly, replacing the space between the date and time with T changes only the text format.
This conversion assumes a fixed offset. For a source timestamp in UTC or with its own explicit offset, preserve that meaning. For a named time zone, use its rules on the event’s date and handle repeated or skipped local times during clock changes. JavaScript’s Date constructor cannot infer the device’s time-zone configuration.
5. Define the event's identity, then store it#
In our example, (org-demo, device-demo, evt-7301) identifies the source event. This identity is separate from the employee numbering scope used for mapping. If the same message arrives again, we want to recognize the existing event.
We assume the event ID is stable and unique within the device for as long as we retain records. A database constraint can enforce this condition, as in this instructional PostgreSQL example:
ALTER TABLE attendance_events
ADD CONSTRAINT attendance_source_event_unique
UNIQUE (organization_id, device_id, source_event_id);The insert operation uses this constraint to handle conflicts, rather than only checking whether a record exists and inserting it in a separate step. If the event already exists, return its result without creating another copy or overwriting an administrative correction.
The constraint needs non-null organization, device and event IDs to identify a duplicate. For a protocol without a stable event ID, derive a key from the available fields and their time precision, then test two legitimate events close together. They must survive as separate records.
If a retransmission and a new event have identical source data, the receiver has no reliable way to tell them apart. Keep the original data for review and link administrative corrections to the event without changing its identity.
When do you need a transaction or a lock?#
If you save an attendance record and update its processing state in the same database, group those changes in a database transaction. If you also need to coordinate concurrent changes to a set of an employee's records, locking the appropriate row inside the transaction can be an additional option. A lock coordinates operations that honor it; it is not required for every event insert.
Also define the transaction boundary for batches. A transaction per record allows independent outcomes, while a transaction for the whole batch commits or rolls back its changes together. Choose behavior that fits the receiving protocol and make the outcome testable.
6. Make processing outcomes explicit#
For this example, we choose a protocol that allows the service to acknowledge acceptance for later processing. The service first stores the event and its context in the durable inbox, then sends the acknowledgement required by the protocol. If durable storage fails, do not acknowledge successful acceptance. A protocol that requires processing to finish before acknowledgement needs a different sequence.
If 1042 has no matching employee, the event remains pending mapping, with its source identity and original time intact. Once the mapping is added, the stored event is processed again. This path does not require the device to resend it.
Accepting an event into the inbox and creating an attendance record are separate outcomes. A processing attempt's result can be represented as:
type ImportOutcome =
| { status: "created"; recordId: string }
| { status: "alreadyExists"; recordId: string }
| { status: "unmapped"; externalPersonId: string }
| { status: "rejected"; reason: string };Use these statuses internally. The device’s acknowledgement must still follow its protocol.
The inbox also deduplicates by event identity so that retransmissions do not create separate pending items for the same event. Once processing completes, link the storage outcome to the original event. If processing is retried after a failure, the attendance record's identity constraint still prevents a second copy.
Count received messages, pending events and created attendance records separately so retransmissions do not inflate the attendance count.
7. Separate displaying data from receiving it#
The interface reads mapped, stored records through the application API. Apply user permissions and organization scope to each read, including requests that supply an employee ID.
After processing our event, the application API could return:
{
"recordId": "attendance-demo",
"employeeId": "employee-demo",
"occurredAt": "2026-10-19T06:02:11.000Z"
}The interface opens employee-demo and formats occurredAt with the example’s +03:00 offset, showing 09:02:11. Using the arrival timestamp instead would show the employee five minutes late.
Determine how the screen needs to refresh. A record reviewed when a user opens an employee profile may only need on-demand fetching and refetching. A continuously monitored screen might need polling, or SSE or WebSocket notifications that prompt it to refresh its data.
After ingestion commits, an open screen may still have an older cached response. Set the cache’s refresh policy alongside the polling or notification mechanism.
Display the basic attendance record first: who it belongs to, its time and its source. Add working-hours and shift rules as a separate stage, because interpreting a set of records requires more than receiving them.
8. Test one event through the entire path#
Start with a test environment, a fictional employee and a known external ID. Send an event from a configured device or a simulator that follows the protocol format, then trace it through to the interface.
With our example data, the expected result is that the first arrival saves evt-7301 and produces attendance record attendance-demo for employee-demo. Resending the event does not increase the attendance record count. If it is processed again after being saved, the outcome is alreadyExists. After fetching the data, the interface shows one record at 09:02:11.
| Test | What it should establish |
|---|---|
| Valid event for a mapped person | It reaches the intended organization and employee context |
| Same number in two different scopes | Mapping uses the correct scope without mixing identities |
| Invalid time or missing required field | The defined validation policy applies and produces a clear outcome |
| Retransmission of a stored event | It creates no additional copy under the matching key |
| Two distinct events close together | They remain separate; the matching key distinguishes them |
| Administrative correction followed by replay of the original event | Event identity remains stable under the correction design |
| A change fails inside a transaction | Changes belonging to that transaction roll back |
| A record arrives late | Event time remains separate from reception time |
| Event for an unmapped person, followed by adding the mapping | The stored event is processed without requiring the device to resend it |
| Saving the event to the inbox fails | No successful acceptance acknowledgement is issued before durable storage |
| The interface fetches the record | It displays the employee and time matching the stored data |
For the first test run, keep the device message, normalized event, database row and API response side by side. Check evt-7301 and 09:02:11 at each step. When adding another device model, rerun the format, identity and retransmission cases against its actual protocol.