Created
September 30, 2016 16:57
-
-
Save justinvdm/f535a9e27e50ab011ee23b3ada5dbad2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/stores/__tests__/helpers-test.js b/src/stores/__tests__/helpers-test.js | |
index 1d8e8a1..6738650 100644 | |
--- a/src/stores/__tests__/helpers-test.js | |
+++ b/src/stores/__tests__/helpers-test.js | |
@@ -21,6 +21,7 @@ import { | |
getScheduledCallActivity, | |
getEvents, | |
getActivityCallNotes, | |
+ getNextScheduledCall, | |
} from 'src/stores/helpers'; | |
@@ -184,6 +185,39 @@ describe('helpers', () => { | |
}); | |
}); | |
+ describe('getNextScheduledCall', () => { | |
+ it('should get the next scheduled call', () => { | |
+ const state = fakeState(); | |
+ | |
+ const target = fakeScheduledCall({ | |
+ id: 3, | |
+ callTime: '2016-09-22T14:31:23.431Z', | |
+ }); | |
+ | |
+ state.entities.scheduledCalls = { | |
+ 1: fakeScheduledCall({ | |
+ id: 1, | |
+ callTime: '2016-09-22T14:31:21.431Z', | |
+ }), | |
+ 2: fakeScheduledCall({ | |
+ id: 2, | |
+ callTime: '2016-09-22T14:31:22.431Z', | |
+ }), | |
+ 3: target, | |
+ 4: fakeScheduledCall({ | |
+ id: 4, | |
+ callTime: '2016-09-22T14:31:24.431Z', | |
+ }), | |
+ 5: fakeScheduledCall({ | |
+ id: 5, | |
+ callTime: '2016-09-22T14:31:25.431Z', | |
+ }), | |
+ }; | |
+ | |
+ expect(getNextScheduledCall(state, '2016-09-22T14:31:23.531Z')).toEqual(target); | |
+ }); | |
+ }); | |
+ | |
describe('getScheduledCallActivity', () => { | |
it('should get the activity for scheduled call', () => { | |
const state = fakeState(); | |
diff --git a/src/stores/helpers.js b/src/stores/helpers.js | |
index 3c3b4c1..f806b4c 100644 | |
--- a/src/stores/helpers.js | |
+++ b/src/stores/helpers.js | |
@@ -1,4 +1,5 @@ | |
-import { values, isUndefined } from 'lodash'; | |
+import moment from 'moment'; | |
+import { values, isUndefined, sortBy, takeWhile, last } from 'lodash'; | |
export const getAuthUserProfile = ({ | |
@@ -55,3 +56,11 @@ export const getEvents = ({ entities: { events } }) => values(events); | |
export const getActivityCallNotes = ({ entities: { callNotes } }, targetActivityId) => | |
values(callNotes) | |
.filter(({ callActivityId }) => callActivityId === targetActivityId); | |
+ | |
+ | |
+export const getNextScheduledCall = (state, time = Date.now()) => { | |
+ let calls = getScheduledCalls(state); | |
+ calls = sortBy(calls, ({ callTime }) => +moment(callTime)); | |
+ calls = takeWhile(calls, ({ callTime }) => moment(callTime).isBefore(time)); | |
+ return last(calls); | |
+}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment