commercetools-sdk-typescript icon indicating copy to clipboard operation
commercetools-sdk-typescript copied to clipboard

Incomplete type information on product availability

Open ghost opened this issue 1 year ago • 5 comments

The type definition for ProductVariantAvailability appears to be incomplete because the response from the API includes the ID and version of the associated inventory entry.

It seems like definition for ProductVariantAvailability should be a superset of ProductVariantChannelAvailability with the addition of the channels map. This would be helpful since an inventory entry could be immediately updated given a product, rather than having to fetch the inventory entry for each variant.

ghost avatar Feb 28 '24 15:02 ghost

This is an example availability for a product:

"availability": {
  "channels": {
    "cd724bd4-52fa-4d6d-b4b0-bb1560d70475": {
      "isOnStock": true,
      "restockableInDays": 10,
      "availableQuantity": 20,
      "version": 1,
      "id": "f64af276-a1ad-4eea-a8bc-89c453742a40"
    }
  }
}

The relevant definitions for the ProductVariantAvailability are:

export interface ProductVariantAvailability {
  /**
   *	For each [InventoryEntry](ctp:api:type:InventoryEntry) with a supply Channel, an entry is added to `channels`.
   *
   *
   */
  readonly channels?: ProductVariantChannelAvailabilityMap
  /**
   *	Indicates whether a Product Variant is in stock.
   *
   *
   */
  readonly isOnStock?: boolean
  /**
   *	Number of days to restock a Product Variant once it is out of stock.
   *
   *
   */
  readonly restockableInDays?: number
  /**
   *	Number of items of the Product Variant that are in stock.
   *
   *
   */
  readonly availableQuantity?: number
}

export interface ProductVariantChannelAvailabilityMap {
  [key: string]: ProductVariantChannelAvailability
}

export interface ProductVariantChannelAvailability {
  /**
   *	Indicates whether a Product Variant is in stock in a specified [Channel](ctp:api:type:Channel).
   *
   *
   */
  readonly isOnStock?: boolean
  /**
   *	Number of days to restock a Product Variant once it is out of stock in a specified [Channel](ctp:api:type:Channel).
   *
   *
   */
  readonly restockableInDays?: number
  /**
   *	Number of items of this Product Variant that are in stock in a specified [Channel](ctp:api:type:Channel).
   *
   *
   */
  readonly availableQuantity?: number
  /**
   *	Unique identifier of the [InventoryEntry](ctp:api:type:InventoryEntry).
   *
   *
   */
  readonly id: string
  /**
   *	Current version of the [InventoryEntry](ctp:api:type:InventoryEntry).
   *
   *
   */
  readonly version: number
}

As you can see they are in the types definition. You may have to access them like:

availability.channels.get("cd724bd4-52fa-4d6d-b4b0-bb1560d70475").availableQuantity

Please see also https://docs.commercetools.com/api/projects/products#productvariantavailability

The SDK only supports documented fields and you should not rely on undocumented ones.

jenschude avatar Feb 28 '24 21:02 jenschude

@jenschude I realized that I forgot to mention our products are using an inventory entry without a channel. This means the resulting availability for a product looks like the following:

"availability": {
  "isOnStock": true,
  "availableQuantity": 20,
  "version": 1,
  "id": "f64af276-a1ad-4eea-a8bc-89c453742a40"
}

ghost avatar Feb 28 '24 21:02 ghost

I also found that the ID and version are included in the Commercetools documentation for ProductVariantAvailability

ghost avatar Feb 29 '24 12:02 ghost

Yes. It has been added today. The changes are already merged to this repository. Both fields will be available with the next release.

Please see also: https://github.com/commercetools/commercetools-sdk-typescript/pull/626/files

jenschude avatar Feb 29 '24 12:02 jenschude

Awesome, thanks!

ghost avatar Feb 29 '24 12:02 ghost

Closing this as completed. 🥂

ajimae avatar May 02 '24 08:05 ajimae