'use strict'; const { AppError } = require('../../core/errors'); const { log: auditLog } = require('../../core/audit'); async function getCrossfitWorkouts(models, orgId, { page = 1, limit = 20 } = {}) { const offset = (page - 1) * limit; const { rows, count } = await models.CrossfitWorkout.findAndCountAll({ where: { organizationId: orgId }, limit, offset, order: [['workoutDate', 'DESC'], ['createdAt', 'DESC']], }); return { rows, count }; } async function getCrossfitWorkoutById(models, id, orgId) { const w = await models.CrossfitWorkout.findOne({ where: { id, organizationId: orgId } }); if (!w) throw new AppError('NOT_FOUND', 'CrossFit workout not found.', 404); return w; } async function createCrossfitWorkout(models, req, orgId, data) { const workout = await models.CrossfitWorkout.create({ ...data, organizationId: orgId, createdByUserId: req.user.id, }); await auditLog(req, 'crossfit.create', 'crossfit_workout', workout.id, { name: data.name }); return workout; } async function deleteCrossfitWorkout(models, req, id, orgId) { const workout = await getCrossfitWorkoutById(models, id, orgId); await auditLog(req, 'crossfit.delete', 'crossfit_workout', id, { name: workout.name }); await workout.destroy(); } module.exports = { getCrossfitWorkouts, getCrossfitWorkoutById, createCrossfitWorkout, deleteCrossfitWorkout };