"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.UsersService = void 0; const common_1 = require("@nestjs/common"); const prisma_service_1 = require("../prisma/prisma.service"); const bcrypt = require("bcryptjs"); const crypto = require("crypto"); let UsersService = class UsersService { constructor(prisma) { this.prisma = prisma; } findByUsername(username) { return this.prisma.user.findUnique({ where: { username } }); } findById(id) { return this.prisma.user.findUnique({ where: { id } }); } create(data) { return this.prisma.user.create({ data }); } updateProfile(id, data) { return this.prisma.user.update({ where: { id }, data }); } findAll() { return this.prisma.user.findMany({ select: { id: true, username: true, email: true, firstName: true, lastName: true, role: true, isPremium: true, canShareRecipes: true, createdAt: true, }, orderBy: { username: 'asc' }, }); } setRole(id, role) { return this.prisma.user.update({ where: { id }, data: { role } }); } setPremium(id, isPremium) { return this.prisma.user.update({ where: { id }, data: { isPremium } }); } setRecipeSharing(id, canShareRecipes) { return this.prisma.user.update({ where: { id }, data: { canShareRecipes } }); } async adminCreate(data) { const existing = await this.prisma.user.findFirst({ where: { OR: [{ username: data.username }, { email: data.email }] }, }); if (existing) { throw new common_1.ConflictException(existing.username === data.username ? 'Användarnamnet är redan taget' : 'E-postadressen används redan'); } const passwordHash = await bcrypt.hash(data.password, 12); return this.prisma.user.create({ data: { username: data.username, email: data.email, passwordHash, role: data.role ?? 'user' }, }); } deleteUser(id) { return this.prisma.user.delete({ where: { id } }); } async resetPassword(id) { const temporaryPassword = crypto.randomBytes(9).toString('base64url').slice(0, 12); const passwordHash = await bcrypt.hash(temporaryPassword, 12); await this.prisma.user.update({ where: { id }, data: { passwordHash } }); return { temporaryPassword }; } updateEmail(id, email) { return this.prisma.user.update({ where: { id }, data: { email } }); } }; exports.UsersService = UsersService; exports.UsersService = UsersService = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [prisma_service_1.PrismaService]) ], UsersService); //# sourceMappingURL=users.service.js.map