generated from moevm/nsql-clean-tempate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from moevm/all-endpoints
All endpoints
- Loading branch information
Showing
7 changed files
with
105 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,28 @@ | ||
import express from "express"; | ||
import { patientService } from "../services/PatientsService"; | ||
|
||
export class PatientController { | ||
static async getPatientInfo(req: express.Request, res: express.Response) { | ||
static async getPatientById(req: express.Request, res: express.Response) { | ||
try { | ||
const patient = await patientService.getPatientById(req.params.id) | ||
|
||
res.json(patient) | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
res.status(400).send(error.message) | ||
} | ||
} | ||
} | ||
|
||
static async getAllPatients(req: express.Request, res: express.Response) { | ||
try { | ||
const patient = await patientService.getAllPatients() | ||
|
||
res.json(patient) | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
res.status(400).send(error.message) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,9 +66,9 @@ class DataInitializer { | |
const patientIds: Types.ObjectId[] = patientUsers.map(patient => patient._id!) as Types.ObjectId[]; | ||
|
||
const testData: PatientDto[] = [ | ||
{ _id: patientIds[0], name: "Максим", surname: "Тишкин", email: "[email protected]", birthdate: "22.08.2002", contact: "987654321", bloodgroup: "A+" }, | ||
{ _id: patientIds[1], name: "Саша", surname: "Морозов", email: "[email protected]", birthdate: "22.08.2002", contact: "987654321", bloodgroup: "A+" }, | ||
{ _id: patientIds[2], name: "Миша", surname: "Переверза", email: "[email protected]", birthdate: "22.08.2002", contact: "987654321", bloodgroup: "A+" }, | ||
{ _id: patientIds[0], name: "Максим", surname: "Тишкин", email: "[email protected]", birthdate: "22.08.2002", contact: "987654321", bloodgroup: "A+", card: "0001" }, | ||
{ _id: patientIds[1], name: "Саша", surname: "Морозов", email: "[email protected]", birthdate: "22.08.2002", contact: "987654321", bloodgroup: "A+", card: "0002" }, | ||
{ _id: patientIds[2], name: "Миша", surname: "Переверза", email: "[email protected]", birthdate: "22.08.2002", contact: "987654321", bloodgroup: "A+", card: "0003" }, | ||
]; | ||
|
||
await PatientsMongoCollection.insertMany(testData); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import mongoose from "mongoose" | ||
import { PatientsMongoCollection } from "../models/mongoose/PatientModel" | ||
import { AppoitmentMongoCollection } from "../models/mongoose/AppoitmentModel" | ||
import { ProceduresMongoCollection } from "../models/mongoose/ProcedureModel" | ||
import { DoctorMongoCollection } from "../models/mongoose/DoctorModel" | ||
|
||
class PatientService { | ||
public async getPatientById(id: string) { | ||
const objId = new mongoose.Types.ObjectId(id) | ||
const patient = await PatientsMongoCollection.findById(objId) | ||
|
||
const appoitments = await AppoitmentMongoCollection.find({patient_id: objId}) | ||
|
||
let myAppoitments: { [key: string]: any }[] = []; | ||
|
||
for(const appoitment of appoitments) { | ||
const procedure = await ProceduresMongoCollection.findOne({_id: appoitment?.procedure_id}) | ||
const doctor = await DoctorMongoCollection.findOne({_id: appoitment?.doctor_id}) | ||
const date = appoitment?.date | ||
const appointmentDetails = { | ||
"date": date, | ||
"procedure": procedure?.name, | ||
"doctor": doctor?.name + " " + doctor?.surname | ||
}; | ||
console.log(appointmentDetails); | ||
|
||
myAppoitments.push(appointmentDetails); | ||
} | ||
console.log({ "name": patient?.name, "surname": patient?.surname, "appoitments": myAppoitments}); | ||
|
||
return { "name": patient?.name, "surname": patient?.surname, "appoitments": myAppoitments} | ||
} | ||
|
||
public async getAllPatients() { | ||
const patients = await PatientsMongoCollection.find({}) | ||
console.log(patients); | ||
|
||
const prettyPatients: { [key: string]: any }[] = [] | ||
patients.forEach(patient => { | ||
const name = patient.name + " " + patient.surname | ||
const age = this.calculateAge(patient.birthdate) | ||
const card = patient.card | ||
const phone = patient.contact | ||
prettyPatients.push({ | ||
"name": name, | ||
"age": age, | ||
"card": card, | ||
"phone": phone | ||
}) | ||
}); | ||
console.log(prettyPatients); | ||
|
||
return prettyPatients | ||
} | ||
|
||
private calculateAge(date: string): number { | ||
const [day, month, year] = date.split(".") | ||
const birthdate = new Date(`${year}-${month}-${day}`) | ||
const currentDate = new Date() | ||
const timediff = currentDate.getTime() - birthdate.getTime() | ||
const age = Math.floor(timediff / (365.25 * 24 * 60 * 60 * 1000)); | ||
return age | ||
|
||
} | ||
} | ||
|
||
export const patientService = new PatientService() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters