Add additional lint rules
This commit is contained in:
parent
02d782c3da
commit
420b18eb78
67 changed files with 3686 additions and 1415 deletions
|
|
@ -9,11 +9,15 @@ import {
|
|||
RegimenResponseSchema,
|
||||
RegimenListResponseSchema,
|
||||
BurnRateResponseSchema,
|
||||
type CreateRegimenInput,
|
||||
type UpdateRegimenInput,
|
||||
type RegimenQueryInput,
|
||||
} from '@meshitrack/shared';
|
||||
import { RegimensRepository } from './regimens.repository.js';
|
||||
import { RegimensService } from './regimens.service.js';
|
||||
import type { RegimenDocument } from '../../schemas/regimen.schema.js';
|
||||
|
||||
type AnyRegimenMedication = {
|
||||
interface SerializedRegimenMedication {
|
||||
medicineId: string;
|
||||
medicineName: string;
|
||||
medicineStrength: number;
|
||||
|
|
@ -22,58 +26,74 @@ type AnyRegimenMedication = {
|
|||
dosage: number;
|
||||
dosageUnit: string;
|
||||
frequency: string;
|
||||
customFrequencyPerDay?: number | null;
|
||||
timeOfDay?: string | null;
|
||||
instructions?: string | null;
|
||||
};
|
||||
customFrequencyPerDay?: number;
|
||||
timeOfDay?: string;
|
||||
instructions?: string;
|
||||
}
|
||||
|
||||
type AnyRegimenDoc = {
|
||||
_id: string | { toString: () => string };
|
||||
interface SerializedRegimenResponse {
|
||||
_id: string;
|
||||
householdId: string;
|
||||
userId: string;
|
||||
name: string;
|
||||
isActive: boolean;
|
||||
medications: AnyRegimenMedication[];
|
||||
medications: SerializedRegimenMedication[];
|
||||
createdBy: string;
|
||||
createdAt: string | { toISOString: () => string };
|
||||
updatedAt: string | { toISOString: () => string };
|
||||
};
|
||||
|
||||
function toStr(v: string | { toString: () => string }): string {
|
||||
return typeof v === 'string' ? v : v.toString();
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
function toIso(v: string | Date | { toISOString: () => string }): string {
|
||||
if (typeof v === 'string') return v;
|
||||
if (v instanceof Date) return v.toISOString();
|
||||
return v.toISOString();
|
||||
}
|
||||
function toRegimenResponse(doc: RegimenDocument): SerializedRegimenResponse {
|
||||
const docAny = doc as unknown as {
|
||||
createdAt?: { toISOString?: () => string } | string;
|
||||
updatedAt?: { toISOString?: () => string } | string;
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
function toRegimenResponse(doc: AnyRegimenDoc) {
|
||||
return {
|
||||
_id: toStr(doc._id),
|
||||
_id: doc._id.toString(),
|
||||
householdId: doc.householdId,
|
||||
userId: doc.userId,
|
||||
name: doc.name,
|
||||
isActive: doc.isActive,
|
||||
medications: doc.medications.map((med) => ({
|
||||
medicineId: med.medicineId,
|
||||
medicineName: med.medicineName,
|
||||
medicineStrength: med.medicineStrength,
|
||||
medicineStrengthUnit: med.medicineStrengthUnit,
|
||||
medicineForm: med.medicineForm,
|
||||
dosage: med.dosage,
|
||||
dosageUnit: med.dosageUnit,
|
||||
frequency: med.frequency,
|
||||
...(med.customFrequencyPerDay != null
|
||||
? { customFrequencyPerDay: med.customFrequencyPerDay }
|
||||
: {}),
|
||||
...(med.timeOfDay ? { timeOfDay: med.timeOfDay } : {}),
|
||||
...(med.instructions ? { instructions: med.instructions } : {}),
|
||||
})),
|
||||
medications: doc.medications.map((med) => {
|
||||
const medAny = med as unknown as {
|
||||
customFrequencyPerDay?: number | null;
|
||||
timeOfDay?: string | null;
|
||||
instructions?: string | null;
|
||||
};
|
||||
const response: SerializedRegimenMedication = {
|
||||
medicineId: med.medicineId,
|
||||
medicineName: med.medicineName,
|
||||
medicineStrength: med.medicineStrength,
|
||||
medicineStrengthUnit: med.medicineStrengthUnit,
|
||||
medicineForm: med.medicineForm,
|
||||
dosage: med.dosage,
|
||||
dosageUnit: med.dosageUnit,
|
||||
frequency: med.frequency,
|
||||
};
|
||||
|
||||
if (medAny.customFrequencyPerDay != null) {
|
||||
response.customFrequencyPerDay = medAny.customFrequencyPerDay;
|
||||
}
|
||||
if (medAny.timeOfDay) {
|
||||
response.timeOfDay = medAny.timeOfDay;
|
||||
}
|
||||
if (medAny.instructions) {
|
||||
response.instructions = medAny.instructions;
|
||||
}
|
||||
|
||||
return response;
|
||||
}),
|
||||
createdBy: doc.createdBy,
|
||||
createdAt: toIso(doc.createdAt),
|
||||
updatedAt: toIso(doc.updatedAt),
|
||||
createdAt: getIsoStr(docAny.createdAt),
|
||||
updatedAt: getIsoStr(docAny.updatedAt),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -105,11 +125,9 @@ export default fp(
|
|||
},
|
||||
handler: async (request, reply) => {
|
||||
const service = fastify.diContainer.resolve('regimensService');
|
||||
const result = await service.list(
|
||||
request.params.householdId,
|
||||
request.user.keycloakId,
|
||||
request.query,
|
||||
);
|
||||
const params = request.params as { householdId: string };
|
||||
const query = request.query as RegimenQueryInput;
|
||||
const result = await service.list(params.householdId, request.user.keycloakId, query);
|
||||
return reply.send({
|
||||
data: result.data.map(toRegimenResponse),
|
||||
pagination: result.pagination,
|
||||
|
|
@ -127,10 +145,8 @@ export default fp(
|
|||
},
|
||||
handler: async (request, reply) => {
|
||||
const service = fastify.diContainer.resolve('regimensService');
|
||||
const data = await service.calculateBurnRates(
|
||||
request.params.householdId,
|
||||
request.user.keycloakId,
|
||||
);
|
||||
const params = request.params as { householdId: string };
|
||||
const data = await service.calculateBurnRates(params.householdId, request.user.keycloakId);
|
||||
return reply.send({ data });
|
||||
},
|
||||
});
|
||||
|
|
@ -145,9 +161,10 @@ export default fp(
|
|||
},
|
||||
handler: async (request, reply) => {
|
||||
const service = fastify.diContainer.resolve('regimensService');
|
||||
const params = request.params as { householdId: string; id: string };
|
||||
const regimen = await service.getById(
|
||||
request.params.id,
|
||||
request.params.householdId,
|
||||
params.id,
|
||||
params.householdId,
|
||||
request.user.keycloakId,
|
||||
);
|
||||
return reply.send(toRegimenResponse(regimen));
|
||||
|
|
@ -165,11 +182,9 @@ export default fp(
|
|||
},
|
||||
handler: async (request, reply) => {
|
||||
const service = fastify.diContainer.resolve('regimensService');
|
||||
const regimen = await service.create(
|
||||
request.body,
|
||||
request.params.householdId,
|
||||
request.user.keycloakId,
|
||||
);
|
||||
const params = request.params as { householdId: string };
|
||||
const body = request.body as CreateRegimenInput;
|
||||
const regimen = await service.create(body, params.householdId, request.user.keycloakId);
|
||||
return reply.status(201).send(toRegimenResponse(regimen));
|
||||
},
|
||||
});
|
||||
|
|
@ -185,11 +200,13 @@ export default fp(
|
|||
},
|
||||
handler: async (request, reply) => {
|
||||
const service = fastify.diContainer.resolve('regimensService');
|
||||
const params = request.params as { householdId: string; id: string };
|
||||
const body = request.body as UpdateRegimenInput;
|
||||
const regimen = await service.update(
|
||||
request.params.id,
|
||||
request.params.householdId,
|
||||
params.id,
|
||||
params.householdId,
|
||||
request.user.keycloakId,
|
||||
request.body,
|
||||
body,
|
||||
);
|
||||
return reply.send(toRegimenResponse(regimen));
|
||||
},
|
||||
|
|
@ -205,11 +222,8 @@ export default fp(
|
|||
},
|
||||
handler: async (request, reply) => {
|
||||
const service = fastify.diContainer.resolve('regimensService');
|
||||
await service.delete(
|
||||
request.params.id,
|
||||
request.params.householdId,
|
||||
request.user.keycloakId,
|
||||
);
|
||||
const params = request.params as { householdId: string; id: string };
|
||||
await service.delete(params.id, params.householdId, request.user.keycloakId);
|
||||
return reply.status(204).send();
|
||||
},
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue