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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user