From 56d5499d4be2bdaf4ea68b346cfd47048ceb8ff2 Mon Sep 17 00:00:00 2001 From: Abdelrahman Elmeky <65960126+Aelmeky@users.noreply.github.com> Date: Mon, 1 Jan 2024 21:01:55 +0200 Subject: [PATCH 1/4] refactor : adding the payment service logic in the service file --- payment/src/api/PaymentAPI.js | 15 ++++----------- payment/src/service/payment-service.js | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 payment/src/service/payment-service.js diff --git a/payment/src/api/PaymentAPI.js b/payment/src/api/PaymentAPI.js index 58f45ad1..98c12a15 100644 --- a/payment/src/api/PaymentAPI.js +++ b/payment/src/api/PaymentAPI.js @@ -1,4 +1,3 @@ -import Stripe from 'stripe'; import axios from 'axios'; import { @@ -10,21 +9,17 @@ import { SECRET_KEY } from '../utils/Constants.js'; import { isValidMongoId } from '../utils/Validation.js'; +import PaymentService from '../service/payment-service.js'; -const stripe = new Stripe(SECRET_KEY); export const payment = (app) => { + const service = new PaymentService(); + app.post('/payment/card', async (req, res) => { try{ const total_amount = Number(req.body.paymentAmount); - const paymentIntent = await stripe.paymentIntents.create({ - amount: parseInt(total_amount * 100), - currency: "usd", - automatic_payment_methods: { - enabled: true, - }, - }); + const paymentIntent = service.createPaymentIntent(total_amount); res.status(OK_STATUS_CODE).send({ clientSecret: paymentIntent.client_secret, }); @@ -61,11 +56,9 @@ export const payment = (app) => { }); } const walletChange = parseFloat(req.body.pricePaidToDoctor); - console.log('walletChange = ', walletChange); const axiosRes = await axios.patch(`${CLINIC_BASE_URL}/doctors/${doctorId}/wallet`, { walletChange }); - // console.log('axiosRes = ', axiosRes); res.status(OK_STATUS_CODE).json({ updatedDoctor: axiosRes.data.updatedDoctor }); } catch(err){ diff --git a/payment/src/service/payment-service.js b/payment/src/service/payment-service.js new file mode 100644 index 00000000..aa9d834f --- /dev/null +++ b/payment/src/service/payment-service.js @@ -0,0 +1,21 @@ +import Stripe from 'stripe'; +import { + SECRET_KEY +} from '../utils/Constants.js'; + +const stripe = new Stripe(SECRET_KEY); + +class PaymentService{ + + async createPaymentIntent(total_amount){ + await stripe.paymentIntents.create({ + amount: parseInt(total_amount * 100), + currency: "usd", + automatic_payment_methods: { + enabled: true, + }, + }); + } +} + +export default PaymentService; \ No newline at end of file From ea19d1f243c1014a43048fe8034751e7be105afd Mon Sep 17 00:00:00 2001 From: Abdelrahman Elmeky <65960126+Aelmeky@users.noreply.github.com> Date: Mon, 1 Jan 2024 21:04:59 +0200 Subject: [PATCH 2/4] refactor : removing unnessasry imports --- payment/src/api/PaymentAPI.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/payment/src/api/PaymentAPI.js b/payment/src/api/PaymentAPI.js index 98c12a15..67b9af82 100644 --- a/payment/src/api/PaymentAPI.js +++ b/payment/src/api/PaymentAPI.js @@ -5,8 +5,7 @@ import { ERROR_STATUS_CODE, CLINIC_BASE_URL, PATIENTS_BASE_URL, - BAD_REQUEST_CODE_400, - SECRET_KEY + BAD_REQUEST_CODE_400 } from '../utils/Constants.js'; import { isValidMongoId } from '../utils/Validation.js'; import PaymentService from '../service/payment-service.js'; From e929f463b36e0c9096fc963de5812af94e91ae41 Mon Sep 17 00:00:00 2001 From: Yehia Mohamed <102627389+YehiaFarghaly@users.noreply.github.com> Date: Tue, 2 Jan 2024 02:49:59 +0200 Subject: [PATCH 3/4] style: change filter style --- .../MainLayout/Header/SearchSection/FilterDropdown.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/client/src/layout/MainLayout/Header/SearchSection/FilterDropdown.js b/client/src/layout/MainLayout/Header/SearchSection/FilterDropdown.js index 9f6dd35d..e88498fe 100644 --- a/client/src/layout/MainLayout/Header/SearchSection/FilterDropdown.js +++ b/client/src/layout/MainLayout/Header/SearchSection/FilterDropdown.js @@ -25,13 +25,17 @@ const FilterDropdown = ({ filterData, onChange, isDrawerOpen, handleDrawerClose setActiveFilterIndex((prevIndex) => (prevIndex + 1) % filterData.length); } }; - return ( {filterData.length > 0 ? ( From 9d388d5646205e0c251f3eed6e92ba41646d64b1 Mon Sep 17 00:00:00 2001 From: Yehia Mohamed <102627389+YehiaFarghaly@users.noreply.github.com> Date: Tue, 2 Jan 2024 03:41:43 +0200 Subject: [PATCH 4/4] edit authentication tests --- .../tests/api-tests/AuthenticationAPI.test.js | 198 ++++++++++-------- authentication/src/utils/TestingUtils.js | 13 +- 2 files changed, 123 insertions(+), 88 deletions(-) diff --git a/authentication/src/tests/api-tests/AuthenticationAPI.test.js b/authentication/src/tests/api-tests/AuthenticationAPI.test.js index a97b52a5..7081045a 100644 --- a/authentication/src/tests/api-tests/AuthenticationAPI.test.js +++ b/authentication/src/tests/api-tests/AuthenticationAPI.test.js @@ -1,11 +1,12 @@ import request from 'supertest'; import app from '../../../app.js'; -import { - connectDBTest, - disconnectDBTest +import { + connectDBTest, + disconnectDBTest, + dropDBTest } from '../../utils/TestingUtils.js'; import User from '../../database/models/Users.js'; -import { describe, beforeEach, afterEach, expect, it, jest } from '@jest/globals'; +import { describe, beforeEach, afterEach, expect, it, jest, afterAll } from '@jest/globals'; import generateUser from '../model-generators/generateUser.js'; import { faker } from '@faker-js/faker'; import axios from 'axios'; @@ -13,155 +14,177 @@ import { PHARMACY_ADMIN_ENUM, DOCTOR_ENUM, PATIENT_ENUM, PHARMACIST_ENUM, CLINIC jest.mock('axios'); import bcrypt from 'bcrypt' describe('POST /signup/:request', () => { - - beforeEach(async () => { - await connectDBTest(); - }); + + beforeEach(async () => { + await connectDBTest(); + }); it('should return 200 OK when posting a new patient', async () => { const userId = faker.database.mongodbObjectId(); + let oldDatabaseRecord = await User.find(); const axiosResponseClinicReq = { - data: null - }; + data: null + }; const axiosResponsePostPatient = { data: { - userId, email: faker.internet.email(), password:faker.internet.password(), userName: faker.internet.userName(), type: PATIENT_ENUM + userId, email: faker.internet.email(), password: faker.internet.password(), userName: faker.internet.userName(), type: PATIENT_ENUM } }; axios.post.mockResolvedValue(axiosResponseClinicReq); axios.post.mockResolvedValue(axiosResponsePostPatient); - const res = await request(app).post(`/signup/${CLINIC_REQ}`).send({type: PATIENT_ENUM, email: faker.internet.email(), userName: faker.internet.userName()}); - let databaseRecord = await User.find(); + const res = await request(app).post(`/signup/${CLINIC_REQ}`).send({ type: PATIENT_ENUM, email: faker.internet.email(), userName: faker.internet.userName() }); + let newDatabaseRecord = await User.find(); expect(res.status).toBe(OK_REQUEST_CODE_200); - expect(databaseRecord.length).toBe(1); + expect(newDatabaseRecord.length).toBe(oldDatabaseRecord.length + 1); }); it('should return 400 for dub users', async () => { const userId = faker.database.mongodbObjectId(); - const email= faker.internet.email(); + const email = faker.internet.email(); const userName = faker.internet.userName(); const user = new User(generateUser(userId, email, userName, PATIENT_ENUM)); await user.save(); - const res = await request(app).post(`/signup/${CLINIC_REQ}`).send({type: PATIENT_ENUM, email: email, userName: userName}); + const res = await request(app).post(`/signup/${CLINIC_REQ}`).send({ type: PATIENT_ENUM, email: email, userName: userName }); expect(res.status).toBe(BAD_REQUEST_CODE_400); expect(JSON.parse(res.text).message).toBe(DUB_EMAIL_ERROR_MESSAGE); }); afterEach(async () => { - await disconnectDBTest(); - }) + await disconnectDBTest(); + }) + + afterAll(async () => { + await dropDBTest(); + }) }) describe('DELETE /users/:id', () => { - - beforeEach(async () => { - await connectDBTest(); - }); + + beforeEach(async () => { + await connectDBTest(); + }); it('should return 200 and delete the user', async () => { const userId = faker.database.mongodbObjectId(); - const email= faker.internet.email(); + const email = faker.internet.email(); const userName = faker.internet.userName(); const user = new User(generateUser(userId, email, userName, PATIENT_ENUM)); await user.save(); - let dataBaseRecord = await User.find(); - expect(dataBaseRecord.length).toBe(1); + let oldDataBaseRecord = await User.find(); const res = await request(app).delete(`/users/${userId}`); - dataBaseRecord = await User.find(); - expect(dataBaseRecord.length).toBe(0); + let newDataBaseRecord = await User.find(); + expect(newDataBaseRecord.length).toBe(oldDataBaseRecord.length - 1); }); it('should return 200 and do not delete any user', async () => { const userId = faker.database.mongodbObjectId(); const secondUserId = faker.database.mongodbObjectId(); - const email= faker.internet.email(); + const email = faker.internet.email(); const userName = faker.internet.userName(); const user = new User(generateUser(userId, email, userName, PATIENT_ENUM)); await user.save(); - let dataBaseRecord = await User.find(); - expect(dataBaseRecord.length).toBe(1); + let oldDataBaseRecord = await User.find(); const res = await request(app).delete(`/users/${secondUserId.toString()}`); - dataBaseRecord = await User.find(); - expect(dataBaseRecord.length).toBe(1); + let newDataBaseRecord = await User.find(); + expect(newDataBaseRecord.length).toBe(oldDataBaseRecord.length); }); afterEach(async () => { - await disconnectDBTest(); - }) + await disconnectDBTest(); + }) + + afterAll(async () => { + await dropDBTest(); + }) }) describe('POST /doctors', () => { - - beforeEach(async () => { - await connectDBTest(); - }); + + beforeEach(async () => { + await connectDBTest(); + }); it('should return 200 and add a doctor', async () => { - const userId = faker.database.mongodbObjectId(); + let oldDatabaseRecord = await User.find(); + const userId = faker.database.mongodbObjectId(); const userName = faker.internet.userName(); - const res = await request(app).post(`/doctors`).send({userId: userId, type: DOCTOR_ENUM, email: faker.internet.email(), userName: userName, password:faker.internet.password()}); + const res = await request(app).post(`/doctors`).send({ userId: userId, type: DOCTOR_ENUM, email: faker.internet.email(), userName: userName, password: faker.internet.password() }); let databaseRecord = await User.find(); expect(res.status).toBe(OK_REQUEST_CODE_200); - expect(databaseRecord.length).toBe(1); + expect(databaseRecord.length).toBe(oldDatabaseRecord.length + 1); expect(databaseRecord[databaseRecord.length - 1].userName).toBe(userName); }); afterEach(async () => { - await disconnectDBTest(); - }) + await disconnectDBTest(); + }) + + afterAll(async () => { + await dropDBTest(); + }) + + afterAll(async () => { + await dropDBTest(); + }) }) describe('POST /pharmacists', () => { - - beforeEach(async () => { - await connectDBTest(); - }); + + beforeEach(async () => { + await connectDBTest(); + }); it('should return 200 and add a pharmacists', async () => { - const userId = faker.database.mongodbObjectId(); + let oldDatabaseRecord = await User.find(); + const userId = faker.database.mongodbObjectId(); const userName = faker.internet.userName(); - const res = await request(app).post(`/pharmacists`).send({userId: userId, type: PHARMACIST_ENUM, email: faker.internet.email(), userName: userName, password:faker.internet.password()}); + const res = await request(app).post(`/pharmacists`).send({ userId: userId, type: PHARMACIST_ENUM, email: faker.internet.email(), userName: userName, password: faker.internet.password() }); let databaseRecord = await User.find(); expect(res.status).toBe(OK_REQUEST_CODE_200); - expect(databaseRecord.length).toBe(1); + expect(databaseRecord.length).toBe(oldDatabaseRecord.length + 1); expect(databaseRecord[databaseRecord.length - 1].userName).toBe(userName); }); afterEach(async () => { - await disconnectDBTest(); - }) + await disconnectDBTest(); + }) + + afterAll(async () => { + await dropDBTest(); + }) }) describe('POST /admins/:request', () => { - - beforeEach(async () => { - await connectDBTest(); - }); + + beforeEach(async () => { + await connectDBTest(); + }); it('should return 200 and add a pharmacy admin', async () => { const userId = faker.database.mongodbObjectId(); const userName = faker.internet.userName(); + const oldDatabaseRecord = await User.find(); const type = PHARMACY_ADMIN_ENUM; const axiosResponseCheckDoctorPharma = { - data: null - }; + data: null + }; const axiosResponsePostPharmacyReq = { - + data: { - userId: userId, email: faker.internet.email(), password:faker.internet.password(), userName: userName, type :type + userId: userId, email: faker.internet.email(), password: faker.internet.password(), userName: userName, type: type } }; axios.post.mockResolvedValue(axiosResponseCheckDoctorPharma); axios.post.mockResolvedValue(axiosResponseCheckDoctorPharma); axios.post.mockResolvedValue(axiosResponsePostPharmacyReq); - const res = await request(app).post(`/admins/${PHARMACY_REQ}`).send({userId: userId, type: type, email: faker.internet.email(), userName: userName, password:faker.internet.password()}); + const res = await request(app).post(`/admins/${PHARMACY_REQ}`).send({ userId: userId, type: type, email: faker.internet.email(), userName: userName, password: faker.internet.password() }); const databaseRecord = await User.find(); expect(res.status).toBe(OK_REQUEST_CODE_200); - expect(databaseRecord.length).toBe(1); + expect(databaseRecord.length).toBe(oldDatabaseRecord.length + 1); expect(databaseRecord[databaseRecord.length - 1].type).toBe(type); }); @@ -169,79 +192,84 @@ describe('POST /admins/:request', () => { const userId = faker.database.mongodbObjectId(); const userName = faker.internet.userName(); const type = PHARMACY_ADMIN_ENUM; - const email= faker.internet.email(); + const email = faker.internet.email(); // const userId = faker.database.mongodbObjectId(); // const userName = faker.internet.userName(); const user = new User(generateUser(userId, email, userName, type)); await user.save(); - const res = await request(app).post(`/admins/${PHARMACY_REQ}`).send({userId: userId, type: type, email: email, userName: userName, password:faker.internet.password()}); - const databaseRecord = await User.find(); + const res = await request(app).post(`/admins/${PHARMACY_REQ}`).send({ userId: userId, type: type, email: email, userName: userName, password: faker.internet.password() }); expect(res.status).toBe(BAD_REQUEST_CODE_400); - expect(databaseRecord.length).toBe(1); expect(JSON.parse(res.text).message).toBe(DUB_USERNAME_ERROR_MESSAGE); }); afterEach(async () => { - await disconnectDBTest(); - }) + await disconnectDBTest(); + }) + + afterAll(async () => { + await dropDBTest(); + }) }) describe('POST /login/:request', () => { - - beforeEach(async () => { - await connectDBTest(); - - }); + + beforeEach(async () => { + await connectDBTest(); + + }); it('should return 400 and incorrect UserName', async () => { const userId = faker.database.mongodbObjectId(); - const email= faker.internet.email(); + const email = faker.internet.email(); const userName = faker.internet.userName(); const wrongUserName = faker.internet.userName(); const password = faker.internet.password(); const salt = await bcrypt.genSalt(); - const HashedPassword = await bcrypt.hash(password, salt); + const HashedPassword = await bcrypt.hash(password, salt); const user = new User(generateUser(userId, email, userName, PATIENT_ENUM, HashedPassword)); await user.save(); - const res = await request(app).post(`/login/${CLINIC_REQ}`).send({wrongUserName, password}); + const res = await request(app).post(`/login/${CLINIC_REQ}`).send({ wrongUserName, password }); expect(res.status).toBe(BAD_REQUEST_CODE_400); expect(JSON.parse(res.text).message).toBe(INCORRECT_USER_ERROR_MESSAGE); }); it('should return 400 and incorrect Password', async () => { const userId = faker.database.mongodbObjectId(); - const email= faker.internet.email(); + const email = faker.internet.email(); const userName = faker.internet.userName(); const password = faker.internet.password(); const wrongPassword = faker.internet.password(); const salt = await bcrypt.genSalt(); - const HashedPassword = await bcrypt.hash(password, salt); + const HashedPassword = await bcrypt.hash(password, salt); const user = new User(generateUser(userId, email, userName, PATIENT_ENUM, HashedPassword)); await user.save(); - const res = await request(app).post(`/login/${CLINIC_REQ}`).send({userName, wrongPassword}); + const res = await request(app).post(`/login/${CLINIC_REQ}`).send({ userName, wrongPassword }); expect(res.status).toBe(BAD_REQUEST_CODE_400); expect(JSON.parse(res.text).message).toBe(INCORRECT_PASSWORD_ERROR_MESSAGE); }); it('should return 200 the correct user', async () => { const userId = faker.database.mongodbObjectId(); - const email= faker.internet.email(); + const email = faker.internet.email(); const userName = faker.internet.userName(); const password = faker.internet.password(); const salt = await bcrypt.genSalt(); - const HashedPassword = await bcrypt.hash(password, salt); + const HashedPassword = await bcrypt.hash(password, salt); const user = new User(generateUser(userId, email, userName, PATIENT_ENUM, HashedPassword)); await user.save(); - const res = await request(app).post(`/login/${CLINIC_REQ}`).send({userName, password}); + const res = await request(app).post(`/login/${CLINIC_REQ}`).send({ userName, password }); expect(res.status).toBe(OK_REQUEST_CODE_200); expect(JSON.parse(res.text).userName).toBe(userName); }); afterEach(async () => { - await disconnectDBTest(); - }) + await disconnectDBTest(); + }) + + afterAll(async () => { + await dropDBTest(); + }) }) - \ No newline at end of file diff --git a/authentication/src/utils/TestingUtils.js b/authentication/src/utils/TestingUtils.js index 4f8c9318..c0ab04a0 100644 --- a/authentication/src/utils/TestingUtils.js +++ b/authentication/src/utils/TestingUtils.js @@ -6,7 +6,6 @@ const connectDBTest = async () => { try{ const mongoURL = process.env.MONGO_URI_TEST; await mongoose.connect(mongoURL); - await mongoose.connection.db.dropDatabase(); } catch(err){ console.error('Error connecting to the database:', err.message); } @@ -14,11 +13,19 @@ const connectDBTest = async () => { const disconnectDBTest = async () => { try{ - await mongoose.connection.db.dropDatabase(); await mongoose.disconnect(); } catch(err){ console.error('Error connecting to the database:', err.message); } }; -export { connectDBTest, disconnectDBTest }; \ No newline at end of file +const dropDBTest = async () => { + try { + await mongoose.connection.db.dropDatabase(); + + } catch (err) { + console.error('error dropping the db', err) + } +} + +export { connectDBTest, disconnectDBTest, dropDBTest }; \ No newline at end of file