Continued rebuild

This commit is contained in:
Ccjcarkbla
2026-06-30 10:15:18 -04:00
parent 7d16b921f9
commit e192aecb2f
16 changed files with 620 additions and 194 deletions
+2 -1
View File
@@ -3,6 +3,7 @@
const express = require('express');
const multer = require('multer');
const path = require('path');
const os = require('os');
const fs = require('fs');
const { models } = require('../../core/db');
const { authenticateToken, authenticateRole } = require('../../core/auth');
@@ -17,7 +18,7 @@ router.prefix = 'org';
const OWNERS = ['Owner', 'SystemAdmin'];
const upload = multer({
dest: process.env.UPLOAD_TEMP_DIR || '/tmp/vitfit-uploads',
dest: process.env.UPLOAD_TEMP_DIR || os.tmpdir(),
limits: { fileSize: 5 * 1024 * 1024 }, // 5 MB for logos
});
+4 -32
View File
@@ -7,20 +7,6 @@ const { log: auditLog } = require('../../core/audit');
const UPLOADS_DIR = () => process.env.UPLOADS_DIR || path.join(__dirname, '..', '..', 'uploads');
function contrastRatio(hex, bgHex = '#1a1a1a') {
const toLuminance = (h) => {
const r = parseInt(h.slice(1, 3), 16) / 255;
const g = parseInt(h.slice(3, 5), 16) / 255;
const b = parseInt(h.slice(5, 7), 16) / 255;
const chan = (c) => c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
return 0.2126 * chan(r) + 0.7152 * chan(g) + 0.0722 * chan(b);
};
const L1 = toLuminance(hex);
const L2 = toLuminance(bgHex);
const lighter = Math.max(L1, L2);
const darker = Math.min(L1, L2);
return (lighter + 0.05) / (darker + 0.05);
}
async function getOrg(models, orgId) {
const org = await models.Organization.findByPk(orgId);
@@ -50,22 +36,6 @@ async function getBranding(models, orgId) {
}
async function updateBranding(models, req, orgId, data) {
const colorFields = ['primaryColor', 'secondaryColor', 'workColor', 'restColor', 'moveColor', 'btnPrimaryBg', 'btnSecondaryBg'];
const failures = [];
for (const field of colorFields) {
if (data[field]) {
const ratio = contrastRatio(data[field]);
if (ratio < 4.5) {
failures.push({ field, color: data[field], ratio: ratio.toFixed(2) });
}
}
}
if (failures.length > 0) {
throw new AppError('VALIDATION_FAILED', `Colors fail minimum contrast ratio (4.5:1) against display background: ${failures.map(f => `${f.field} (${f.ratio}:1)`).join(', ')}`, 400);
}
const branding = await getBranding(models, orgId);
await branding.update(data);
await auditLog(req, 'branding.update', 'branding', branding.id, data);
@@ -85,11 +55,13 @@ async function uploadLogo(models, req, orgId, file) {
fs.closeSync(fd);
const result = await fileTypeFromBuffer(buf);
detectedMime = result?.mime;
} finally {
} catch {
await fs.promises.unlink(file.path).catch(() => {});
throw new AppError('UPLOAD_TYPE_INVALID', 'Could not read uploaded file.', 400);
}
if (!detectedMime || !ALLOWED.includes(detectedMime)) {
await fs.promises.unlink(file.path).catch(() => {});
throw new AppError('UPLOAD_TYPE_INVALID', 'Only PNG, JPG, GIF, and WebP images are supported.', 400);
}
@@ -112,4 +84,4 @@ async function uploadLogo(models, req, orgId, file) {
return branding;
}
module.exports = { getOrg, updateOrg, getBranding, updateBranding, uploadLogo, contrastRatio };
module.exports = { getOrg, updateOrg, getBranding, updateBranding, uploadLogo };
+2 -1
View File
@@ -3,6 +3,7 @@
const express = require('express');
const multer = require('multer');
const path = require('path');
const os = require('os');
const { models } = require('../../core/db');
const { authenticateToken, authenticateRole } = require('../../core/auth');
const { ok, created, collection, noContent } = require('../../core/respond');
@@ -17,7 +18,7 @@ router.prefix = 'videos';
const COACH_PLUS = ['Owner', 'Coach', 'SystemAdmin'];
const upload = multer({
dest: process.env.UPLOAD_TEMP_DIR || '/tmp/vitfit-uploads',
dest: process.env.UPLOAD_TEMP_DIR || os.tmpdir(),
limits: { fileSize: parseInt(process.env.UPLOAD_MAX_BYTES || '524288000', 10) },
});
+8 -1
View File
@@ -5,7 +5,7 @@ const { models } = require('../../core/db');
const { authenticateToken, authenticateRole } = require('../../core/auth');
const { ok, created, collection, noContent } = require('../../core/respond');
const { AppError } = require('../../core/errors');
const { createWorkout: createWorkoutSchema } = require('../../core/schemas/workouts');
const { createWorkout: createWorkoutSchema, updateWorkout: updateWorkoutSchema } = require('../../core/schemas/workouts');
const svc = require('./service');
const router = express.Router();
@@ -54,6 +54,13 @@ router.get('/:id', authenticateToken, authenticateRole(COACH_PLUS), async (req,
} catch (err) { next(err); }
});
router.patch('/:id', authenticateToken, authenticateRole(COACH_PLUS), validate(updateWorkoutSchema), async (req, res, next) => {
try {
const workout = await svc.updateWorkout(models, req, parseInt(req.params.id, 10), req.user.organizationId, req.body);
ok(res, workout);
} catch (err) { next(err); }
});
router.delete('/:id', authenticateToken, authenticateRole(COACH_PLUS), async (req, res, next) => {
try {
await svc.deleteWorkout(models, req, parseInt(req.params.id, 10), req.user.organizationId);
+23 -4
View File
@@ -50,9 +50,11 @@ async function getWorkoutById(models, id, orgId) {
async function getTodayWorkouts(models, orgId) {
const org = await models.Organization.findByPk(orgId, { attributes: ['timezone'] });
const tz = org?.timezone || 'UTC';
const now = DateTime.now().setZone(tz);
const start = now.startOf('day').toUTC().toJSDate();
const end = now.endOf('day').toUTC().toJSDate();
// workoutDate is stored as UTC midnight when submitted as a date string ("YYYY-MM-DD"),
// so we compare against the UTC date range matching today's local date.
const todayStr = DateTime.now().setZone(tz).toFormat('yyyy-MM-dd');
const start = new Date(`${todayStr}T00:00:00.000Z`);
const end = new Date(`${todayStr}T23:59:59.999Z`);
const rows = await models.Workout.findAll({
where: { organizationId: orgId, workoutDate: { [Op.between]: [start, end] } },
@@ -77,6 +79,23 @@ async function createWorkout(models, req, orgId, data) {
return getWorkoutById(models, workout.id, orgId);
}
async function updateWorkout(models, req, id, orgId, data) {
const workout = await models.Workout.findOne({ where: { id, organizationId: orgId } });
if (!workout) throw new AppError('NOT_FOUND', 'Workout not found.', 404);
for (const slot of [1, 2, 3, 4]) {
const vid = data[`video${slot}`];
if (vid) {
const exists = await models.Video.findOne({ where: { id: vid, organizationId: orgId } });
if (!exists) throw new AppError('VALIDATION_FAILED', `Video ${vid} not found in your library.`, 400, `video${slot}`);
}
}
await workout.update(data);
await auditLog(req, 'workout.update', 'workout', id, { name: workout.name });
return getWorkoutById(models, id, orgId);
}
async function deleteWorkout(models, req, id, orgId) {
const workout = await models.Workout.findOne({ where: { id, organizationId: orgId } });
if (!workout) throw new AppError('NOT_FOUND', 'Workout not found.', 404);
@@ -89,4 +108,4 @@ async function deleteWorkout(models, req, id, orgId) {
await workout.destroy();
}
module.exports = { getWorkouts, getWorkoutById, getTodayWorkouts, createWorkout, deleteWorkout };
module.exports = { getWorkouts, getWorkoutById, getTodayWorkouts, createWorkout, updateWorkout, deleteWorkout };