Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.90 KB | None | 0 0
  1. (function() {
  2. angular
  3. .module('CourtRuleManager')
  4. .factory('userService',
  5. [
  6. '$q',
  7. '$http',
  8. 'configService',
  9. '$rootScope',
  10. '$cookies',
  11. '$location',
  12. userService
  13. ])
  14.  
  15. function userService($q, $http, configService, $rootScope, $cookies, $location) {
  16.  
  17. var users = [
  18. { username: 'bob', password: 'ewq', _id: 123 },
  19. { username: 'alice', password: 'ewq', _id: 345 },
  20. { username: 'charlie', password: 'ewq', _id: 456 }
  21. ];
  22.  
  23. //method declarations
  24. var api = {
  25. findUserByCredentials: findUserByCredentials,
  26. findUserById: findUserById,
  27. isLoggedIn: isLoggedIn,
  28. getCurrentUserRole: getCurrentUserRole,
  29. verifyEmail: verifyEmail,
  30. resetPassword: resetPassword,
  31. getProfileInfo: getProfileInfo,
  32. confirmPassword: confirmPassword,
  33. saveUser: saveUser,
  34. findUserByEmail: findUserByEmail,
  35. getUserByEmail: getUserByEmail,
  36. isLoggedIn: isLoggedIn,
  37. getUsers: getUsers,
  38. getUsersByRole: getUsersByRole,
  39. getUsersByCustomer: getUsersByCustomer
  40. };
  41.  
  42. return api;
  43.  
  44. //method definitions
  45. function findUserByCredentials(username, password) {
  46.  
  47. return $q(function (resolve, reject) {
  48. var token = $cookies.get('token');
  49. var userId = $cookies.get('userId');
  50. $http({
  51. method: 'post',
  52. url: configService.const.apiUrl + 'user',
  53. data: { 'Email': username, 'password': password }, //"username="+username+"&password="+password,
  54. headers: { 'Content-Type': 'application/json' }
  55. })
  56. .then(Success, Error);
  57.  
  58. function Success(response) {
  59. if (response.data.is_success) {
  60. //bootbox.dialog({
  61. // message: '<div class="msg-success">' + response.data.message + '</div>',
  62. // size: 'small',
  63. // closeButton: false,
  64. // timeOut: 3000
  65. //});
  66.  
  67.  
  68. resolve(response.data);
  69. } else {
  70.  
  71. //debugger;
  72. //bootbox.dialog({
  73. // message: '<div class="msg-danger">' + response.data.message + '</div>',
  74. // size: 'small',
  75. // closeButton: false,
  76. // timeOut: 3000
  77. //});
  78. resolve(false);
  79. }
  80. }
  81.  
  82. function Error(response) {
  83.  
  84. debugger;
  85. console.log(response.data);
  86.  
  87. if (response.status <= 0) {
  88. bootbox.dialog({
  89. message:
  90. '<div class="msg-danger">Destination is unreachable or request timeout occur, please check the request url.</div>',
  91. size: 'medium',
  92. closeButton: false,
  93. timeOut: 3000
  94. });
  95. } else if (response.message) {
  96. bootbox.dialog({
  97. message: '<div class="msg-danger">' + response.message + '</div>',
  98. size: 'small',
  99. closeButton: false,
  100. timeOut: 3000
  101. });
  102. } else {
  103. bootbox.dialog({
  104. message: '<div class="msg-danger">' + response.data.message + '</div>',
  105. size: 'small',
  106. closeButton: false,
  107. timeOut: 3000
  108. });
  109. }
  110. reject(false);
  111. }
  112. });
  113.  
  114. }
  115.  
  116. function getUserByEmail(email) {
  117.  
  118. return $q(function(resolve, reject) {
  119. $http({
  120. method: 'get',
  121. url: configService.const.apiUrl + 'user/' + email,
  122. headers: { 'Content-Type': 'application/json' }
  123. })
  124. .then(Success, Error);
  125.  
  126. function Success(response) {
  127. if (response.data.is_success) {
  128. resolve(response.data.content);
  129. } else {
  130. bootbox.dialog({
  131. message: '<div class="msg-danger">' + response.data.message + '</div>',
  132. size: 'small',
  133. closeButton: false,
  134. timeOut: 3000
  135. });
  136. resolve(null);
  137. }
  138. }
  139.  
  140. function Error(response) {
  141. if (response.status <= 0) {
  142. bootbox.dialog({
  143. message:
  144. '<div class="msg-danger">Destination is unreachable or request timeout occur, please check the request url.</div>',
  145. size: 'medium',
  146. closeButton: false,
  147. timeOut: 3000
  148. });
  149. } else if (response.message) {
  150. bootbox.dialog({
  151. message: '<div class="msg-danger">' + response.message + '</div>',
  152. size: 'small',
  153. closeButton: false,
  154. timeOut: 3000
  155. });
  156. } else {
  157. bootbox.dialog({
  158. message: '<div class="msg-danger">' + response.data.message + '</div>',
  159. size: 'small',
  160. closeButton: false,
  161. timeOut: 3000
  162. });
  163. }
  164.  
  165. reject(null);
  166. }
  167. });
  168.  
  169. }
  170.  
  171. function isLoggedIn() {
  172. var token = $cookies.get('token');
  173. if (token == null) {
  174. return false;
  175. } else {
  176. return true;
  177. }
  178. }
  179.  
  180. function getUsers() {
  181.  
  182. return $q(function(resolve, reject) {
  183. $http({
  184. method: 'get',
  185. url: configService.const.apiUrl + 'user/',
  186. headers: { 'Content-Type': 'application/json' }
  187. })
  188. .then(Success, Error);
  189.  
  190. function Success(response) {
  191. if (response.data.is_success) {
  192. resolve(response.data.content);
  193. } else {
  194. bootbox.dialog({
  195. message: '<div class="msg-danger">' + response.data.message + '</div>',
  196. size: 'small',
  197. closeButton: false,
  198. timeOut: 3000
  199. });
  200. resolve(null);
  201. }
  202. }
  203.  
  204. function Error(response) {
  205. if (response.status <= 0) {
  206. bootbox.dialog({
  207. message:
  208. '<div class="msg-danger">Destination is unreachable or request timeout occur, please check the request url.</div>',
  209. size: 'medium',
  210. closeButton: false,
  211. timeOut: 3000
  212. });
  213. } else if (response.message) {
  214. bootbox.dialog({
  215. message: '<div class="msg-danger">' + response.message + '</div>',
  216. size: 'small',
  217. closeButton: false,
  218. timeOut: 3000
  219. });
  220. } else {
  221. bootbox.dialog({
  222. message: '<div class="msg-danger">' + response.data.message + '</div>',
  223. size: 'small',
  224. closeButton: false,
  225. timeOut: 3000
  226. });
  227. }
  228.  
  229. reject(null);
  230. }
  231. });
  232.  
  233. }
  234.  
  235. function getUsersByRole(roleID) {
  236.  
  237. return $q(function(resolve, reject) {
  238. $http({
  239. method: 'get',
  240. url: configService.const.apiUrl + 'user/' + roleID,
  241. headers: { 'Content-Type': 'application/json' }
  242. })
  243. .then(Success, Error);
  244.  
  245. function Success(response) {
  246. if (response.data.is_success) {
  247. resolve(response.data.content);
  248. } else {
  249. bootbox.dialog({
  250. message: '<div class="msg-danger">' + response.data.message + '</div>',
  251. size: 'small',
  252. closeButton: false,
  253. timeOut: 3000
  254. });
  255. resolve(null);
  256. }
  257. }
  258.  
  259. function Error(response) {
  260. if (response.status <= 0) {
  261. bootbox.dialog({
  262. message:
  263. '<div class="msg-danger">Destination is unreachable or request timeout occur, please check the request url.</div>',
  264. size: 'medium',
  265. closeButton: false,
  266. timeOut: 3000
  267. });
  268. } else if (response.message) {
  269. bootbox.dialog({
  270. message: '<div class="msg-danger">' + response.message + '</div>',
  271. size: 'small',
  272. closeButton: false,
  273. timeOut: 3000
  274. });
  275. } else {
  276. bootbox.dialog({
  277. message: '<div class="msg-danger">' + response.data.message + '</div>',
  278. size: 'small',
  279. closeButton: false,
  280. timeOut: 3000
  281. });
  282. }
  283.  
  284. reject(null);
  285. }
  286. });
  287. }
  288.  
  289. function findUserById(id) {
  290. return $q(function(resolve, reject) {
  291. $http({
  292. method: 'get',
  293. url: configService.const.apiUrl + 'user/' + id,
  294. })
  295. .then(Success, Error);
  296.  
  297. function Success(response) {
  298. console.log("Successful - findUserById()");
  299. console.log(response);
  300. var users = response.data.content;
  301. users = users.filter(function(u) {
  302. return u.id == id
  303. })
  304. var user = users.length > 0 ? users[0] : {}
  305. resolve(user);
  306. }
  307.  
  308. function Error(response) {
  309. console.log("Unsuccessful - findUserById()");
  310. }
  311.  
  312. })
  313. }
  314.  
  315. function getUsersByCustomer(customerID) {
  316.  
  317. return $q(function(resolve, reject) {
  318. $http({
  319. method: 'get',
  320. url: configService.const.apiUrl + 'user/' + customerID,
  321. headers: { 'Content-Type': 'application/json' }
  322. })
  323. .then(Success, Error);
  324.  
  325. function Success(response) {
  326. if (response.data.is_success) {
  327. resolve(response.data.content);
  328. } else {
  329. bootbox.dialog({
  330. message: '<div class="msg-danger">' + response.data.message + '</div>',
  331. size: 'small',
  332. closeButton: false,
  333. timeOut: 3000
  334. });
  335. resolve(null);
  336. }
  337. }
  338.  
  339. function Error(response) {
  340. console.log("Unsuccessful - findUserById()");
  341. }
  342. })
  343. }
  344.  
  345. function findUserByEmail(email) {
  346. return $q(function(resolve, reject) {
  347. var h = $http({
  348. method: "GET",
  349. url: configService.const.apiUrl + 'user' + '/filter?email=' + encodeURIComponent(email),
  350. })
  351. .then(Success, Error);
  352.  
  353. function Success(response) {
  354. resolve(response.data && response.data.content[0]);
  355. }
  356.  
  357. function Error(response) {
  358. console.warn(response.data && response.data.message);
  359. }
  360. });
  361. }
  362.  
  363. function isLoggedIn() {
  364. var token = $cookies.get('token');
  365. return token != null;
  366. }
  367.  
  368. function getCurrentUserRole() {
  369. return $cookies.get('role');
  370. }
  371.  
  372. function verifyEmail(email, isNewUser) {
  373. isNewUser = isNewUser || false
  374. return $q(function(resolve, reject) {
  375. $http({
  376. method: "GET",
  377. url: configService.const.apiUrl + 'user' + '/forgotpassword',
  378. params: {
  379. email: email,
  380. url: 'http://localhost:53220/#!/reset-password',
  381. isNewUser: isNewUser
  382. }
  383. })
  384. .then(Success, Error);
  385.  
  386. function Success(response) {
  387. console.log("Success - verifyEmail");
  388. }
  389.  
  390. function Error(response) {
  391. console.log("Unsuccessful - verifyEmail");
  392. }
  393. });
  394. }
  395.  
  396. function resetPassword(first_password, email, token) {
  397. return $q(function(resolve, reject) {
  398. $http({
  399. method: "post",
  400. url: configService.const.apiUrl + 'user' + '/resetpassword',
  401. params: { new_password: first_password, email: email, token: token },
  402. headers: { 'Content-Type': 'application/json' }
  403. })
  404. .then(Success, Error);
  405.  
  406. function Success(response) {
  407. bootbox.dialog({
  408. message: '<div class="msg-success">Your password has been reset.</div>',
  409. size: 'medium',
  410. closeButton: true,
  411. timeOut: 4000
  412. });
  413. $location.path("/login");
  414. }
  415.  
  416. function Error(response) {
  417. }
  418. });
  419. }
  420.  
  421. function getProfileInfo() {
  422. return $q(function(resolve, reject) {
  423. $http({
  424. method: "get",
  425. url: configService.const.apiUrl + 'user' + '/filter',
  426. params: { email: "kenny.k.luong@gmail.com" }
  427. })
  428. .then(Success, Error);
  429.  
  430. function Success(response) {
  431. console.log("Successful - GetProfileInfo");
  432. console.log(response);
  433. resolve(response);
  434. }
  435.  
  436. function Error(response) {
  437. console.log("Unsuccessful - GetProfileInfo");
  438. }
  439. });
  440. }
  441.  
  442. function confirmPassword(current_password, new_password, email) {
  443. return $q(function(resolve, reject) {
  444. $http({
  445. method: "get",
  446. url: configService.const.apiUrl + 'user' + '/confirmpassword',
  447. params: { password: current_password, email: email },
  448. headers: { 'Content-Type': 'application/json' }
  449. })
  450. .then(Success, Error);
  451.  
  452. function Success(response) {
  453. console.log("Successful - confirmPassword");
  454. console.log(response);
  455. if (response.data.is_success) {
  456. changePassword(current_password, new_password, email);
  457. }
  458. }
  459.  
  460. function Error(response) {
  461. console.log("Unsuccessful - confirmPassword");
  462. }
  463. })
  464. }
  465.  
  466. function changePassword(current_password, new_password, email) {
  467. return $q(function(resolve, reject) {
  468. $http({
  469. method: "post",
  470. url: configService.const.apiUrl + 'user' + '/changepassword',
  471. params: { current_password: current_password, new_password: new_password, email: email }
  472. })
  473. .then(Success, Error);
  474.  
  475. function Success(response) {
  476. console.log("Successful - changePassword");
  477.  
  478. }
  479.  
  480. function Error(response) {
  481. console.log("Unsuccessful - changePassword");
  482. }
  483. })
  484. }
  485.  
  486. // PUT: /api/user
  487. // POST: /api/user/new
  488.  
  489. function saveUser(user) {
  490. return $q(function(resolve, reject) {
  491. $http({
  492. method: user._id > 0 ? 'put' : 'post',
  493. url: user._id > 0
  494. ? configService.const.apiUrl + 'user'
  495. : configService.const.apiUrl + 'user' + '/new',
  496. data: user,
  497. headers: { 'Content-Type': 'application/json' }
  498. })
  499. .then(Success, Error);
  500.  
  501. function Success(response) {
  502. if (response.data.is_success) {
  503. bootbox.dialog({
  504. message: '<div class="msg-success">' + response.data.message + '</div>',
  505. size: 'small',
  506. closeButton: false,
  507. timeOut: 3000
  508. });
  509. resolve(true);
  510. } else {
  511. bootbox.dialog({
  512. message: '<div class="msg-danger">' + response.data.message + '</div>',
  513. size: 'small',
  514. closeButton: false,
  515. timeOut: 3000
  516. });
  517. resolve(false);
  518. }
  519. }
  520.  
  521. function Error(response) {
  522. if (response.status <= 0) {
  523. bootbox.dialog({
  524. message:
  525. '<div class="msg-danger">Destination is unreachable or request timeout occur, please check the request url.</div>',
  526. size: 'medium',
  527. closeButton: false,
  528. timeOut: 3000
  529. });
  530. } else if (response.message) {
  531. bootbox.dialog({
  532. message: '<div class="msg-danger">' + response.message + '</div>',
  533. size: 'small',
  534. closeButton: false,
  535. timeOut: 3000
  536. });
  537. } else {
  538. bootbox.dialog({
  539. message: '<div class="msg-danger">' + response.data.message + '</div>',
  540. size: 'small',
  541. closeButton: false,
  542. timeOut: 3000
  543. });
  544. }
  545.  
  546. reject(false);
  547. }
  548. });
  549. }
  550.  
  551. }
  552.  
  553. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement