Skip to content

Commit 6f72ace

Browse files
committed
Implement PartnerRequestableItemsService
- if the partner is not in any groups, return all items in organization - if the partner is a member in some groups, returns items tagged by those groups
1 parent cb18458 commit 6f72ace

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

app/controllers/api/v1/partner_requests_controller.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ def create
2121
def show
2222
return head :forbidden unless api_key_valid?
2323

24-
organization = Organization.find(params[:id])
25-
# replace this with items from partner group
26-
render json: organization.valid_items, status: :ok
24+
items = PartnerRequestableItemsService.new(organization_id: params[:id], partner_id: params[:partner_id]).call
25+
render json: items, status: :ok
2726
rescue ActiveRecord::RecordNotFound => e
2827
render json: { error: e.message }, status: :bad_request
2928
end

app/models/partner.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class Partner < ApplicationRecord
3030

3131
has_many :requests, dependent: :destroy
3232

33+
has_many :partner_group_memberships, dependent: :destroy
34+
has_many :partner_groups, through: :partner_group_memberships
35+
has_many :requestable_items, through: :partner_groups, source: :items
36+
3337
has_many_attached :documents
3438

3539
validates :organization, presence: true
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class PartnerRequestableItemsService
2+
def initialize(organization_id:, partner_id:)
3+
@organization_id = organization_id
4+
@partner_id = partner_id
5+
end
6+
7+
def call
8+
return organization.valid_items if partner.partner_groups.empty?
9+
10+
partner.requestable_items.active.visible.map do |item|
11+
{
12+
id: item.id,
13+
partner_key: item.partner_key,
14+
name: item.name
15+
}
16+
end
17+
end
18+
19+
private
20+
21+
attr_reader :organization_id, :partner_id
22+
23+
def organization
24+
@organization ||= Organization.find(organization_id)
25+
end
26+
27+
def partner
28+
@partner ||= Partner.find(partner_id)
29+
end
30+
end

0 commit comments

Comments
 (0)