Skip to content

Commit

Permalink
refractor backend
Browse files Browse the repository at this point in the history
  • Loading branch information
youngbryanyu committed Feb 22, 2024
1 parent d808218 commit 7b380d4
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 17 deletions.
3 changes: 3 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ coverage

# ignore Docker files
docker-compose.*.yml

# ignore firebase files
firebase-key**.json
7 changes: 6 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
"keywords": [
"fitness",
"nutrition",
"mobile"
"mobile",
"exercise",
"macros",
"exercise-tracker",
"macro-tracker",
"simple"
],
"dependencies": {
"crypto-js": "^4.2.0",
Expand Down
8 changes: 4 additions & 4 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Application setup */
import express, { Express } from 'express';
import mongoose from 'mongoose';
import authRoute from './auth/routes/authRoutes';
import authRoute from './auth-deprecated/routes/authRoutes';
import healthCheckRoute from './healthCheck/routes/healthCheckRoutes';
import { API_URLS_V1 } from './constants';
import logger from './logging/logger';
Expand Down Expand Up @@ -75,18 +75,18 @@ class App {
* Connect to the MongoDB using the connection URL in the configuration object. Exits with error if connection fails.
* @returns Returns a promise indicating completion of the async function.
*/
public async connectToDatabase(): Promise<void> {
public async connectToMongoDB(): Promise<void> {
/* Get connection URL from environment variables */
const mongoUrl: string = Config.get('MONGO_DB.CONNECTION_URL');

/* Try connecting to the database specified by the connection URL, with retries */
/* Try connecting to mongodb, with retries */
let attempts = 0;
const maxAttempts: number = Config.get('MONGO_DB.CONNECTION_RETRIES');
const retryTimeout: number = Config.get('MONGO_DB.CONNECTION_RETRY_TIMEOUT');
while (attempts < maxAttempts) {
try {
await mongoose.connect(mongoUrl);
logger.info('Successfully connected to MongoDB.');
logger.info('Successfully connected to MongoDB');
return;
} catch (error) {
logger.error('Failed to connect to MongoDB: ', error);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function startApp(port: number) {

try {
/* Wait for the MongoDB connection to be established */
await appInstance.connectToDatabase();
await appInstance.connectToMongoDB();

/* Start the server after successful database connection */
await appInstance.startServer(port);
Expand Down
6 changes: 3 additions & 3 deletions backend/tests/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('App Tests', () => {
appInstance.closeServer(PORT);
});

describe('connectToDatabase', () => {
describe('connectToMongoDB', () => {
it('should successfully connect to MongoDB', async () => {
/* Set up mocks and spies */
jest.spyOn(mongoose, 'connect').mockImplementation(() => {
Expand All @@ -42,7 +42,7 @@ describe('App Tests', () => {
});

/* Connect to DB */
await appInstance.connectToDatabase();
await appInstance.connectToMongoDB();

/* Test against expected */
expect(mongoose.connect).toHaveBeenCalled();
Expand All @@ -64,7 +64,7 @@ describe('App Tests', () => {
jest.spyOn(logger, 'info');

/* Connect to database */
await appInstance.connectToDatabase();
await appInstance.connectToMongoDB();

/* Test against expected */
expect(process.exit).toHaveBeenCalledWith(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import Config from 'simple-app-config';
import { NextFunction, Request, Response } from 'express';
import { createRequest, createResponse, MockRequest, MockResponse } from 'node-mocks-http';
import { User } from '../../../src/auth/models/user';
import { FailedLoginUser } from '../../../src/auth/models/failedLoginUser';
import { RefreshToken } from '../../../src/auth/models/refreshToken';
import { LockedOutUser } from '../../../src/auth/models/lockedOutUser';
import AuthController from '../../../src/auth/controllers/authController';
import { User } from '../../../src/auth-deprecated/models/user';
import { FailedLoginUser } from '../../../src/auth-deprecated/models/failedLoginUser';
import { RefreshToken } from '../../../src/auth-deprecated/models/refreshToken';
import { LockedOutUser } from '../../../src/auth-deprecated/models/lockedOutUser';
import AuthController from '../../../src/auth-deprecated/controllers/authController';
import { API_URLS_V1, GENERIC_RESPONSES } from '../../../src/constants';
import { HEADERS, AUTH_RESPONSES } from '../../../src/auth/constants';
import { HEADERS, AUTH_RESPONSES } from '../../../src/auth-deprecated/constants';
import CryptoJS from 'crypto-js';
import mongoose from 'mongoose';
import jwt from 'jsonwebtoken';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* Unit tests for the auth routes */
import request from 'supertest';
import AuthController from '../../../src/auth/controllers/authController';
import AuthController from '../../../src/auth-deprecated/controllers/authController';
import { API_URLS_V1, GENERIC_RESPONSES } from '../../../src/constants';
import App from '../../../src/app';
import Config from 'simple-app-config';

/* Mock the controller functions */
jest.mock('../../../src/auth/controllers/authController', () => ({
jest.mock('../../../src/auth-deprecated/controllers/authController', () => ({
register: jest.fn().mockImplementation((req, res) => {
res.sendStatus(201);
}),
Expand Down

0 comments on commit 7b380d4

Please sign in to comment.