This commit is contained in:
+1
-1
Submodule graphql-contracts updated: 93b8e21325...571a2c8172
+34
-2
@@ -58,6 +58,11 @@ export const typeDefs = `#graphql
|
||||
categoryName: String!
|
||||
unit: String!
|
||||
imageUrl: String
|
||||
offersCount: Int
|
||||
averagePrice: Float
|
||||
priceVariationPercent: Float
|
||||
priceSeries: [Float!]!
|
||||
currency: String
|
||||
description: String
|
||||
isActive: Boolean!
|
||||
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) {
|
||||
const supplier = await prisma.supplier.findUnique({ where: { uuid: input.supplierUuid } })
|
||||
if (supplier === null) throw new Error(`Supplier ${input.supplierUuid} does not exist`)
|
||||
@@ -219,8 +252,7 @@ export const resolvers = {
|
||||
orderBy: { name: 'asc' },
|
||||
}),
|
||||
|
||||
product: (_: unknown, args: { uuid: string }) =>
|
||||
prisma.product.findUnique({ where: { uuid: args.uuid } }),
|
||||
product: (_: unknown, args: { uuid: string }) => productWithPriceSummary(args.uuid),
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user