Skip to content

Commit

Permalink
Merge branch 'main' into chat-ui-improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmadHoseiny committed Dec 16, 2023
2 parents 57385bf + 4c2f859 commit 3d264c2
Show file tree
Hide file tree
Showing 125 changed files with 5,118 additions and 9,595 deletions.
2 changes: 1 addition & 1 deletion authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "app.js",
"scripts": {
"dev": "nodemon start.js",
"test": "jest --runInBand",
"test": "jest -i",
"lint": "eslint . --fix"
},
"author": "",
Expand Down
3 changes: 2 additions & 1 deletion authentication/src/api/resetPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const resetPassword = (app) => {

try {
const userRecord = await user.findUserByEmail(email);
if(!userRecord ){
if(!userRecord){
throw new Error('invalid user in the system');
}

Expand Down Expand Up @@ -53,6 +53,7 @@ export const resetPassword = (app) => {

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
res.status(500).json({ message: 'Failed to send email' });
} else {
res.json({ message: 'Email sent' });
Expand Down
185 changes: 108 additions & 77 deletions authentication/src/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BAD_REQUEST_CODE_400,
CLINIC_ADMIN_ENUM,
CLINIC_REQ,
COMMUNICATION_USER_POST_URL,
DOCOTOR_CHECK_DOC_USERS,
DOCOTOR_SIGNUP_URL,
DOCTOR_ENUM,
Expand All @@ -19,6 +20,7 @@ import {
PATIENT_SIGNUP_URL,
PHARMACIST_BASE_URL,
PHARMACIST_ENUM,
PHARMACIST_SIGNUP_URL,
PHARMACY_ADMIN_ENUM,
PHARMACY_REQ,
SERVER_ERROR_REQUEST_CODE_500,
Expand All @@ -29,7 +31,7 @@ import ResetPasswordService from '../service/reset-password-service.js';


export const user = (app) => {
const user = new UserService();
const service = new UserService();
const resetPassword = new ResetPasswordService();

const createToken = (id, userName, type) => {
Expand All @@ -38,7 +40,7 @@ export const user = (app) => {
});
};



app.post('/signup/:request', async (req, res) => {
try {
Expand All @@ -49,47 +51,55 @@ export const user = (app) => {
let email = null;
let userName = null;
switch (type) {
case PATIENT_ENUM:
email = req.body.email;
userName = req.body.userName;
break;
case DOCTOR_ENUM: case PHARMACIST_ENUM:
email = req.body.userData.email;
userName = req.body.userData.userName;
break;
default:
throw new Error('invalid type of user');
case PATIENT_ENUM:
email = req.body.email;
userName = req.body.userName;
break;
case DOCTOR_ENUM:case PHARMACIST_ENUM:
email = req.body.userData.email;
userName = req.body.userData.userName;
break;
default:
throw new Error('invalid type of user');
}

const checkEmail = await user.findUserByEmail(email);
const checkEmail = await service.findUserByEmail(email);
if (checkEmail) {
throw new Error(DUB_EMAIL_ERROR_MESSAGE);
}

const checkUserName = await user.findUserByUserName(userName);
const checkUserName = await service.findUserByUserName(userName);
if (checkUserName) {
throw new Error(DUB_USERNAME_ERROR_MESSAGE);
}
switch (requestFrom) {
case CLINIC_REQ: await axios.post(DOCOTOR_CHECK_DOC_USERS, { email, userName }); break;
case PHARMACY_REQ: await axios.post(`${PHARMACIST_BASE_URL}check-pharmacist-req`, { email, userName }); break;
default: throw new Error('invalid system');
} // <= here same
switch(requestFrom){
case CLINIC_REQ:await axios.post(DOCOTOR_CHECK_DOC_USERS, { email, userName });break;
case PHARMACY_REQ:await axios.post(`${PHARMACIST_BASE_URL}check-pharmacist-req`, { email, userName });break;
default: throw new Error('invalid system');
}

// TODO: check if i put the docotor/ pharma request
switch (type) {
case PATIENT_ENUM:
signupData = await axios.post(PATIENT_SIGNUP_URL, req.body);
break;
case DOCTOR_ENUM:
signupData = await axios.post(DOCOTOR_SIGNUP_URL, req.body);
break;
case PHARMACIST_ENUM:
signupData = await axios.post(PHARMACIST_SIGNUP_URL, req.body);
break;
default:
throw new Error('invalid type of user');
}

if (type == PATIENT_ENUM) {
signupData = await axios.post(PATIENT_SIGNUP_URL, req.body);
await user.signupUser(signupData.data);
const userId = signupData.data.userId;
await axios.post(`${COMMUNICATION_USER_POST_URL}/${userId}`);
await service.signupUser(signupData.data);
}

res.status(OK_REQUEST_CODE_200).end();
} catch (err) {
if (err.response) {
if (err.response.data.errCode == DUPLICATE_KEY_ERROR_CODE) {
res
.status(BAD_REQUEST_CODE_400)
.send({ message: err.response.data.errMessage });
} else
res
.status(BAD_REQUEST_CODE_400)
.send({ message: err.response.data.errMessage });
Expand All @@ -105,25 +115,27 @@ export const user = (app) => {
app.delete('/users/:id', async (req, res) => {
try {
const userId = req.params.id;
await user.deleteUser(userId);
await service.deleteUser(userId);
await axios.delete(`${COMMUNICATION_USER_POST_URL}/${userId}`);

res.status(OK_REQUEST_CODE_200).end();
} catch (err) {
console.log(err);
res
.status(SERVER_ERROR_REQUEST_CODE_500)
.send({ message: 'coudn\'t delete the user' });
}
});


app.patch('/users/:id/email/:email', async (req, res) => {
try {
const id = req.params.id;
const email = req.params.email;
const systemUser = await user.findUserByEmail(email);
const systemUser = await service.findUserByEmail(email);
if (systemUser) {
res.status(BAD_REQUEST_CODE_400).send({ errCode: DUPLICATE_KEY_ERROR_CODE, message: "this email is already exist in the system" });
} else {
const systemUser = await user.updateEmail(id, email);
const systemUser = await service.updateEmail(id, email);
//TODO: check the way of update akw
if (systemUser)
res.status(OK_REQUEST_CODE_200).end();
Expand All @@ -137,7 +149,9 @@ export const user = (app) => {

app.post('/doctors', async (req, res) => {
try {
await user.signupUser(req.body);
const userId = req.body.userId;
await axios.post(`${COMMUNICATION_USER_POST_URL}/${userId}`);
await service.signupUser(req.body);
res.status(OK_REQUEST_CODE_200).end();
} catch (err) {
console.log(err.message);
Expand All @@ -149,7 +163,10 @@ export const user = (app) => {

app.post('/pharmacists', async (req, res) => {
try {
await user.signupUser(req.body);
const userId = req.body.userId;
await axios.post(`${COMMUNICATION_USER_POST_URL}/${userId}`);
await axios.post(`${PHARMACIST_BASE_URL}archive/${userId}`);
await service.signupUser(req.body);
res.status(OK_REQUEST_CODE_200).end();
} catch (err) {
console.log(err.message);
Expand All @@ -163,26 +180,38 @@ export const user = (app) => {
try {
const requestFrom = req.params.request; // clinic, pharmacy
const userName = req.body.userName;
const email = req.body.email;

const checkUserName = await user.findUserByUserName(userName);
const checkUserName = await service.findUserByUserName(userName);
if (checkUserName) {
throw new Error(DUB_USERNAME_ERROR_MESSAGE);
}

await axios.post(DOCOTOR_CHECK_DOC_USERS, { userName });
await axios.post(`${PHARMACIST_BASE_URL}check-pharmacist-req`, { userName });
const checkEmail = await service.findUserByEmail(email);
if (checkEmail) {
throw new Error(DUB_EMAIL_ERROR_MESSAGE);
}

let signupData = null;
switch (requestFrom) {
case CLINIC_REQ: signupData = await axios.post(ADMIN_Clinic_SIGNUP_URL, req.body); break;
case PHARMACY_REQ: signupData = await axios.post(ADMIN_Pharmacy_SIGNUP_URL, req.body); break;
case CLINIC_REQ: await axios.post(DOCOTOR_CHECK_DOC_USERS, { email, userName }); break;
case PHARMACY_REQ: await axios.post(`${PHARMACIST_BASE_URL}check-pharmacist-req`, { email, userName }); break;
default: throw new Error('invalid system');
}

await user.signupUser(signupData.data);
let signupData = null;
switch(requestFrom){
case CLINIC_REQ: signupData = await axios.post(ADMIN_Clinic_SIGNUP_URL, req.body);break;
case PHARMACY_REQ: signupData = await axios.post(ADMIN_Pharmacy_SIGNUP_URL, req.body);break;
default: throw new Error('invalid system');
}

const userId = signupData.data.userId;
await axios.post(`${COMMUNICATION_USER_POST_URL}/${userId}`);
await service.signupUser(signupData.data);

res.status(OK_REQUEST_CODE_200).send({ message: 'admin added' });
} catch (err) {
console.log(err);
if (err.response) {
// coming from other services
if (err.response.data.errCode == DUPLICATE_KEY_ERROR_CODE) {
Expand All @@ -200,7 +229,7 @@ export const user = (app) => {
}
});

const sendUserToken = (logedinUser, res, reset) => {
const sendUserToken = (logedinUser, res, reset) =>{
const token = createToken(
logedinUser.userId,
logedinUser.userName,
Expand All @@ -213,55 +242,58 @@ export const user = (app) => {
res.send({
id: logedinUser.userId,
userName: logedinUser.userName,
type: (logedinUser.type == PHARMACY_ADMIN_ENUM || logedinUser.type == CLINIC_ADMIN_ENUM) ? ADMIN_FRONT_ENUM : logedinUser.type,
reset: reset
type: (logedinUser.type == PHARMACY_ADMIN_ENUM || logedinUser.type == CLINIC_ADMIN_ENUM) ?ADMIN_FRONT_ENUM:logedinUser.type,
reset:reset
});
}
app.post('/login/:request', async (req, res) => {
try {
const requestFrom = req.params.request;
const logedinUser = await user.loginUser(req);
switch (requestFrom) {
case CLINIC_REQ: if (logedinUser.type == PHARMACIST_ENUM || logedinUser.type == PHARMACY_ADMIN_ENUM) throw new Error('invalid user'); break; //TODO: admin in login
case PHARMACY_REQ: if (logedinUser.type == DOCTOR_ENUM || logedinUser.type == CLINIC_ADMIN_ENUM) throw new Error('invalid user'); break; //TODO: admin in login
default: throw new Error('invalid system');
const requestFrom = req.params.request;
const logedinUser = await service.loginUser(req);
switch(requestFrom){
case CLINIC_REQ: if (logedinUser.type == PHARMACIST_ENUM || logedinUser.type == PHARMACY_ADMIN_ENUM) throw new Error('invalid user'); break; //TODO: admin in login
case PHARMACY_REQ: if (logedinUser.type == DOCTOR_ENUM || logedinUser.type == CLINIC_ADMIN_ENUM) throw new Error('invalid user'); break; //TODO: admin in login
default: throw new Error('invalid system');
}
if (logedinUser.type == PHARMACY_ADMIN_ENUM || logedinUser.type == CLINIC_ADMIN_ENUM) {
if(logedinUser.type == PHARMACY_ADMIN_ENUM || logedinUser.type == CLINIC_ADMIN_ENUM){
logedinUser.type = ADMIN_FRONT_ENUM
}
sendUserToken(logedinUser, res, false)
} catch (err) {
if (err.message == 'incorrect Password') {
// access reset service
const userData = await user.findUserByUserName(req.body.userName);
if (userData.email) {
if(err.message == 'incorrect Password'){
const userData = await service.findUserByUserName(req.body.userName);
if(userData.email){
const resetUser = await resetPassword.getRecordByEmail(userData.email);
if (!resetUser || new Date() > new Date(resetUser.resetTokenExpiration)) {
if(!resetUser || new Date() > new Date(resetUser.resetTokenExpiration)){
await resetPassword.removeRecordByEmail(userData.email);
res.status(BAD_REQUEST_CODE_400).send({ message: err.message });
} else{
if(req.body.password == resetUser.OTP){
await resetPassword.removeRecordByEmail(userData.email);
res.status(BAD_REQUEST_CODE_400).send({ message: err.message });
} else {
if (req.body.password == resetUser.OTP) {
await resetPassword.removeRecordByEmail(userData.email);
//TODO: let him
sendUserToken(userData, res, true)
}
else
res.status(BAD_REQUEST_CODE_400).send({ message: err.message });
console.log('err: ', err.message);

sendUserToken(userData, res, true)
}
} else {
res.status(BAD_REQUEST_CODE_400).send({ message: err.message });
console.log('err: ', err.message);

else
res.status(BAD_REQUEST_CODE_400).send({ message: err.message });
}
} else{
res.status(BAD_REQUEST_CODE_400).send({ message: err.message });
}
}
else
res.status(BAD_REQUEST_CODE_400).send({ message: err.message });
console.log('err: ', err.message);
}
});

app.get('/user/:id/email', async (req, res) => {
try{
const id = req.params.id;
const email = await service.getuserEmail(id);
res.send(email);
} catch (err){
res.status(SERVER_ERROR_REQUEST_CODE_500).send({ message:err.message });
}
})

app.get('/check-user', async (req, res) => {
const token = req.cookies.jwt;
if (token) {
Expand All @@ -282,20 +314,19 @@ export const user = (app) => {
});

app.patch('/change-password/:userId', async (req, res) => {
try {
try{
const userId = req.params.userId;
const { password } = req.body;
await user.updatePassword(userId, password);
await service.updatePassword(userId, password);
res.status(OK_REQUEST_CODE_200).end();
} catch (err) {
} catch (err){
console.log(err);
res.status(SERVER_ERROR_REQUEST_CODE_500).send({ message: err.message });
res.status(SERVER_ERROR_REQUEST_CODE_500).send({ message:err.message });
}
})

app.get('/remove-cookie', (req, res) => {
res.cookie('jwt', '', { expires: new Date(0), path: '/' });
res.status(200).end();
});

};
2 changes: 2 additions & 0 deletions authentication/src/database/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const userSchema = mongoose.Schema({
},
email:{
type:String,
required:true,
unique: true,
},
userName:{
type:String,
Expand Down
Loading

0 comments on commit 3d264c2

Please sign in to comment.