sporedimk/price-compare-api/dist/user/user.service.js
2025-05-06 10:01:03 +02:00

174 lines
6.0 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.UserService = void 0;
const common_1 = require("@nestjs/common");
const prisma_service_1 = require("../prisma/prisma.service");
let UserService = class UserService {
prisma;
constructor(prisma) {
this.prisma = prisma;
}
async getWatchlist(userId) {
const user = await this.prisma.user.findUnique({
where: { id: userId },
});
if (!user) {
throw new common_1.NotFoundException(`User with ID ${userId} not found`);
}
return this.prisma.watchlistItem.findMany({
where: { userId },
include: {
product: {
include: {
prices: {
orderBy: { lastUpdated: 'desc' },
take: 1,
include: { source: true },
},
},
},
},
});
}
async addToWatchlist(userId, productId) {
const user = await this.prisma.user.findUnique({
where: { id: userId },
});
if (!user) {
throw new common_1.NotFoundException(`User with ID ${userId} not found`);
}
const product = await this.prisma.product.findUnique({
where: { id: productId },
});
if (!product) {
throw new common_1.NotFoundException(`Product with ID ${productId} not found`);
}
return this.prisma.watchlistItem.upsert({
where: {
userId_productId: {
userId,
productId,
},
},
update: {},
create: {
userId,
productId,
},
});
}
async removeFromWatchlist(userId, productId) {
const watchlistItem = await this.prisma.watchlistItem.findUnique({
where: {
userId_productId: {
userId,
productId,
},
},
});
if (!watchlistItem) {
throw new common_1.NotFoundException(`Watchlist item for user ${userId} and product ${productId} not found`);
}
return this.prisma.watchlistItem.delete({
where: {
userId_productId: {
userId,
productId,
},
},
});
}
async getNotifications(userId) {
const user = await this.prisma.user.findUnique({
where: { id: userId },
});
if (!user) {
throw new common_1.NotFoundException(`User with ID ${userId} not found`);
}
return this.prisma.notification.findMany({
where: { userId },
include: {
product: {
include: {
prices: {
orderBy: { lastUpdated: 'desc' },
take: 1,
include: { source: true },
},
},
},
},
});
}
async configureNotification(userId, data) {
const user = await this.prisma.user.findUnique({
where: { id: userId },
});
if (!user) {
throw new common_1.NotFoundException(`User with ID ${userId} not found`);
}
const product = await this.prisma.product.findUnique({
where: { id: data.productId },
});
if (!product) {
throw new common_1.NotFoundException(`Product with ID ${data.productId} not found`);
}
return this.prisma.notification.upsert({
where: {
userId_productId: {
userId,
productId: data.productId,
},
},
update: {
priceThreshold: data.priceThreshold,
percentThreshold: data.percentThreshold,
notifyOnAnyChange: data.notifyOnAnyChange ?? false,
isActive: true,
},
create: {
userId,
productId: data.productId,
priceThreshold: data.priceThreshold,
percentThreshold: data.percentThreshold,
notifyOnAnyChange: data.notifyOnAnyChange ?? false,
},
});
}
async removeNotification(userId, productId) {
const notification = await this.prisma.notification.findUnique({
where: {
userId_productId: {
userId,
productId,
},
},
});
if (!notification) {
throw new common_1.NotFoundException(`Notification for user ${userId} and product ${productId} not found`);
}
return this.prisma.notification.delete({
where: {
userId_productId: {
userId,
productId,
},
},
});
}
};
exports.UserService = UserService;
exports.UserService = UserService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
], UserService);
//# sourceMappingURL=user.service.js.map