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

Task E #4

Merged
merged 10 commits into from
Oct 10, 2023
19 changes: 10 additions & 9 deletions server/config/DB&ServerConfig.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import dotenv from 'dotenv';
dotenv.config();
let BACKEND_SERVER_PORT: Number = 3000;
const MONGO_URI: String = process.env.MONGO_URI || '';
import * as dotenv from 'dotenv';
dotenv.config({ path: '../src/.env' });

if (process.env.PORT) BACKEND_SERVER_PORT = Number(process.env.PORT);

export const config = {
server: { port: BACKEND_SERVER_PORT },
DB: { url: MONGO_URI }
const MONGO_URI: string = process.env.MONGO_URI || '';
const BACKEND_SERVER_PORT = process.env.PORT ? Number(process.env.PORT) : 3000;
const config = {
server: { PORT: BACKEND_SERVER_PORT },
DB: { URL: MONGO_URI }
};

console.log(config);
export default config;
70 changes: 40 additions & 30 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "pharmacy",
"name": "Clinic",
"version": "1.0.0",
"description": "Full-stack website",
"main": "server.ts",
"scripts": {
"start": "ts server.ts",
"build": "tsc",
"start": "node build/src/start.js",
"dev": "nodemon server.ts",
"test": "jest tests/ --forceExit",
"prettier": "prettier --check **/*.ts",
Expand All @@ -16,12 +17,15 @@
},
"license": "ISC",
"dependencies": {
"@types/cookie-parser": "^1.4.4",
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.2",
"compression": "^1.7.4",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"faker": "^5.5.3",
"helmet": "^6.0.1",
"http-status-codes": "^2.2.0",
"ioredis": "^5.3.2",
Expand All @@ -34,7 +38,10 @@
"devDependencies": {
"@types/bcryptjs": "^2.4.4",
"@types/compression": "^1.7.3",
"@types/cors": "^2.8.14",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.18",
"@types/faker": "^6.6.9",
"@types/jest": "^29.5.5",
"@types/jsonwebtoken": "^9.0.3",
"@types/morgan": "^1.9.6",
Expand All @@ -44,7 +51,7 @@
"ioredis-mock": "^8.8.3",
"jest": "^29.7.0",
"mongodb-memory-server": "^8.13.0",
"nodemon": "^2.0.22",
"nodemon": "^3.0.1",
"prettier": "^3.0.0",
"supertest": "^6.3.3",
"ts-jest": "^29.1.1",
Expand Down
9 changes: 9 additions & 0 deletions server/src/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PORT=123

MONGO_URI=kljbh

TOKEN_SECRET=

STRIPE_SECRET_KEY=

REDIS_SECRET_PASS=
51 changes: 0 additions & 51 deletions server/src/app.ts

This file was deleted.

7 changes: 5 additions & 2 deletions server/src/controllers/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ const controller = (res: Response) => {
return async (...args: any[]) => {
try {
const result = await service(...args);
if (result.JWTToken) {
res.cookie('token', result.JWTToken, { httpOnly: true });
delete result.JWTToken; // in order not to send the token in the res as json
}

res.status(result.status).json(result);
} catch (error: any) {
// console.log(error);

console.log(error);
if (error instanceof HttpError) {
res.status(error.statusCode).send(error.message);
} else {
Expand Down
1 change: 0 additions & 1 deletion server/src/middlewares/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { StatusCodes } from 'http-status-codes';

const isAuthenticated = async (req: Request, res: Response, next: NextFunction) => {
try {
// Get the JWT token from the request headers, query parameters, or cookies, as needed
const token = req.headers.authorization || req.query.token || req.cookies.token;

if (!token) return res.status(StatusCodes.UNAUTHORIZED).json({ message: 'Missing token' });
Expand Down
15 changes: 12 additions & 3 deletions server/src/middlewares/authorization.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Request, Response, NextFunction } from 'express';
import { StatusCodes } from 'http-status-codes';
import { Model, Document } from 'mongoose';

import { HttpError, verifyToken } from '../utils';
const isAuthorized = (authorizedRole: string) => (req: Request, res: Response, next: NextFunction) => {
try {
const { role } = req.body.user;
Expand Down Expand Up @@ -29,5 +29,14 @@ const isResourceOwner =
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json(e.message);
}
};

export { isAuthorized, isResourceOwner };
const decodeJWTToken = (res: Response, req: Request) => {
try {
const JWTToken = req.cookies.token;
if (!JWTToken) return;
const decodedToken = verifyToken(JWTToken);
req.body.decodedToken = decodedToken;
} catch (error) {
res.status(StatusCodes.BAD_REQUEST).json('Token is not valid');
}
};
export { isAuthorized, isResourceOwner, decodeJWTToken };
9 changes: 5 additions & 4 deletions server/src/models/package.model.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import mongoose, { Document, Schema } from 'mongoose';

interface IPackage extends Document {
interface IPackage {
name: string;
price: number;
sessionDiscount: number;
medicineDiscount: number;
familyDiscount: number;
isLatest: boolean;
}

const packageSchema = new Schema<IPackage>({
type IPackageDocument = IPackage & Document;
const packageSchema = new Schema<IPackageDocument>({
name: { type: String, required: true },
price: { type: Number, required: true },
sessionDiscount: { type: Number, required: true },
Expand All @@ -18,6 +18,7 @@ const packageSchema = new Schema<IPackage>({
isLatest: { type: Boolean, required: true }
});

const PackageModel = mongoose.model<IPackage>('Package', packageSchema);
const PackageModel = mongoose.model<IPackageDocument>('Package', packageSchema);

export default PackageModel;
export { IPackage, IPackageDocument };
2 changes: 1 addition & 1 deletion server/src/models/request.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const requestSchema = new Schema<IRequest>({
degree: { type: [String], required: true },
licenses: { type: [String], required: true },
status: { type: String, enum: ['Pending', 'Approved', 'Rejected'], required: true },
date: { type: Date,default:Date.now(), required: true }
date: { type: Date, default: Date.now(), required: true }
});

const Request = mongoose.model<IRequest>('Request', requestSchema);
Expand Down
Loading