Skip to content

Commit

Permalink
Merge pull request #82 from atlp-rwanda/ft-Querries
Browse files Browse the repository at this point in the history
Admin query management
  • Loading branch information
leandreAlly authored Jul 26, 2024
2 parents d71149f + 4376142 commit 8ac7857
Show file tree
Hide file tree
Showing 16 changed files with 579 additions and 5 deletions.
100 changes: 100 additions & 0 deletions src/__test__/querries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import app from "../app";
import request from "supertest";
import database_models, {
connectionToDatabase,
} from "../database/config/db.config";
import { deleteTableData } from "../utils/database.utils";
import { NewUser } from "../mock/static";
import { v4 as uuidV4 } from "uuid";
import { Token } from "../database/models/token";
import { log } from "console";

jest.setTimeout(30000);

function logErrors(
err: { stack: any },
_req: any,
_res: any,
next: (arg0: any) => void,
) {
console.log(err.stack);
next(err);
}

const Jest_request = request(app.use(logErrors));

let token = "";
let querryId: string;

describe("QUERRIES TEST", () => {
beforeAll(async () => {
await connectionToDatabase();
});
afterAll(async () => {
await deleteTableData(database_models.User, "users");
await deleteTableData(database_models.Querries, "querries");
await deleteTableData(database_models.Token, "tokens");
});
it("it should register a user and return 201", async () => {
const { body } = await Jest_request.post("/api/v1/users/register")
.send(NewUser)
.expect(201);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual(
"Account Created successfully, Please Verify your Account",
);

const tokenRecord = await Token.findOne();
token = tokenRecord?.dataValues.token ?? "";
});

it("should successfully login a user and return 200", async () => {
const loginUser = {
email: NewUser.email,
password: NewUser.password,
};

await database_models.User.update(
{ isVerified: true, role: "12afd4f1-0bed-4a3b-8ad5-0978dabf8fcd" },
{ where: { email: loginUser.email } },
);

const { body } = await Jest_request.post("/api/v1/users/login")
.send(loginUser)
.expect(200);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Login successfully!");
expect(body.data).toBeDefined();
token = body.data;
});

// =================================QUERRIES TESTS================================

it("It should create querry when customer want to communicate to app owner", async () => {
const newQuerry = {
firstName: "Izanyibuka",
lastName: "Yvette",
subject: "Biiter",
email: "[email protected]",
message: "I hated this app",
};
const { body } = await Jest_request.post("/api/v1/querries")
.send(newQuerry)
.expect(201);
expect(body.status).toStrictEqual("CREATED");
querryId = body.data.id;
});

it("It should return all querries", async () => {
const { body } = await Jest_request.get("/api/v1/querries").set(
"Authorization",
`Bearer ${token}`,
);
});
it("It should return a single querry", async () => {
const { body } = await Jest_request.get(`/api/v1/querries/${querryId}`).set(
"Authorization",
`Bearer ${token}`,
);
});
});
34 changes: 34 additions & 0 deletions src/__test__/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,38 @@ describe("SERVER API TEST", () => {
expect(body.status).toStrictEqual("SERVER ERROR");
expect(body.message).toBe("Something went wrong!");
});

//===============================Querries======================
it("should return 500 when something went wrong on creating a querry", async () => {
(read_function as jest.Mock).mockRejectedValueOnce(new Error("Test error"));
const { body } = await Jest_request.post(`/api/v1/querries`)
.send({
firstName: "john",
lastName: "doe",
subject: "Biiter",
email: "[email protected]",
message: "I hated this app",
})
.expect(500);
expect(body.status).toStrictEqual("SERVER ERROR");
expect(body.message).toBe("Something went wrong!");
});

it("should return 500 when something went wrong on getting all querries", async () => {
(read_function as jest.Mock).mockRejectedValueOnce(new Error("Test error"));
const { body } = await Jest_request.get(`/api/v1/querries`)
.set("Authorization", `Bearer ${token}`)
.expect(500);
expect(body.status).toStrictEqual("SERVER ERROR");
expect(body.message).toBe("Something went wrong!");
});

it("should return 500 when something went wrong on getting a single querry", async () => {
(read_function as jest.Mock).mockRejectedValueOnce(new Error("Test error"));
const { body } = await Jest_request.get(`/api/v1/querries/{id}`)
.set("Authorization", `Bearer ${token}`)
.expect(500);
expect(body.status).toStrictEqual("SERVER ERROR");
expect(body.message).toBe("Something went wrong!");
});
});
94 changes: 94 additions & 0 deletions src/controllers/querriesController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Request, Response } from "express";
import { sendResponse } from "../utils/http.exception";
import { insert_function, read_function } from "../utils/db_methods";
import { querriesModelAttributes } from "../types/model";
import { Querries } from "../database/models/querry";

