Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- angular
- .module('CourtRuleManager')
- .factory('userService',
- [
- '$q',
- '$http',
- 'configService',
- '$rootScope',
- '$cookies',
- '$location',
- userService
- ])
- function userService($q, $http, configService, $rootScope, $cookies, $location) {
- var users = [
- { username: 'bob', password: 'ewq', _id: 123 },
- { username: 'alice', password: 'ewq', _id: 345 },
- { username: 'charlie', password: 'ewq', _id: 456 }
- ];
- //method declarations
- var api = {
- findUserByCredentials: findUserByCredentials,
- findUserById: findUserById,
- isLoggedIn: isLoggedIn,
- getCurrentUserRole: getCurrentUserRole,
- verifyEmail: verifyEmail,
- resetPassword: resetPassword,
- getProfileInfo: getProfileInfo,
- confirmPassword: confirmPassword,
- saveUser: saveUser,
- findUserByEmail: findUserByEmail,
- getUserByEmail: getUserByEmail,
- isLoggedIn: isLoggedIn,
- getUsers: getUsers,
- getUsersByRole: getUsersByRole,
- getUsersByCustomer: getUsersByCustomer
- };
- return api;
- //method definitions
- function findUserByCredentials(username, password) {
- return $q(function (resolve, reject) {
- var token = $cookies.get('token');
- var userId = $cookies.get('userId');
- $http({
- method: 'post',
- url: configService.const.apiUrl + 'user',
- data: { 'Email': username, 'password': password }, //"username="+username+"&password="+password,
- headers: { 'Content-Type': 'application/json' }
- })
- .then(Success, Error);
- function Success(response) {
- if (response.data.is_success) {
- //bootbox.dialog({
- // message: '<div class="msg-success">' + response.data.message + '</div>',
- // size: 'small',
- // closeButton: false,
- // timeOut: 3000
- //});
- resolve(response.data);
- } else {
- //debugger;
- //bootbox.dialog({
- // message: '<div class="msg-danger">' + response.data.message + '</div>',
- // size: 'small',
- // closeButton: false,
- // timeOut: 3000
- //});
- resolve(false);
- }
- }
- function Error(response) {
- debugger;
- console.log(response.data);
- if (response.status <= 0) {
- bootbox.dialog({
- message:
- '<div class="msg-danger">Destination is unreachable or request timeout occur, please check the request url.</div>',
- size: 'medium',
- closeButton: false,
- timeOut: 3000
- });
- } else if (response.message) {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- } else {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.data.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- }
- reject(false);
- }
- });
- }
- function getUserByEmail(email) {
- return $q(function(resolve, reject) {
- $http({
- method: 'get',
- url: configService.const.apiUrl + 'user/' + email,
- headers: { 'Content-Type': 'application/json' }
- })
- .then(Success, Error);
- function Success(response) {
- if (response.data.is_success) {
- resolve(response.data.content);
- } else {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.data.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- resolve(null);
- }
- }
- function Error(response) {
- if (response.status <= 0) {
- bootbox.dialog({
- message:
- '<div class="msg-danger">Destination is unreachable or request timeout occur, please check the request url.</div>',
- size: 'medium',
- closeButton: false,
- timeOut: 3000
- });
- } else if (response.message) {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- } else {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.data.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- }
- reject(null);
- }
- });
- }
- function isLoggedIn() {
- var token = $cookies.get('token');
- if (token == null) {
- return false;
- } else {
- return true;
- }
- }
- function getUsers() {
- return $q(function(resolve, reject) {
- $http({
- method: 'get',
- url: configService.const.apiUrl + 'user/',
- headers: { 'Content-Type': 'application/json' }
- })
- .then(Success, Error);
- function Success(response) {
- if (response.data.is_success) {
- resolve(response.data.content);
- } else {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.data.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- resolve(null);
- }
- }
- function Error(response) {
- if (response.status <= 0) {
- bootbox.dialog({
- message:
- '<div class="msg-danger">Destination is unreachable or request timeout occur, please check the request url.</div>',
- size: 'medium',
- closeButton: false,
- timeOut: 3000
- });
- } else if (response.message) {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- } else {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.data.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- }
- reject(null);
- }
- });
- }
- function getUsersByRole(roleID) {
- return $q(function(resolve, reject) {
- $http({
- method: 'get',
- url: configService.const.apiUrl + 'user/' + roleID,
- headers: { 'Content-Type': 'application/json' }
- })
- .then(Success, Error);
- function Success(response) {
- if (response.data.is_success) {
- resolve(response.data.content);
- } else {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.data.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- resolve(null);
- }
- }
- function Error(response) {
- if (response.status <= 0) {
- bootbox.dialog({
- message:
- '<div class="msg-danger">Destination is unreachable or request timeout occur, please check the request url.</div>',
- size: 'medium',
- closeButton: false,
- timeOut: 3000
- });
- } else if (response.message) {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- } else {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.data.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- }
- reject(null);
- }
- });
- }
- function findUserById(id) {
- return $q(function(resolve, reject) {
- $http({
- method: 'get',
- url: configService.const.apiUrl + 'user/' + id,
- })
- .then(Success, Error);
- function Success(response) {
- console.log("Successful - findUserById()");
- console.log(response);
- var users = response.data.content;
- users = users.filter(function(u) {
- return u.id == id
- })
- var user = users.length > 0 ? users[0] : {}
- resolve(user);
- }
- function Error(response) {
- console.log("Unsuccessful - findUserById()");
- }
- })
- }
- function getUsersByCustomer(customerID) {
- return $q(function(resolve, reject) {
- $http({
- method: 'get',
- url: configService.const.apiUrl + 'user/' + customerID,
- headers: { 'Content-Type': 'application/json' }
- })
- .then(Success, Error);
- function Success(response) {
- if (response.data.is_success) {
- resolve(response.data.content);
- } else {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.data.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- resolve(null);
- }
- }
- function Error(response) {
- console.log("Unsuccessful - findUserById()");
- }
- })
- }
- function findUserByEmail(email) {
- return $q(function(resolve, reject) {
- var h = $http({
- method: "GET",
- url: configService.const.apiUrl + 'user' + '/filter?email=' + encodeURIComponent(email),
- })
- .then(Success, Error);
- function Success(response) {
- resolve(response.data && response.data.content[0]);
- }
- function Error(response) {
- console.warn(response.data && response.data.message);
- }
- });
- }
- function isLoggedIn() {
- var token = $cookies.get('token');
- return token != null;
- }
- function getCurrentUserRole() {
- return $cookies.get('role');
- }
- function verifyEmail(email, isNewUser) {
- isNewUser = isNewUser || false
- return $q(function(resolve, reject) {
- $http({
- method: "GET",
- url: configService.const.apiUrl + 'user' + '/forgotpassword',
- params: {
- email: email,
- url: 'http://localhost:53220/#!/reset-password',
- isNewUser: isNewUser
- }
- })
- .then(Success, Error);
- function Success(response) {
- console.log("Success - verifyEmail");
- }
- function Error(response) {
- console.log("Unsuccessful - verifyEmail");
- }
- });
- }
- function resetPassword(first_password, email, token) {
- return $q(function(resolve, reject) {
- $http({
- method: "post",
- url: configService.const.apiUrl + 'user' + '/resetpassword',
- params: { new_password: first_password, email: email, token: token },
- headers: { 'Content-Type': 'application/json' }
- })
- .then(Success, Error);
- function Success(response) {
- bootbox.dialog({
- message: '<div class="msg-success">Your password has been reset.</div>',
- size: 'medium',
- closeButton: true,
- timeOut: 4000
- });
- $location.path("/login");
- }
- function Error(response) {
- }
- });
- }
- function getProfileInfo() {
- return $q(function(resolve, reject) {
- $http({
- method: "get",
- url: configService.const.apiUrl + 'user' + '/filter',
- params: { email: "kenny.k.luong@gmail.com" }
- })
- .then(Success, Error);
- function Success(response) {
- console.log("Successful - GetProfileInfo");
- console.log(response);
- resolve(response);
- }
- function Error(response) {
- console.log("Unsuccessful - GetProfileInfo");
- }
- });
- }
- function confirmPassword(current_password, new_password, email) {
- return $q(function(resolve, reject) {
- $http({
- method: "get",
- url: configService.const.apiUrl + 'user' + '/confirmpassword',
- params: { password: current_password, email: email },
- headers: { 'Content-Type': 'application/json' }
- })
- .then(Success, Error);
- function Success(response) {
- console.log("Successful - confirmPassword");
- console.log(response);
- if (response.data.is_success) {
- changePassword(current_password, new_password, email);
- }
- }
- function Error(response) {
- console.log("Unsuccessful - confirmPassword");
- }
- })
- }
- function changePassword(current_password, new_password, email) {
- return $q(function(resolve, reject) {
- $http({
- method: "post",
- url: configService.const.apiUrl + 'user' + '/changepassword',
- params: { current_password: current_password, new_password: new_password, email: email }
- })
- .then(Success, Error);
- function Success(response) {
- console.log("Successful - changePassword");
- }
- function Error(response) {
- console.log("Unsuccessful - changePassword");
- }
- })
- }
- // PUT: /api/user
- // POST: /api/user/new
- function saveUser(user) {
- return $q(function(resolve, reject) {
- $http({
- method: user._id > 0 ? 'put' : 'post',
- url: user._id > 0
- ? configService.const.apiUrl + 'user'
- : configService.const.apiUrl + 'user' + '/new',
- data: user,
- headers: { 'Content-Type': 'application/json' }
- })
- .then(Success, Error);
- function Success(response) {
- if (response.data.is_success) {
- bootbox.dialog({
- message: '<div class="msg-success">' + response.data.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- resolve(true);
- } else {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.data.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- resolve(false);
- }
- }
- function Error(response) {
- if (response.status <= 0) {
- bootbox.dialog({
- message:
- '<div class="msg-danger">Destination is unreachable or request timeout occur, please check the request url.</div>',
- size: 'medium',
- closeButton: false,
- timeOut: 3000
- });
- } else if (response.message) {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- } else {
- bootbox.dialog({
- message: '<div class="msg-danger">' + response.data.message + '</div>',
- size: 'small',
- closeButton: false,
- timeOut: 3000
- });
- }
- reject(false);
- }
- });
- }
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement