Fixed issue #903 - member_ids missing in organization list API endpoint.

This commit is contained in:
Martin Edenhofer 2017-04-10 13:32:16 +02:00
parent b00dc74e8e
commit c662df0d11
5 changed files with 22 additions and 25 deletions

View file

@ -91,8 +91,11 @@ curl http://localhost/api/v1/organizations -v -u #{login}:#{password}
}, status: :ok }, status: :ok
return return
end end
list = []
render json: organizations organizations.each { |organization|
list.push organization.attributes_with_association_ids
}
render json: list
end end
=begin =begin
@ -293,7 +296,7 @@ curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Co
list = [] list = []
organization_all.each { |organization| organization_all.each { |organization|
list.push organization.attributes list.push organization.attributes_with_association_ids
} }
render json: list, status: :ok render json: list, status: :ok
end end

View file

@ -428,7 +428,7 @@ class UsersController < ApplicationController
list = [] list = []
user_all.each { |user| user_all.each { |user|
list.push user.attributes list.push user.attributes_with_association_ids
} }
render json: list, status: :ok render json: list, status: :ok
end end

View file

@ -15,7 +15,6 @@ class Organization < ApplicationModel
load 'organization/search_index.rb' load 'organization/search_index.rb'
include Organization::SearchIndex include Organization::SearchIndex
has_and_belongs_to_many :users
has_many :members, class_name: 'User' has_many :members, class_name: 'User'
validates :name, presence: true validates :name, presence: true
@ -34,11 +33,4 @@ class Organization < ApplicationModel
domain.downcase! domain.downcase!
end end
def cache_delete
super
# delete asset caches
key = "Organization::member_ids::#{id}"
Cache.delete(key)
end
end end

View file

@ -33,22 +33,14 @@ returns
data[ app_model_user ] = {} data[ app_model_user ] = {}
end end
if !data[ app_model_organization ][ id ] if !data[ app_model_organization ][ id ]
local_attributes = attributes local_attributes = attributes_with_association_ids
# set temp. current attributes to assets pool to prevent # set temp. current attributes to assets pool to prevent
# loops, will be updated with lookup attributes later # loops, will be updated with lookup attributes later
data[ app_model_organization ][ id ] = local_attributes data[ app_model_organization ][ id ] = local_attributes
# get organizations if local_attributes['member_ids']
key = "Organization::member_ids::#{id}" local_attributes['member_ids'].each { |local_user_id|
local_member_ids = Cache.get(key)
if !local_member_ids
local_member_ids = member_ids
Cache.write(key, local_member_ids)
end
local_attributes['member_ids'] = local_member_ids
if local_member_ids
local_member_ids.each { |local_user_id|
next if data[ app_model_user ][ local_user_id ] next if data[ app_model_user ][ local_user_id ]
user = User.lookup(id: local_user_id) user = User.lookup(id: local_user_id)
next if !user next if !user

View file

@ -419,7 +419,7 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest
assert_equal(result_user1['id'], result[0]['id']) assert_equal(result_user1['id'], result[0]['id'])
assert_equal("Customer#{firstname}", result[0]['firstname']) assert_equal("Customer#{firstname}", result[0]['firstname'])
assert_equal('Customer Last', result[0]['lastname']) assert_equal('Customer Last', result[0]['lastname'])
assert_not(result[0]['role_ids']) assert(result[0]['role_ids'])
assert_not(result[0]['roles']) assert_not(result[0]['roles'])
get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&expand=true", {}, @headers.merge('Authorization' => credentials) get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&expand=true", {}, @headers.merge('Authorization' => credentials)
@ -538,6 +538,7 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest
assert_response(200) assert_response(200)
result = JSON.parse(@response.body) result = JSON.parse(@response.body)
assert_equal(result.class, Array) assert_equal(result.class, Array)
assert_equal(result[0]['member_ids'].class, Array)
assert(result.length >= 3) assert(result.length >= 3)
get '/api/v1/organizations?limit=40&page=1&per_page=2', {}, @headers.merge('Authorization' => credentials) get '/api/v1/organizations?limit=40&page=1&per_page=2', {}, @headers.merge('Authorization' => credentials)
@ -546,7 +547,9 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest
assert_equal(Array, result.class) assert_equal(Array, result.class)
organizations = Organization.order(:id).limit(2) organizations = Organization.order(:id).limit(2)
assert_equal(organizations[0].id, result[0]['id']) assert_equal(organizations[0].id, result[0]['id'])
assert_equal(organizations[0].member_ids, result[0]['member_ids'])
assert_equal(organizations[1].id, result[1]['id']) assert_equal(organizations[1].id, result[1]['id'])
assert_equal(organizations[1].member_ids, result[1]['member_ids'])
assert_equal(2, result.count) assert_equal(2, result.count)
get '/api/v1/organizations?limit=40&page=2&per_page=2', {}, @headers.merge('Authorization' => credentials) get '/api/v1/organizations?limit=40&page=2&per_page=2', {}, @headers.merge('Authorization' => credentials)
@ -555,7 +558,10 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest
assert_equal(Array, result.class) assert_equal(Array, result.class)
organizations = Organization.order(:id).limit(4) organizations = Organization.order(:id).limit(4)
assert_equal(organizations[2].id, result[0]['id']) assert_equal(organizations[2].id, result[0]['id'])
assert_equal(organizations[2].member_ids, result[0]['member_ids'])
assert_equal(organizations[3].id, result[1]['id']) assert_equal(organizations[3].id, result[1]['id'])
assert_equal(organizations[3].member_ids, result[1]['member_ids'])
assert_equal(2, result.count) assert_equal(2, result.count)
# show/:id # show/:id
@ -563,12 +569,16 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest
assert_response(200) assert_response(200)
result = JSON.parse(@response.body) result = JSON.parse(@response.body)
assert_equal(result.class, Hash) assert_equal(result.class, Hash)
assert_equal(result['member_ids'].class, Array)
assert_not(result['members'])
assert_equal(result['name'], 'Rest Org') assert_equal(result['name'], 'Rest Org')
get "/api/v1/organizations/#{@organization2.id}", {}, @headers.merge('Authorization' => credentials) get "/api/v1/organizations/#{@organization2.id}", {}, @headers.merge('Authorization' => credentials)
assert_response(200) assert_response(200)
result = JSON.parse(@response.body) result = JSON.parse(@response.body)
assert_equal(result.class, Hash) assert_equal(result.class, Hash)
assert_equal(result['member_ids'].class, Array)
assert_not(result['members'])
assert_equal(result['name'], 'Rest Org #2') assert_equal(result['name'], 'Rest Org #2')
# search as agent # search as agent
@ -578,7 +588,7 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body) result = JSON.parse(@response.body)
assert_equal(Array, result.class) assert_equal(Array, result.class)
assert_equal('Zammad Foundation', result[0]['name']) assert_equal('Zammad Foundation', result[0]['name'])
assert_not(result[0]['member_ids']) assert(result[0]['member_ids'])
assert_not(result[0]['members']) assert_not(result[0]['members'])
get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&expand=true", {}, @headers.merge('Authorization' => credentials) get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&expand=true", {}, @headers.merge('Authorization' => credentials)