From 0dfa048bd5ef7563128fe4d6b5adb64b5272f937 Mon Sep 17 00:00:00 2001 From: Mohamed Khaled <113786472+Mohamed-Khaled308@users.noreply.github.com> Date: Sat, 7 Oct 2023 18:24:36 +0300 Subject: [PATCH 1/3] apply eslint rules --- patient/src/api/patient.js | 58 ++++---- patient/src/database/models/Patient.js | 138 +++++++++--------- .../database/repository/patient-repository.js | 22 +-- patient/src/service/patient-service.js | 22 +-- patient/src/utils/Validation.js | 4 +- 5 files changed, 123 insertions(+), 121 deletions(-) diff --git a/patient/src/api/patient.js b/patient/src/api/patient.js index 90f5aac0..43d75965 100644 --- a/patient/src/api/patient.js +++ b/patient/src/api/patient.js @@ -1,37 +1,39 @@ import PatientService from '../service/patient-service.js'; import { - EMPTY_SIZE, - NOT_FOUND_STATUS_CODE, - OK_STATUS_CODE, + EMPTY_SIZE, + NOT_FOUND_STATUS_CODE, + OK_STATUS_CODE, } from '../utils/Constants.js'; import { isValidMongoId } from '../utils/Validation.js'; export const patient = (app) => { - const service = new PatientService(); + const service = new PatientService(); - app.get('/all-patients', async (req, res) => { - const allPatients = await service.getAllPatient(); - if (allPatients.length > EMPTY_SIZE) { - res.status(OK_STATUS_CODE).json(allPatients); - } else { - res.status(NOT_FOUND_STATUS_CODE).json({ - message: 'patients not found', - }); - } - }); + app.get('/all-patients', async (req, res) => { + const allPatients = await service.getAllPatient(); + if (allPatients.length > EMPTY_SIZE) { + res.status(OK_STATUS_CODE).json(allPatients); + } else { + res.status(NOT_FOUND_STATUS_CODE).json({ + message: 'patients not found', + }); + } + }); - app.get('/family-members/:id', async (req, res) => { - const { id } = req.params; - if (!isValidMongoId(id)) { - return res - .status(404) - .json({ message: 'family members not found' }); - } - try { - const data = await service.getFamilyMembers(id); - res.status(200).json(data); - } catch (err) { - res.status(404).json({ message: 'family members not found' }); - } - }); + app.get('/family-members/:id', async (req, res) => { + const { id } = req.params; + if (!isValidMongoId(id)) { + return res + .status(NOT_FOUND_STATUS_CODE) + .json({ message: 'family members not found' }); + } + try { + const data = await service.getFamilyMembers(id); + res.status(OK_STATUS_CODE).json(data); + } catch (err) { + res.status(NOT_FOUND_STATUS_CODE).json({ + message: 'family members not found', + }); + } + }); }; diff --git a/patient/src/database/models/Patient.js b/patient/src/database/models/Patient.js index 2f683dc9..9187df57 100644 --- a/patient/src/database/models/Patient.js +++ b/patient/src/database/models/Patient.js @@ -2,79 +2,79 @@ import mongoose from 'mongoose'; import { GENDERS, FAMILY_RELATIONS } from '../../utils/Constants.js'; const Patient = mongoose.Schema({ - name: { - type: String, - required: true, - }, + name: { + type: String, + required: true, + }, - userName: { - type: String, - required: true, - unique: true, - }, + userName: { + type: String, + required: true, + unique: true, + }, - email: { - type: String, - required: true, - unique: true, - }, + email: { + type: String, + required: true, + unique: true, + }, - password: { - type: String, - required: true, - }, + password: { + type: String, + required: true, + }, - dateOfBirth: { - type: Date, - required: true, - }, - gender: { - type: String, - enum: GENDERS, - required: true, - }, - mobileNumber: { - type: String, - required: true, - }, - emergencyContact: { - name: { - type: String, - required: true, - }, - mobile: { - type: String, - required: true, - }, - }, - familyMembers: [ - { - name: { - type: String, - required: true, - }, - nationalId: { - type: String, - required: true, - unique: true, - }, - age: { - type: Number, - required: true, - }, - gender: { - type: String, - enum: GENDERS, - required: true, - }, - relation: { - type: String, - enum: FAMILY_RELATIONS, - required: true, - }, - }, - ], - //..... + dateOfBirth: { + type: Date, + required: true, + }, + gender: { + type: String, + enum: GENDERS, + required: true, + }, + mobileNumber: { + type: String, + required: true, + }, + emergencyContact: { + name: { + type: String, + required: true, + }, + mobile: { + type: String, + required: true, + }, + }, + familyMembers: [ + { + name: { + type: String, + required: true, + }, + nationalId: { + type: String, + required: true, + unique: true, + }, + age: { + type: Number, + required: true, + }, + gender: { + type: String, + enum: GENDERS, + required: true, + }, + relation: { + type: String, + enum: FAMILY_RELATIONS, + required: true, + }, + }, + ], + //..... }); const PatientModel = mongoose.model('Patient', Patient); diff --git a/patient/src/database/repository/patient-repository.js b/patient/src/database/repository/patient-repository.js index 4250bc84..083efcd2 100644 --- a/patient/src/database/repository/patient-repository.js +++ b/patient/src/database/repository/patient-repository.js @@ -2,17 +2,17 @@ import PatientModel from '../models/Patient.js'; import { FAMILY_MEMBERS_PROJECTION } from '../../utils/Constants.js'; class PatientRepository { - async findAllPatients() { - const allPatients = await PatientModel.find(); - return allPatients; - } - async findFamilyMembers(id) { - const familyMembers = await PatientModel.findById( - id, - FAMILY_MEMBERS_PROJECTION - ); - return familyMembers; - } + async findAllPatients() { + const allPatients = await PatientModel.find(); + return allPatients; + } + async findFamilyMembers(id) { + const familyMembers = await PatientModel.findById( + id, + FAMILY_MEMBERS_PROJECTION + ); + return familyMembers; + } } export default PatientRepository; diff --git a/patient/src/service/patient-service.js b/patient/src/service/patient-service.js index 0bf15e39..c38f9787 100644 --- a/patient/src/service/patient-service.js +++ b/patient/src/service/patient-service.js @@ -1,19 +1,19 @@ import PatientRepository from '../database/repository/patient-repository.js'; class PatientService { - constructor() { - this.repository = new PatientRepository(); - } + constructor() { + this.repository = new PatientRepository(); + } - async getAllPatient() { - const patients = await this.repository.findAllPatients(); - return patients; - } + async getAllPatient() { + const patients = await this.repository.findAllPatients(); + return patients; + } - async getFamilyMembers(id) { - const familyMembers = await this.repository.findFamilyMembers(id); - return familyMembers; - } + async getFamilyMembers(id) { + const familyMembers = await this.repository.findFamilyMembers(id); + return familyMembers; + } } export default PatientService; diff --git a/patient/src/utils/Validation.js b/patient/src/utils/Validation.js index e1293bf1..afa83b65 100644 --- a/patient/src/utils/Validation.js +++ b/patient/src/utils/Validation.js @@ -1,5 +1,5 @@ -import mongoose from "mongoose"; +import mongoose from 'mongoose'; export const isValidMongoId = (id) => { - return mongoose.Types.ObjectId.isValid(id); + return mongoose.Types.ObjectId.isValid(id); }; From a0ef9b5183ae31743496b4ce466ee9e107798621 Mon Sep 17 00:00:00 2001 From: Mohamed Khaled <113786472+Mohamed-Khaled308@users.noreply.github.com> Date: Sun, 8 Oct 2023 01:05:17 +0300 Subject: [PATCH 2/3] following rest api conventions --- patient/src/api/patient.js | 60 +++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/patient/src/api/patient.js b/patient/src/api/patient.js index 43d75965..37c76f26 100644 --- a/patient/src/api/patient.js +++ b/patient/src/api/patient.js @@ -1,39 +1,39 @@ import PatientService from '../service/patient-service.js'; import { - EMPTY_SIZE, - NOT_FOUND_STATUS_CODE, - OK_STATUS_CODE, + EMPTY_SIZE, + NOT_FOUND_STATUS_CODE, + OK_STATUS_CODE, } from '../utils/Constants.js'; import { isValidMongoId } from '../utils/Validation.js'; export const patient = (app) => { - const service = new PatientService(); + const service = new PatientService(); - app.get('/all-patients', async (req, res) => { - const allPatients = await service.getAllPatient(); - if (allPatients.length > EMPTY_SIZE) { - res.status(OK_STATUS_CODE).json(allPatients); - } else { - res.status(NOT_FOUND_STATUS_CODE).json({ - message: 'patients not found', - }); - } - }); + app.get('/patients', async (req, res) => { + const allPatients = await service.getAllPatient(); + if (allPatients.length > EMPTY_SIZE) { + res.status(OK_STATUS_CODE).json(allPatients); + } else { + res.status(NOT_FOUND_STATUS_CODE).json({ + message: 'patients not found', + }); + } + }); - app.get('/family-members/:id', async (req, res) => { - const { id } = req.params; - if (!isValidMongoId(id)) { - return res - .status(NOT_FOUND_STATUS_CODE) - .json({ message: 'family members not found' }); - } - try { - const data = await service.getFamilyMembers(id); - res.status(OK_STATUS_CODE).json(data); - } catch (err) { - res.status(NOT_FOUND_STATUS_CODE).json({ - message: 'family members not found', - }); - } - }); + app.get('/family-members/:id', async (req, res) => { + const { id } = req.params; + if (!isValidMongoId(id)) { + return res + .status(NOT_FOUND_STATUS_CODE) + .json({ message: 'family members not found' }); + } + try { + const data = await service.getFamilyMembers(id); + res.status(OK_STATUS_CODE).json(data); + } catch (err) { + res.status(NOT_FOUND_STATUS_CODE).json({ + message: 'family members not found', + }); + } + }); }; From 189abe5b07e8f4e8bc8f0502d6733ef29b4b88dc Mon Sep 17 00:00:00 2001 From: Mohamed Khaled <113786472+Mohamed-Khaled308@users.noreply.github.com> Date: Sun, 8 Oct 2023 15:05:34 +0300 Subject: [PATCH 3/3] solving style error --- .../database/repository/patient-repository.js | 36 ++++++++-------- patient/src/service/patient-service.js | 43 ++++++++----------- patient/src/utils/Validation.js | 3 +- 3 files changed, 37 insertions(+), 45 deletions(-) diff --git a/patient/src/database/repository/patient-repository.js b/patient/src/database/repository/patient-repository.js index f8a03ff5..1c80d3a1 100644 --- a/patient/src/database/repository/patient-repository.js +++ b/patient/src/database/repository/patient-repository.js @@ -2,25 +2,25 @@ import PatientModel from '../models/Patient.js'; import { FAMILY_MEMBERS_PROJECTION } from '../../utils/Constants.js'; class PatientRepository { - async findAllPatients() { - const allPatients = await PatientModel.find(); - return allPatients; - } - async findFamilyMembers(id) { - const familyMembers = await PatientModel.findById( - id, - FAMILY_MEMBERS_PROJECTION - ); - return familyMembers; - } + async findAllPatients() { + const allPatients = await PatientModel.find(); + return allPatients; + } + async findFamilyMembers(id) { + const familyMembers = await PatientModel.findById( + id, + FAMILY_MEMBERS_PROJECTION + ); + return familyMembers; + } - async addFamilyMember(id, familyMembers) { - return await PatientModel.findOneAndUpdate( - { _id: id }, - { familyMembers }, - { new: true, runValidators: true } - ).select(FAMILY_MEMBERS_PROJECTION); - } + async addFamilyMember(id, familyMembers) { + return await PatientModel.findOneAndUpdate( + { _id: id }, + { familyMembers }, + { new: true, runValidators: true } + ).select(FAMILY_MEMBERS_PROJECTION); + } } export default PatientRepository; diff --git a/patient/src/service/patient-service.js b/patient/src/service/patient-service.js index f6ff41cf..7212c68f 100644 --- a/patient/src/service/patient-service.js +++ b/patient/src/service/patient-service.js @@ -1,34 +1,27 @@ import PatientRepository from '../database/repository/patient-repository.js'; class PatientService { - constructor() { - this.repository = new PatientRepository(); - } - constructor() { - this.repository = new PatientRepository(); - } + constructor() { + this.repository = new PatientRepository(); + } - async getAllPatient() { - const patients = await this.repository.findAllPatients(); - return patients; - } - async getAllPatient() { - const patients = await this.repository.findAllPatients(); - return patients; - } + async getAllPatient() { + const patients = await this.repository.findAllPatients(); + return patients; + } - async getFamilyMembers(id) { - const familyMembers = await this.repository.findFamilyMembers(id); - return familyMembers; - } + async getFamilyMembers(id) { + const familyMembers = await this.repository.findFamilyMembers(id); + return familyMembers; + } - async addFamilyMember(id, updates) { - const familyMembers = await this.repository.addFamilyMember( - id, - updates - ); - return familyMembers; - } + async addFamilyMember(id, updates) { + const familyMembers = await this.repository.addFamilyMember( + id, + updates + ); + return familyMembers; + } } export default PatientService; diff --git a/patient/src/utils/Validation.js b/patient/src/utils/Validation.js index c2990fdf..afa83b65 100644 --- a/patient/src/utils/Validation.js +++ b/patient/src/utils/Validation.js @@ -1,6 +1,5 @@ import mongoose from 'mongoose'; -import mongoose from 'mongoose'; export const isValidMongoId = (id) => { - return mongoose.Types.ObjectId.isValid(id); + return mongoose.Types.ObjectId.isValid(id); };