Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Updated LineItemSerializer and added CreditCardSerializer for account section

Damian Legawiec0c4a6324ea22
diff --git a/api/app/serializers/spree/api/v3/credit_card_serializer.rb b/api/app/serializers/spree/api/v3/credit_card_serializer.rb
new file mode 100644
index 0000000000..566d92247d
--- /dev/null
+++ b/api/app/serializers/spree/api/v3/credit_card_serializer.rb
@@ -0,0 +1,12 @@
+module Spree
+ module Api
+ module V3
+ class CreditCardSerializer < BaseSerializer
+ typelize cc_type: :string, last_digits: :string, month: :number, year: :number,
+ name: 'string | null', default: :boolean
+
+ attributes :cc_type, :last_digits, :month, :year, :name, :default
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v3/line_item_serializer.rb b/api/app/serializers/spree/api/v3/line_item_serializer.rb
index 09b7d04e35..320c4d1202 100644
--- a/api/app/serializers/spree/api/v3/line_item_serializer.rb
+++ b/api/app/serializers/spree/api/v3/line_item_serializer.rb
@@ -10,7 +10,8 @@ module Spree
promo_total: :string, display_promo_total: :string,
pre_tax_amount: :string, display_pre_tax_amount: :string,
discounted_amount: :string, display_discounted_amount: :string,
- compare_at_amount: 'string | null', display_compare_at_amount: 'string | null'
+ compare_at_amount: 'string | null', display_compare_at_amount: 'string | null',
+ thumbnail_url: 'string | null'
attribute :variant_id do |line_item|
line_item.variant&.prefix_id
@@ -33,7 +34,11 @@ module Spree
amount.present? && amount.positive? ? amount.to_s : nil
end
- many :images, resource: Spree.api.image_serializer
+ # Thumbnail URL for line item (variant thumbnail or product thumbnail)
+ attribute :thumbnail_url do |line_item|
+ image_url_for(line_item.thumbnail)
+ end
+
many :option_values, resource: Spree.api.option_value_serializer
end
end
diff --git a/api/spec/serializers/spree/api/v3/line_item_serializer_spec.rb b/api/spec/serializers/spree/api/v3/line_item_serializer_spec.rb
new file mode 100644
index 0000000000..7bbc33f128
--- /dev/null
+++ b/api/spec/serializers/spree/api/v3/line_item_serializer_spec.rb
@@ -0,0 +1,109 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::LineItemSerializer do
+ let(:store) { @default_store }
+ let(:product) { create(:product, stores: [store]) }
+ let(:variant) { create(:variant, product: product) }
+ let(:order) { create(:order, store: store) }
+ let(:line_item) { create(:line_item, order: order, variant: variant, quantity: 2) }
+ let(:base_params) { { store: store, currency: 'USD' } }
+
+ subject { described_class.new(line_item, params: base_params).to_h }
+
+ describe 'serialized attributes' do
+ it 'includes standard attributes' do
+ expect(subject).to include(
+ 'id' => line_item.prefix_id,
+ 'variant_id' => variant.prefix_id,
+ 'quantity' => 2,
+ 'name' => product.name,
+ 'slug' => product.slug
+ )
+ end
+
+ it 'includes price attributes' do
+ expect(subject).to have_key('price')
+ expect(subject).to have_key('display_price')
+ expect(subject).to have_key('total')
+ expect(subject).to have_key('display_total')
+ end
+
+ it 'includes adjustment attributes' do
+ expect(subject).to have_key('adjustment_total')
+ expect(subject).to have_key('display_adjustment_total')
+ expect(subject).to have_key('promo_total')
+ expect(subject).to have_key('display_promo_total')
+ end
+
+ it 'includes tax attributes' do
+ expect(subject).to have_key('additional_tax_total')
+ expect(subject).to have_key('display_additional_tax_total')
+ expect(subject).to have_key('included_tax_total')
+ expect(subject).to have_key('display_included_tax_total')
+ end
+
+ it 'includes timestamp attributes' do
+ expect(subject).to have_key('created_at')
+ expect(subject).to have_key('updated_at')
+ end
+
+ it 'includes option_values array' do
+ expect(subject['option_values']).to be_an(Array)
+ end
+ end
+
+ describe 'thumbnail_url' do
+ context 'when variant has an image' do
+ let(:image) { create(:image) }
+ let(:variant) { create(:variant, product: product, images: [image]) }
+
+ before do
+ variant.update_thumbnail!
+ end
+
+ it 'returns the variant thumbnail URL' do
+ expect(subject['thumbnail_url']).to be_present
+ expect(subject['thumbnail_url']).to include(image.attachment.filename.to_s)
+ end
+ end
+
+ context 'when variant has no image but product has an image' do
+ let(:image) { create(:image) }
+ let(:variant) { product.master }
+
+ before do
+ product.images << image
+ product.update_thumbnail!
+ end
+
+ it 'returns the product thumbnail URL' do
+ expect(subject['thumbnail_url']).to be_present
+ expect(subject['thumbnail_url']).to include(image.attachment.filename.to_s)
+ end
+ end
+
+ context 'when neither variant nor product has an image' do
+ it 'returns nil' do
+ expect(subject['thumbnail_url']).to be_nil
+ end
+ end
+ end
+
+ describe 'compare_at_amount' do
+ context 'when variant has no compare_at_price' do
+ it 'returns nil' do
+ expect(subject['compare_at_amount']).to be_nil
+ end
+ end
+
+ context 'when variant has compare_at_price' do
+ before do
+ variant.prices.first.update!(compare_at_amount: 29.99)
+ end
+
+ it 'returns the compare_at_amount as string' do
+ expect(subject['compare_at_amount']).to be_present
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rb
index b2844aedbb..7aca9e3259 100644
--- a/core/app/models/spree/line_item.rb
+++ b/core/app/models/spree/line_item.rb
@@ -49,8 +49,15 @@ module Spree
after_create :update_tax_charge
- delegate :sku, :should_track_inventory?, :product, :options_text, :slug, :product_id, :dimensions_unit, :weight_unit, :images, :option_values, to: :variant
+ delegate :sku, :should_track_inventory?, :product, :options_text, :slug, :product_id, :dimensions_unit, :weight_unit, :option_values, to: :variant
delegate :name, :description, :brand, :category, to: :product
+
+ # Returns the thumbnail image for this line item
+ # Prefers variant thumbnail, falls back to product thumbnail
+ # @return [Spree::Image, nil]
+ def thumbnail
+ variant.thumbnail || product.thumbnail
+ end
delegate :tax_zone, to: :order
delegate :digital?, :can_supply?, to: :variant
diff --git a/sdk/src/client.ts b/sdk/src/client.ts
index b5566a6510..edb5f6a379 100644
--- a/sdk/src/client.ts
+++ b/sdk/src/client.ts
@@ -13,6 +13,8 @@ import type {
AddLineItemParams,
UpdateLineItemParams,
UpdateOrderParams,
+ AddressParams,
+ StoreCreditCard,
StoreProduct,
StoreOrder,
StoreLineItem,
@@ -28,6 +30,9 @@ import type {
StoreUser,
} from './types';
+// Re-export types for convenience
+export type { AddressParams, StoreCreditCard };
+
export interface SpreeClientConfig {
/** Base URL of the Spree API (e.g., 'https://api.mystore.com') */
baseUrl: string;
@@ -579,7 +584,7 @@ export class SpreeClient {
* Create an address
*/
create: (
- address: Omit<StoreAddress, 'id'>,
+ address: AddressParams,
options?: RequestOptions
): Promise<StoreAddress> =>
this.request<StoreAddress>('POST', '/customer/addresses', {
@@ -592,7 +597,7 @@ export class SpreeClient {
*/
update: (
id: string,
- address: Partial<Omit<StoreAddress, 'id'>>,
+ address: Partial<AddressParams>,
options?: RequestOptions
): Promise<StoreAddress> =>
this.request<StoreAddress>('PATCH', `/customer/addresses/${id}`, {
@@ -606,6 +611,36 @@ export class SpreeClient {
delete: (id: string, options?: RequestOptions): Promise<void> =>
this.request<void>('DELETE', `/customer/addresses/${id}`, options),
},
+
+ /**
+ * Nested resource: Credit Cards
+ */
+ creditCards: {
+ /**
+ * List customer credit cards
+ */
+ list: (
+ params?: ListParams,
+ options?: RequestOptions
+ ): Promise<PaginatedResponse<StoreCreditCard>> =>
+ this.request<PaginatedResponse<StoreCreditCard>>(
+ 'GET',
+ '/customer/credit_cards',
+ { ...options, params: params as Record<string, string | number | undefined> }
+ ),
+
+ /**
+ * Get a credit card by ID
+ */
+ get: (id: string, options?: RequestOptions): Promise<StoreCreditCard> =>
+ this.request<StoreCreditCard>('GET', `/customer/credit_cards/${id}`, options),
+
+ /**
+ * Delete a credit card
+ */
+ delete: (id: string, options?: RequestOptions): Promise<void> =>
+ this.request<void>('DELETE', `/customer/credit_cards/${id}`, options),
+ },
};
// ============================================
diff --git a/sdk/src/types/generated/StoreCreditCard.ts b/sdk/src/types/generated/StoreCreditCard.ts
new file mode 100644
index 0000000000..12d9c4853e
--- /dev/null
+++ b/sdk/src/types/generated/StoreCreditCard.ts
@@ -0,0 +1,15 @@
+// Typelizer digest af446e7ffc65a184154c23090150a4a0
+//
+// DO NOT MODIFY: This file was automatically generated by Typelizer.
+
+type StoreCreditCard = {
+ id: string;
+ cc_type: string;
+ last_digits: string;
+ month: number;
+ year: number;
+ name: string | null;
+ default: boolean;
+}
+
+export default StoreCreditCard;
diff --git a/sdk/src/types/generated/StoreLineItem.ts b/sdk/src/types/generated/StoreLineItem.ts
index 0712fc8708..61f93263fe 100644
--- a/sdk/src/types/generated/StoreLineItem.ts
+++ b/sdk/src/types/generated/StoreLineItem.ts
@@ -1,7 +1,7 @@
-// Typelizer digest 163ec6334fcadbf4bd409c5bb099ea39
+// Typelizer digest 2ed123356d0b083b118b251b6e967d3b
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
-import type {StoreImage, StoreOptionValue} from '@/types'
+import type {StoreOptionValue} from '@/types'
type StoreLineItem = {
id: string;
@@ -30,7 +30,7 @@ type StoreLineItem = {
created_at: string;
updated_at: string;
compare_at_amount: string | null;
- images: Array<StoreImage>;
+ thumbnail_url: string | null;
option_values: Array<StoreOptionValue>;
}
diff --git a/sdk/src/types/generated/index.ts b/sdk/src/types/generated/index.ts
index 3e76168f5e..b33cb27e1a 100644
--- a/sdk/src/types/generated/index.ts
+++ b/sdk/src/types/generated/index.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 59d305d08b75993b80175db851f6906a
+// Typelizer digest ca58ea9c80739b0f8d98caf2b78e9064
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
export type { default as StoreAddress } from './StoreAddress'
@@ -12,6 +12,7 @@ export type { default as AdminUser } from './AdminUser'
export type { default as AdminVariant } from './AdminVariant'
export type { default as StoreBase } from './StoreBase'
export type { default as StoreCountry } from './StoreCountry'
+export type { default as StoreCreditCard } from './StoreCreditCard'
export type { default as StoreDigitalLink } from './StoreDigitalLink'
export type { default as StoreImage } from './StoreImage'
export type { default as StoreLineItem } from './StoreLineItem'