export const createQuerry = async (req: Request, res: Response) => {
try {
const { firstName, lastName, email, subject, message } = req.body;

const newQuerry = await insert_function<Querries>("Querries", "create", {
firstName,
lastName,
email,
subject,
message,
});

return sendResponse(
res,
201,
"CREATED",
"Message sent successfully",
newQuerry,
);
} catch (error: unknown) {
return sendResponse(
res,
500,
"SERVER ERROR",
"Something went wrong!",
error as Error,
);
}
};

export const read_allQuerries = async (req: Request, res: Response) => {
try {
const querries = await read_function<querriesModelAttributes>(
"Querries",
"findAll",
);

return sendResponse(
res,
200,
"SUCCESS",
"Querries fetched successfully",
querries,
);
} catch (error: unknown) {
return sendResponse(
res,
500,
"SERVER ERROR",
"Something went wrong!",
error as Error,
);
}
};

export const read_oneQuerry = async (req: Request, res: Response) => {
try {
const querries = await read_function<querriesModelAttributes>(
"Querries",
"findOne",
{
where: {
id: req.params.id,
},
},
);

if (!querries) {
return sendResponse(res, 404, "NOT FOUND", "Querry not found");
}

return sendResponse(
res,
200,
"SUCCESS",
"Querry fetched successfully",
querries,
);
} catch (error: unknown) {
return sendResponse(
res,
500,
"SERVER ERROR",
"Something went wrong!",
error as Error,
);
}
};
51 changes: 51 additions & 0 deletions src/database/migrations/20240724174446-create_Querry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use strict";

const { UUIDV4 } = require("sequelize");

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable("querries", {
id: {
type: Sequelize.UUID,
defaultValue: UUIDV4,
primaryKey: true,
allowNull: false,
},

firstName: {
type: Sequelize.STRING,
allowNull: false,
},
lastName: {
type: Sequelize.STRING,
allowNull: false,
},
subject: {
type: Sequelize.STRING,
allowNull: false,
},
email: {
type: Sequelize.STRING,
allowNull: false,
},
message: {
type: Sequelize.STRING,
allowNull: false,
},

createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
});
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async down(queryInterface, Sequelize) {
await queryInterface.dropTable("querries");
},
};
17 changes: 17 additions & 0 deletions src/database/migrations/20240726051557-extend-message-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.changeColumn("querries", "message", {
type: Sequelize.TEXT,
allowNull: false,
});
},

down: async (queryInterface, Sequelize) => {
await queryInterface.changeColumn("querries", "message", {
type: Sequelize.STRING(255),
allowNull: false,
});
},
};
7 changes: 4 additions & 3 deletions src/database/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import blacklist_model from "./blacklist";
import cart_model from "./cart";
import category_model from "./category";
import message_model from "./message";
//import order_model from "./order";
import paymemt_model from "./payments";
import product_model from "./product";
import reset_model from "./resetPassword";
import Role_model from "./role";
//import sales_model from "./sales";
import token_model from "./token";
import wish_model from "./wishlist";
import notification_model from "./notification";

import reviewModel from "./review";
import order_model from "./order";
import sales_model from "./sales";
import querries_model from "./querry";

const Models = (sequelize: Sequelize) => {
const Product = product_model(sequelize);
const Category = category_model(sequelize);
Expand All @@ -32,9 +32,9 @@ const Models = (sequelize: Sequelize) => {
const Order = order_model(sequelize);
const Sales = sales_model(sequelize);
const Notification = notification_model(sequelize);
const Querries = querries_model(sequelize);

const review = reviewModel(sequelize);
//const Order = order_model(sequelize);
return {
Product,
Category,
Expand All @@ -51,6 +51,7 @@ const Models = (sequelize: Sequelize) => {
Order,
Sales,
Notification,
Querries,
};
};

Expand Down
53 changes: 53 additions & 0 deletions src/database/models/querry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { DataTypes, Model, Sequelize, UUIDV4 } from "sequelize";
import { querriesModelAttributes } from "../../types/model";

export class Querries extends Model<querriesModelAttributes> {
public id!: string;
public firstName!: string;
public lastName!: boolean;
public subject!: string;
public email!: string;
public message!: string;
}

const querries_model = (sequelize: Sequelize) => {
Querries.init(
{
id: {
type: DataTypes.UUID,
defaultValue: UUIDV4,
primaryKey: true,
allowNull: false,
},
firstName: {
type: DataTypes.STRING,
allowNull: false,
},
lastName: {
type: DataTypes.STRING,
allowNull: false,
},
subject: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
},
message: {
type: DataTypes.TEXT,
allowNull: false,
},
},

{
sequelize,
tableName: "querries",
},
);

return Querries;
};

export default querries_model;
Loading

0 comments on commit 8ac7857

Please sign in to comment.