Skip to content

Commit

Permalink
Merge pull request #56 from incredible-phoenix246/chore/unit-testing
Browse files Browse the repository at this point in the history
Chore/unit testing
  • Loading branch information
Idimmusix authored Jul 21, 2024
2 parents 01bd8d7 + 96a992e commit 0630cbc
Show file tree
Hide file tree
Showing 25 changed files with 905 additions and 1,816 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"start:dev": "ts-node-dev --respawn --transpile-only ./src/index",
"start": "ts-node --transpile-only src/index.ts",
"test": "jest --passWithNoTests",
"test": "jest ",
"typeorm": "typeorm-ts-node-commonjs",
"build": "tsc",
"prod": "node dist/index.js"
Expand All @@ -19,6 +19,8 @@
"@types/jest": "^29.5.12",
"@types/node": "^16.18.103",
"@types/supertest": "^6.0.2",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
"ts-node": "^10.9.1",
"typescript": "^5.5.3"
},
Expand All @@ -35,7 +37,7 @@
"dayjs": "^1.11.12",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"express-jwt": "^8.4.1",
"express-validator": "^7.1.0",
"handlebars": "^4.7.8",
"jest": "^29.7.0",
"jsonwebtoken": "^9.0.2",
Expand All @@ -45,6 +47,8 @@
"pino-pretty": "^11.2.1",
"reflect-metadata": "^0.1.14",
"supertest": "^7.0.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1",
"ts-jest": "^29.2.3",
"ts-node-dev": "^2.0.0",
"typeorm": "^0.3.20"
Expand Down
123 changes: 123 additions & 0 deletions src/controllers/AuthController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,56 @@ import { AuthService } from "../services";

const authService = new AuthService();

/**
* @swagger
* tags:
* name: Auth
* description: Authentication related routes
*/

/**
* @swagger
* api/v1/auth/signup:
* post:
* summary: Sign up a new user
* tags: [Auth]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* firstName:
* type: string
* lastName:
* type: string
* email:
* type: string
* password:
* type: string
* phone:
* type: string
* responses:
* 201:
* description: The user was successfully created
* content:
* application/json:
* schema:
* type: object
* properties:
* mailSent:
* type: string
* newUser:
* type: object
* access_token:
* type: string
* 409:
* description: User already exists
* 500:
* description: Some server error
*/

const signUp = async (req: Request, res: Response, next: NextFunction) => {
try {
const { mailSent, newUser, access_token } = await authService.signUp(
Expand All @@ -14,6 +64,44 @@ const signUp = async (req: Request, res: Response, next: NextFunction) => {
}
};

/**
* @swagger
* /api/v1/auth/verify-otp:
* post:
* summary: Verify the user's email using OTP
* tags: [Auth]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* otp:
* type: integer
* description: The OTP sent to the user's email
* token:
* type: string
* description: The token received during sign up
* responses:
* 200:
* description: OTP verified successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* description: Success message
* 400:
* description: Invalid OTP or verification token has expired
* 404:
* description: User not found
* 500:
* description: Some server error
*/

const verifyOtp = async (req: Request, res: Response, next: NextFunction) => {
try {
const { otp, token } = req.body;
Expand All @@ -24,6 +112,41 @@ const verifyOtp = async (req: Request, res: Response, next: NextFunction) => {
}
};

/**
* @swagger
* /api/v1/auth/login:
* post:
* summary: Log in a user
* tags: [Auth]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* email:
* type: string
* password:
* type: string
* responses:
* 200:
* description: Successful login
* content:
* application/json:
* schema:
* type: object
* properties:
* access_token:
* type: string
* user:
* type: object
* 401:
* description: Invalid credentials
* 500:
* description: Some server error
*/

const login = async (req: Request, res: Response, next: NextFunction) => {
try {
const { access_token, user } = await authService.login(req.body);
Expand Down
136 changes: 136 additions & 0 deletions src/controllers/TestimonialsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { Request, Response } from "express";
import { AppDataSource } from "../data-source";
import { Testimonial } from "../models/Testimonial";

export default class TestimonialsController {
/**
* @swagger
* tags:
* name: Testimonials
* description: Testimonial related routes
*/

/**
* @swagger
* api/v1/testimonials:
* post:
* summary: Create a new testimonial
* tags: [Testimonials]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* client_name:
* type: string
* client_position:
* type: string
* testimonial:
* type: string
* responses:
* 201:
* description: Testimonial created successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* status_code:
* type: integer
* data:
* type: object
* 500:
* description: Some server error
*/
public async createTestimonial(req: Request, res: Response) {
const { client_name, client_position, testimonial } = req.body;

// Get the user ID from the request
const userId = (req as Record<string, any>).user.id;

// Create a new testimonial
const testimonialInstance = AppDataSource.getRepository(Testimonial).create(
{
user_id: userId,
client_name,
client_position,
testimonial,
}
);

// Save the testimonial
await AppDataSource.getRepository(Testimonial).save(testimonialInstance);

// Return the testimonial
res.status(201).json({
message: "Testimonial created successfully",
status_code: 201,
data: testimonialInstance,
});
}

/**
* @swagger
* api/v1/testimonials/{id}:
* get:
* summary: Retrieve a testimonial by ID
* tags: [Testimonials]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* description: Testimonial ID
* responses:
* 200:
* description: Testimonial retrieved successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* status_code:
* type: integer
* data:
* type: object
* 404:
* description: Testimonial not found
* 500:
* description: Some server error
*/

public async getTestimonial(req: Request, res: Response) {
try {
// Get the user ID from the request
// const userId = (req as Record<string, any>).user.id;
const { testimonial_id } = req.params;

// Get the testimonial
const testimonial = await AppDataSource.getRepository(
Testimonial
).findOne({ where: { id: testimonial_id } });

if (!testimonial) {
return res
.status(404)
.send({ message: "Testimonial not found", status_code: 404 });
}

// Return the testimonial
res.status(200).json({
message: "Testimonial retrieved successfully",
status_code: 200,
data: testimonial,
});
} catch (error) {
res.status(500).send({ message: error.message });
}
}
}
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import express, { Express, Request, Response } from "express";
import config from "./config";
import dotenv from "dotenv";
import cors from "cors";
import { userRouter, authRoute } from "./routes";
import { notificationRouter } from "./routes/notificationsettings"
import { userRouter, authRoute, testimonialRoute } from "./routes";
import { notificationRouter } from "./routes/notificationsettings";

import { routeNotFound, errorHandler } from "./middleware";
import swaggerUi from "swagger-ui-express";
import swaggerSpec from "./swaggerConfig";

dotenv.config();

Expand All @@ -35,17 +38,24 @@ server.get("/", (req: Request, res: Response) => {
});
server.use("/api/v1", userRouter);
server.use("/api/v1/auth", authRoute);
server.use("/api/v1", testimonialRoute);
server.use("/api/v1/docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
server.use(routeNotFound);
server.use(errorHandler);
server.use("/api/v1/settings", notificationRouter);

AppDataSource.initialize()
.then(async () => {
// seed().catch(log.error);
server.use(express.json());
server.get("/", (req: Request, res: Response) => {
res.send("Hello world");
});

server.listen(port, () => {
log.info(`Server is listening on port ${port}`);
});
})
.catch((error) => console.error(error));

export default server;

Loading

0 comments on commit 0630cbc

Please sign in to comment.