Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vcp 9 backend #2

Merged
merged 6 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions patient/src/api/patient.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
import PatientService from '../service/patient-service.js';
import { EMPTY_SIZE, NOT_FOUND_STATUS_CODE, OK_STATUS_CODE } from '../utils/Constants.js';
import {
EMPTY_SIZE,
NOT_FOUND_STATUS_CODE,
OK_STATUS_CODE,
} from '../utils/Constants.js';
import { isValidMongoId } from '../utils/Validation.js';

export const patient = ( app ) => {
export const patient = (app) => {
const service = new PatientService();

app.get( '/all-patients', async ( req,res ) => {
Mohamed-Khaled308 marked this conversation as resolved.
Show resolved Hide resolved
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' } );
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',
});
}
});
};
47 changes: 24 additions & 23 deletions patient/src/database/models/Patient.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,82 @@
import mongoose from 'mongoose';
import { GENDERS } from '../../utils/Constants.js';
import { GENDERS, FAMILY_RELATIONS } from '../../utils/Constants.js';

const Patient = mongoose.Schema( {
const Patient = mongoose.Schema({
name: {
type: String,
required: true
required: true,
},

userName: {
type: String,
required: true,
unique: true
unique: true,
},

email: {
type: String,
required: true,
unique: true
unique: true,
},

password: {
type: String,
required: true
required: true,
},

dateOfBirth: {
type: Date,
required: true
required: true,
},
gender: {
type: String,
enum: GENDERS,
required: true
required: true,
},
mobileNumber: {
type: String,
required: true
required: true,
},
emergencyContact: {
name: {
type: String,
required: true
required: true,
},
mobile: {
type: String,
required: true
}
required: true,
},
},
familyMembers: [
{
name: {
type: String,
required: true
required: true,
},
nationalId: {
type: String,
required: true,
unique: true
unique: true,
},
age: {
type: Number,
required: true
required: true,
},
gender: {
type: String,
enum: GENDERS,
required: true
required: true,
},
relation: {
type: String,
required: true
}
}
]
enum: FAMILY_RELATIONS,
required: true,
},
},
],
//.....
} );
});

const PatientModel = mongoose.model( 'Patient', Patient );
const PatientModel = mongoose.model('Patient', Patient);

export default PatientModel;
export default PatientModel;
12 changes: 9 additions & 3 deletions patient/src/database/repository/patient-repository.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
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;
}
}

export default PatientRepository;
export default PatientRepository;
7 changes: 5 additions & 2 deletions patient/src/service/patient-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import PatientRepository from '../database/repository/patient-repository.js';
class PatientService {
constructor() {
this.repository = new PatientRepository();

}

async getAllPatient() {
const patients = await this.repository.findAllPatients();
return patients;
}

async getFamilyMembers(id) {
const familyMembers = await this.repository.findFamilyMembers(id);
return familyMembers;
}
}

export default PatientService;
export default PatientService;
4 changes: 3 additions & 1 deletion patient/src/utils/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ export const OK_STATUS_CODE = 200;
export const NOT_FOUND_STATUS_CODE = 404;
export const ERROR_STATUS_CODE = 500;
export const EMPTY_SIZE = 0;
export const PORT_NUMBER = 8002;
export const PORT_NUMBER = 8002;
export const FAMILY_RELATIONS = ['WIFE', 'HUSBAND', 'CHILD'];
export const FAMILY_MEMBERS_PROJECTION = 'familyMembers';
5 changes: 5 additions & 0 deletions patient/src/utils/Validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import mongoose from 'mongoose';

export const isValidMongoId = (id) => {
return mongoose.Types.ObjectId.isValid(id);
};
Loading