diff --git a/graphql-contracts b/graphql-contracts index e96e5af..1af133d 160000 --- a/graphql-contracts +++ b/graphql-contracts @@ -1 +1 @@ -Subproject commit e96e5afd012a9bcc3d8e9738ef795c6da1893f76 +Subproject commit 1af133dd20a65cc7bae89023f3adbc6e7108c0a1 diff --git a/src/schema.ts b/src/schema.ts index 190be16..b131043 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -99,6 +99,11 @@ export const typeDefs = `#graphql name: String imageUrl: String offersCount: Int + averagePrice: Float + priceVariationPercent: Float + priceSeries: [Float!]! + currency: String + unit: String } type Supplier { @@ -255,6 +260,11 @@ function productView(doc: ArangoDoc, catalog: Map) { name: product?.name ?? doc.product_name ?? doc.name ?? null, imageUrl: product?.imageUrl ?? null, offersCount: doc.offers_count ?? doc.offersCount ?? null, + averagePrice: doc.average_price ?? null, + priceVariationPercent: doc.price_variation_percent ?? null, + priceSeries: doc.price_series ?? [], + currency: doc.currency ?? null, + unit: doc.unit ?? null, } } @@ -1368,9 +1378,30 @@ export const resolvers = { ${bounds.filter} COLLECT product_uuid = node.product_uuid INTO offers LET first_offer = FIRST(offers).node + LET price_series = ( + FOR offer_row IN offers + LET price = TO_NUMBER(offer_row.node.price_per_unit) + FILTER price > 0 + SORT price ASC + RETURN price + ) + LET average_price = LENGTH(price_series) > 0 ? AVERAGE(price_series) : null + LET price_variation_percent = average_price > 0 + ? ((LAST(price_series) - FIRST(price_series)) / 2 / average_price) * 100 + : 0 SORT first_offer.product_name ASC LIMIT @offset, @limit - RETURN { uuid: product_uuid, product_uuid, product_name: first_offer.product_name, offers_count: LENGTH(offers) } + RETURN { + uuid: product_uuid, + product_uuid, + product_name: first_offer.product_name, + offers_count: LENGTH(offers), + average_price, + price_variation_percent, + price_series, + currency: first_offer.currency, + unit: first_offer.unit + } `, { offset: args.offset ?? 0, limit: args.limit ?? 50, ...bounds.vars }) const products = await cursor.all() return products.map((product: ArangoDoc) => productView(product, productCatalog))