"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const express_1 = require("express"); const asyncHandler_1 = require("../utils/asyncHandler"); const db_1 = require("../db"); const schema_1 = require("../db/schema"); const drizzle_orm_1 = require("drizzle-orm"); const auth_1 = require("../utils/auth"); const auth_2 = require("../utils/auth"); const errorHandler_1 = require("../middleware/errorHandler"); const validation_1 = require("../utils/validation"); const router = (0, express_1.Router)(); router.use(auth_1.authMiddleware); router.get('/', (0, asyncHandler_1.asyncHandler)(async (req, res) => { const profiles = await db_1.db .select() .from(schema_1.repeatProfiles) .where((0, drizzle_orm_1.eq)(schema_1.repeatProfiles.userId, req.user.userId)) .orderBy((0, drizzle_orm_1.asc)(schema_1.repeatProfiles.createdAt)); res.json({ profiles }); })); router.post('/', (0, asyncHandler_1.asyncHandler)(async (req, res) => { const data = validation_1.repeatProfileSchema.parse(req.body); const userId = req.user.userId; const now = Date.now(); const newProfile = { id: (0, auth_2.generateId)('rp'), userId, name: data.name, repeat: data.repeat, repeatInterval: data.repeatInterval, repeatDays: data.repeatDays, createdAt: now, updatedAt: now, }; await db_1.db.insert(schema_1.repeatProfiles).values(newProfile); res.status(201).json(newProfile); })); router.patch('/:id', (0, asyncHandler_1.asyncHandler)(async (req, res) => { const data = validation_1.repeatProfileUpdateSchema.parse(req.body); const userId = req.user.userId; const existing = await db_1.db .select() .from(schema_1.repeatProfiles) .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema_1.repeatProfiles.id, req.params.id), (0, drizzle_orm_1.eq)(schema_1.repeatProfiles.userId, userId))) .limit(1); if (existing.length === 0) { throw new errorHandler_1.AppError('NOT_FOUND', 'Repeat profile not found', 404); } const now = Date.now(); const updated = await db_1.db .update(schema_1.repeatProfiles) .set({ ...data, updatedAt: now }) .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema_1.repeatProfiles.id, req.params.id), (0, drizzle_orm_1.eq)(schema_1.repeatProfiles.userId, userId))) .returning(); res.json(updated[0]); })); router.delete('/:id', (0, asyncHandler_1.asyncHandler)(async (req, res) => { const userId = req.user.userId; const existing = await db_1.db .select() .from(schema_1.repeatProfiles) .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema_1.repeatProfiles.id, req.params.id), (0, drizzle_orm_1.eq)(schema_1.repeatProfiles.userId, userId))) .limit(1); if (existing.length === 0) { throw new errorHandler_1.AppError('NOT_FOUND', 'Repeat profile not found', 404); } await db_1.db .delete(schema_1.repeatProfiles) .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema_1.repeatProfiles.id, req.params.id), (0, drizzle_orm_1.eq)(schema_1.repeatProfiles.userId, userId))); res.status(204).send(); })); exports.default = router; //# sourceMappingURL=repeatProfiles.js.map