-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Querries):Admin querry management
- Loading branch information
YvetteNyibuka
committed
Jul 26, 2024
1 parent
d71149f
commit 4376142
Showing
16 changed files
with
579 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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!"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/database/migrations/20240726051557-extend-message-length.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.