Skip to content

Commit

Permalink
Merge pull request #34972 from dimagi/ze/geospatial-assignment-workflow
Browse files Browse the repository at this point in the history
Geospatial Assignment Workflow
  • Loading branch information
zandre-eng authored Aug 20, 2024
2 parents 04c72a5 + 4bcdf3e commit d3497fc
Show file tree
Hide file tree
Showing 9 changed files with 409 additions and 40 deletions.
4 changes: 3 additions & 1 deletion corehq/apps/geospatial/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def headers(self):
DataTablesColumn(_("case_id"), prop_name="type.exact"),
DataTablesColumn(_("gps_point"), prop_name="type.exact"),
DataTablesColumn(_("link"), prop_name="name.exact", css_class="case-name-link"),
DataTablesColumn(_("name"), prop_name="name.exact"),
)
headers.custom_sort = [[2, 'desc']]
return headers
Expand All @@ -125,7 +126,8 @@ def rows(self):
cases.append([
display.case_id,
coordinates,
display.case_link
display.case_link,
display.case_name,
])
return cases

Expand Down
47 changes: 18 additions & 29 deletions corehq/apps/geospatial/static/geospatial/js/case_grouping_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,18 @@ hqDefine("geospatial/js/case_grouping_map",[
exportableCases = self.casesToExport();
}

const casesToExport = _.map(exportableCases, function (caseItem) {
return caseItem.toJson();
});

let csvStr = "";
let groupNameByID = _.object(_.map(caseGroupsInstance.generatedGroups, function(group) {
const groupNameByID = _.object(_.map(caseGroupsInstance.generatedGroups, function(group) {
return [group.groupId, group.name]
}));

const casesToExport = _.map(exportableCases, function (caseItem) {
let caseJson = caseItem.toJson();
caseJson.groupName = groupNameByID[caseJson.groupId];
return caseJson;
});

// Write headers first
let headers = [
const headers = [
gettext('Group ID'),
gettext('Group Name'),
gettext('Group Center Coordinates'),
Expand All @@ -87,29 +88,17 @@ hqDefine("geospatial/js/case_grouping_map",[
gettext('Case Coordinates'),
gettext('Case ID'),
]
csvStr = (headers).join(",");
csvStr += "\n";

_.forEach(casesToExport, function (caseToExport) {
csvStr += [
caseToExport.groupId,
groupNameByID[caseToExport.groupId],
caseToExport.groupCenterCoordinates,
caseToExport.caseName,
caseToExport.owner_name,
caseToExport.coordinates,
caseToExport.caseId,
].join(",");
csvStr += "\n";
});
const cols = [
'groupId',
'groupName',
'groupCenterCoordinates',
'caseName',
'owner_name',
'coordinates',
'caseId'
]

// Download CSV file
const hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvStr);
hiddenElement.target = '_blank';
hiddenElement.download = `Grouped Cases (${utils.getTodayDate()}).csv`;
hiddenElement.click();
hiddenElement.remove();
utils.downloadCsv(casesToExport, headers, cols, 'Group Export');
};

self.addGroupDataToCases = function(caseGroups, groupsData, assignDefaultGroup) {
Expand Down
50 changes: 41 additions & 9 deletions corehq/apps/geospatial/static/geospatial/js/geospatial_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ hqDefine("geospatial/js/geospatial_map", [

var mapModel;
var polygonFilterModel;
var assignmentManagerModel;

function showMapControls(state) {
$("#geospatial-map").toggle(state);
Expand Down Expand Up @@ -76,19 +77,21 @@ hqDefine("geospatial/js/geospatial_map", [
mapModel.caseMapItems().forEach((caseModel) => {
if (result[userId].includes(caseModel.itemId)) {
cases.push(caseModel);
mapModel.caseGroupsIndex[caseModel.itemId] = {groupId: groupId, item: caseModel};
mapModel.caseGroupsIndex[caseModel.itemId] = {
groupId: groupId,
item: caseModel,
assignedUserId: userId,
};
}
});
connectUserWithCasesOnMap(user, cases);
self.connectUserWithCasesOnMap(user, cases);
groupId += 1;
});
self.setBusy(false);
};

self.runCaseDisbursementAlgorithm = function (cases, users) {
self.setBusy(true);
self.clearConnectionLines = function (cases) {
let mapInstance = mapModel.mapInstance;

let caseData = [];
cases.forEach(function (c) {
const layerId = mapModel.getLineFeatureId(c.itemId);
Expand All @@ -106,6 +109,13 @@ hqDefine("geospatial/js/geospatial_map", [
});
});

return caseData;
};

self.runCaseDisbursementAlgorithm = function (cases, users) {
self.setBusy(true);
const caseData = self.clearConnectionLines(cases);

self.setDisbursementParameters = function (parameters) {
var parametersList = [
{name: gettext("Max cases per user"), value: parameters.max_cases_per_user},
Expand Down Expand Up @@ -163,7 +173,7 @@ hqDefine("geospatial/js/geospatial_map", [
});
};

function connectUserWithCasesOnMap(user, cases) {
self.connectUserWithCasesOnMap = function (user, cases) {
cases.forEach((caseModel) => {
const lineCoordinates = [
[user.itemData.coordinates.lng, user.itemData.coordinates.lat],
Expand Down Expand Up @@ -194,7 +204,7 @@ hqDefine("geospatial/js/geospatial_map", [
},
});
});
}
};

return self;
};
Expand Down Expand Up @@ -299,6 +309,9 @@ hqDefine("geospatial/js/geospatial_map", [
$filterSelect.append(new Option(userLocationName, self.selectedLocation));
$filterSelect.val(self.selectedLocation).trigger('change');
self.loadUsers();
} else if (shouldShowUsers) {
// If only checkbox is ticked, then load all users
self.loadUsers();
}
};

Expand All @@ -325,7 +338,17 @@ hqDefine("geospatial/js/geospatial_map", [
const editUrl = initialPageData.reverse('edit_commcare_user', userData.id);
const link = `<a class="ajax_dialog" href="${editUrl}" target="_blank">${userData.username}</a>`;

return [userData.id, {'coordinates': {'lat': lat, 'lng': lng}, 'link': link, 'type': 'user'}];
const userInfo = {
'coordinates': {
'lat': lat,
'lng': lng,
},
'link': link,
'type': 'user',
'name': userData.username,
'primary_loc_name': userData.primary_loc_name,
};
return [userData.id, userInfo];
}));

const userMapItems = mapModel.addMarkersToMap(userData, userMarkerColors);
Expand Down Expand Up @@ -407,6 +430,14 @@ hqDefine("geospatial/js/geospatial_map", [
}
}

function initAssignmentReview() {
const $manageAssignmentModal = $("#review-assignments");
if ($manageAssignmentModal.length) {
assignmentManagerModel = models.AssignmentManager(mapModel, disbursementRunner);
$manageAssignmentModal.koApplyBindings(assignmentManagerModel);
}
}

function loadCases(caseData) {
mapModel.removeMarkersFromMap(mapModel.caseMapItems());
mapModel.caseMapItems([]);
Expand All @@ -416,7 +447,7 @@ hqDefine("geospatial/js/geospatial_map", [
// Index by case_id
var casesById = _.object(_.map(casesWithGPS, function (item) {
if (item[1]) {
return [item[0], {'coordinates': item[1], 'link': item[2], 'type': 'case'}];
return [item[0], {'coordinates': item[1], 'link': item[2], 'type': 'case', 'name': item[3]}];
}
}));
const caseMapItems = mapModel.addMarkersToMap(casesById, caseMarkerColors);
Expand All @@ -439,6 +470,7 @@ hqDefine("geospatial/js/geospatial_map", [
mapModel.mapInstance.on('load', () => {
initPolygonFilters();
initUserFilters();
initAssignmentReview();
});

// Hide controls until data is displayed
Expand Down
Loading

0 comments on commit d3497fc

Please sign in to comment.