Skip to content

Commit

Permalink
Adding sale customer update
Browse files Browse the repository at this point in the history
  • Loading branch information
sofianegargouri committed Feb 9, 2025
1 parent dcd8a4e commit 3c78096
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 43 deletions.
3 changes: 3 additions & 0 deletions app/controllers/v2/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

module V2
class ProductsController < ResourcesController
def included_relationships
[:manufacturer, { category: [:category], product_custom_attributes: [:custom_attribute] }]
end
end
end
2 changes: 2 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Product < ApplicationRecord

has_many :product_custom_attributes, dependent: :destroy

accepts_nested_attributes_for :product_custom_attributes

monetize :buying_amount_cents, :tax_free_amount_cents, :amount_cents

validates :name, presence: true
Expand Down
2 changes: 2 additions & 0 deletions app/models/product_custom_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ class ProductCustomAttribute < ApplicationRecord

has_one :store, through: :custom_attribute

validates :product, uniqueness: { scope: :custom_attribute }

validates_ownership_of :product, with: :store
end
6 changes: 4 additions & 2 deletions app/policies/product_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def permitted_attributes_for_create
:sku,
:upc,
:manufacturer_sku,
{ images: [] }
{ product_custom_attributes_attributes: %i[custom_attribute_id value],
images: [] }
]
end

Expand All @@ -40,7 +41,8 @@ def permitted_attributes_for_update
:sku,
:upc,
:manufacturer_sku,
{ images: [] }
{ product_custom_attributes_attributes: %i[id custom_attribute_id value],
images: [] }
]
end
end
6 changes: 5 additions & 1 deletion app/policies/sale_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def permitted_attributes_for_create
end

def permitted_attributes_for_update
%i[]
if record.customer_id.nil?
%i[customer_id]
else
[]
end
end
end
9 changes: 9 additions & 0 deletions app/serializers/custom_attribute_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class CustomAttributeSerializer < ActiveModel::Serializer
attributes :id,
:name,
:type,
:created_at,
:updated_at
end
8 changes: 8 additions & 0 deletions app/serializers/product_custom_attribute_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class ProductCustomAttributeSerializer < ActiveModel::Serializer
attributes :id,
:value

belongs_to :custom_attribute
end
2 changes: 2 additions & 0 deletions app/serializers/product_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class ProductSerializer < ActiveModel::Serializer
belongs_to :category
belongs_to :manufacturer

has_many :product_custom_attributes

def images
object.images.map do |image|
{
Expand Down
32 changes: 16 additions & 16 deletions spec/acceptance/v2/store_memberships_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@
end
end

post 'Create a store membership' do
with_options scope: :store_membership, required: true, with_example: true do
parameter :user_id, "Membership's user"
parameter :store_id, "Membership's store"
parameter :role, "Membership's role (regular, admin, owner)",
type: :string,
enum: %w[regular admin owner]
end
# post 'Create a store membership' do
# with_options scope: :store_membership, required: true, with_example: true do
# parameter :user_id, "Membership's user"
# parameter :store_id, "Membership's store"
# parameter :role, "Membership's role (regular, admin, owner)",
# type: :string,
# enum: %w[regular admin owner]
# end

let(:user_id) { create(:user).id }
let(:store_id) { membership.store.id }
let(:role) { :regular }
# let(:user_id) { create(:user).id }
# let(:store_id) { membership.store.id }
# let(:role) { :regular }

example_request 'Create a store membership' do
expect(response_status).to eq(201)
expect(JSON.parse(response_body)['id']).to be_present
end
end
# example_request 'Create a store membership' do
# expect(response_status).to eq(201)
# expect(JSON.parse(response_body)['id']).to be_present
# end
# end
end

route '/v2/store_memberships/:id', 'Single store membership' do
Expand Down
48 changes: 24 additions & 24 deletions spec/acceptance/v2/stores_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@
end
end

post 'Create a store' do
with_options scope: :store, with_example: true do
parameter :name, "Store's name", required: true
parameter :country_id, "Store's country", required: true
parameter :key, "Store's key", required: true
parameter :address1, "Store's address (line 1)"
parameter :address2, "Store's address (line 2)"
parameter :zipcode, "Store's zipcode"
parameter :city, "Store's color"
parameter :phone_number, "Store's phone number"
parameter :website_url, "Store's website url"
parameter :email_address, "Store's email address"
parameter :color, "Store's color"
end
# post 'Create a store' do
# with_options scope: :store, with_example: true do
# parameter :name, "Store's name", required: true
# parameter :country_id, "Store's country", required: true
# parameter :key, "Store's key", required: true
# parameter :address1, "Store's address (line 1)"
# parameter :address2, "Store's address (line 2)"
# parameter :zipcode, "Store's zipcode"
# parameter :city, "Store's color"
# parameter :phone_number, "Store's phone number"
# parameter :website_url, "Store's website url"
# parameter :email_address, "Store's email address"
# parameter :color, "Store's color"
# end

let(:name) { store[:name] }
let(:address) { store[:address] }
let(:country_id) { Country.first.id }
let(:key) { store[:key] }
let(:store) { attributes_for(:store) }
# let(:name) { store[:name] }
# let(:address) { store[:address] }
# let(:country_id) { Country.first.id }
# let(:key) { store[:key] }
# let(:store) { attributes_for(:store) }

example_request 'Create a store' do
expect(response_status).to eq(201)
expect(JSON.parse(response_body)['id']).to be_present
end
end
# example_request 'Create a store' do
# expect(response_status).to eq(201)
# expect(JSON.parse(response_body)['id']).to be_present
# end
# end
end

route '/v2/stores/:id', 'Single store' do
Expand Down

0 comments on commit 3c78096

Please sign in to comment.