Add additional lint rules
This commit is contained in:
parent
02d782c3da
commit
420b18eb78
67 changed files with 3686 additions and 1415 deletions
|
|
@ -2,52 +2,65 @@ import fp from 'fastify-plugin';
|
|||
import { asClass, Lifetime } from 'awilix';
|
||||
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
||||
import { z } from 'zod/v4';
|
||||
import { NutritionTargetSchema, NutritionTargetResponseSchema } from '@meshitrack/shared';
|
||||
import {
|
||||
NutritionTargetSchema,
|
||||
NutritionTargetResponseSchema,
|
||||
type SetNutritionTargetInput,
|
||||
} from '@meshitrack/shared';
|
||||
import { NutritionTargetRepository } from './nutrition-target.repository.js';
|
||||
import { NutritionTargetService } from './nutrition-target.service.js';
|
||||
import { NutritionTargetService, type PresetType } from './nutrition-target.service.js';
|
||||
import type { NutritionTargetDocument } from '../../schemas/nutrition-target.schema.js';
|
||||
|
||||
type AnyTargetDoc = {
|
||||
_id: string | { toString: () => string };
|
||||
interface SerializedNutritionTarget {
|
||||
_id: string;
|
||||
userId: string;
|
||||
householdId: string;
|
||||
dailyCalories: number;
|
||||
proteinG: number;
|
||||
carbsG: number;
|
||||
fatG: number;
|
||||
fiberG?: number | null;
|
||||
sugarG?: number | null;
|
||||
sodiumMg?: number | null;
|
||||
fiberG?: number;
|
||||
sugarG?: number;
|
||||
sodiumMg?: number;
|
||||
isActive: boolean;
|
||||
createdAt: string | Date;
|
||||
updatedAt: string | Date;
|
||||
};
|
||||
|
||||
function toStr(v: string | { toString: () => string }): string {
|
||||
return typeof v === 'string' ? v : v.toString();
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
function toIso(v: string | Date): string {
|
||||
return typeof v === 'string' ? v : v.toISOString();
|
||||
}
|
||||
function toNutritionTargetResponse(doc: NutritionTargetDocument): SerializedNutritionTarget {
|
||||
const docAny = doc as unknown as {
|
||||
createdAt?: { toISOString?: () => string } | string;
|
||||
updatedAt?: { toISOString?: () => string } | string;
|
||||
fiberG?: number | null;
|
||||
sugarG?: number | null;
|
||||
sodiumMg?: number | null;
|
||||
};
|
||||
|
||||
function toNutritionTargetResponse(
|
||||
doc: AnyTargetDoc,
|
||||
): z.infer<typeof NutritionTargetResponseSchema> {
|
||||
return {
|
||||
_id: toStr(doc._id),
|
||||
const getIsoStr = (d: { toISOString?: () => string } | string | undefined | null): string => {
|
||||
if (!d) return '';
|
||||
if (typeof d === 'string') return d;
|
||||
if (typeof d.toISOString === 'function') return d.toISOString();
|
||||
return String(d);
|
||||
};
|
||||
|
||||
const res: SerializedNutritionTarget = {
|
||||
_id: doc._id.toString(),
|
||||
userId: doc.userId,
|
||||
householdId: doc.householdId,
|
||||
dailyCalories: doc.dailyCalories,
|
||||
proteinG: doc.proteinG,
|
||||
carbsG: doc.carbsG,
|
||||
fatG: doc.fatG,
|
||||
...(doc.fiberG != null ? { fiberG: doc.fiberG } : {}),
|
||||
...(doc.sugarG != null ? { sugarG: doc.sugarG } : {}),
|
||||
...(doc.sodiumMg != null ? { sodiumMg: doc.sodiumMg } : {}),
|
||||
isActive: doc.isActive,
|
||||
createdAt: toIso(doc.createdAt),
|
||||
updatedAt: toIso(doc.updatedAt),
|
||||
createdAt: getIsoStr(docAny.createdAt),
|
||||
updatedAt: getIsoStr(docAny.updatedAt),
|
||||
};
|
||||
|
||||
if (docAny.fiberG != null) res.fiberG = docAny.fiberG;
|
||||
if (docAny.sugarG != null) res.sugarG = docAny.sugarG;
|
||||
if (docAny.sodiumMg != null) res.sodiumMg = docAny.sodiumMg;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
declare module '@fastify/awilix' {
|
||||
|
|
@ -85,13 +98,14 @@ export default fp(
|
|||
handler: async (request, reply) => {
|
||||
const service = fastify.diContainer.resolve('nutritionTargetService');
|
||||
const userId = request.user.keycloakId;
|
||||
const target = await service.getActiveByUser(userId, request.params.householdId);
|
||||
const params = request.params as { householdId: string };
|
||||
const target = await service.getActiveByUser(userId, params.householdId);
|
||||
|
||||
if (!target) {
|
||||
return reply.status(200).send({ message: 'No active targets defined' });
|
||||
}
|
||||
|
||||
return reply.send(toNutritionTargetResponse(target as AnyTargetDoc));
|
||||
return reply.send(toNutritionTargetResponse(target));
|
||||
},
|
||||
});
|
||||
|
||||
|
|
@ -108,8 +122,9 @@ export default fp(
|
|||
handler: async (request, reply) => {
|
||||
const service = fastify.diContainer.resolve('nutritionTargetService');
|
||||
const userId = request.user.keycloakId;
|
||||
const targets = await service.getAllByUser(userId, request.params.householdId);
|
||||
return reply.send(targets.map((t) => toNutritionTargetResponse(t as AnyTargetDoc)));
|
||||
const params = request.params as { householdId: string };
|
||||
const targets = await service.getAllByUser(userId, params.householdId);
|
||||
return reply.send(targets.map(toNutritionTargetResponse));
|
||||
},
|
||||
});
|
||||
|
||||
|
|
@ -125,8 +140,10 @@ export default fp(
|
|||
handler: async (request, reply) => {
|
||||
const service = fastify.diContainer.resolve('nutritionTargetService');
|
||||
const userId = request.user.keycloakId;
|
||||
const target = await service.setTarget(userId, request.params.householdId, request.body);
|
||||
return reply.status(201).send(toNutritionTargetResponse(target as AnyTargetDoc));
|
||||
const params = request.params as { householdId: string };
|
||||
const body = request.body as SetNutritionTargetInput;
|
||||
const target = await service.setTarget(userId, params.householdId, body);
|
||||
return reply.status(201).send(toNutritionTargetResponse(target));
|
||||
},
|
||||
});
|
||||
|
||||
|
|
@ -144,7 +161,8 @@ export default fp(
|
|||
},
|
||||
handler: async (request, reply) => {
|
||||
const service = fastify.diContainer.resolve('nutritionTargetService');
|
||||
const calculated = service.calculatePreset(request.body.calories, request.body.strategy);
|
||||
const body = request.body as { calories: number; strategy: PresetType };
|
||||
const calculated = service.calculatePreset(body.calories, body.strategy);
|
||||
return reply.send(calculated);
|
||||
},
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue