Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.61 KB | None | 0 0
  1. 'CompaniesService', ['$http', '$rootScope', 'RealTimeService', 'Utils', '$filter', function ($http, $rootScope, RealTimeService, Utils) {
  2.  
  3. var serviceScope = $rootScope.$new(),
  4. initialized = false,
  5. initListeners = [],
  6. amountChangeListeners = [],
  7. storage = {
  8. companies: [],
  9. clientCompanies: [],
  10. deletedCompanies: [],
  11. currentCompany: {}
  12. },
  13. callbackForArchive,
  14. callbackForSavedCompanies = {};
  15.  
  16. Utils.customizeObj(storage);
  17.  
  18. $http.get('/company/clients').then(function (resp) {
  19. for (var i = 0; i < resp.data.length; i++) {
  20. var custCompany = Utils.customizeObj(resp.data[i]);
  21. if (resp.data[i].deletedAt) {
  22. storage.deletedCompanies.push(custCompany);
  23. } else {
  24. storage.clientCompanies.push(custCompany);
  25. storage.companies.push(custCompany);
  26. }
  27. }
  28. checkInit();
  29. },
  30. function (resp) {
  31. console.error(resp);
  32. throw 'Cannot initialize CompaniesService!';
  33. });
  34.  
  35. $http.get('/company/current').then(function (resp) {
  36. Utils.customizeObj(resp.data, storage.currentCompany);
  37. storage.companies.push(storage.currentCompany);
  38. checkInit();
  39. },
  40. function (resp) {
  41. console.error(resp);
  42. throw 'Cannot initialize CompaniesService!';
  43. });
  44.  
  45. var checkInitsCount = 0;
  46.  
  47. function checkInit() {
  48. checkInitsCount++;
  49. if (checkInitsCount === 2) {
  50. initialized = true;
  51.  
  52. for (var i = 0; i < initListeners.length; i++) {
  53. initListeners[i]();
  54. }
  55. }
  56. }
  57.  
  58. RealTimeService.subscribe('/company', serviceScope, function (newChange) {
  59.  
  60. console.log(newChange);
  61.  
  62. if (!initialized) {
  63. return;
  64. }
  65.  
  66. var companyId = newChange.data.id;
  67. if (newChange.action == 'COMPANY_SAVED') {
  68.  
  69. var company = Utils.findById(storage.companies, companyId);
  70.  
  71. if (company) {
  72. Utils.customizeObj(newChange.data, company);
  73. } else {
  74. var newClient = Utils.customizeObj(newChange.data);
  75. storage.companies.push(newClient);
  76. storage.clientCompanies.push(newClient);
  77.  
  78. for (var i = 0; i < amountChangeListeners.length; i++) {
  79. amountChangeListeners[i]();
  80. }
  81.  
  82. if (callbackForSavedCompanies[companyId]) {
  83. callbackForSavedCompanies[companyId](companyId);
  84. }
  85. }
  86.  
  87. } else if (newChange.action == 'COMPANY_ARCHIVED') {
  88. storage.deletedCompanies.push(Utils.findById(storage.companies, companyId));
  89.  
  90. Utils.deleteById(storage.companies, companyId);
  91. Utils.deleteById(storage.clientCompanies, companyId);
  92.  
  93. if (callbackForArchive && (typeof callbackForArchive == 'function')) {
  94. callbackForArchive(companyId);
  95. }
  96.  
  97. for (var i = 0; i < amountChangeListeners.length; i++) {
  98. amountChangeListeners[i]();
  99. }
  100. } else if (newChange.action == 'COMPANY_DEARCHIVED') {
  101.  
  102. var company = Utils.customizeObj(newChange.data);
  103.  
  104. storage.clientCompanies.push(company);
  105. storage.companies.push(company);
  106.  
  107. for (var i = 0; i < amountChangeListeners.length; i++) {
  108. amountChangeListeners[i]();
  109. }
  110. }
  111. });
  112.  
  113. function _addAmountChangeListener(callback) {
  114. amountChangeListeners.push(callback);
  115. }
  116.  
  117. function getCurrentCompany() {
  118. return storage.currentCompany;
  119. }
  120.  
  121. function getClientCompanies(callback) {
  122. addInitListener(callback);
  123. return storage.clientCompanies;
  124. }
  125.  
  126. function getAllCompanies() {
  127. return storage.companies;
  128. }
  129.  
  130. function getUnassignedCompany() {
  131. for (var i = 0; i < storage.companies.length; i++) {
  132. if (storage.companies[i].isDefault) {
  133. return storage.companies[i];
  134. }
  135. }
  136. }
  137.  
  138. function getOneById(companyId, callback) {
  139. if (callback) {
  140. callbackForArchive = callback;
  141. }
  142. return Utils.findById(storage.companies, companyId) || Utils.findById(storage.deletedCompanies, companyId);
  143. }
  144.  
  145. function searchByAllCompanies(keyword) {
  146. return search(keyword, true)
  147. }
  148.  
  149. function searchByClientCompanies(keyword) {
  150. return search(keyword, false)
  151. }
  152.  
  153. function search(keyword, includeCurrent) {
  154.  
  155. if (!keyword) {
  156. throw 'Keyword is required!'
  157. }
  158.  
  159. var searchResult = [],
  160. keywordLowerCase = keyword.toLowerCase();
  161.  
  162. for (var i = 0; i < storage.companies.length; i++) {
  163.  
  164. var company = storage.companies[i];
  165. if (!includeCurrent && storage.currentCompany.id === company.id) {
  166. continue;
  167. }
  168.  
  169. if (company.name.toLowerCase().indexOf(keywordLowerCase) > -1) {
  170. searchResult.push(company);
  171. }
  172. }
  173.  
  174. return searchResult;
  175. }
  176.  
  177. function archive(companyId, callback) {
  178. if (!companyId) throw 'companyId is required!';
  179. $http.get('/company/archive/' + companyId).then(function (resp) {
  180. callback(resp);
  181. }, function (resp) {
  182. callback(resp);
  183. });
  184. }
  185.  
  186. function dearchive(companyId, callback) {
  187. if (!companyId) throw 'companyId is required!';
  188. var future = $http.get('/company/dearchive/' + companyId);
  189. Utils.handleCallback(future, callback);
  190. }
  191.  
  192. function getClientCompaniesInfo(scope, callback) {
  193.  
  194. if (!scope || !scope.$id) {
  195. throw 'Second argument must be Angularjs scope!'
  196. }
  197.  
  198. var info = Utils.customizeObj({});
  199.  
  200. $http.get('/company/clients/info').then(function (resp) {
  201. Utils.customizeObj(resp.data, info);
  202.  
  203. if (callback) {
  204. callback(resp.data);
  205. }
  206.  
  207. }, function (resp) {
  208. if (callback) {
  209. callback(resp.data);
  210. }
  211. });
  212.  
  213. RealTimeService.subscribe('/clientCompaniesInfo', scope, function (newChange) {
  214.  
  215. console.log(newChange);
  216.  
  217. if (newChange.action == 'CLIENT_COMPANY_INFO') {
  218.  
  219. var data = JSON.parse(newChange.data);
  220.  
  221. for (var companyId in data) {
  222. Utils.defineReadOnlyProperty(info, companyId, Utils.customizeObj(data[companyId]));
  223. }
  224. } else if (newChange.action == 'COMPANY_ARCHIVED') {
  225. delete info[newChange.data.id];
  226.  
  227. if (callbackForArchive && (typeof callbackForArchive == 'function')) {
  228. callbackForArchive(newChange.data.id);
  229. }
  230. }
  231. });
  232.  
  233. return info;
  234. }
  235.  
  236. function getCompanyInfo(companyId, scope, callback) {
  237.  
  238. if (!companyId) {
  239. throw 'companyId is required!'
  240. }
  241.  
  242. if (!scope || !scope.$id) {
  243. throw 'Second argument must be Angularjs scope!'
  244. }
  245.  
  246. var info = Utils.customizeObj({});
  247.  
  248. $http.get('/company/' + companyId + '/info').then(function (resp) {
  249. Utils.customizeObj(resp.data, info);
  250.  
  251. if (callback) {
  252. callback(resp.data);
  253. }
  254.  
  255. }, function (resp) {
  256. if (callback) {
  257. callback(resp.data);
  258. }
  259. });
  260.  
  261. RealTimeService.subscribe('/clientCompaniesInfo', scope, function (newChange) {
  262.  
  263. console.log(newChange);
  264.  
  265. if (newChange.action == 'CLIENT_COMPANY_INFO') {
  266.  
  267. var data = JSON.parse(newChange.data);
  268.  
  269. for (var key in data) {
  270. if (key === companyId) {
  271. Utils.customizeObj(data[companyId], info);
  272. }
  273. }
  274. }
  275. });
  276.  
  277. return info;
  278. }
  279.  
  280. function getCompanyData(scope, companyId, callback) {
  281.  
  282. if (!companyId) {
  283. throw 'companyId is required!'
  284. }
  285.  
  286. if (!scope || !scope.$id) {
  287. throw 'Second argument must be Angularjs scope!'
  288. }
  289.  
  290. var companyDto = Utils.customizeObj({});
  291.  
  292. $http.get('/company/' + companyId + '/data').then(function (resp) {
  293. Utils.customizeObj(resp.data, companyDto);
  294.  
  295. if (callback) {
  296. callback(resp.data);
  297. }
  298.  
  299. }, function (resp) {
  300. if (callback) {
  301. callback(resp.data);
  302. }
  303. });
  304.  
  305. RealTimeService.subscribe('/companyDto/' + companyId, scope, function (newChange) {
  306.  
  307. console.log(newChange);
  308.  
  309. if (newChange.action == 'COMPANY_DTO_CHANGED') {
  310.  
  311. var data = JSON.parse(newChange.data);
  312.  
  313. for (var key in data) {
  314. if (key === companyId) {
  315. Utils.customizeObj(data[companyId], companyDto);
  316. }
  317. }
  318. }
  319. });
  320.  
  321. return companyDto;
  322. }
  323.  
  324. function isInitialized() {
  325. return initialized;
  326. }
  327.  
  328. function save(company, callback) {
  329.  
  330. if (!company) throw 'company is required!';
  331.  
  332. var method = 'POST';
  333. if (company.id) {
  334. method = 'PUT';
  335. }
  336.  
  337. var future = $http({
  338. url: '/company',
  339. method: method,
  340. data: company
  341. });
  342.  
  343. if (!company.id && callback) {
  344. future.then(function (resp) {
  345. var companyId = resp.data.id;
  346. if (!getOneById(companyId)) {
  347. callbackForSavedCompanies[companyId] = callback;
  348. } else {
  349. callback(resp.data);
  350. }
  351. });
  352. } else {
  353. Utils.handleCallback(future, callback);
  354. }
  355. }
  356.  
  357. function saveCompanySettingsDto(companyDto, callback) {
  358.  
  359. if (!companyDto) throw 'companyDto is required!';
  360.  
  361. var future = $http({
  362. url: '/settings/companyProfile/',
  363. method: 'PUT',
  364. data: companyDto
  365. });
  366.  
  367. Utils.handleCallback(future, callback);
  368. }
  369.  
  370. function saveForCompanyProfile(data, toUpdate, callback) {
  371.  
  372. if (!data) throw 'data is required!';
  373. if (!toUpdate) throw 'toUpdate is required!';
  374.  
  375. if (toUpdate === 'addresses') {
  376. data = { addresses: data }
  377. }
  378.  
  379. var url = '/settings/companyProfile/' + toUpdate,
  380. method = 'PUT',
  381. saveData = angular.copy(data);
  382.  
  383. $http({
  384. url: url,
  385. method: method,
  386. data: data
  387. }).then(function (resp) {
  388. if (callback) {
  389. callback(resp.data, saveData);
  390. }
  391. }, function (resp) {
  392. if (callback) {
  393. callback(resp.data, saveData);
  394. }
  395. });
  396.  
  397. }
  398.  
  399. function getCompanyForSettingPage(callback) {
  400.  
  401. var info = {};
  402.  
  403. $http.get('/settings/companyProfile/').then(function (resp) {
  404. Utils.customizeObj(resp.data, info);
  405.  
  406. if (callback) {
  407. callback(resp.data);
  408. }
  409.  
  410. }, function (resp) {
  411. if (callback) {
  412. callback(resp.data);
  413. }
  414. });
  415. return info;
  416. }
  417.  
  418. function changeUserAndPass(user, password, id, callback) {
  419.  
  420. if (!id) {
  421. throw 'id is required!'
  422. }
  423.  
  424. var info = Utils.customizeObj({});
  425.  
  426. $http.get('/company/' + id + '/changeUserAndPass?userName=' + user + '&password=' + password).then(function (resp) {
  427. Utils.customizeObj(resp.data, info);
  428.  
  429. if (callback) {
  430. callback(resp.data);
  431. }
  432.  
  433. }, function (resp) {
  434. if (callback) {
  435. callback(resp.data);
  436. }
  437. });
  438. return info;
  439. }
  440.  
  441. function isNameUnique(name, id, callback) {
  442.  
  443. if (!name) {
  444. throw 'name is required!'
  445. }
  446.  
  447. var info = Utils.customizeObj({});
  448.  
  449. $http.get('/company/isCompanyNameUnique?name=' + name + '&id=' + id).then(function (resp) {
  450. Utils.customizeObj(resp.data, info);
  451.  
  452. if (callback) {
  453. callback(resp.data);
  454. }
  455.  
  456. }, function (resp) {
  457. if (callback) {
  458. callback(resp.data);
  459. }
  460. });
  461. return info;
  462. }
  463.  
  464. function isDomainUnique(domain, id, callback) {
  465.  
  466. if (!domain) {
  467. throw 'domain is required!'
  468. }
  469.  
  470. var info = Utils.customizeObj({});
  471.  
  472. $http.get('/company/isCompanyDomainUnique?domain=' + domain + '&id=' + id).then(function (resp) {
  473. Utils.customizeObj(resp.data, info);
  474.  
  475. if (callback) {
  476. callback(resp.data);
  477. }
  478.  
  479. }, function (resp) {
  480. if (callback) {
  481. callback(resp.data);
  482. }
  483. });
  484. return info;
  485. }
  486.  
  487. function isCompanyInboxUnique(inbox, callback) {
  488.  
  489. if (!inbox) {
  490. throw 'inbox is required!'
  491. }
  492.  
  493. var info = Utils.customizeObj({});
  494.  
  495. $http.get('/isCompanyInboxUnique?inbox=' + inbox).then(function (resp) {
  496. Utils.customizeObj(resp.data, info);
  497.  
  498. if (callback) {
  499. callback(resp.data);
  500. }
  501.  
  502. }, function (resp) {
  503. if (callback) {
  504. callback(resp.data);
  505. }
  506. });
  507. return info;
  508. }
  509.  
  510. function saveLogo(croppedImage, companyId, callback) {
  511.  
  512. if (!croppedImage) throw 'croppedImage is required!';
  513. if (!companyId) throw 'companyId is required!';
  514.  
  515. var future = $http({
  516. url: '/company/' + companyId + '/logo',
  517. method: 'POST',
  518. data: croppedImage
  519. });
  520.  
  521. Utils.handleCallback(future, callback);
  522. }
  523.  
  524.  
  525. function saveCover(croppedImage, companyId, callback) {
  526.  
  527. if (!croppedImage) throw 'croppedImage is required!';
  528. if (!companyId) throw 'companyId is required!';
  529.  
  530. var url = '/company/' + companyId + '/cover',
  531. method = 'POST';
  532.  
  533. var future = $http({
  534. url: url,
  535. method: method,
  536. data: croppedImage
  537. });
  538.  
  539. Utils.handleCallback(future, callback);
  540. }
  541.  
  542. function deleteCover(companyId, callback) {
  543.  
  544. if (!companyId) throw 'companyId is required!';
  545.  
  546. var future = $http({
  547. url: '/company/' + companyId + '/false',
  548. method: 'DELETE'
  549. });
  550.  
  551. Utils.handleCallback(future, callback);
  552. }
  553.  
  554. function deleteLogo(companyId, callback) {
  555.  
  556. if (!companyId) throw 'companyId is required!';
  557.  
  558. var future = $http({
  559. url: '/company/' + companyId + '/true',
  560. method: 'DELETE'
  561. });
  562.  
  563. Utils.handleCallback(future, callback);
  564. }
  565.  
  566. function addInitListener(callback) {
  567.  
  568. if (!callback) return;
  569.  
  570. if (!initialized) {
  571. initListeners.push(callback);
  572. } else {
  573. callback();
  574. }
  575. }
  576.  
  577. function getCompanyContacts(scope, companyId, callback) {
  578.  
  579. if (!scope || !scope.$id) {
  580. throw 'First argument must be Angularjs scope!';
  581. }
  582. if (!companyId) {
  583. throw 'Second argument must be companyId!';
  584. }
  585.  
  586. return Utils.getListByUrl('/company/' + companyId + '/contacts', callback, function (list) {
  587.  
  588. RealTimeService.subscribe('/companyContact', scope, function (newChange) {
  589.  
  590. console.log(newChange);
  591.  
  592. if (newChange.action == 'COMPANY_CONTACT') {
  593. list.splice(0, list.length);
  594. Utils.customizeArray(newChange.data, list);
  595. }
  596. });
  597.  
  598. RealTimeService.subscribe('/subscription/' + companyId, scope, function (newChange) {
  599.  
  600. console.log(newChange);
  601.  
  602. if (!newChange.data.contact) {
  603. return;
  604. }
  605.  
  606. var contact = Utils.findById(list, newChange.data.contact.id);
  607.  
  608. if (contact) {
  609.  
  610. if (newChange.action == 'SUBSCRIPTION_SAVED') {
  611. Utils.defineReadOnlyProperty(contact, 'subscription', Utils.customizeObj(newChange.data));
  612. } else if (newChange.action == 'SUBSCRIPTION_DELETED') {
  613. delete contact.subscription;
  614. }
  615. }
  616.  
  617. });
  618. });
  619. }
  620.  
  621. function saveAllowingCreateCustomTags(value) {
  622. if (value === undefined) {
  623. throw 'First parameter required!'
  624. }
  625.  
  626. $http.put('/settings/companyProfile/allowUsersCreateTags?allow=' + value, {});
  627. }
  628.  
  629. function getCompanySMTPSettings(callback) {
  630. var future = $http.get('/settings/companyProfile/SMTPSettings');
  631. Utils.handleCallback(future, callback);
  632. }
  633.  
  634. function checkSMTPLoginUnique(email, callback) {
  635. var future = $http.get('/settings/companyProfile/SMTPSettings/checkEmailUnique?email=' + email);
  636. Utils.handleCallback(future, callback);
  637. }
  638.  
  639. function getPermissions(callback) {
  640. var future = $http.get('/settings/companyProfile/permissions');
  641. Utils.handleCallback(future, callback);
  642. }
  643.  
  644. function savePermissions(permissions, callback) {
  645. $http.post('/settings/companyProfile/permissions', permissions);
  646. }
  647.  
  648. function getTechRoles(callback) {
  649. var future = $http.get('/settings/companyProfile/getRoleNames');
  650. Utils.handleCallback(future, callback);
  651. }
  652.  
  653. function getEmailAlias(callback) {
  654. var future = $http.get('/settings/companyProfile/emailAlias');
  655. Utils.handleCallback(future, callback);
  656. }
  657.  
  658. function saveEmailAlias(emailAlias, callback) {
  659. var future = $http.post('/settings/companyProfile/emailAlias', { emailAlias: emailAlias });
  660. Utils.handleCallback(future, callback);
  661. }
  662.  
  663. return {
  664. _addInitListener: addInitListener,
  665. _addAmountChangeListener: _addAmountChangeListener,
  666. _search: search,
  667. _isInitialized: isInitialized,
  668. getCurrentCompany: getCurrentCompany,
  669. getClientCompanies: getClientCompanies,
  670. getAllCompanies: getAllCompanies,
  671. getOneById: getOneById,
  672. getUnassignedCompany: getUnassignedCompany,
  673. searchByAllCompanies: searchByAllCompanies,
  674. searchByClientCompanies: searchByClientCompanies,
  675. getClientCompaniesInfo: getClientCompaniesInfo,
  676. getCompanyInfo: getCompanyInfo,
  677. archive: archive,
  678. dearchive: dearchive,
  679. save: save,
  680. saveForCompanyProfile: saveForCompanyProfile,
  681. saveCompanySettingsDto: saveCompanySettingsDto,
  682. getCompanyForSettingPage: getCompanyForSettingPage,
  683. getCompanyData: getCompanyData,
  684. isNameUnique: isNameUnique,
  685. isDomainUnique: isDomainUnique,
  686. isCompanyInboxUnique: isCompanyInboxUnique,
  687. changeUserAndPass: changeUserAndPass,
  688. saveCover: saveCover,
  689. saveLogo: saveLogo,
  690. deleteCover: deleteCover,
  691. deleteLogo: deleteLogo,
  692. getCompanyContacts: getCompanyContacts,
  693. saveAllowingCreateCustomTags: saveAllowingCreateCustomTags,
  694. getCompanySMTPSettings: getCompanySMTPSettings,
  695. checkSMTPLoginUnique: checkSMTPLoginUnique,
  696. getPermissions: getPermissions,
  697. savePermissions: savePermissions,
  698. getTechRoles: getTechRoles,
  699. getEmailAlias: getEmailAlias,
  700. saveEmailAlias: saveEmailAlias
  701. }
  702.  
  703. }]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement