Skip to content

Commit

Permalink
compose odata $filter param for sync
Browse files Browse the repository at this point in the history
  • Loading branch information
shulkaolka committed Oct 10, 2023
1 parent d6c7cb4 commit dcf175a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
9 changes: 8 additions & 1 deletion templates/lib/triggers/trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ async function processTrigger(msg, cfg, snapshot, incomingMessageHeaders, tokenD
}

if (syncParam && snapshot.lastUpdated) {
parameters[syncParam] = snapshot.lastUpdated;
if (syncParam === '$filter'){
if (!snapshotKey){
throw new Error('snapshotKey params should be specified!')
}
parameters[syncParam] = `${snapshotKey} gt datetime'${snapshot.lastUpdated}'`;
} else {
parameters[syncParam] = snapshot.lastUpdated;
}
}
logger.debug("Parameters were populated from configuration: %j", parameters);

Expand Down
24 changes: 22 additions & 2 deletions templates/lib/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,35 @@ function getMetadata(metadata) {
return newMetadata;
}

function isMicrosoftJsonDate(dateStr) {
const regex = /^\/Date\((\d+)([+-]\d{4})?\)\/$/;
if (regex.test(dateStr)) {
const match = dateStr.match(regex);
const milliseconds = parseInt(match[1]);
const timeZoneOffset = match[2] ? parseInt(match[2]) / 100 : 0;
return new Date(milliseconds + timeZoneOffset * 60 * 60 * 1000);;
} else {
return null;
}
}

async function dataAndSnapshot(newElement, snapshot, snapshotKey, standardSnapshot, self) {
if (Array.isArray(newElement.data)) {
this.logger.info("Found %s items in response data", newElement.data.length);
let lastObjectDate = 0;
let emittedItemsCount = 0;
for (let i = 0; i < newElement.data.length; i++) {
const newObject = { ...newElement, data: newElement.data[i] };
const currentObjectDate = lodashGet(newObject.data, snapshotKey)
let currentObjectDate = lodashGet(newObject.data, snapshotKey)
? lodashGet(newObject.data, snapshotKey)
: lodashGet(newObject.data, standardSnapshot);
if (currentObjectDate){
const parsedDate = isMicrosoftJsonDate(currentObjectDate);
if (parsedDate) {
this.logger.info("Microsoft JSON date format detected, parsed date: %s", parsedDate);
currentObjectDate = parsedDate;
}
}
if (!snapshot.lastUpdated) {
if (compareDate(currentObjectDate, lastObjectDate)) {
lastObjectDate = currentObjectDate;
Expand Down Expand Up @@ -128,5 +147,6 @@ module.exports = {
getMetadata,
dataAndSnapshot,
getElementDataFromResponse,
mapFormDataBody
mapFormDataBody,
isMicrosoftJsonDate
};
17 changes: 17 additions & 0 deletions templates/lib/utils/helpers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const {
isMicrosoftJsonDate
} = require("./helpers");

describe("Helpers", () => {
describe("isMicrosoftJsonDate", () => {
it("should convert MicrosoftJsonDate", () => {
const date = isMicrosoftJsonDate("/Date(1577836800000)/");
expect(date).toEqual(new Date("2020-01-01T00:00:00.000Z"));
});

it("should return null", () => {
const date = isMicrosoftJsonDate("2020-01-01T00:00:00.000Z");
expect(date).toEqual(null);
});
});
});

0 comments on commit dcf175a

Please sign in to comment.