‘use strict’;
/**
* @ngdoc service
* @name interceptcms.dataService
* @description
* # dataService
* Service in the interceptcms.
*/
angular.module(‘interceptcms’)
.factory(‘dataService’, function($http, $state, $log, alert, stateService, authToken, utils, socketService, sailsValidation, RESOURCES) {
// USE SOCKET IO TO SEND OUT NOTIFICATION OF RECORD UPDATE
function sendNotification (user, room, msg, link) {
socketService.emit('message', user, { room: room, msg: msg + ": " + link});
}
// CREATE NOTIFICATION RECORD IN THE DB
// SEND NOTIFICATION TO USER
function postNotificationToDB(APP_URL, model, room, action, title, id, updatedValues, user) {
var notification = {
model: model,
action: action,
title: title,
updatedValues: updatedValues.join(','),
link: "<a href='" + APP_URL + "#/" + model + id + "'>Link</a>",
user: user
};
// TO DO GET LIST OF USERS WATCHING ACTIONS IN DB
// SEND AS AN ARRAY IN THE QUERY STRING TO POST NOTIFICATIONS TO DB
var user = authToken.getUsername();
// SEND NOTIFICATION TO USERS
//$http.post(RESOURCES.API_URL + 'notification/?user=' + user, notification).success(function(data) {
$http.post(RESOURCES.API_URL + 'notification/', notification).success(function(data) {
var notificationRoom = room + ": " + model;
var notificationMsg = model + " " + action + " '" + title + "' ";
getUsersToNotify("admin", notificationRoom, notificationMsg, notification.link, data.id);
});
}
function getUsersToNotify(type, room, msg, link, id) {
$http.get(RESOURCES.API_URL + 'user/?userType=' + type).success(function(data) {
for (var i = data.length - 1; i >= 0; i--) {
var tmpObj = {notification: id, user: data[i].name};
$http.post(RESOURCES.API_URL + 'usernotification/', tmpObj);
}
for (var i = data.length - 1; i >= 0; i--) {
sendNotification(data[i].name, room, msg, link);
};
});
}
// RETURNED OBJECT
var dataService = {};
// SET NOTIFICATION TYPE
var room = 'notifications';
// GET DATA FROM API
dataService.getData = function (url, model, callback) {
$http.get(url).success(function(obj) {
// ADD NEW STATE TO HISTORY
stateService.stateLoaded();
return callback(obj, model);
}).error(function(err){
alert('warning', "Unable to get " + model + ".", err.message);
});
};
dataService.confirmDelete = function(model, id, user) {
var answer = window.prompt("Are you sure you want to delete this record? \r Type 'y' to delete.");
if (answer === 'y') {
dataService.deleteRecord(model, id, user);
}
};
dataService.deleteRecord = function(model, id, user) {
// SET MSG VARIABLES
var title = "";
var updatedValues = [];
// RECORD DB NOTIFICATION TO USER
// SEND NOTIFICATION TO USER
postNotificationToDB(RESOURCES.APP_URL, model, room, "Deleted", title, id, updatedValues, user);
$http.get(RESOURCES.API_URL + model + "/destroy/" + id);
stateService.listView(model);
};
// EXTERNAL MODELS ARE QUERIED FOR MODEL PRIMARY FIELD AND ID
// THIS DATA IS USED WHEN CREATING DROPDOWNS IN THE UI
dataService.getRelatedData = function(val, model, callback) {
$http.get(RESOURCES.API_URL + model + "/get").success(function(obj) {
val = "dd_" + val;
return callback(val, obj);
}).error(function(err){
alert('warning', "Unable to get " + model.trim() + ".", err.message);
});
};
// STRIP OUT UNEDITED PROPERTIES BEFORE SUBMITTING
// ALLOWS US TO TRACK MODIFICATIONS
// IF THE OBJECT IS MODIFIED SUBMIT / ELSE SEND ALERT TO USER
dataService.processBeforeSubmitting = function(originalObj, editedObj) {
var obj = utils.compareObjectsKeepChanges(originalObj, editedObj);
if (obj) {
submit(obj);
} else {
alert('warning', "No changes were found.");
}
};
// SUBMIT DATA TO API
function submit(obj) {
var user = authToken.getUsername();
// SET UP
var model = ($state.params.model) ? $state.params.model : $state.current.name.split('/')[0];
var url = RESOURCES.API_URL + model;
var id = ($state.params.id) ? '/' + $state.params.id : "";
var submittedMsg = (id) ? "Updated" : "Created";
var createEdit = true;
// REMOVE RECORD KEEPING ELEMENTS FROM OBJECT
obj = utils.stripObj(obj, RESOURCES.RECORD_KEEPING_DATA);
// SET NOTIFICATION TO SPECIFY UPDATED PROPERTIES
var updatedValues = [];
if (id) {
for (var k in obj) {
updatedValues.push(k);
}
}
// UPDATE EXISTING OBJECT
// TO INCLUDE UPDATED OR CREATED BY
if (id) {
url += id;
obj.updatedBy = user;
} else {
obj.createdBy = user;
}
// POST DATA TO API
$http.post(RESOURCES.API_URL + model + id, obj).success(function(data) {
// SET MSG VARIABLES
var title = (obj[model] !== undefined) ? obj[model] : obj.title;
// IF RECORD WAS CREATED
// GET AND SET NEW RECORDS ID
if (!id) {
id = "/" + data.id;
}
// SEND ALERT TO USER
alert("success", model + submittedMsg, "Thank you.");
// RECORD DB NOTIFICATION TO USER
// SEND NOTIFICATION TO USER
postNotificationToDB(RESOURCES.APP_URL, model, room, submittedMsg, title, id, updatedValues, user);
stateService.loadRecordView(model, id);
}).error(function (data) {
var err = {};
err.model = model;
if (data.raw) {
if (data.raw.code === 11000) {
err.msg = data.raw.err;
}
}
sailsValidation(err);
});
}
return dataService;
});