mirror of
https://github.com/colanode/colanode.git
synced 2025-12-29 00:25:03 +01:00
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import {
|
|
InteractionAttribute,
|
|
InteractionAttributes,
|
|
} from '../types/interactions';
|
|
import { compareDate, compareString } from './utils';
|
|
|
|
export const mergeInteractionAttributes = (
|
|
attributes: InteractionAttributes | null | undefined,
|
|
attribute: InteractionAttribute,
|
|
value: string
|
|
): InteractionAttributes | null => {
|
|
if (!attributes) {
|
|
return { [attribute]: value };
|
|
}
|
|
|
|
if (attribute === 'firstSeenAt') {
|
|
const date = new Date(value);
|
|
|
|
if (
|
|
!attributes.firstSeenAt ||
|
|
compareDate(attributes.firstSeenAt, date) < 0
|
|
) {
|
|
return { ...attributes, firstSeenAt: date };
|
|
}
|
|
}
|
|
|
|
if (attribute === 'lastSeenAt') {
|
|
const date = new Date(value);
|
|
|
|
if (
|
|
!attributes.lastSeenAt ||
|
|
compareDate(attributes.lastSeenAt, date) > 0
|
|
) {
|
|
return { ...attributes, lastSeenAt: date };
|
|
}
|
|
}
|
|
|
|
if (attribute === 'lastReceivedTransactionId') {
|
|
if (
|
|
!attributes.lastReceivedTransactionId ||
|
|
compareString(attributes.lastReceivedTransactionId, value) > 0
|
|
) {
|
|
return { ...attributes, lastReceivedTransactionId: value };
|
|
}
|
|
}
|
|
|
|
if (attribute === 'lastSeenTransactionId') {
|
|
if (
|
|
!attributes.lastSeenTransactionId ||
|
|
compareString(attributes.lastSeenTransactionId, value) > 0
|
|
) {
|
|
return { ...attributes, lastSeenTransactionId: value };
|
|
}
|
|
}
|
|
|
|
return null;
|
|
};
|