Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###
- handles all communications with API concerning channels
- during development we will have some localstorage stubs in here
- ###
- angular.module('cbuResources').service 'TeamService',
- [
- 'ApiService',
- '$q',
- 'cbServices',
- 'Restangular',
- 'AccountService'
- (ApiService, $q, cbServices, Restangular, AccountService) ->
- @Key = 'teams'
- backendReady = yes
- group = null
- account = null
- @teams = []
- Auth = cbServices.get('Auth')
- getPromise = (resolver) ->
- deferred = $q.defer()
- deferred.resolve resolver
- deferred.promise
- getCurrentGroup = (groupHash) ->
- svc.getById(groupHash).then (team) ->
- group = team
- getCurrentAccount = (accountHash) ->
- AccountService.ensureLoaded (accounts) ->
- account = AccountService.getByHash(accountHash)
- getFakeProfiles = (accountHash) ->
- group.customGET("account/#{accountHash}/user-group-share", {}, {'X-Api-Token': Auth.getToken()})
- setFakeProfiles = (accountHash, fakeProfiles, groupShareHash) ->
- if (groupShareHash == null)
- group.customPOST({ fake_profiles: fakeProfiles }, "account/#{accountHash}/user-group-share", {}, {'X-Api-Token': Auth.getToken()})
- else
- group.customPUT({ fake_profiles: fakeProfiles }, "account/#{accountHash}/user-group-share/#{groupShareHash}", {}, {'X-Api-Token': Auth.getToken()})
- # public
- # import default methods
- # getAll, get, getById, add, update and delete
- svc = ApiService.getInstance(@Key, backendReady)
- svc.ensureLoaded = (cb) =>
- svc.getAll().then (teams) =>
- @teams = teams
- svc.setAll teams, cb
- svc.setAll = (teams, cb) =>
- unless @teams.length
- @teams = teams
- if typeof cb is 'function'
- cb(@teams)
- svc.getGroupsByAccountWithContexts = (account) ->
- # Inject groups in scope with active for user groups
- query = {}
- query['search[primary_account]'] = account.hash
- query['view'] = 'with_contexts'
- svc.getBy(query)
- svc.getFakeProfiles = (groupHash, accountHash) ->
- getCurrentGroup(groupHash).then () ->
- getCurrentAccount(accountHash).then () ->
- getFakeProfiles(accountHash)
- svc.setFakeProfiles = (groupHash, accountHash, fakeProfiles, groupShareHash) ->
- getCurrentGroup(groupHash).then () ->
- getCurrentAccount(accountHash).then () ->
- setFakeProfiles(accountHash, fakeProfiles, groupShareHash)
- svc.create = () ->
- _.cloneDeep
- name: ''
- members: []
- # return the exports
- svc
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement