(function(){ "use strict"; angular .module("app.repositories") .factory("repository.user", userRepository); function userRepository(webApi, dataTableConstant, IM, personHelper){ return { add: add, get: get, getActive: getActive, getBlocked: getBlocked, getCountries: getCountries, getLanguages: getLanguages, getSecurityQuestions: getSecurityQuestions, getUserCards: getUserCards, changePassword: changePassword, lockById: lockById, addUserCard: addUserCard, removeById: removeById, removeUserCard: removeUserCard, updateDate: updateDate, updateUserCard: updateUserCard, unlockById: unlockById, update: update, updateUserImage: updateUserImage, updateObjectKeys: updateObjectKeys }; /** * Create a new user depends on it role (user, admin). * @param {object} data */ function add(data){ return webApi.createUserProfile(data); } /** * Retrieve a user entity. * @param {number|null} id */ function get(id){ return webApi.getUserProfileById({ "url": {"id": id} }); } /** * Retrieve all existing users from DB. * @param {string|null} query * @param {string|null} orderBy * @param {string|null} orderDirection * @param {string|number} currentPage * @param {string|number} itemsPerPage * @returns {*} */ function getActive(currentPage, itemsPerPage, queryParams){ queryParams.pageNumber = currentPage || dataTableConstant.CURRENT_PAGE; queryParams.pageSize = itemsPerPage || dataTableConstant.REQUEST_PAGE_SIZE; queryParams = excludeNullable(queryParams); return webApi.getUserProfileActive({ before: [], after: queryParams }) } /** * Retrieve blocked users from DB. * @param {string|null} query * @param {string|null} orderBy * @param {string|null} orderDirection * @param {string|number} currentPage * @param {string|number} itemsPerPage * @returns {*} */ function getBlocked(currentPage, itemsPerPage, queryParams){ queryParams.pageNumber = currentPage || dataTableConstant.CURRENT_PAGE; queryParams.pageSize = itemsPerPage || dataTableConstant.REQUEST_PAGE_SIZE; queryParams = excludeNullable(queryParams); return webApi.getUserProfileBlocked({ before: [], after: queryParams }) } function getCountries(){ return webApi.getCountries([]); } function getLanguages(){ return webApi.getLanguages([]); } function getSecurityQuestions(){ return webApi.getSecurityQuestions([]); } function getUserCards(id){ return webApi.getUserCards({ "url": { "userId": id } }); } /** * Modify password for users only. * @param {object} data * @param {number|string} id */ function changePassword(data, id){ return webApi.userProfileChangePassword({ url: { "id": id }, data: data }); } /** * Set locked status to specified user. * @param {number} id * @returns {*} */ function lockById (id){ return webApi.blockUserProfileById({ "url": { "id": id } }); } function addUserCard(card, userId){ return webApi.addUserCard({ "url": { "userId": userId }, "data": card }); } function updateUserCard(card, userId){ return webApi.updateUserCard({ "url": { "userId": userId, "cardId": card.id }, "data": card }); } function removeUserCard(card, userId){ return webApi.removeUserCard({ "url": { "userId": userId, "cardId": card.id } }); } function removeById (){ } /** * Update user details. * @param {object} data * @param {number|null} id */ function update(data, id){ return webApi.updateUserProfileById({ url: { "id": id }, data: data }); } function updateObjectKeys(oldObj, newObj, exceptKeys){ var exceptProps = exceptKeys || []; for (var key in newObj) { if (newObj.hasOwnProperty(key)) { if (exceptProps.indexOf(key) == -1) { oldObj[key] = newObj[key]; } } } } /** * Update the date value inside input via jQuery. * @param {element|string} id * @param {number|string|date} value * @param {boolean|undefined} isEmpty * @param {string|date} endDate */ function updateDate(id, value, isEmpty, endDate){ personHelper.updateDate(id, value, isEmpty, endDate); } function updateUserImage(id, data){ return webApi.uploadUserImage({ "url": { "id": id }, "data": data, "options": { "transformRequest": angular.identity, "headers": { 'Content-Type': undefined } } }); } function excludeNullable(model) { var result = {}; for (var key in model) { if (model.hasOwnProperty(key)) { if (model[key] != null && model[key] != "") { result[key] = model[key]; } } } return result; } /** * Set unlocked status to specified user. * @param {number} id * @returns {*} */ function unlockById (id){ return webApi.unblockUserProfileById({ "url": { "id": id } }); } } // IoC container. userRepository.$inject = [ "webApi", "appConstant.dataTable", "helpers.internalMessages", "helpers.person" ]; })();