const Pool = require('../../models/Pool'); class PoolRepository { constructor(uow) { this.uow = uow; } async create({ pool_name, description = null, price = 0.00, pool_type = 'other', is_active = true, created_by = null }) { const conn = this.uow.connection; const [res] = await conn.execute( `INSERT INTO pools (pool_name, description, price, pool_type, is_active, created_by) VALUES (?, ?, ?, ?, ?, ?)`, [pool_name, description, price, pool_type, is_active, created_by] ); return new Pool({ id: res.insertId, pool_name, description, price, pool_type, is_active, created_by }); } async findAll() { const conn = this.uow.connection; // switched to connection try { console.debug('[PoolRepository.findAll] querying pools'); const [rows] = await conn.execute( `SELECT id, pool_name, description, price, pool_type, is_active, created_by, updated_by, created_at, updated_at FROM pools ORDER BY created_at DESC` ); console.debug('[PoolRepository.findAll] rows fetched', { count: rows.length }); return rows.map(r => new Pool(r)); } catch (err) { console.error('[PoolRepository.findAll] query failed', err); // Surface a consistent error up the stack const e = new Error('Failed to fetch pools'); e.status = 500; e.cause = err; throw e; } } // Update is_active flag (replaces old state transitions) async updateActive(id, is_active, updated_by = null) { const conn = this.uow.connection; const [rows] = await conn.execute(`SELECT id FROM pools WHERE id = ?`, [id]); if (!rows || rows.length === 0) { const err = new Error('Pool not found'); err.status = 404; throw err; } await conn.execute( `UPDATE pools SET is_active = ?, updated_by = ?, updated_at = NOW() WHERE id = ?`, [is_active, updated_by, id] ); const [updated] = await conn.execute( `SELECT id, pool_name, description, price, pool_type, is_active, created_by, updated_by, created_at, updated_at FROM pools WHERE id = ?`, [id] ); return new Pool(updated[0]); } } module.exports = PoolRepository;