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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Add ability to check if a variant is already in a Wishlist.
Matthew Kennedy79c18d42a863
diff --git a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rbindex d017dace97..f1b04db322 100644--- a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb+++ b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb@@ -146,6 +146,10 @@ module Spree).serializable_hashend+ def serializer_params+ super.merge(is_variant_included: params[:is_variant_included])+ end+def render_error_item_quantityrender json: { error: I18n.t('spree.api.v2.wishlist.wrong_quantity') }, status: 422enddiff --git a/api/app/serializers/spree/v2/storefront/wishlist_serializer.rb b/api/app/serializers/spree/v2/storefront/wishlist_serializer.rbindex 80aff4970f..6050f5f0c2 100644--- a/api/app/serializers/spree/v2/storefront/wishlist_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/wishlist_serializer.rb@@ -6,6 +6,10 @@ module Spreeattributes :token, :name, :is_private, :is_default+ attribute :variant_included do |wishlist, params|+ wishlist.include?(params[:is_variant_included])+ end+has_many :wished_itemsendenddiff --git a/api/docs/v2/storefront/index.yaml b/api/docs/v2/storefront/index.yamlindex 587c6d6c8d..1f76ec7318 100644--- a/api/docs/v2/storefront/index.yaml+++ b/api/docs/v2/storefront/index.yaml@@ -233,7 +233,7 @@ paths:- bearerAuth: []'/api/v2/storefront/account/addresses/{id}':delete:- description: 'Removes selected Address for the signed in User. It uses soft-delete for addresses with shipments and orders.'+ description: Removes selected Address for the signed in User. It uses soft-delete for addresses with shipments and orders.tags:- AccountoperationId: remove-address@@ -298,13 +298,13 @@ paths:name: 'filter[payment_method_id]'schema:type: string- example: "2"+ example: '2'description: Filter based on payment method ID- $ref: '#/components/parameters/CreditCardIncludeParam'- $ref: '#/components/parameters/SparseFieldsParam''/api/v2/storefront/account/credit_cards/{id}':delete:- description: 'Removes selected credit card for the signed in User. It uses soft-delete.'+ description: Removes selected credit card for the signed in User. It uses soft-delete.tags:- AccountoperationId: remove-credit-card@@ -480,44 +480,42 @@ paths:shipping_address:data: nullincluded:- - id: "2"- type: "promotion"- attributes:- name: "10% Discount"- description: "Super discount for you"- amount: "-22.0"- display_amount: "$22.00"- - id: "4"- type: "user"- attributes:- email: "[email protected]"- - id: "1"- type: "address"- attributes:- firstname: "Bob"- lastname: "Billerson"- address1: "1600 Clear View Pkwy"- address2: "Mountain View"- city: "LA"+ - id: '2'+ type: promotion+ attributes:+ name: 10% Discount+ description: Super discount for you+ amount: '-22.0'+ display_amount: $22.00+ - id: '4'+ type: user+ attributes:+ email: [email protected]+ - id: '1'+ type: address+ attributes:+ firstname: Bob+ lastname: Billerson+ address1: 1600 Clear View Pkwy+ address2: Mountain View+ city: LAzipcode: '94043'- phone: "(+1) 123 456 789"- state_name: "California"- state_code: "CA"- country_name: "United States of America"- country_iso3: "USA"+ phone: (+1) 123 456 789+ state_name: California+ state_code: CA+ country_name: United States of America+ country_iso3: USAcompany: null- - id: "2832"- type: "shipment"+ - id: '2832'+ type: shipmentattributes:- number: "H121354"+ number: H121354free: falsefinal_price: '10.0'- display_final_price: '$10.00'+ display_final_price: $10.00tracking_url: 'https://tools.usps.com/go/TrackConfirmAction?tRef=fullpage&tLc=2&text28777=&tLabels=4123412434%2C'- state: "shipped"+ state: shippedshipped_at: '2019-01-02 13:42:12'--parameters:- $ref: '#/components/parameters/CartIncludeParam'- $ref: '#/components/parameters/SparseFieldsParam'@@ -536,7 +534,7 @@ paths:'404':$ref: '#/components/responses/404NotFound'security:- - bearerAuth: [ ]+ - bearerAuth: []summary: Deletes Cartget:description: Returns contents of the currently used Cart@@ -1008,7 +1006,7 @@ paths:properties:payment_method_id:type: string- example: "1"+ example: '1'description: ID of selected payment methodshipments_attributes:type: object@@ -1100,7 +1098,7 @@ paths:summary: Complete Checkout/api/v2/storefront/checkout/add_store_credit:post:- description: 'Adds Store Credit payment if the currently signed in User has any available.'+ description: Adds Store Credit payment if the currently signed in User has any available.tags:- CheckoutoperationId: add-store-credit@@ -1128,7 +1126,7 @@ paths:description: Amount of Store Credit to use. As much as possible Store Credit will be applied if no amount is passed.schema:type: string- example: "100"+ example: '100'- $ref: '#/components/parameters/CartIncludeParam'- $ref: '#/components/parameters/SparseFieldsParam'summary: Add Store Credit@@ -1236,7 +1234,7 @@ paths:schema:type: stringexample: alpha- description: 'Find Products with Property Brand Name with value Alpha'+ description: Find Products with Property Brand Name with value Alpha- in: queryname: 'filter[show_deleted]'schema:@@ -3697,7 +3695,7 @@ paths:name: 'filter[parent_permalink]'schema:type: string- example: 'clothes'+ example: clothesdescription: Fetch children nodes of specified Taxon permalink- in: queryname: 'filter[taxonomy_id]'@@ -13427,8 +13425,8 @@ paths:summary: Get Menu Details/api/v2/storefront/wishlists:get:- description: Returns a list of wishlists for the current user in the current store.- summary: List all wishlists+ description: 'Returns all wishlists available to the current user, in the current store.'+ summary: List all Wishliststags:- WishlistsoperationId: list-wishlists-for-current-user@@ -13436,9 +13434,13 @@ paths:- $ref: '#/components/parameters/WishlistIncludeParam'- $ref: '#/components/parameters/WishlistSparseFieldsParam'- $ref: '#/components/parameters/PerPageParam'+ - $ref: '#/components/parameters/WishlistIsVariantIncludedParam'responses:'200':- description: Returns a hash with a data property that contains an array of wishlists. Each entry in the array is a separate wishlist object.+ description: |-+ A successful response returns a `data` array containing each **wishlist** object.++ If you requested specific fields to be returned through the **fields** query parameter, your response body might differ from the one shown in the schema below.content:application/vnd.api+json:schema:@@ -13447,44 +13449,131 @@ paths:Standard response:value:data:- - id: '21'+ - id: '4'type: wishlistattributes:- token: snK3N5nCx2GV3fWzRV4rH9VE- name: Black Friday- is_private: true+ token: gD1QP6SybtooojnxtkXPvcvh+ name: My Wishlist+ is_private: falseis_default: false+ variant_included: truerelationships:wished_items:- data: []- - id: '22'+ data:+ - id: '4'+ type: wished_item+ - id: '5'+ type: wished_item+ - id: '8'type: wishlistattributes:- token: TG5gyZjUbg4L1RY75ekJ3qmM+ token: LKWCxupq2gsN9DRhwuNBCrf5name: Fathers Dayis_private: true+ is_default: true+ variant_included: false+ relationships:+ wished_items:+ data:+ - id: '6'+ type: wished_item+ - id: '7'+ type: wishlist+ attributes:+ token: QHnjcCvz21Dtzc6isWyiyBxM+ name: Black Friday+ is_private: falseis_default: false+ variant_included: falserelationships:wished_items:data: []- - id: '23'+ meta:+ count: 3+ total_count: 3+ total_pages: 1+ links:+ self: 'http://localhost:3000/api/v2/storefront/wishlists?is_variant_included=150'+ next: 'http://localhost:3000/api/v2/storefront/wishlists?page=1'+ prev: 'http://localhost:3000/api/v2/storefront/wishlists?page=1'+ last: 'http://localhost:3000/api/v2/storefront/wishlists?page=1'+ first: 'http://localhost:3000/api/v2/storefront/wishlists?page=1'+ is_variant_included=150:+ value:+ data:+ - id: '4'type: wishlistattributes:- token: LhCmvs8XryASJz7SmWQ6vvJW+ token: gD1QP6SybtooojnxtkXPvcvhname: My Wishlistis_private: false+ is_default: false+ variant_included: true+ relationships:+ wished_items:+ data:+ - id: '4'+ type: wished_item+ - id: '5'+ type: wished_item+ - id: '8'+ type: wishlist+ attributes:+ token: LKWCxupq2gsN9DRhwuNBCrf5+ name: Fathers Day+ is_private: trueis_default: true+ variant_included: truerelationships:wished_items:data:- - id: '11'- type: wished_variant+ - id: '6'+ type: wished_item+ - id: '7'+ type: wishlist+ attributes:+ token: QHnjcCvz21Dtzc6isWyiyBxM+ name: Black Friday+ is_private: false+ is_default: false+ variant_included: false+ relationships:+ wished_items:+ data: []meta:count: 3total_count: 3total_pages: 1links:- self: 'http://localhost:3000/api/v2/storefront/wishlists'+ self: 'http://localhost:3000/api/v2/storefront/wishlists?is_variant_included=150'+ next: 'http://localhost:3000/api/v2/storefront/wishlists?page=1'+ prev: 'http://localhost:3000/api/v2/storefront/wishlists?page=1'+ last: 'http://localhost:3000/api/v2/storefront/wishlists?page=1'+ first: 'http://localhost:3000/api/v2/storefront/wishlists?page=1'+ 'fields[wishlist]=name':+ value:+ data:+ - id: '4'+ type: wishlist+ attributes:+ name: My Wishlist+ relationships: {}+ - id: '8'+ type: wishlist+ attributes:+ name: Fathers Day+ relationships: {}+ - id: '7'+ type: wishlist+ attributes:+ name: Black Friday+ relationships: {}+ meta:+ count: 3+ total_count: 3+ total_pages: 1+ links:+ self: 'http://localhost:3000/api/v2/storefront/wishlists?fields[wishlist]=name'next: 'http://localhost:3000/api/v2/storefront/wishlists?page=1'prev: 'http://localhost:3000/api/v2/storefront/wishlists?page=1'last: 'http://localhost:3000/api/v2/storefront/wishlists?page=1'@@ -13494,14 +13583,17 @@ paths:security:- bearerAuth: []post:- description: Creates a new wishlist for current user in the current store.- summary: Create a wishlist+ description: Creates a new wishlist for the current user in the current store.+ summary: Create a Wishlisttags:- WishlistsoperationId: create-wishlistresponses:'200':- description: Returns the new wishlist object.+ description: |-+ A successful response returns a `data` object containing the **wishlist**.++ If you requested specific fields to be returned through the **fields** query parameter, your response body might differ from the one shown in the schema below.content:application/vnd.api+json:schema:@@ -13517,6 +13609,7 @@ paths:name: My Wishlistis_private: falseis_default: true+ variant_included: falserelationships:wished_items:data: []@@ -13532,10 +13625,10 @@ paths:properties:name:type: string- description: The wishlist name.+ description: The Wishlist name.is_private:type: boolean- description: Whether the wishlist is public or private. Defaults to true (private).+ description: Set the Wishlist to public or private. Defaults to true (private).is_default:type: booleandescription: Whether the wishlist is set to the users default wishlist for the current store. Defaults to false.@@ -13549,18 +13642,22 @@ paths:is_default: true'/api/v2/storefront/wishlists/{token}':get:- description: 'Retrieves the details of an existing wishlist. Supply the unique wishlist token, and we will return the corresponding wishlist information.'- summary: Retrieve a wishlist+ description: 'Retrieves the details of an existing wishlist. Supply the unique wishlist token, and the API will return the corresponding wishlist information.'+ summary: Retrieve a Wishlisttags:- Wishlists- operationId: get-wishlist-details-for-current-user+ operationId: get-wishlist-detailsparameters:- $ref: '#/components/parameters/Token'- $ref: '#/components/parameters/WishlistIncludeParam'- $ref: '#/components/parameters/WishlistSparseFieldsParam'+ - $ref: '#/components/parameters/WishlistIsVariantIncludedParam'responses:'200':- description: Returns the requested wishlist object.+ description: |-+ A successful response returns a `data` object containing the **wishlist**.++ If you requested specific fields to be returned through the **fields** query parameter, your response body might differ from the one shown in the schema below.content:application/vnd.api+json:schema:@@ -13576,6 +13673,7 @@ paths:name: My Wishlistis_private: falseis_default: true+ variant_included: falserelationships:wished_items:data:@@ -13591,6 +13689,7 @@ paths:name: My Wishlistis_private: falseis_default: true+ variant_included: falserelationships:wished_items:data:@@ -13690,8 +13789,8 @@ paths:'403':$ref: '#/components/responses/403Forbidden'put:- description: Updates the specific wishlist by setting the values of the parameters passed. Any parameters not provided will be left unchanged.- summary: Update a wishlist+ description: Updates the specific Wishlist by setting the values passed in the request body. Any parameters not provided will be left unchanged.+ summary: Update a Wishlisttags:- WishlistsoperationId: update-wishlist@@ -13732,7 +13831,10 @@ paths:- $ref: '#/components/parameters/Token'responses:'200':- description: Returns the updated wishlist object.+ description: |-+ A successful response returns a `data` object containing the **wishlist**.++ If you requested specific fields to be returned through the **fields** query parameter, your response body might differ from the one shown in the schema below.content:application/vnd.api+json:schema:@@ -13758,8 +13860,8 @@ paths:security:- bearerAuth: []delete:- description: Delete a wishlist.- summary: Delete a wishlist+ description: This operation deletes the wishlist identified in the URI `token`.+ summary: Delete a Wishlisttags:- WishlistsoperationId: delete-wishlist@@ -13767,7 +13869,10 @@ paths:- $ref: '#/components/parameters/Token'responses:'200':- description: Returns the deleted wishlist object.+ description: |-+ A successful response returns a `data` object containing the **wishlist**.++ If you requested specific fields to be returned through the **fields** query parameter, your response body might differ from the one shown in the schema below.content:application/vnd.api+json:schema:@@ -13783,6 +13888,7 @@ paths:name: My Wishlistis_private: falseis_default: true+ variant_included: falserelationships:wished_items:data: []@@ -13792,20 +13898,21 @@ paths:- bearerAuth: []/api/v2/storefront/wishlists/default:get:- description: |-- Returns the default wishlist for the current user, in the current store.-- If the user does not have a default wishlist in the current store one will be created on request.- summary: Retrieve the default wishlist+ description: 'Returns the default wishlist for the current user, in the current store. If the user does not have a default wishlist in the current store one will be created.'+ summary: Retrieve the Default Wishlisttags:- WishlistsoperationId: get-default-wishlistparameters:- $ref: '#/components/parameters/WishlistIncludeParam'- $ref: '#/components/parameters/WishlistSparseFieldsParam'+ - $ref: '#/components/parameters/WishlistIsVariantIncludedParam'responses:'200':- description: Returns the default wishlist object.+ description: |-+ A successful response returns a `data` object containing the **wishlist**.++ If you requested specific fields to be returned through the **fields** query parameter, your response body might differ from the one shown in the schema below.content:application/vnd.api+json:schema:@@ -13821,6 +13928,7 @@ paths:name: My Wishlistis_private: falseis_default: true+ variant_included: falserelationships:wished_items:data: []@@ -13830,11 +13938,11 @@ paths:- bearerAuth: []'/api/v2/storefront/wishlists/{token}/add_item':post:- summary: Add an item to a wishlist+ summary: Add Item to Wishlistdescription: |-- The add item endpoint adds a variant to an existing wishlist by creating a new wished item. A wished item in a wishlist is comparable to a line item in a cart.+ The Add Item to Wishlist endpoint adds a variant to an existing wishlist by creating a new `wished_item`. A wished item in a wishlist is comparable to a line item in a cart.- Each wished item references a single variant, holds the desired quantity and returns the variant price and total for the variant in the desired quantity.+ Each wished item references a single variant, holds the desired quantity and returns price information determined by the variant price in current currency, and desired quantity.tags:- WishlistsoperationId: add-item-to-wishlist@@ -13861,12 +13969,12 @@ paths:variant_id: '149'quantity: 3description: |-- Passing a quantity value is optional for those wishlist systems that do not support quantities. In this scenario the retuned quantity value will always be 1, allowing the add-to-cart and pricing to accuratly reflect the single representation of the wished item.+ Passing a quantity value is optional for those wishlist systems that do not support quantities. In this scenario, the retuned quantity value will always be `1`, allowing the add-to-cart and pricing to reflect the single representation of the wished item accurately.- If the wishlist system you are building allows for setting quantity, add the quantity attribute to the request body, and ensure that it is an integer value greater than zero.+ If the wishlist system you are building allows for setting quantity, add the quantity attribute to the request body with an integer value greater than zero.responses:'200':- description: Returns the new wished item object.+ description: A successful response returns a `data` object containing the **wished_item**.content:application/vnd.api+json:schema:@@ -13904,8 +14012,8 @@ paths:required: true'/api/v2/storefront/wishlists/{token}/set_item_quantity/{item_id}':patch:- description: Sets the wished item quantity.- summary: Set item quantity+ description: This endpoint sets the wished item quantity.+ summary: Set Item Quantitytags:- WishlistsoperationId: update-wished-variant-quantity@@ -13931,7 +14039,7 @@ paths:description: ''responses:'200':- description: Returns the updated wished item object.+ description: A successful response returns a `data` object containing the **wished_item**.content:application/vnd.api+json:schema:@@ -13963,8 +14071,8 @@ paths:- bearerAuth: []'/api/v2/storefront/wishlists/{token}/remove_item/{item_id}':delete:- description: Removes the wished item from the wishlist.- summary: Delete item from wishlist+ description: Removes a wished item from a wishlist.+ summary: Delete Item from Wishlisttags:- WishlistsoperationId: delete-wished-variant-from-wishlist@@ -13973,11 +14081,11 @@ paths:- $ref: '#/components/parameters/ItemId'responses:'200':- description: Returns the deleted wished item object.+ description: A successful response returns a `data` object containing the **wished_item**.content:application/vnd.api+json:schema:- $ref: '#/components/schemas/Account'+ $ref: '#/components/schemas/WishedItem''403':$ref: '#/components/responses/403Forbidden'security:@@ -14349,8 +14457,7 @@ components:token:type: stringexample: abcdef123456- description: |-- Used for authorizing any action for an Order within Spree’s API.+ description: Used for authorizing any action for an Order within Spree’s API.CreditCardList:required:- included@@ -14468,54 +14575,21 @@ components:example: 1080description: Actual height of imageUser:- required:- - data- properties:- data:- $ref: '#/components/schemas/UserAttributesAndRelationships'- included:- type: array- items:- anyOf:- - $ref: '#/components/schemas/default_billing_address'- - $ref: '#/components/schemas/default_shipping_address'- UserAttributes:- type: object- properties:- email:- type: string- store_credits:- type: integer- completed_orders:- type: integer- UserAttributesAndRelationships:type: objectproperties:id:type: string+ example: '1'type:type: string+ default: userattributes:- $ref: '#/components/schemas/UserAttributes'- relationships:type: objectproperties:- default_billing_address:- type: object- properties:- data:- type: array- items:- allOf:- - $ref: '#/components/schemas/Relation'- default_shipping_address:- type: object- properties:- data:- type: array- items:- allOf:- - $ref: '#/components/schemas/Relation'+ email:+ type: string+ format: email+ example: [email protected]LineItem:properties:id:@@ -16391,6 +16465,9 @@ components:type: booleanis_default:type: boolean+ variant_included:+ type: boolean+ description: Returns `true` or `false` depending on the variant ID passed in through the `is_variant_included` query param.WishlistAttributesAndRelationships:description: ''type: object@@ -16399,6 +16476,7 @@ components:type: stringtype:type: string+ default: wishlistattributes:$ref: '#/components/schemas/WishlistAttributes'relationships:@@ -16419,9 +16497,8 @@ components:included:type: arrayitems:- anyOf:+ oneOf:- $ref: '#/components/schemas/VariantAttributesAndRelationships'- - $ref: '#/components/schemas/WishlistAttributesAndRelationships'required:- dataWishedItemAttributes:@@ -16444,6 +16521,7 @@ components:type: stringtype:type: string+ default: wished_itemattributes:$ref: '#/components/schemas/WishedItemAttributes'relationships:@@ -16454,11 +16532,6 @@ components:properties:data:$ref: '#/components/schemas/Relation'- wishlist:- type: object- properties:- data:- $ref: '#/components/schemas/Relation'parameters:Id:name: id@@ -16649,8 +16722,15 @@ components:in: queryschema:type: string- example: include=wished_items.variant- description: Specify what related resources (relationships) you would like to receive in the response body. You can also fetch relationships of relationships.+ example: wished_items.variant+ description: 'Specify the related resources you would like to receive in the `included` section of the response body. You can also retrieve related resources of included resources by using the dot notation, eg. `wished_items.variant`. [More information](https://jsonapi.org/format/#fetching-includes)'+ WishlistIsVariantIncludedParam:+ name: is_variant_included+ in: query+ schema:+ type: string+ example: '150'+ description: 'Check for the presence of a variant in a wishlist by passing the variant ID, if the variant is present in the wishlist, the `variant_included` attribute will return `true`'FilterByIds:in: queryname: 'filter[ids]'@@ -16710,7 +16790,7 @@ components:WishlistSparseFieldsParam:in: queryname: fields- description: Specify what attributes for given types you would like to receive in the response body. Response examples are available.+ description: Specify the attributes you would like returned for the given types in the response body.example: '[wishlist]=name,is_default'schema:type: stringdiff --git a/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb b/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rbindex 5b5386c7cd..f687786555 100644--- a/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb@@ -97,9 +97,17 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request dexpect(json_response['data']['attributes']['name']).to eq (wishlist.name)expect(json_response['data']['attributes']['is_private']).to eq (wishlist.is_private?)expect(json_response['data']['attributes']['is_default']).to eq (wishlist.is_default?)+ expect(json_response['data']['attributes']['variant_included']).to be falseexpect(json_response['data']['relationships']['wished_items']['data'].first['id']).to eq(wished_item.id.to_s)end+ it 'returns is_variant_included true when the variant is already added to the wishlist' do+ get "/api/v2/storefront/wishlists/#{wishlist.token}?is_variant_included=#{wished_item.variant_id}", headers: headers_bearer++ expect(response).to have_http_status(:ok)+ expect(json_response['data']['attributes']['variant_included']).to be true+ end+context 'when a request is sent by random user with no auth' doit 'returns 404 when wishlist is set to is_private: true' doget "/api/v2/storefront/wishlists/#{wishlist_private.token}"diff --git a/api/spec/serializers/spree/v2/storefront/wishlist_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/wishlist_serializer_spec.rbindex 29922ecf9e..cad305003d 100644--- a/api/spec/serializers/spree/v2/storefront/wishlist_serializer_spec.rb+++ b/api/spec/serializers/spree/v2/storefront/wishlist_serializer_spec.rb@@ -18,7 +18,8 @@ describe Spree::V2::Storefront::WishlistSerializer doname: wishlist.name,token: wishlist.token,is_private: wishlist.is_private,- is_default: wishlist.is_default+ is_default: wishlist.is_default,+ variant_included: false},relationships: {wished_items: {