Skip to content

Commit

Permalink
Api code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Feb 27, 2024
1 parent 9df5669 commit c9a37b7
Show file tree
Hide file tree
Showing 14 changed files with 791 additions and 732 deletions.
1,197 changes: 640 additions & 557 deletions api/coverage/cobertura-coverage.xml

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions api/src/common/AuthHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Request } from 'express'
import * as Helper from './Helper'
import * as env from '../config/env.config'

/**
* Check whether the request is from the backend or not.
*
* @export
* @param {Request} req
* @returns {boolean}
*/
export const isBackend = (req: Request): boolean => !!req.headers.origin && Helper.trim(req.headers.origin, '/') === Helper.trim(env.BACKEND_HOST, '/')

/**
* Check whether the request is from the frontend or not.
*
* @export
* @param {Request} req
* @returns {boolean}
*/
export const isFrontend = (req: Request): boolean => !!req.headers.origin && Helper.trim(req.headers.origin, '/') === Helper.trim(env.FRONTEND_HOST, '/')

/**
* Get authentification cookie name.
*
* @param {Request} req
* @returns {string}
*/
export const getAuthCookieName = (req: Request): string => {
if (isBackend(req)) {
// Backend auth cookie name
return env.BACKEND_AUTH_COOKIE_NAME
}

if (isFrontend(req)) {
// Frontend auth cookie name
return env.FRONTEND_AUTH_COOKIE_NAME
}

// Mobile app and unit tests auth header name
return env.X_ACCESS_TOKEN
}
4 changes: 2 additions & 2 deletions api/src/common/DatabaseHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as env from '../config/env.config'
* @param {boolean} debug
* @returns {Promise<boolean>}
*/
export async function Connect(debug: boolean = false): Promise<boolean> {
export const Connect = async (debug: boolean = false): Promise<boolean> => {
let options: ConnectOptions = {}

if (env.DB_SSL) {
Expand Down Expand Up @@ -41,6 +41,6 @@ export async function Connect(debug: boolean = false): Promise<boolean> {
* @param {boolean} force
* @returns {Promise<void>}
*/
export async function Close(force: boolean = false): Promise<void> {
export const Close = async (force: boolean = false): Promise<void> => {
await mongoose.connection.close(force)
}
67 changes: 8 additions & 59 deletions api/src/common/Helper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { Request } from 'express'
import mongoose from 'mongoose'
import validator from 'validator'
import * as env from '../config/env.config'

/**
* Convert string to boolean.
Expand All @@ -12,7 +10,7 @@ import * as env from '../config/env.config'
* @param {string} input
* @returns {boolean}
*/
export function StringToBoolean(input: string): boolean {
export const StringToBoolean = (input: string): boolean => {
try {
return Boolean(JSON.parse(input.toLowerCase()))
} catch {
Expand All @@ -28,7 +26,7 @@ export function StringToBoolean(input: string): boolean {
* @param {string} filePath
* @returns {Promise<boolean>}
*/
export async function exists(filePath: string): Promise<boolean> {
export const exists = async (filePath: string): Promise<boolean> => {
try {
await fs.access(filePath)
return true
Expand All @@ -46,7 +44,7 @@ export async function exists(filePath: string): Promise<boolean> {
* @param {boolean} recursive
* @returns {Promise<void>}
*/
export async function mkdir(folder: string) {
export const mkdir = async (folder: string) => {
await fs.mkdir(folder, { recursive: true })
}

Expand All @@ -58,7 +56,7 @@ export async function mkdir(folder: string) {
* @param {string} char
* @returns {string}
*/
export function trim(str: string, char: string): string {
export const trim = (str: string, char: string): string => {
let res = str
while (res.charAt(res.length - 1) === char) {
res = res.substring(0, res.length - 1)
Expand All @@ -74,7 +72,7 @@ export function trim(str: string, char: string): string {
* @param {string} part2
* @returns {string}
*/
export function joinURL(part1: string, part2: string): string {
export const joinURL = (part1: string, part2: string): string => {
const p1 = trim(part1, '/')
let p2 = part2

Expand All @@ -92,9 +90,7 @@ export function joinURL(part1: string, part2: string): string {
* @param {string} filename
* @returns {string}
*/
export function getFilenameWithoutExtension(filename: string): string {
return path.parse(filename).name
}
export const getFilenameWithoutExtension = (filename: string): string => path.parse(filename).name

/**
* Clone an object or an array.
Expand All @@ -104,65 +100,18 @@ export function getFilenameWithoutExtension(filename: string): string {
*/
export const clone = (obj: any) => (Array.isArray(obj) ? Array.from(obj) : ({ ...obj }))

/**
* Check whether the request is from the backend or not.
*
* @export
* @param {Request} req
* @returns {boolean}
*/
export function isBackend(req: Request): boolean {
return !!req.headers.origin && trim(req.headers.origin, '/') === trim(env.BACKEND_HOST, '/')
}

/**
* Check whether the request is from the frontend or not.
*
* @export
* @param {Request} req
* @returns {boolean}
*/
export function isFrontend(req: Request): boolean {
return !!req.headers.origin && trim(req.headers.origin, '/') === trim(env.FRONTEND_HOST, '/')
}

/**
* Get authentification cookie name.
*
* @param {Request} req
* @returns {string}
*/
export const getAuthCookieName = (req: Request): string => {
if (isBackend(req)) {
// Backend auth cookie name
return env.BACKEND_AUTH_COOKIE_NAME
}

if (isFrontend(req)) {
// Frontend auth cookie name
return env.FRONTEND_AUTH_COOKIE_NAME
}

// Mobile app and unit tests auth header name
return env.X_ACCESS_TOKEN
}

/**
* Check ObjectId.
*
* @param {?string} id
* @returns {boolean}
*/
export function isValidObjectId(id?: string) {
return mongoose.isValidObjectId(id)
}
export const isValidObjectId = (id?: string) => mongoose.isValidObjectId(id)

/**
* Check email.
*
* @param {string} email
* @returns {boolean}
*/
export function isValidEmail(email?: string) {
return !!email && validator.isEmail(email)
}
export const isValidEmail = (email?: string) => !!email && validator.isEmail(email)
2 changes: 1 addition & 1 deletion api/src/common/MailHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as env from '../config/env.config'
* @param {nodemailer.SendMailOptions} mailOptions
* @returns {Promise<unknown>}
*/
export function sendMail(mailOptions: nodemailer.SendMailOptions) {
export const sendMail = (mailOptions: nodemailer.SendMailOptions) => {
const transporterOptions: SMTPTransport.Options = {
host: env.SMTP_HOST,
port: env.SMTP_PORT,
Expand Down
12 changes: 6 additions & 6 deletions api/src/controllers/agencyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import * as Helper from '../common/Helper'
* @param {Response} res
* @returns {unknown}
*/
export async function validate(req: Request, res: Response) {
export const validate = async (req: Request, res: Response) => {
const { body }: { body: movininTypes.ValidateAgencyPayload } = req
const { fullName } = body

Expand All @@ -48,7 +48,7 @@ export async function validate(req: Request, res: Response) {
* @param {Response} res
* @returns {unknown}
*/
export async function update(req: Request, res: Response) {
export const update = async (req: Request, res: Response) => {
const { body }: { body: movininTypes.UpdateAgencyPayload } = req
const { _id } = body

Expand Down Expand Up @@ -100,7 +100,7 @@ export async function update(req: Request, res: Response) {
* @param {Response} res
* @returns {unknown}
*/
export async function deleteAgency(req: Request, res: Response) {
export const deleteAgency = async (req: Request, res: Response) => {
const { id } = req.params

try {
Expand Down Expand Up @@ -154,7 +154,7 @@ export async function deleteAgency(req: Request, res: Response) {
* @param {Response} res
* @returns {unknown}
*/
export async function getAgency(req: Request, res: Response) {
export const getAgency = async (req: Request, res: Response) => {
const { id } = req.params

try {
Expand Down Expand Up @@ -201,7 +201,7 @@ export async function getAgency(req: Request, res: Response) {
* @param {Response} res
* @returns {unknown}
*/
export async function getAgencies(req: Request, res: Response) {
export const getAgencies = async (req: Request, res: Response) => {
try {
const page = Number.parseInt(req.params.page, 10)
const size = Number.parseInt(req.params.size, 10)
Expand Down Expand Up @@ -253,7 +253,7 @@ export async function getAgencies(req: Request, res: Response) {
* @param {Response} res
* @returns {unknown}
*/
export async function getAllAgencies(req: Request, res: Response) {
export const getAllAgencies = async (req: Request, res: Response) => {
try {
let data = await User.aggregate(
[
Expand Down
22 changes: 11 additions & 11 deletions api/src/controllers/bookingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import * as Helper from '../common/Helper'
* @param {Response} res
* @returns {unknown}
*/
export async function create(req: Request, res: Response) {
export const create = async (req: Request, res: Response) => {
try {
const { body }: { body: movininTypes.Booking } = req
const booking = new Booking(body)
Expand All @@ -49,7 +49,7 @@ export async function create(req: Request, res: Response) {
* @param {string} notificationMessage
* @returns {*}
*/
async function notifyAgency(user: env.User, bookingId: string, agency: env.User, notificationMessage: string) {
const notifyAgency = async (user: env.User, bookingId: string, agency: env.User, notificationMessage: string) => {
// notification
const message = `${user.fullName} ${notificationMessage} ${bookingId}.`
const notification = new Notification({
Expand Down Expand Up @@ -94,7 +94,7 @@ async function notifyAgency(user: env.User, bookingId: string, agency: env.User,
* @param {Response} res
* @returns {unknown}
*/
export async function checkout(req: Request, res: Response) {
export const checkout = async (req: Request, res: Response) => {
try {
let user: env.User | null
const { body }: { body: movininTypes.CheckoutPayload } = req
Expand Down Expand Up @@ -211,7 +211,7 @@ export async function checkout(req: Request, res: Response) {
* @param {env.Booking} booking
* @returns {*}
*/
async function notifyRenter(booking: env.Booking) {
const notifyRenter = async (booking: env.Booking) => {
const renter = await User.findById(booking.renter)

if (!renter) {
Expand Down Expand Up @@ -314,7 +314,7 @@ async function notifyRenter(booking: env.Booking) {
* @param {Response} res
* @returns {unknown}
*/
export async function update(req: Request, res: Response) {
export const update = async (req: Request, res: Response) => {
try {
const { body }: { body: movininTypes.Booking } = req
if (!body._id) {
Expand Down Expand Up @@ -374,7 +374,7 @@ export async function update(req: Request, res: Response) {
* @param {Response} res
* @returns {unknown}
*/
export async function updateStatus(req: Request, res: Response) {
export const updateStatus = async (req: Request, res: Response) => {
try {
const { body }: { body: movininTypes.UpdateStatusPayload } = req
const { ids: _ids, status } = body
Expand Down Expand Up @@ -406,7 +406,7 @@ export async function updateStatus(req: Request, res: Response) {
* @param {Response} res
* @returns {unknown}
*/
export async function deleteBookings(req: Request, res: Response) {
export const deleteBookings = async (req: Request, res: Response) => {
try {
const { body }: { body: string[] } = req
const ids = body.map((id) => new mongoose.Types.ObjectId(id))
Expand All @@ -429,7 +429,7 @@ export async function deleteBookings(req: Request, res: Response) {
* @param {Response} res
* @returns {unknown}
*/
export async function getBooking(req: Request, res: Response) {
export const getBooking = async (req: Request, res: Response) => {
const { id } = req.params

try {
Expand Down Expand Up @@ -504,7 +504,7 @@ export async function getBooking(req: Request, res: Response) {
* @param {Response} res
* @returns {unknown}
*/
export async function getBookings(req: Request, res: Response) {
export const getBookings = async (req: Request, res: Response) => {
try {
const { body }: { body: movininTypes.GetBookingsPayload } = req
const page = Number.parseInt(req.params.page, 10)
Expand Down Expand Up @@ -688,7 +688,7 @@ export async function getBookings(req: Request, res: Response) {
* @param {Response} res
* @returns {unknown}
*/
export async function hasBookings(req: Request, res: Response) {
export const hasBookings = async (req: Request, res: Response) => {
const { renter } = req.params

try {
Expand Down Expand Up @@ -719,7 +719,7 @@ export async function hasBookings(req: Request, res: Response) {
* @param {Response} res
* @returns {unknown}
*/
export async function cancelBooking(req: Request, res: Response) {
export const cancelBooking = async (req: Request, res: Response) => {
const { id } = req.params

try {
Expand Down
Loading

0 comments on commit c9a37b7

Please sign in to comment.