Skip to content

Commit

Permalink
Merge pull request #32 from openintegrationhub/initialSnapshot
Browse files Browse the repository at this point in the history
InitialSnapshot support
  • Loading branch information
SvenHoeffler committed Mar 12, 2024
2 parents 82605aa + c9d1561 commit 6e7c746
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
6 changes: 5 additions & 1 deletion templates/lib/triggers/trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

const spec = require("../spec.json");
const { dataAndSnapshot, getMetadata, getElementDataFromResponse, executeCall } = require("../utils/helpers");
const { dataAndSnapshot, getMetadata, getElementDataFromResponse, executeCall, getInitialSnapshotValue } = require("../utils/helpers");
const { createPaginator } = require("../utils/paginator");
const componentJson = require("../../component.json");

Expand All @@ -35,6 +35,10 @@ async function processTrigger(msg, cfg, snapshot, incomingMessageHeaders, tokenD

logger.info("Incoming snapshot: %j", snapshot);

snapshot.lastUpdated = getInitialSnapshotValue(cfg, snapshot);

logger.info("Using snapshot: %j", snapshot);

logger.info(
'Trigger settings - "snapshotKey": %s, "arraySplittingKey": %s, "syncParam": %s, "skipSnapshot": %s',
snapshotKey,
Expand Down
22 changes: 22 additions & 0 deletions templates/lib/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,27 @@ function getElementDataFromResponse(splittingKey, res) {
return splittingKey.split(".").reduce((p, c) => (p && p[c]) || null, res);
}
}

function getInitialSnapshotValue(cfg, snapshot) {
let initialSnapshot;

if (snapshot && snapshot.lastUpdated) {
initialSnapshot = snapshot.lastUpdated;
} else {
initialSnapshot = new Date(0).getTime();
}

if (cfg && cfg.nodeSettings && cfg.nodeSettings.initialSnapshot) {
const initial = dayjs(cfg.nodeSettings.initialSnapshot);
const incoming = dayjs(initialSnapshot);
if (initial.isValid && incoming.isValid && initial.isAfter(incoming)) {
initialSnapshot = cfg.nodeSettings.initialSnapshot;
}
}

return initialSnapshot;
}

module.exports = {
compareDate,
mapFieldNames,
Expand All @@ -235,4 +256,5 @@ module.exports = {
mapFormDataBody,
isMicrosoftJsonDate,
executeCall,
getInitialSnapshotValue
};
37 changes: 36 additions & 1 deletion templates/lib/utils/helpers.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const {
isMicrosoftJsonDate
isMicrosoftJsonDate,
getInitialSnapshotValue,
} = require("./helpers");
const dayjs = require('dayjs');

describe("Helpers", () => {
describe("isMicrosoftJsonDate", () => {
Expand All @@ -14,4 +16,37 @@ describe("Helpers", () => {
expect(date).toEqual(null);
});
});

describe("getInitialSnapshotValue", () => {
it("should return null date when nothing is set", () => {
const initialSnapshot = getInitialSnapshotValue({}, {});
expect(initialSnapshot).toEqual(new Date(0).getTime());
});

it("should return received snapshot value when no initial Snapshot is set", () => {
const initialSnapshot = getInitialSnapshotValue({}, { lastUpdated: dayjs('10-20-2020').format() });
expect(initialSnapshot).toEqual(dayjs('10-20-2020').format());
});

it("should return initialSnapshot value when no snapshot is received", () => {
const initialSnapshot = getInitialSnapshotValue({ nodeSettings: { initialSnapshot: dayjs('10-19-2020').format() } }, {});
expect(initialSnapshot).toEqual(dayjs('10-19-2020').format());
});

it("should return received snapshot value when it is newer than initial snapshot", () => {
const initialSnapshot = getInitialSnapshotValue({ nodeSettings: { initialSnapshot: dayjs('10-19-2020').format() } }, { lastUpdated: dayjs('10-20-2020').format() });
expect(initialSnapshot).toEqual(dayjs('10-20-2020').format());
});

it("should return initialSnapshot value when it is is newer than received snapshot", () => {
const initialSnapshot = getInitialSnapshotValue({ nodeSettings: { initialSnapshot: dayjs('10-21-2020').format() } }, { lastUpdated: dayjs('10-20-2020').format() });
expect(initialSnapshot).toEqual(dayjs('10-21-2020').format());
});

it("should return received snapshot if initialSnapshot is malformed", () => {
const initialSnapshot = getInitialSnapshotValue({ nodeSettings: { initialSnapshot: dayjs('1021-2020').format() } }, { lastUpdated: dayjs('10-20-2020').format() });
expect(initialSnapshot).toEqual(dayjs('10-20-2020').format());
});

});
});

0 comments on commit 6e7c746

Please sign in to comment.