Cialo: typeId+side zamiast nazwy PL w pomiarach obwodow (migracja z backfillem, kontrakt API: z.enum typow, grupowanie serii po type+side, legacy name nullable, swagger, testy)

This commit is contained in:
tisachi
2026-10-17 11:16:54 +02:00
parent 81a7b0202e
commit ba17fb1d6a
9 changed files with 235 additions and 52 deletions

View File

@@ -851,7 +851,12 @@ export function createAdminRoutes({ prisma }: AppDeps) {
action: 'circumference.added',
entityType: 'user',
entityId: req.params.id,
details: { name: data.name, value: data.value, byAdmin: true },
details: {
typeId: data.typeId,
side: data.side ?? null,
value: data.value,
byAdmin: true,
},
});
res.status(201).json(result);
}),
@@ -877,7 +882,13 @@ export function createAdminRoutes({ prisma }: AppDeps) {
action: 'circumference.removed',
entityType: 'user',
entityId: req.params.id,
details: { name: result.name, value: result.value, byAdmin: true },
details: {
typeId: result.typeId,
side: result.side,
name: result.name,
value: result.value,
byAdmin: true,
},
});
res.status(204).end();
}),

View File

@@ -6,6 +6,7 @@ import { validate } from '../middleware/validate.js';
import {
addCircumference,
addWeight,
CIRCUMFERENCE_TYPE_IDS,
deleteCircumference,
deleteWeight,
getBodyParameters,
@@ -59,7 +60,8 @@ export const weightSchema = z.object({
});
export const circumferenceSchema = z.object({
name: z.string().min(1).max(100),
typeId: z.enum(CIRCUMFERENCE_TYPE_IDS),
side: z.enum(['left', 'right']).nullish(),
value: z.number().positive().max(300),
date: z.string().datetime({ offset: true }).optional(),
});
@@ -303,7 +305,7 @@ export function createMeRoutes({ prisma }: AppDeps) {
* @openapi
* /me/body-parameters/circumference:
* post:
* summary: Add circumference measurement (name + value)
* summary: Add circumference measurement (type + side + value)
* tags: [Profile]
* responses:
* 201: { description: Created }
@@ -319,7 +321,7 @@ export function createMeRoutes({ prisma }: AppDeps) {
action: 'circumference.added',
entityType: 'user',
entityId: req.user!.id,
details: { name: data.name, value: data.value },
details: { typeId: data.typeId, side: data.side ?? null, value: data.value },
});
res.status(201).json(result);
}),
@@ -369,7 +371,12 @@ export function createMeRoutes({ prisma }: AppDeps) {
action: 'circumference.removed',
entityType: 'user',
entityId: req.user!.id,
details: { name: result.name, value: result.value },
details: {
typeId: result.typeId,
side: result.side,
name: result.name,
value: result.value,
},
});
res.status(204).end();
}),

View File

@@ -53,8 +53,23 @@ export interface WeightInput {
date?: string;
}
export type CircumferenceSide = 'left' | 'right';
/** Znane typy pomiarów obwodu (kolejność zgodna z listą na froncie). */
export const CIRCUMFERENCE_TYPE_IDS = [
'chest',
'waist',
'hips',
'neck',
'biceps',
'forearm',
'thigh',
'calf',
] as const;
export interface CircumferenceInput {
name: string;
typeId: string;
side?: CircumferenceSide | null;
value: number;
date?: string;
}
@@ -180,18 +195,32 @@ export async function getBodyParameters(prisma: PrismaClient, userId: string) {
orderBy: { date: 'asc' },
}),
]);
const byName = new Map<
string,
{ name: string; points: { id: string; value: number; date: string }[] }
>();
interface Series {
typeId: string | null;
side: string | null;
/** Znacząca tylko dla starszych pomiarów (typeId = null) — zachowana swobodna nazwa. */
name: string | null;
points: { id: string; value: number; date: string }[];
}
const byKey = new Map<string, Series>();
for (const c of circumferences) {
const entry = byName.get(c.name) ?? { name: c.name, points: [] };
// Nowe: grupujemy po (typeId, side). Starsze bez typeId: po zachowanej nazwie.
const key = c.typeId ? `t:${c.typeId}:${c.side ?? ''}` : `n:${c.name ?? ''}`;
let entry = byKey.get(key);
if (!entry) {
entry = {
typeId: c.typeId,
side: c.typeId ? c.side : null,
name: c.typeId ? null : c.name,
points: [],
};
byKey.set(key, entry);
}
entry.points.push({ id: c.id, value: c.value, date: c.date.toISOString() });
byName.set(c.name, entry);
}
return {
weight: weight.map((w) => ({ id: w.id, value: w.value, date: w.date.toISOString() })),
circumferences: [...byName.values()],
circumferences: [...byKey.values()],
};
}
@@ -210,12 +239,19 @@ export async function addCircumference(
const entry = await prisma.circumferenceMeasurement.create({
data: {
userId,
name: input.name,
typeId: input.typeId,
side: input.side ?? null,
value: input.value,
date: input.date ? new Date(input.date) : new Date(),
},
});
return { id: entry.id, name: entry.name, value: entry.value, date: entry.date };
return {
id: entry.id,
typeId: entry.typeId,
side: entry.side,
value: entry.value,
date: entry.date,
};
}
export async function deleteWeight(prisma: PrismaClient, userId: string, id: string) {
@@ -229,7 +265,14 @@ export async function deleteCircumference(prisma: PrismaClient, userId: string,
const entry = await prisma.circumferenceMeasurement.findFirst({ where: { id, userId } });
if (!entry) throw notFound('Measurement not found');
await prisma.circumferenceMeasurement.delete({ where: { id: entry.id } });
return { id: entry.id, name: entry.name, value: entry.value, date: entry.date };
return {
id: entry.id,
typeId: entry.typeId,
side: entry.side,
name: entry.name,
value: entry.value,
date: entry.date,
};
}
export async function getHistory(prisma: PrismaClient, userId: string, month?: string) {

View File

@@ -177,9 +177,13 @@ export const openapi: Record<string, unknown> = {
'application/json': {
schema: {
type: 'object',
required: ['name', 'value'],
required: ['typeId', 'value'],
properties: {
name: { type: 'string' },
typeId: {
type: 'string',
enum: ['chest', 'waist', 'hips', 'neck', 'biceps', 'forearm', 'thigh', 'calf'],
},
side: { type: 'string', enum: ['left', 'right'], nullable: true },
value: { type: 'number' },
date: { type: 'string' },
},
@@ -1516,9 +1520,13 @@ export const openapi: Record<string, unknown> = {
'application/json': {
schema: {
type: 'object',
required: ['name', 'value'],
required: ['typeId', 'value'],
properties: {
name: { type: 'string' },
typeId: {
type: 'string',
enum: ['chest', 'waist', 'hips', 'neck', 'biceps', 'forearm', 'thigh', 'calf'],
},
side: { type: 'string', enum: ['left', 'right'], nullable: true },
value: { type: 'number' },
date: { type: 'string', format: 'date-time' },
},