Skip to content

Commit

Permalink
fix - app error & use module paths
Browse files Browse the repository at this point in the history
Fix - app error ISIL-ESTE#103
Database Seeding Enhancement: Improving REST API Initialization ISIL-ESTE#108
 Suggestion - Organizing Interfaces in an "Interfaces" Folder ISIL-ESTE#129
  • Loading branch information
bellaabdelouahab committed Sep 20, 2023
1 parent ba9eef7 commit 6fa463d
Show file tree
Hide file tree
Showing 25 changed files with 536 additions and 129 deletions.
32 changes: 0 additions & 32 deletions backend-app/constants/default_roles.ts

This file was deleted.

3 changes: 2 additions & 1 deletion backend-app/controllers/auth_controllers/auth_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { promisify } from 'util';
import validator from 'validator';
import AppError from '@utils/app_error';
import Role from '@utils/authorization/role/role';
import { REQUIRE_ACTIVATION } from '../../config/app_config';
import { REQUIRE_ACTIVATION } from '@config/app_config';
import {
getGithubOAuthUser,
getGithubOAuthToken,
Expand Down Expand Up @@ -83,6 +83,7 @@ export const githubHandler = async (
};

function validateCredentialsTypes(email: string, password: string) {
// TODO: impllement joi to validate url params ( only if it's compatible with swagger )
if (!email || !password) {
throw new AppError(404, 'Please provide email or password');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { Request, Response, NextFunction } from 'express';
import AppError from '../../utils/app_error';
import AppError from '@utils/app_error';

interface Repository {
id: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response, NextFunction } from 'express';
import AppError from '../../utils/app_error';
import AppError from '@utils/app_error';
import * as calendar_validators from './calendar_validators';

export const updateCalendar = async (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import Calendar from '@models/calendar/calendar_model';
export async function validateCalendar(req: Request) {
const calendarid = req.params.calendarid;
if (!calendarid) {
throw new AppError('Calendar id is required', 400);
throw new AppError(400, 'Calendar id is required');
}
const calendar = await Calendar.findById(calendarid);
if (!calendar) {
throw new AppError('Calendar not found with that id', 404);
throw new AppError(404, 'Calendar not found with that id');
}
return calendar;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import AppError from '../../utils/app_error';
import AppError from '@utils/app_error';
import validator from 'validator';
import * as calendar_validators from './calendar_validators';
import { ObjectId } from 'mongoose';
Expand All @@ -15,17 +15,17 @@ export const inviteUsersByEmail = async (req: Request, res: Response) => {
// check if user is a participant and the calendar is shareable
if (!calendar.participants.includes(userid))
throw new AppError(
'You do not have permission to invite users to this calendar',
403
403,
'You do not have permission to invite users to this calendar'
);
if (!calendar.isShareAble) {
throw new AppError('This calendar is not shareable', 403);
throw new AppError(403, 'This calendar is not shareable');
}
}
// get emails from request body
const emails = req.body.emails;
if (!emails) {
throw new AppError('Emails are required', 400);
throw new AppError(400, 'Emails are required');
}
// check if emails are valid
const validEmails: string[] = [];
Expand Down Expand Up @@ -55,7 +55,7 @@ export const removeCalendarParticipants = async (
// check if user is the calendar owner
// @ts-ignore
if (calendar.owner.toString() === !userid.toString()) {
throw new AppError('You are not the owner of this calendar', 403);
throw new AppError(403, 'You are not the owner of this calendar');
}
// get list of participants to remove from calendar
const listOfParticipants: string[] = req.body.participants;
Expand Down
6 changes: 3 additions & 3 deletions backend-app/controllers/users_controllers/user_controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Request, Response, NextFunction } from 'express';
import User, { IUser } from '../../models/user/user_model';
import User, { IUser } from '@models/user/user_model';
import * as base from '../base_controller';
import AppError from '../../utils/app_error';
import sanitizeRequestBody from '../../utils/sanitize_request_body';
import AppError from '@utils/app_error';
import sanitizeRequestBody from '@utils/sanitize_request_body';
import { Model } from 'mongoose';

export const getMe = (req: Request, res: Response) => {
Expand Down
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion backend-app/middlewares/authorization.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import AppError from '../utils/app_error';
import AppError from '@utils/app_error';
import { Request, Response, NextFunction } from 'express';

interface User {
Expand Down
4 changes: 2 additions & 2 deletions backend-app/middlewares/global_error_handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Request, Response, NextFunction } from 'express';
import httpStatus from 'http-status-codes';
import { CURRENT_ENV } from '../config/app_config';
import AppError from '../utils/app_error';
import { CURRENT_ENV } from '@config/app_config';
import AppError from '@utils/app_error';

/**
* Error handling middleware
Expand Down
2 changes: 1 addition & 1 deletion backend-app/middlewares/morgan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import morgan from 'morgan';
import logger from '@utils/logger';
import { CURRENT_ENV } from '../config/app_config';
import { CURRENT_ENV } from '@config/app_config';

// Create a stream object with a 'write' function that will be used by `morgan`
const stream = {
Expand Down
2 changes: 1 addition & 1 deletion backend-app/middlewares/rate_limit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import rateLimit from 'express-rate-limit';
import { RATE_LIMIT_PER_HOUR } from '../config/app_config';
import { RATE_LIMIT_PER_HOUR } from '@config/app_config';

export default rateLimit({
// @ts-ignore
Expand Down
47 changes: 21 additions & 26 deletions backend-app/models/user/role_model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mongoose, { Schema, Document } from 'mongoose';
import Actions from '../../constants/actions';
import Actions from '@constants/actions';
import validator from 'validator';

export interface IRole extends Document {
Expand All @@ -25,36 +25,31 @@ const roleSchema: Schema = new Schema<IRole>(
validator: (value: string) => value.length > 0,
},
},
authorities: [
{
type: String,
required: true,
default: [],
validate: {
validator: (value: string[]) =>
value.every((v) =>
validator.isIn(v, Object.values(Actions))
),
},
authorities: {
type: [String],
required: true,
default: [],
validate: {
validator: (value: string[]) =>
value.every((v) =>
validator.isIn(v, Object.values(Actions))
),
},
],
restrictions: [
{
type: String,
required: true,
default: [],
validate: {
validator: (value: string[]) =>
value.every((v) =>
validator.isIn(v, Object.values(Actions))
),
},
},
restrictions: {
type: [String],
required: true,
default: [],
validate: {
validator: (value: string[]) =>
value.every((v) =>
validator.isIn(v, Object.values(Actions))
),
},
],
},
},
{
timestamps: true,
versionKey: false,
}
);

Expand Down
4 changes: 2 additions & 2 deletions backend-app/models/user/user_model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import mongoose, { Document, Model, Schema } from 'mongoose';
import validator from 'validator';
import bcrypt from 'bcrypt';
import Actions from '../../constants/actions';
import metaData from '../../constants/meta_data';
import Actions from '@constants/actions';
import metaData from '@constants/meta_data';
import { randomBytes, createHash } from 'crypto';

export interface IUser extends Document {
Expand Down
Loading

0 comments on commit 6fa463d

Please sign in to comment.