diff --git a/example/data.js b/example/data.js index 3ab001e..973f38f 100644 --- a/example/data.js +++ b/example/data.js @@ -73,6 +73,24 @@ const bumpSyncToken = function(cal) { cal.syncToken = parts.slice(0, -1).join('/') + '/' + (parseInt(parts[parts.length - 1]) + 1); }; +const eventFilter = function(start, end, eventStart, eventEnd) { + return (!start || start <= eventEnd) && (!end || end >= eventStart); +}; + +const recurringEventFilter = function(start, end, event) { + if (!event.recurring) { + return false; + } + + const eventStart = event.startDate; + const eventEnd = event.recurring.until ? event.recurring.until : end; + return eventFilter(start, end, eventStart, eventEnd); +}; + +const singleEventFilter = function(start, end, event) { + return eventFilter(start, end, event.startDate, event.endDate); +}; + module.exports.getCalendar = async function({ calendarId, // principalId, @@ -125,8 +143,8 @@ module.exports.getEventsByDate = async function({ return _.filter(data.events, (v) => { return v.calendarId === calendarId && ( - (v.startDate >= start && v.endDate <= end) || - v.weekly + recurringEventFilter(start, end, v) || + singleEventFilter(start, end, v) ); }); }; diff --git a/src/common/xBuild.ts b/src/common/xBuild.ts index debef91..592be75 100644 --- a/src/common/xBuild.ts +++ b/src/common/xBuild.ts @@ -6,9 +6,11 @@ type XmlElement = { [tag: string]: any; }; -const nsMap = mapKeys(namespaces, (v, k) => { - return `@xmlns:${k}`; -}); +const nsMap = function() { + return mapKeys(namespaces, (v, k) => { + return `@xmlns:${k}`; + }); +}; export const buildTag = function(namespaceURI: string, localName: string) { return `${nsLookup[namespaceURI]}:${localName}`; @@ -24,7 +26,7 @@ export const build = function(obj: XmlElement) { export const multistatus = function(responses?: any, other?: object): XmlElement { const res = { - [buildTag('DAV:', 'multistatus')]: nsMap + [buildTag('DAV:', 'multistatus')]: nsMap() }; if (responses?.length) { res[buildTag('DAV:', 'multistatus')][buildTag('DAV:', 'response')] = responses; @@ -71,7 +73,7 @@ export const preconditionFail = function(url: string, reason: string) { const res = { 'D:error': Object.assign({ [buildTag('urn:ietf:params:xml:ns:caldav', reason)]: url - }, nsMap) + }, nsMap()) }; return build(res); }; diff --git a/src/common/xml.ts b/src/common/xml.ts index 284ec57..f78485b 100644 --- a/src/common/xml.ts +++ b/src/common/xml.ts @@ -21,7 +21,7 @@ export const get = function(path: string, doc: Document) { }; export const getWithChildren = function(path: string, doc: Document) { - const propNode = get('/D:propfind/D:prop', doc); + const propNode = get(path, doc); const children = propNode[0] ? (Array.from(propNode[0].childNodes) as Element[]) : []; return { propNode, children }; };