FgForrest / evitaDB

evitaDB is a specialized database with an easy-to-use API for e-commerce systems. It is a low-latency NoSQL in-memory engine that handles all the complex tasks that e-commerce systems have to deal with on a daily basis. evitaDB is expected to act as a fast secondary lookup/search index used by front stores.
https://evitadb.io
Other
62 stars 7 forks source link

feat(#651): sort by discount #672

Closed novoj closed 1 month ago

novoj commented 1 month ago

We should build on issue #549 and introduce a new sort option that would allow to specify a list of price lists that should be considered as discounted. The query engine should then calculate the discounted price according to the inner record price handling rules and calculate the discount compared to the selling price:

{
  queryProduct(
    filterBy: {
      entityPrimaryKeyInSet: [
        103885
      ],
      priceInCurrency: EUR,
      priceInPriceLists: [
        "employee-basic-price",
        "basic"
      ]
    }
    orderBy: {
      priceDiscount: {
        order: ASC
        inPriceLists: [
          "reference",
          "basic"
        ]
      }
  ) {
    recordPage {
      data {
        primaryKey
        attributes {
          code
        }
        priceForSale {
          priceWithoutTax
          priceWithTax
          taxRate
        }
        referencePrice: price(priceList: ["reference", "basic") {
          priceWithoutTax
          priceWithTax
          taxRate
        }
      }
    }
  }
}

And in evitaQL:

query(
    collection("Product"),
    filterBy(
        priceInPriceLists("basic"),
        priceInCurrency("EUR"),
        priceBetween(100, 103)
    ),
    orderBy(
        priceDiscount(ASC, "reference", "basic")
    )
    require(
      entityFetch(
        attributeContent("code"),
        priceContentRespectingFilter()
      )
    )
)

If the discount result is negative (i.e. the discounted price is greater than the selling price), it's considered to be zero. Sorting in ascending order means that the products with no discount are returned first, and the products with the largest discount are returned last. Descending order returns the product with the largest discount first.

❗ Important change

The sellable property on the PriceContract interface has been renamed to indexed because it better describes its purpose. It's also more aligned with property naming in the attribute schema - which also uses the `indexed' keyword for the same purpose.