maestrano/mnoe-admin-panel

View on GitHub
src/app/components/mnoe-api/admin/organizations.svc.coffee

Summary

Maintainability
Test Coverage
# Service for managing the users.
@App.service 'MnoeOrganizations', (MnoeAdminApiSvc) ->
  _self = @

  @list = (limit, offset, sort, params = {}) ->
    params["order_by"] = sort
    params["limit"] = limit
    params["offset"] = offset
    promise = MnoeAdminApiSvc.all("organizations").getList(params).then(
      (response) ->
        notifyListObservers(promise)
        response
    )

  observerCallbacks = []

  # Subscribe callback functions to be called if 'list' has been changed
  @registerListChangeCb = (callback) ->
    observerCallbacks.push(callback)

  # Call this when you know 'list' has been changed
  notifyListObservers = (listPromise) ->
    _.forEach observerCallbacks, (callback) ->
      callback(listPromise)

  @search = (terms) ->
    MnoeAdminApiSvc.all("organizations").getList({terms: terms})

  @inArrears = () ->
    MnoeAdminApiSvc.all('organizations').all('in_arrears').getList()

  @get = (id) ->
    MnoeAdminApiSvc.one('organizations', id).get()

  @count = () ->
    MnoeAdminApiSvc.all('organizations').customGET('count')

  @create = (organization) ->
    MnoeAdminApiSvc.all('/organizations').post(organization)

  @update = (organization) ->
    MnoeAdminApiSvc.one('/organizations', organization.id).patch(organization)

  return @