(function () { 'use strict'; var serviceId = 'myBadgesAdminDataContext'; angular.module('mybadgesAdmin').factory(serviceId, ['$q', '$http', '$upload', 'myBadgesAdminConfig', datacontext]); function datacontext($q, $http, $upload, config) { var service = { getGraphicsForUser: getGraphicsForUser, getGraphicsByOrg: getGraphicsByOrg, createNewGraphic: createNewGraphic, deleteGraphic: deleteGraphic, updateGraphic: updateGraphic, getTemplatesForUser: getTemplatesForUser, getTemplatesForUserToIssue: getTemplatesForUserToIssue, getTemplateTotals: getTemplateTotals, getTemplatesTotals: getTemplatesTotals, getPublishedTemplatesForUser: getPublishedTemplatesForUser, getTemplatesByOrgs: getTemplatesByOrgs, getTemplatesByOrgsToIssue: getTemplatesByOrgsToIssue, getClaimReportingByOrgs: getClaimReportingByOrgs, getPublishedTemplatesByOrgs: getPublishedTemplatesByOrgs, createNewTemplate: createNewTemplate, getTemplate: getTemplate, updateTemplate: updateTemplate, changeTemplateStatus: changeTemplateStatus, deleteTemplate: deleteTemplate, getLayeringByGraphic: getLayeringByGraphic, createNewLayering: createNewLayering, getLayeringById: getLayering, updateLayering: updateLayering, deleteLayering: deleteLayering, getBadgeIssuersForUser: getBadgeIssuersForUser, getBadgeIssuers: getBadgeIssuers, getAllIssuersForInstance: getAllIssuersForInstance, getGraphic: getGraphic, cloneGraphic: cloneGraphic, getIssuedBadge: getIssuedBadge, getIssuedByTemplate: getIssuedByTemplate, getIssuedByClaimCode: getIssuedByClaimCode, getIssuedByUser: getIssuedByUser, issueBadge: issueBadge, updateIssuedBadge: updateIssuedBadge, deleteIssuedBadge: deleteIssuedBadge, bakeAndIssue: bakeAndIssue, getIssuedByTemplateUsers: getIssuedByTemplateUsers, issueBadgeThenBake: issueBadgeThenBake, addRule: addRule, getIssuedByUserForOrg: getIssuedByUserForOrg, getDirectBadgeIssuersForUser: getDirectBadgeIssuersForUser, expireBadge: expireBadge, revokeBadge: revokeBadge, getSocialTrackingsForIssuedBadge: getSocialTrackingsForIssuedBadge, exportSocialTrackingsToCsv: exportSocialTrackingsToCsv, exportIssuedBadgesToCsv: exportIssuedBadgesToCsv, downloadCsvFile: downloadCsvFile, sendToEvents: sendToEvents, uploadEvidence: uploadEvidence, getEvidence: getEvidence, allocateBadgeTemplateForIssuing: allocateBadgeTemplateForIssuing, getAllocations: getAllocations, withdrawAllocations: withdrawAllocations }; return service; /* ************************************ * Handlers * ************************************/ function handleError(response) { // The API response from the server should be returned in a // nomralized format. However, if the request was not handled by the // server (or what not handles properly - ex. server error), then we // may have to normalize it on our end, as best we can. if ( !angular.isObject(response.data) || !response.data.message ) { return ($q.reject("An unknown error occurred.")); } // Otherwise, use expected error message. return ($q.reject(response.data.message)); } // I transform the successful response, unwrapping the application data // from the API response payload. function handleSuccess(response) { return (response.data); } // Get badge issuers accessible by the current user function getBadgeIssuersForUser() { var request = $http({ method: "get", url: config.mybadgesUrl + 'organisations/issuers' }); return (request.then(function (response) { return response.data.issuers; }, handleError)); } // Get badge issuers accessible by the current user function getDirectBadgeIssuersForUser() { var request = $http({ method: "get", url: config.mybadgesUrl + 'organisations/issuers/direct' }); return (request.then(function (response) { return response.data.issuers; }, handleError)); } // Get badge issuers associated with the given orgids function getBadgeIssuers(orgIds) { var request = $http({ method: "post", url: config.mybadgesUrl + 'organisations/issuers', data: orgIds }); return (request.then(function (response) { return response.data.issuers; }, handleError)); } function uploadEvidence(files, filename, contentType, progressHandler) { var payload = new FormData(); payload.append("name", filename); payload.append("files", files[0]); payload.append("contentType", contentType); return $upload.http({ method: "post", url: config.resourcesUrl + 'api/FileResources', data: payload, headers: { 'Content-Type': undefined }, }).progress(function (e) { //inform the user what progress we are making on the upload var percent = Math.round((e.loaded * 100.0) / e.total); return progressHandler(percent); }).success(function (data, status, headers, config) { return data; }); } function getEvidence(id) { var request = $http({ method: "get", url: config.resourcesUrl + 'api/FileResources/' + id, }); return (request.then(function (response) { return response.data; }, handleError)); } // Get badge issuers in a given instance function getAllIssuersForInstance(instance) { var request = $http({ method: "get", url: config.mybadgesUrl + 'organisations/' + instance + '/issuers' }); return (request.then(function (response) { return response.data.issuers; }, handleError)); } /* ************************************ * Badge Graphics * ************************************/ // Get the graphics that the current user can access function getGraphicsForUser() { var request = $http({ method: "get", url: config.mybadgesUrl + 'organisation/graphics' + '?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } // Get the graphics that are associated with the users organisation ID function getGraphicsByOrg(orgIds) { var request = $http({ method: "post", url: config.mybadgesUrl + 'organisation/graphics', data: orgIds }); return (request.then(handleSuccess, handleError)); } // Get the graphics that are associated with the users organisation ID function getGraphic(graphicId) { var request = $http({ method: "post", url: config.mybadgesUrl + '/graphics/' + graphicId, data: orgIds }); return (request.then(handleSuccess, handleError)); } // Create a new graphic function createNewGraphic(graphic) { var request = $http({ method: "post", url: config.mybadgesUrl + 'graphics', data: graphic }); return (request.then(handleSuccess, handleError)); } // Update existing graphic function updateGraphic(graphic) { var request = $http({ method: "put", url: config.mybadgesUrl + 'graphics/' + graphic.id, data: graphic }); return (request.then(handleSuccess, handleError)); } // Delete a graphic function deleteGraphic(graphicId) { var request = $http({ method: "delete", url: config.mybadgesUrl + 'graphics/' + graphicId }); return (request.then(handleSuccess, handleError)); } // Clone a graphic function cloneGraphic(graphicId) { var request = $http({ method: "get", url: config.mybadgesUrl + 'badgegraphics/' + graphicId + '/clone' }); return (request.then(handleSuccess, handleError)); } /* ************************************ * Badge Templates * ************************************/ // Get badge templates that this user can access function getTemplatesForUser() { var request = $http({ method: "get", url: config.mybadgesUrl + 'organisation/badgetemplates?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } // Get badge templates that this user can issue function getTemplatesForUserToIssue() { var request = $http({ method: "get", url: config.mybadgesUrl + 'organisation/badgetemplates/issuable?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } // Get badge templates issued and clicked totals function getTemplateTotals(id) { var request = $http({ method: "get", url: config.mybadgesUrl + 'badgetemplates/' + id + '/totals?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } // Get badge templates issued and clicked totals function getTemplatesTotals(ids) { var request = $http({ method: "post", data: ids, url: config.mybadgesUrl + 'badgetemplates/totals?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } // Get published badge templates that this user can access function getPublishedTemplatesForUser() { var request = $http({ method: "get", url: config.mybadgesUrl + 'organisation/publishedbadgetemplates?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } // Get badge templates for a list of org ids function getTemplatesByOrgs(orgIds) { var request = $http({ method: "post", url: config.mybadgesUrl + 'organisation/badgetemplates', data: orgIds }); return (request.then(handleSuccess, handleError)); } // Get badge templates to issue for a list of org ids function getTemplatesByOrgsToIssue(orgIds) { var request = $http({ method: "post", url: config.mybadgesUrl + 'organisation/badgetemplates/issuable', data: orgIds }); return (request.then(handleSuccess, handleError)); } // Get badge templates for a list of org ids function getClaimReportingByOrgs(orgIds) { var request = $http({ method: "post", url: config.mybadgesUrl + 'claimcode/organisations', data: orgIds }); return (request.then(handleSuccess, handleError)); } // Get published badge templates for a list of org ids function getPublishedTemplatesByOrgs(orgIds) { var request = $http({ method: "post", url: config.mybadgesUrl + 'organisation/publishedbadgetemplates', data: orgIds }); return (request.then(handleSuccess, handleError)); } // Create a new badge template function createNewTemplate(model) { var request = $http({ method: "post", url: config.mybadgesUrl + 'badgetemplates', data: model }); return (request.then(handleSuccess, handleError)); } // Get existing badge template function getTemplate(templateId) { var request = $http({ method: "get", url: config.mybadgesUrl + 'badgetemplates/' + templateId }); return (request.then(handleSuccess, handleError)); } // Update existing badge template function updateTemplate(template) { var request = $http({ method: "put", url: config.mybadgesUrl + 'badgetemplates/' + template.id, data: template }); return (request.then(handleSuccess, handleError)); } function addRule(template, orgId) { var request = $http({ method: "post", url: config.activityUrl + 'api/badgerule/instant/' + template.id + '/' + orgId, }); return (request.then(handleSuccess, handleError)); } // Change the status of an existing badge template function changeTemplateStatus(id, model) { var request = $http({ method: "put", url: config.mybadgesUrl + 'badgetemplates/' + id + '/status', data: model }); return (request.then(handleSuccess, handleError)); } // Delete a badge template function deleteTemplate(templateId) { var request = $http({ method: "delete", url: config.mybadgesUrl + 'badgetemplates/' + templateId }); return (request.then(handleSuccess, handleError)); } function allocateBadgeTemplateForIssuing(allocations) { var request = $http({ method: "post", url: config.mybadgesUrl + 'badgeOrganisations/issuers', data: allocations }); return (request.then(handleSuccess, handleError)); } function withdrawAllocations(allocations) { var request = $http({ method: "post", url: config.mybadgesUrl + 'badgeOrganisations/withdrawnissuers', data: allocations }); return (request.then(handleSuccess, handleError)); } function getAllocations(templateId) { var request = $http({ method: "get", url: config.mybadgesUrl + 'badgeOrganisations/' + templateId }); return (request.then(handleSuccess, handleError)); } /* ************************************ * Badge Graphic Layerings * ************************************/ // Get the layering record for a badge graphic function getLayeringByGraphic(graphicId) { var request = $http({ method: "get", url: config.mybadgesUrl + 'graphics/' + graphicId + '/graphiclayering' }); return (request.then(handleSuccess, handleError)); } // Create a new layering for a badge graphic function createNewLayering(layering) { var request = $http({ method: "post", url: config.mybadgesUrl + 'graphiclayerings', data: layering }); return (request.then(handleSuccess, handleError)); } // Get existing graphic layering by it's id function getLayering(layeringId) { var request = $http({ method: "get", url: config.mybadgesUrl + 'graphiclayerings/' + layeringId }); return (request.then(handleSuccess, handleError)); } // Update existing graphic layering function updateLayering(layering) { var request = $http({ method: "put", url: config.mybadgesUrl + 'graphiclayerings/' + layering.id, data: layering }); return (request.then(handleSuccess, handleError)); } // Delete a layering record function deleteLayering(layeringId) { var request = $http({ method: "delete", url: config.mybadgesUrl + 'graphiclayerings/' + layeringId }); return (request.then(handleSuccess, handleError)); } /* ************************************ * Badge Issuing * ************************************/ // Get the issued badge by issued badge id function getIssuedBadge(issueId) { var request = $http({ method: "get", url: config.mybadgesUrl + 'issuedbadges/' + issueId + '?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } function getIssuedByTemplate(templateId, status) { var request = $http({ method: "get", url: config.mybadgesUrl + 'badgetemplates/' + templateId + '/issuedbadges/' + status + '?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } // Get the issued badges by badge template id function getIssuedByClaimCode(codeId, status) { var request = $http({ method: "get", url: config.mybadgesUrl + 'claimcodes/' + codeId + '/issuedbadges/?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } // Get the issued badges by user id function getIssuedByUser(userId, status) { var request = $http({ method: "get", url: config.mybadgesUrl + 'users/' + userId + '/issuedbadges/' + status + '?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } // Get the issued badges by user id function getIssuedByUserForOrg(userId, status) { var request = $http({ method: "get", url: config.mybadgesUrl + 'mybadgereports/issuedbadges/user/' + userId + '?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } // Issue a new badge function issueBadge(template) { var request = $http({ method: "post", url: config.mybadgesUrl + 'issuedbadges', data: template }); return (request.then(handleSuccess, handleError)); } // Issue and bake a batch of new badges function bakeAndIssue(issueIds, issuedByName, organisationId) { var request = $http({ method: "post", url: config.mybadgesUrl + 'issuedbadges/bakery', data: { issuedByName: issuedByName, issueIds: issueIds, organisationId: organisationId } }); return (request.then(handleSuccess, handleError)); } function sendToEvents(developerKey, authToken, events) { var request = $http({ method: "post", url: config.activityUrl + 'api/ActivityEvents/bulk', data: events, headers: { 'ApiKey': developerKey, 'Authorization': 'Bearer ' + authToken } }); return (request.then(handleSuccess, handleError)); } // Issue and bake a badge - does admin checking function issueBadgeThenBake(template) { var request = $http({ method: "post", url: config.mybadgesUrl + 'issuedbadges/user/bakery', data: template }); return (request.then(handleSuccess, handleError)); } // Update existing issued badge function updateIssuedBadge(issued) { var request = $http({ method: "put", url: config.mybadgesUrl + 'issuedbadges/' + issued.id, data: issued }); return (request.then(handleSuccess, handleError)); } // Delete issued badge function deleteIssuedBadge(issuedId) { var request = $http({ method: "delete", url: config.mybadgesUrl + 'issuedbadges/' + issuedId }); return (request.then(handleSuccess, handleError)); } // Get the issued badges by badge template id function getIssuedByTemplateUsers(templateId, status) { var request = $http({ method: "get", url: config.mybadgesUrl + 'badgetemplates/' + templateId + '/summaries/users/issuedbadges/' + status }); return (request.then(handleSuccess, handleError)); } function expireBadge(issuedBadgeId) { var request = $http({ method: "post", url: config.mybadgesUrl + '/SetExpired/' + config.mybadgesSecretKey + '/' + issuedBadgeId + '/' }); return (request.then(handleSuccess, handleError)); } function revokeBadge(issuedBadgeId) { var request = $http({ method: "post", url: config.mybadgesUrl + '/SetRevoked/' + config.mybadgesSecretKey + '/' + issuedBadgeId + '/' }); return (request.then(handleSuccess, handleError)); } function getSocialTrackingsForIssuedBadge(badgeId, issuedBadgeIds) { var request = $http({ method: "post", url: config.mybadgesUrl + 'socialtrackings/issuedbadges/' + badgeId, data: issuedBadgeIds }); return (request.then(handleSuccess, handleError)); } function exportSocialTrackingsToCsv(badgeId, searchTerm, startDate, endDate) { var request = $http({ method: "get", url: config.mybadgesUrl + 'reports/socialtrackings/' + badgeId + '/exports/csv?badgeId=' + badgeId + '&searchQuery=' + searchTerm + '&startDate=' + startDate + '&endDate=' + endDate }); return (request.then(handleSuccess, handleError)); } function exportIssuedBadgesToCsv(badgeId, searchTerm) { var request = $http({ method: "get", url: config.mybadgesUrl + 'reports/issuedbadges/' + badgeId + '/exports/csv?badgeId=' + badgeId + '&searchQuery=' + searchTerm }); return (request.then(handleSuccess, handleError)); } function downloadCsvFile(fileName) { var request = $http({ method: "get", url: config.mybadgesUrl + 'reports/exports/csv?fileName=' + fileName, headers: { 'x-filename': fileName + '.csv' } }); return (request.success(function (data, status, headers, request) { handleDownload(data, headers, request) }).error(function (data) { handleError(data); })); } function handleDownload(data, headers, request) { headers = headers(); var filename = request.headers['x-filename']; var contentType = headers['content-type']; var linkElement = document.createElement('a'); try { /** * This creates a link element containing the csv file to download * Then clicks the link **/ var blob = new Blob([data], { type: contentType }); var url = window.URL.createObjectURL(blob); linkElement.setAttribute('href', url); linkElement.setAttribute("download", filename); var clickEvent = new MouseEvent("click", { "view": window, "bubbles": true, "cancelable": false }); linkElement.dispatchEvent(clickEvent); } catch (ex) { console.log(ex); } } } })();