Add product detail price summaries
Build and deploy Docker image / build (push) Successful in 2m35s

This commit is contained in:
Ruslan Bakiev
2026-08-24 14:20:22 +07:00
parent 5ab16917a1
commit 02c6df0d02
2 changed files with 35 additions and 3 deletions
+34 -2
View File
@@ -58,6 +58,11 @@ export const typeDefs = `#graphql
categoryName: String! categoryName: String!
unit: String! unit: String!
imageUrl: String imageUrl: String
offersCount: Int
averagePrice: Float
priceVariationPercent: Float
priceSeries: [Float!]!
currency: String
description: String description: String
isActive: Boolean! isActive: Boolean!
createdAt: String! createdAt: String!
@@ -167,6 +172,34 @@ function productData(input: ProductInput) {
} }
} }
async function productWithPriceSummary(uuid: string) {
const product = await prisma.product.findUnique({ where: { uuid } })
if (product === null) return null
const quotes = await prisma.quote.findMany({
where: { productId: product.id, status: 'active' },
select: { pricePerUnit: true, currency: true },
orderBy: { pricePerUnit: 'asc' },
take: 100,
})
const priceSeries = quotes.map(quote => Number(quote.pricePerUnit))
const averagePrice = priceSeries.length === 0
? null
: priceSeries.reduce((sum, price) => sum + price, 0) / priceSeries.length
const priceVariationPercent = averagePrice === null || averagePrice <= 0
? 0
: ((priceSeries.at(-1)! - priceSeries[0]) / 2 / averagePrice) * 100
return {
...product,
offersCount: quotes.length,
averagePrice,
priceVariationPercent,
priceSeries,
currency: quotes[0]?.currency ?? 'USD',
}
}
async function quoteData(input: QuoteInput) { async function quoteData(input: QuoteInput) {
const supplier = await prisma.supplier.findUnique({ where: { uuid: input.supplierUuid } }) const supplier = await prisma.supplier.findUnique({ where: { uuid: input.supplierUuid } })
if (supplier === null) throw new Error(`Supplier ${input.supplierUuid} does not exist`) if (supplier === null) throw new Error(`Supplier ${input.supplierUuid} does not exist`)
@@ -219,8 +252,7 @@ export const resolvers = {
orderBy: { name: 'asc' }, orderBy: { name: 'asc' },
}), }),
product: (_: unknown, args: { uuid: string }) => product: (_: unknown, args: { uuid: string }) => productWithPriceSummary(args.uuid),
prisma.product.findUnique({ where: { uuid: args.uuid } }),
quotes: async (_: unknown, args: { productUuid?: string; supplierUuid?: string; status?: string; limit?: number; offset?: number }) => { quotes: async (_: unknown, args: { productUuid?: string; supplierUuid?: string; status?: string; limit?: number; offset?: number }) => {
const product = args.productUuid ? await prisma.product.findUnique({ where: { uuid: args.productUuid } }) : null const product = args.productUuid ? await prisma.product.findUnique({ where: { uuid: args.productUuid } }) : null