92 lines
3.8 KiB
JavaScript
92 lines
3.8 KiB
JavaScript
"use strict";
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.PriceService = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const prisma_service_1 = require("../prisma/prisma.service");
|
|
let PriceService = class PriceService {
|
|
prisma;
|
|
constructor(prisma) {
|
|
this.prisma = prisma;
|
|
}
|
|
async getPricesForProduct(productId) {
|
|
return this.prisma.price.findMany({
|
|
where: { productId },
|
|
include: { source: true },
|
|
orderBy: { lastUpdated: 'desc' },
|
|
});
|
|
}
|
|
async comparePrices(productIds) {
|
|
const products = await this.prisma.product.findMany({
|
|
where: { id: { in: productIds } },
|
|
include: {
|
|
prices: {
|
|
include: { source: true },
|
|
orderBy: { lastUpdated: 'desc' },
|
|
take: 1,
|
|
},
|
|
},
|
|
});
|
|
return products.map((product) => ({
|
|
id: product.id,
|
|
name: product.name,
|
|
description: product.description,
|
|
category: product.category,
|
|
availability: product.availability,
|
|
latestPrice: product.prices[0] || null,
|
|
}));
|
|
}
|
|
async getPriceHistory(productId, days = 30) {
|
|
const startDate = new Date();
|
|
startDate.setDate(startDate.getDate() - days);
|
|
const prices = await this.prisma.price.findMany({
|
|
where: {
|
|
productId,
|
|
lastUpdated: { gte: startDate },
|
|
},
|
|
include: { source: true },
|
|
orderBy: { lastUpdated: 'asc' },
|
|
});
|
|
const priceHistory = prices.reduce((acc, price) => {
|
|
const date = price.lastUpdated.toISOString().split('T')[0];
|
|
if (!acc[date]) {
|
|
acc[date] = {
|
|
date,
|
|
regularPrice: price.regularPrice,
|
|
discountedPrice: price.discountedPrice,
|
|
source: price.source.name,
|
|
};
|
|
}
|
|
return acc;
|
|
}, {});
|
|
return {
|
|
productId,
|
|
history: Object.values(priceHistory),
|
|
priceRange: {
|
|
min: Math.min(...prices.map(p => p.discountedPrice || p.regularPrice)),
|
|
max: Math.max(...prices.map(p => p.regularPrice)),
|
|
},
|
|
averagePrice: prices.reduce((sum, p) => sum + p.regularPrice, 0) / prices.length,
|
|
discountStats: {
|
|
maxDiscount: Math.max(...prices.map(p => p.discountPercentage || 0)),
|
|
averageDiscount: prices.filter(p => p.discountPercentage)
|
|
.reduce((sum, p) => sum + (p.discountPercentage || 0), 0) /
|
|
prices.filter((p) => p.discountPercentage).length || 0,
|
|
},
|
|
};
|
|
}
|
|
};
|
|
exports.PriceService = PriceService;
|
|
exports.PriceService = PriceService = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
|
|
], PriceService);
|
|
//# sourceMappingURL=price.service.js.map
|