Skip to content

Commit

Permalink
feat: antecedentes endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloose committed Nov 16, 2023
1 parent 148be8e commit 54565af
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 4 deletions.
59 changes: 57 additions & 2 deletions src/router/patient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ router.put(
}[]
*/

const medicalAntecedentSchema = z.object({
const medicalAntecedentResponseSchema = z.object({
id: z.number(),
name: z.string(),
value: z.string(),
Expand All @@ -535,7 +535,62 @@ router.get(
description: `Obtiene los antecedentes de un paciente. Los antecedentes no están atados a ninguna cita. En caso de no haber, devuelve un array vacío.`,
responses: {
200: {
schema: z.array(medicalAntecedentSchema),
schema: z.array(medicalAntecedentResponseSchema),
},
},
}),
);

router.post(
"/:id/antecedentes",
authMiddleware,
async (req, res) => {
const { medicalAntecedent, error, status } =
await appointmentService.createMedicalAntecedent(
Number(req.params.id),
req.body,
);
if (error) {
res.status(status).send({
error: error,
});
return;
}
res.send(medicalAntecedent);
},
docs({
description: `Crea un nuevo antecedente para el paciente {id}.`,
body: medicalAntecedentResponseSchema,
responses: {
200: {
schema: medicalAntecedentResponseSchema,
},
},
}),
);

router.delete(
"/:id/antecedentes/:aid",
authMiddleware,
async (req, res) => {
const { medicalAntecedent, error, status } =
await appointmentService.deleteMedicalAntecedent(
Number(req.params.id),
Number(req.params.aid),
);
if (error) {
res.status(status).send({
error: error,
});
return;
}
res.send(medicalAntecedent);
},
docs({
description: `Elimina el antecedente {aid} del paciente {id}.`,
responses: {
200: {
schema: medicalAntecedentResponseSchema,
},
},
}),
Expand Down
4 changes: 2 additions & 2 deletions src/schemas/appointment.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export const AppointmentSchema = z.object({
});

export const MedicalAntecedentSchema = z.object({
clave: z.string(),
valor: z.string(),
name: z.string(),
value: z.string(),
});

export const idSchema = z.number().min(1).int();
Expand Down
63 changes: 63 additions & 0 deletions src/services/appointment.services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { prisma } from "../db";
import {
MedicalAntecedentSchema,
PhysicalExplorationSchema,
VitalSignsSchema,
WorkPlanSchema,
Expand Down Expand Up @@ -497,4 +498,66 @@ export class AppointmentService {
};
}
}

async createMedicalAntecedent(idPatient: number, data: object) {
try {
const validationResult = MedicalAntecedentSchema.safeParse(data);
if (!validationResult.success) {
return {
error: validationResult.error.formErrors,
medicalAntecedent: null,
status: 400,
};
}
const result = await prisma.medicalAntecedent.create({
data: {
patient: {
connect: {
id: idPatient,
},
},
...validationResult.data,
},
});

return {
error: null,
medicalAntecedent: result,
status: 201,
};
} catch (error) {
console.log(error);
return {
medicalAntecedent: null,
error: "something went wrong!",
status: 500,
};
}
}

async deleteMedicalAntecedent(idPatient: number, idAntecedent: number) {
try {
const result = await prisma.medicalAntecedent.delete({
where: {
id: idAntecedent,
patient: {
id: idPatient,
},
},
});

return {
error: null,
medicalAntecedent: result,
status: 200,
};
} catch (error) {
console.log(error);
return {
medicalAntecedent: null,
error: "something went wrong!",
status: 500,
};
}
}
}

0 comments on commit 54565af

Please sign in to comment.