120 lines
4.6 KiB
JavaScript
120 lines
4.6 KiB
JavaScript
const UnitOfWork = require('../../database/UnitOfWork');
|
|
const PoolRepository = require('../../repositories/pool/poolRepository');
|
|
const db = require('../../database/database');
|
|
|
|
function isValidPoolType(pool_type) {
|
|
return pool_type === 'coffee' || pool_type === 'other';
|
|
}
|
|
|
|
async function createPool({ pool_name, description = null, price = 0.00, subscription_coffee_id = null, pool_type = 'other', is_active = true, created_by = null }) {
|
|
if (!isValidPoolType(pool_type)) {
|
|
const err = new Error('Invalid pool_type. Allowed: coffee, other');
|
|
err.status = 400;
|
|
throw err;
|
|
}
|
|
|
|
let normalizedSubscriptionCoffeeId = null;
|
|
if (subscription_coffee_id !== null && subscription_coffee_id !== undefined && String(subscription_coffee_id).trim() !== '') {
|
|
const sid = Number(subscription_coffee_id);
|
|
if (!Number.isFinite(sid) || sid <= 0) {
|
|
const err = new Error('Invalid subscription_coffee_id');
|
|
err.status = 400;
|
|
throw err;
|
|
}
|
|
const [rows] = await db.query('SELECT id FROM coffee_table WHERE id = ? LIMIT 1', [sid]);
|
|
if (!rows.length) {
|
|
const err = new Error('Selected subscription not found');
|
|
err.status = 400;
|
|
throw err;
|
|
}
|
|
normalizedSubscriptionCoffeeId = sid;
|
|
}
|
|
|
|
const uow = new UnitOfWork();
|
|
try {
|
|
console.debug('[PoolService.createPool] start', { pool_name, pool_type, subscription_coffee_id: normalizedSubscriptionCoffeeId });
|
|
await uow.start();
|
|
const repo = new PoolRepository(uow);
|
|
const pool = await repo.create({ pool_name, description, price, subscription_coffee_id: normalizedSubscriptionCoffeeId, pool_type, is_active, created_by });
|
|
await uow.commit();
|
|
console.debug('[PoolService.createPool] success', { id: pool.id });
|
|
return pool;
|
|
} catch (err) {
|
|
console.error('[PoolService.createPool] error', err);
|
|
try { await uow.rollback(); } catch (_) { console.warn('[PoolService.createPool] rollback failed'); }
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async function listPools() {
|
|
const uow = new UnitOfWork();
|
|
try {
|
|
console.debug('[PoolService.listPools] start');
|
|
// Ensure connection exists even for read-only
|
|
await uow.start();
|
|
const repo = new PoolRepository(uow);
|
|
const pools = await repo.findAll();
|
|
await uow.commit(); // harmless commit; ensures proper cleanup if UnitOfWork requires it
|
|
console.debug('[PoolService.listPools] success', { count: pools.length });
|
|
return pools;
|
|
} catch (err) {
|
|
console.error('[PoolService.listPools] error', err);
|
|
try { await uow.rollback(); } catch (_) { console.warn('[PoolService.listPools] rollback failed'); }
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async function updatePoolState(id, is_active, actorUserId) {
|
|
if (typeof is_active !== 'boolean') {
|
|
const err = new Error('is_active must be a boolean');
|
|
err.status = 400;
|
|
throw err;
|
|
}
|
|
const uow = new UnitOfWork();
|
|
try {
|
|
console.debug('[PoolService.updatePoolState] start', { id, is_active });
|
|
await uow.start();
|
|
const repo = new PoolRepository(uow);
|
|
const updated = await repo.updateActive(id, is_active, actorUserId);
|
|
await uow.commit();
|
|
console.debug('[PoolService.updatePoolState] success', { id, is_active });
|
|
return updated;
|
|
} catch (err) {
|
|
console.error('[PoolService.updatePoolState] error', err);
|
|
try { await uow.rollback(); } catch (_) { console.warn('[PoolService.updatePoolState] rollback failed'); }
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async function updatePoolSubscription({ id, subscription_coffee_id = null, actorUserId = null }) {
|
|
let normalizedSubscriptionCoffeeId = null;
|
|
if (subscription_coffee_id !== null && subscription_coffee_id !== undefined && String(subscription_coffee_id).trim() !== '') {
|
|
const sid = Number(subscription_coffee_id);
|
|
if (!Number.isFinite(sid) || sid <= 0) {
|
|
const err = new Error('Invalid subscription_coffee_id');
|
|
err.status = 400;
|
|
throw err;
|
|
}
|
|
const [rows] = await db.query('SELECT id FROM coffee_table WHERE id = ? LIMIT 1', [sid]);
|
|
if (!rows.length) {
|
|
const err = new Error('Selected subscription not found');
|
|
err.status = 400;
|
|
throw err;
|
|
}
|
|
normalizedSubscriptionCoffeeId = sid;
|
|
}
|
|
|
|
const uow = new UnitOfWork();
|
|
try {
|
|
await uow.start();
|
|
const repo = new PoolRepository(uow);
|
|
const updated = await repo.updateSubscriptionLink(id, normalizedSubscriptionCoffeeId, actorUserId);
|
|
await uow.commit();
|
|
return updated;
|
|
} catch (err) {
|
|
try { await uow.rollback(); } catch (_) { console.warn('[PoolService.updatePoolSubscription] rollback failed'); }
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
module.exports = { createPool, listPools, updatePoolState, updatePoolSubscription }; |