(function(){ "use strict"; angular .module("app.repositories") .factory("repository.admin", adminRepository); function adminRepository(webApi, dataTableConstant){ return { add: add, get: getById, getActive: getActive, getBlocked: getBlocked, lockById: lockById, removeById: removeById, unlockById: unlockById, update: update, updateObjectKeys: updateObjectKeys, changePassword:changePassword }; /** * Create a new admin depends on it role (user, admin). * @param {object} data */ function add(data){ return webApi.createAdminProfile({ "firstName": data.FirstName, "lastName": data.LastName, "email": data.Email, "phoneNumber": data.PhoneNumber }); } /** * Retrieve an admin entity. * @param {number|null} id */ function getById(id){ if (id) { return webApi.getAdminProfileById({ "url": {"id": id} }); } else { return webApi.getAdminProfileByToken([]); } } /** * Retrieve all existing admins 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.getAdminProfileActive({ before: [], after: queryParams }) } /** * Retrieve blocked admins 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.getAdminProfileBlocked({ before: [], after: queryParams }) } /** * Set locked status to specified admin. * @param {number} id * @returns {*} */ function lockById (id){ return webApi.blockAdminProfileById({ "url": { "id": id } }); } function removeById (){ } /** * Modify password for users only. * @param {object} data * @param {number|string} id */ function changePassword(data, id){ return webApi.adminProfileChangePassword({ url: { "id": id }, data: data }); } /** * Update admin details. * @param {object} data * @param {number|null} id */ function update(data, id){ if (!id || id == "") { return webApi.updateAdminProfileByToken(data); } else { return webApi.updateAdminProfileById({ url: { "id": id }, data: data }); } } function updateObjectKeys(oldObj, newObj){ for (var key in newObj) { if (newObj.hasOwnProperty(key)) { oldObj[key] = newObj[key]; } } } 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 admin. * @param {number} id * @returns {*} */ function unlockById (id){ return webApi.unblockAdminProfileById({ "url": { "id": id } }); } } // IoC container. adminRepository.$inject = [ "webApi", "appConstant.dataTable" ]; })();