Skip to content

Commit

Permalink
[SDESK-2201] Assignment item parameter in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
nrvikas committed Jan 8, 2018
1 parent 6aeed3a commit 02d63b1
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 18 deletions.
7 changes: 5 additions & 2 deletions client/actions/assignments/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const query = ({
* @param {boolean} force - Force using the API instead of local store
* @return Promise
*/
const fetchAssignmentById = (id, force = false) => (
const fetchAssignmentById = (id, force = false, recieve = true) => (
(dispatch, getState, {api}) => {
// Test if the Assignment item is already loaded into the store
// If so, return that instance instead
Expand All @@ -105,7 +105,10 @@ const fetchAssignmentById = (id, force = false) => (

return api('assignments').getById(id)
.then((item) => {
dispatch(self.receivedAssignments([item]));
if (recieve) {
dispatch(self.receivedAssignments([item]));
}

return Promise.resolve(item);
}, (error) => Promise.reject(error));
}
Expand Down
106 changes: 106 additions & 0 deletions client/actions/assignments/tests/ui_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,110 @@ describe('actions.assignments.ui', () => {
});
});
});

describe('updatePreviewItemOnRouteUpdate', () => {
const newAssignment = {
_id: 'as3',
_type: 'assignments',
coverage_id: 'c2',
planning_item: 'p1',
assigned_to: {
user: 'ident1',
desk: 'desk1',
},
planning: {
ednote: 'Photo coverage',
scheduled: '2016-10-15T14:01:11',
g2_content_type: 'photo',
},
};

const restrictedAssignment = {
_id: 'as4',
_type: 'assignments',
coverage_id: 'c2',
planning_item: 'p1',
assigned_to: {
user: 'ident2',
desk: 'desk2',
},
planning: {
ednote: 'Photo coverage',
scheduled: '2016-10-15T14:01:11',
g2_content_type: 'photo',
},
};

beforeEach(() => {
delete store.services.$location.search;
sinon.stub(assignmentsUi, 'preview').returns(Promise.resolve());
});

afterEach(() => {
restoreSinonStub(assignmentsUi.preview);
restoreSinonStub(assignmentsApi.fetchAssignmentById);
restoreSinonStub(store.services.$location.search);
});

it('Previews assignment if already in store', (done) => {
store.services['$location'] = {
...store.services['$location'],
search: sinon.stub().callsFake(() => ({item: 'as1'})),
};

return store.test(done, assignmentsUi.updatePreviewItemOnRouteUpdate())
.then(() => {
expect(assignmentsUi.preview.callCount).toBe(1);
done();
});
});

it('Fetches assignment if not in store', (done) => {
store.services['$location'] = {
...store.services['$location'],
search: sinon.stub().callsFake(() => ({item: 'as3'}))
};

sinon.stub(assignmentsApi, 'fetchAssignmentById').callsFake(() => (Promise.resolve(newAssignment)));
return store.test(done, assignmentsUi.updatePreviewItemOnRouteUpdate())
.then(() => {
expect(assignmentsUi.preview.callCount).toBe(1);
done();
});
});

it('Does not preview assignment if user is not part of assignment item desk', (done) => {
store.services['$location'] = {
...store.services['$location'],
search: sinon.stub().callsFake(() => ({item: 'as4'})),
};

sinon.stub(assignmentsApi, 'fetchAssignmentById').callsFake(() => (Promise.resolve(restrictedAssignment)));
return store.test(done, assignmentsUi.updatePreviewItemOnRouteUpdate())
.then(() => {
expect(assignmentsUi.preview.callCount).toBe(0);
expect(services.notify.error.callCount).toBe(1);
expect(services.notify.error.args[0]).toEqual(
['Insufficient privileges to view the assignment']);
done();
});
});

it('Notifies if assignment does not exist', (done) => {
store.services['$location'] = {
...store.services['$location'],
search: sinon.stub().callsFake(() => ({item: 'as5'})),
};

sinon.stub(assignmentsApi, 'fetchAssignmentById').callsFake(() => (Promise.reject()));
return store.test(done, assignmentsUi.updatePreviewItemOnRouteUpdate())
.then(() => {
expect(assignmentsUi.preview.callCount).toBe(0);
expect(services.notify.error.callCount).toBe(1);
expect(services.notify.error.args[0]).toEqual(
['Assignment does not exist']);
done();
});
});
});
});
69 changes: 59 additions & 10 deletions client/actions/assignments/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,67 @@ const loadAssignments = (
* Action dispatcher to load first page of the list of assignments for current list settings.
*/
const reloadAssignments = (filterByState) => (
(dispatch) => {
(dispatch, getState, {$location, notify, desks}) => {
if (!filterByState || filterByState.length <= 0) {
// Load all assignment groups
let dispatches = [];

Object.keys(ASSIGNMENTS.LIST_GROUPS).forEach((key) => {
const states = ASSIGNMENTS.LIST_GROUPS[key].states;

dispatch(self.changeLastAssignmentLoadedPage(states));
dispatch(queryAndSetAssignmentListGroups(states));
dispatches.push(dispatch(self.queryAndSetAssignmentListGroups(states)));
});

return Promise.all(dispatches);
} else {
dispatch(self.changeLastAssignmentLoadedPage(
assignmentUtils.getAssignmentGroupByStates(filterByState)));
return dispatch(queryAndSetAssignmentListGroups(filterByState));
}
}
);

return Promise.resolve();
const updatePreviewItemOnRouteUpdate = () => (
(dispatch, getState, {$location, notify, desks}) => {
// Load assignment item in URL
const urlItem = $location.search().item;

if (urlItem && urlItem !== get(selectors.getCurrentAssignment(getState()), '_id')) {
const assignment =
get(selectors.getStoredAssignments(getState()), urlItem);

if (!assignment) {
// Fetch it from backend
return dispatch(assignments.api.fetchAssignmentById(urlItem, false, false))
.then((item) => {
if (item) {
// Preview only if user is a member of that assignment's desk
const currentUserId = selectors.getCurrentUserId(getState());
const user = desks.deskMembers[item.assigned_to.desk].find(
(u) => u._id === currentUserId);

if (user) {
// For previewing, add it to the store even though it might be from another
dispatch(assignments.api.receivedAssignments([item]));
return dispatch(self.preview(item));
} else {
notify.error('Insufficient privileges to view the assignment');
$location.search('item', null);
return dispatch(self.closePreview());
}
}
},
() => {
notify.error('Assignment does not exist');
return dispatch(self.closePreview());
});
} else {
return dispatch(self.preview(assignment));
}
} else {
return Promise.resolve();
}
}
);

Expand Down Expand Up @@ -235,14 +280,15 @@ const addToAssignmentListGroup = (assignments, group) => (
* @return object
*/
const preview = (assignment) => (
(dispatch) => (
(dispatch, getState, {$timeout, $location}) => (
dispatch(assignments.api.loadPlanningAndEvent(assignment))
.then(() => (
dispatch({
.then(() => {
$timeout(() => $location.search('item', get(assignment, '_id', null)));
return dispatch({
type: ASSIGNMENTS.ACTIONS.PREVIEW_ASSIGNMENT,
payload: assignment,
})
))
});
})
)
);

Expand All @@ -251,7 +297,10 @@ const preview = (assignment) => (
* @return object
*/
const closePreview = () => (
{type: ASSIGNMENTS.ACTIONS.CLOSE_PREVIEW_ASSIGNMENT}
(dispatch, getState, {$timeout, $location}) => {
$timeout(() => $location.search('item', null));
return dispatch({type: ASSIGNMENTS.ACTIONS.CLOSE_PREVIEW_ASSIGNMENT});
}
);

/**
Expand Down Expand Up @@ -728,7 +777,7 @@ const self = {
onArchivePreviewImageClick,
showRemoveAssignmentModal,
removeAssignment,

updatePreviewItemOnRouteUpdate,
lockAssignment,
lockPlanning,
lockAssignmentAndPlanning,
Expand Down
11 changes: 10 additions & 1 deletion client/controllers/AssignmentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import {WORKSPACE} from '../constants';
AssignmentController.$inject = [
'$element',
'$scope',
'$location',
'desks',
'sdPlanningStore',
'$q',
];
export function AssignmentController(
$element,
$scope,
$location,
desks,
sdPlanningStore,
$q
Expand All @@ -42,7 +44,14 @@ export function AssignmentController(
currentStageId: get(desks, 'active.stage'),
},
});
return store.dispatch(actions.assignments.ui.reloadAssignments());
return store.dispatch(actions.assignments.ui.reloadAssignments())
.then(() => store.dispatch(actions.assignments.ui.updatePreviewItemOnRouteUpdate()))
.then(() => {
$scope.$watch(
() => $location.search().item,
() => store.dispatch(actions.assignments.ui.updatePreviewItemOnRouteUpdate())
);
});
}
);

Expand Down
13 changes: 8 additions & 5 deletions client/utils/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,15 @@ export const getTestActionStore = () => {
},
$timeout: sinon.spy((func) => func()),
api: sinon.spy((resource) => (store.spies.api[resource])),

$location: {
search: sinon.spy(() => ({}))
$location: {search: sinon.spy(() => (Promise.resolve()))},
desks: {
getCurrentDeskId: sinon.spy(() => 'desk1'),
active: {desk: 'desk1'},
deskMembers: {
desk1: [{_id: 'ident1'}],
desk2: [{_id: 'ident2'}],
},
},

desks: {getCurrentDeskId: sinon.spy(() => 'desk1')},
superdesk: {intent: sinon.spy(() => (Promise.resolve()))},
lock: {
isLocked: (item) => {
Expand Down

0 comments on commit 02d63b1

Please sign in to comment.