Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. ###
  2. handles all communications with API concerning channels
  3.  
  4. during development we will have some localstorage stubs in here
  5. ###
  6. angular.module('cbuResources').service 'TeamService',
  7. [
  8. 'ApiService',
  9. '$q',
  10. 'cbServices',
  11. 'Restangular',
  12. 'AccountService'
  13.  
  14. (ApiService, $q, cbServices, Restangular, AccountService) ->
  15. @Key = 'teams'
  16. backendReady = yes
  17. group = null
  18. account = null
  19. @teams = []
  20. Auth = cbServices.get('Auth')
  21.  
  22. getPromise = (resolver) ->
  23. deferred = $q.defer()
  24. deferred.resolve resolver
  25. deferred.promise
  26.  
  27. getCurrentGroup = (groupHash) ->
  28. svc.getById(groupHash).then (team) ->
  29. group = team
  30.  
  31. getCurrentAccount = (accountHash) ->
  32. AccountService.ensureLoaded (accounts) ->
  33. account = AccountService.getByHash(accountHash)
  34.  
  35. getFakeProfiles = (accountHash) ->
  36. group.customGET("account/#{accountHash}/user-group-share", {}, {'X-Api-Token': Auth.getToken()})
  37.  
  38. setFakeProfiles = (accountHash, fakeProfiles, groupShareHash) ->
  39. if (groupShareHash == null)
  40. group.customPOST({ fake_profiles: fakeProfiles }, "account/#{accountHash}/user-group-share", {}, {'X-Api-Token': Auth.getToken()})
  41. else
  42. group.customPUT({ fake_profiles: fakeProfiles }, "account/#{accountHash}/user-group-share/#{groupShareHash}", {}, {'X-Api-Token': Auth.getToken()})
  43. # public
  44. # import default methods
  45. # getAll, get, getById, add, update and delete
  46. svc = ApiService.getInstance(@Key, backendReady)
  47.  
  48. svc.ensureLoaded = (cb) =>
  49. svc.getAll().then (teams) =>
  50. @teams = teams
  51. svc.setAll teams, cb
  52.  
  53. svc.setAll = (teams, cb) =>
  54. unless @teams.length
  55. @teams = teams
  56. if typeof cb is 'function'
  57. cb(@teams)
  58.  
  59. svc.getGroupsByAccountWithContexts = (account) ->
  60. # Inject groups in scope with active for user groups
  61. query = {}
  62. query['search[primary_account]'] = account.hash
  63. query['view'] = 'with_contexts'
  64. svc.getBy(query)
  65.  
  66. svc.getFakeProfiles = (groupHash, accountHash) ->
  67. getCurrentGroup(groupHash).then () ->
  68. getCurrentAccount(accountHash).then () ->
  69. getFakeProfiles(accountHash)
  70.  
  71. svc.setFakeProfiles = (groupHash, accountHash, fakeProfiles, groupShareHash) ->
  72. getCurrentGroup(groupHash).then () ->
  73. getCurrentAccount(accountHash).then () ->
  74. setFakeProfiles(accountHash, fakeProfiles, groupShareHash)
  75.  
  76. svc.create = () ->
  77. _.cloneDeep
  78. name: ''
  79. members: []
  80.  
  81. # return the exports
  82. svc
  83. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement