Compare commits
3 Commits
28671146d1
...
355cb0ec90
Author | SHA1 | Date | |
---|---|---|---|
355cb0ec90 | |||
19d265a0e6 | |||
a6593cb76f |
@ -28,6 +28,6 @@
|
|||||||
"formatter": {
|
"formatter": {
|
||||||
"indentStyle": "tab",
|
"indentStyle": "tab",
|
||||||
"indentWidth": 2,
|
"indentWidth": 2,
|
||||||
"lineWidth": 64
|
"lineWidth": 80
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -22,10 +22,7 @@ const logger = new Logger({
|
|||||||
* - error: "exist" if the user already exists
|
* - error: "exist" if the user already exists
|
||||||
* - Otherwise, the registered user data
|
* - Otherwise, the registered user data
|
||||||
*/
|
*/
|
||||||
async function registerUser(
|
async function registerUser(req: Request, res: Response): Promise<unknown> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<unknown> {
|
|
||||||
const body = req.body;
|
const body = req.body;
|
||||||
if (!body) {
|
if (!body) {
|
||||||
logger.warn(`Invalid input data (${req.ip})`);
|
logger.warn(`Invalid input data (${req.ip})`);
|
||||||
@ -59,8 +56,7 @@ async function registerUser(
|
|||||||
lastName: `${body.lastName}`,
|
lastName: `${body.lastName}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
const RegisterServiceResult =
|
const RegisterServiceResult = await UserService.register(sanitizeData);
|
||||||
await UserService.register(sanitizeData);
|
|
||||||
|
|
||||||
if (RegisterServiceResult.error === "gdprNotApproved") {
|
if (RegisterServiceResult.error === "gdprNotApproved") {
|
||||||
logger.warn(`GDPR not approved (${req.ip})`);
|
logger.warn(`GDPR not approved (${req.ip})`);
|
||||||
@ -79,10 +75,7 @@ async function registerUser(
|
|||||||
|
|
||||||
// SUCCESS
|
// SUCCESS
|
||||||
logger.info(`User registered successfully (${req.ip})`);
|
logger.info(`User registered successfully (${req.ip})`);
|
||||||
return res
|
return res.type("application/json").status(201).json(RegisterServiceResult);
|
||||||
.type("application/json")
|
|
||||||
.status(201)
|
|
||||||
.json(RegisterServiceResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -93,10 +86,7 @@ async function registerUser(
|
|||||||
*
|
*
|
||||||
* @return {Promise<void>} A promise that resolves when the user is logged in or rejects with an error.
|
* @return {Promise<void>} A promise that resolves when the user is logged in or rejects with an error.
|
||||||
*/
|
*/
|
||||||
async function loginUser(
|
async function loginUser(req: Request, res: Response): Promise<void> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<void> {
|
|
||||||
const body = req.body;
|
const body = req.body;
|
||||||
if (!body) {
|
if (!body) {
|
||||||
res.type("application/json").status(400).json({
|
res.type("application/json").status(400).json({
|
||||||
@ -131,10 +121,7 @@ async function loginUser(
|
|||||||
message: "Invalid password.",
|
message: "Invalid password.",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
res
|
res.type("application/json").status(200).json(LoginServiceResult);
|
||||||
.type("application/json")
|
|
||||||
.status(200)
|
|
||||||
.json(LoginServiceResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getAllUsers(req: Request, res: Response) {
|
async function getAllUsers(req: Request, res: Response) {
|
||||||
@ -170,10 +157,7 @@ async function getAllUsers(req: Request, res: Response) {
|
|||||||
error: "Internal server error",
|
error: "Internal server error",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return res
|
return res.type("application/json").status(200).json(AllUserResponse);
|
||||||
.type("application/json")
|
|
||||||
.status(200)
|
|
||||||
.json(AllUserResponse);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getUser(req: Request, res: Response) {
|
async function getUser(req: Request, res: Response) {
|
||||||
@ -267,14 +251,11 @@ async function editUser(req: Request, res: Response) {
|
|||||||
//TODO Interface
|
//TODO Interface
|
||||||
const modifiedData = {};
|
const modifiedData = {};
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
if (body.firstName)
|
if (body.firstName) modifiedData.firstName = `${body.firstName}`;
|
||||||
modifiedData.firstName = `${body.firstName}`;
|
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
if (body.lastName)
|
if (body.lastName) modifiedData.lastName = `${body.lastName}`;
|
||||||
modifiedData.lastName = `${body.lastName}`;
|
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
if (body.displayName)
|
if (body.displayName) modifiedData.displayName = `${body.displayName}`;
|
||||||
modifiedData.displayName = `${body.displayName}`;
|
|
||||||
//TODO Case handled with hashing by the service.
|
//TODO Case handled with hashing by the service.
|
||||||
//if (body.password) modifiedData.password = `${body.password}`;
|
//if (body.password) modifiedData.password = `${body.password}`;
|
||||||
|
|
||||||
@ -290,31 +271,21 @@ async function editUser(req: Request, res: Response) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (EditUserServiceResult.error !== "none") {
|
if (EditUserServiceResult.error !== "none") {
|
||||||
logger.error(
|
logger.error(`Error occurred during user edit (${req.ip})`);
|
||||||
`Error occurred during user edit (${req.ip})`,
|
|
||||||
);
|
|
||||||
return res.type("application/json").status(500).json({
|
return res.type("application/json").status(500).json({
|
||||||
error: "Internal server error",
|
error: "Internal server error",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return res
|
return res.type("application/json").status(200).json(EditUserServiceResult);
|
||||||
.type("application/json")
|
|
||||||
.status(200)
|
|
||||||
.json(EditUserServiceResult);
|
|
||||||
}
|
}
|
||||||
//Not itself or
|
//Not itself or
|
||||||
logger.warn(
|
logger.warn(`Unauthorized access attempt, not self or admin (${req.ip})`);
|
||||||
`Unauthorized access attempt, not self or admin (${req.ip})`,
|
|
||||||
);
|
|
||||||
return res.type("application/json").status(403).json({
|
return res.type("application/json").status(403).json({
|
||||||
error: "Unauthorized",
|
error: "Unauthorized",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteUser(
|
async function deleteUser(req: Request, res: Response): Promise<Response> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const authHeader = req.headers.authorization;
|
const authHeader = req.headers.authorization;
|
||||||
const bearerToken = authHeader?.split(" ")[1];
|
const bearerToken = authHeader?.split(" ")[1];
|
||||||
if (!bearerToken) {
|
if (!bearerToken) {
|
||||||
@ -340,13 +311,9 @@ async function deleteUser(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (sourceUser.is_admin || sourceUser.id === payload.sub) {
|
if (sourceUser.is_admin || sourceUser.id === payload.sub) {
|
||||||
const deleteUserServiceResult = await UserService.delete(
|
const deleteUserServiceResult = await UserService.delete(`${targetUserId}`);
|
||||||
`${targetUserId}`,
|
|
||||||
);
|
|
||||||
if (!deleteUserServiceResult) {
|
if (!deleteUserServiceResult) {
|
||||||
logger.error(
|
logger.error(`Error occurred during user delete (${req.ip})`);
|
||||||
`Error occurred during user delete (${req.ip})`,
|
|
||||||
);
|
|
||||||
return res.type("application/json").status(500).json({
|
return res.type("application/json").status(500).json({
|
||||||
error: "Internal server error",
|
error: "Internal server error",
|
||||||
});
|
});
|
||||||
|
@ -17,14 +17,9 @@ const logger = new Logger({
|
|||||||
*
|
*
|
||||||
* @returns A Promise that resolves to the response object with a status code and a JSON message indicating the success or failure of brand creation.
|
* @returns A Promise that resolves to the response object with a status code and a JSON message indicating the success or failure of brand creation.
|
||||||
*/
|
*/
|
||||||
async function createBrand(
|
async function createBrand(req: Request, res: Response): Promise<Response> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const body: IDbBrand = req.body;
|
const body: IDbBrand = req.body;
|
||||||
const doesExist = await BrandService.getBySlug(
|
const doesExist = await BrandService.getBySlug(`${body.slug_name}`);
|
||||||
`${body.slug_name}`,
|
|
||||||
);
|
|
||||||
if (doesExist) {
|
if (doesExist) {
|
||||||
logger.error("Brand already exists");
|
logger.error("Brand already exists");
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
@ -42,9 +37,7 @@ async function createBrand(
|
|||||||
error: "Failed to create brand",
|
error: "Failed to create brand",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
logger.info(
|
logger.info(`Brand created successfully ! (${body.slug_name})`);
|
||||||
`Brand created successfully ! (${body.slug_name})`,
|
|
||||||
);
|
|
||||||
return res.status(201).json({
|
return res.status(201).json({
|
||||||
message: "Brand created successfully",
|
message: "Brand created successfully",
|
||||||
});
|
});
|
||||||
@ -57,10 +50,7 @@ async function createBrand(
|
|||||||
* @param {Response} res - The HTTP response object.
|
* @param {Response} res - The HTTP response object.
|
||||||
* @return {Promise<Response>} A promise that resolves with the HTTP response.
|
* @return {Promise<Response>} A promise that resolves with the HTTP response.
|
||||||
*/
|
*/
|
||||||
async function updateBrand(
|
async function updateBrand(req: Request, res: Response): Promise<Response> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const body: IDbBrand = req.body;
|
const body: IDbBrand = req.body;
|
||||||
const brandSlug = req.params["brandSlug"];
|
const brandSlug = req.params["brandSlug"];
|
||||||
if (!brandSlug) {
|
if (!brandSlug) {
|
||||||
@ -100,10 +90,7 @@ async function updateBrand(
|
|||||||
* @param {Response} res - The response object to send the result.
|
* @param {Response} res - The response object to send the result.
|
||||||
* @returns {Promise<Response>} - A promise that resolves to the response with the retrieved brand.
|
* @returns {Promise<Response>} - A promise that resolves to the response with the retrieved brand.
|
||||||
*/
|
*/
|
||||||
async function getBySlugBrand(
|
async function getBySlugBrand(req: Request, res: Response): Promise<Response> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const brandSlug = req.params["brandSlug"];
|
const brandSlug = req.params["brandSlug"];
|
||||||
if (!brandSlug) {
|
if (!brandSlug) {
|
||||||
logger.error("Brand slug is missing");
|
logger.error("Brand slug is missing");
|
||||||
@ -129,10 +116,7 @@ async function getBySlugBrand(
|
|||||||
* @param {Response} res - The response object.
|
* @param {Response} res - The response object.
|
||||||
* @returns {Promise<Response>} - A promise with the response object.
|
* @returns {Promise<Response>} - A promise with the response object.
|
||||||
*/
|
*/
|
||||||
async function getAllBrand(
|
async function getAllBrand(_req: Request, res: Response): Promise<Response> {
|
||||||
_req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const brands = await BrandService.getAll();
|
const brands = await BrandService.getAll();
|
||||||
if (!brands) {
|
if (!brands) {
|
||||||
logger.error("Failed to retrieve brands");
|
logger.error("Failed to retrieve brands");
|
||||||
@ -156,10 +140,7 @@ async function getAllBrand(
|
|||||||
* @param {Response} res - The response object.
|
* @param {Response} res - The response object.
|
||||||
* @returns {Promise<Response>} - The response object indicating the success or failure of the delete operation.
|
* @returns {Promise<Response>} - The response object indicating the success or failure of the delete operation.
|
||||||
*/
|
*/
|
||||||
async function deleteBrand(
|
async function deleteBrand(req: Request, res: Response): Promise<Response> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const brandSlug = req.params["brandSlug"];
|
const brandSlug = req.params["brandSlug"];
|
||||||
if (!brandSlug) {
|
if (!brandSlug) {
|
||||||
logger.error("Brand slug is missing");
|
logger.error("Brand slug is missing");
|
||||||
|
@ -15,14 +15,9 @@ const logger = new Logger({
|
|||||||
* @param {Response} res - The response object to send back to the client.
|
* @param {Response} res - The response object to send back to the client.
|
||||||
* @returns {Promise<Response>} The response object indicating the outcome of the category creation.
|
* @returns {Promise<Response>} The response object indicating the outcome of the category creation.
|
||||||
*/
|
*/
|
||||||
async function createCategory(
|
async function createCategory(req: Request, res: Response): Promise<Response> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const body: IDbCategory = req.body;
|
const body: IDbCategory = req.body;
|
||||||
const doesExist = await CategoryService.getBySlug(
|
const doesExist = await CategoryService.getBySlug(`${body.slug_name}`);
|
||||||
`${body.slug_name}`,
|
|
||||||
);
|
|
||||||
if (doesExist) {
|
if (doesExist) {
|
||||||
logger.error("Category already exists");
|
logger.error("Category already exists");
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
@ -39,9 +34,7 @@ async function createCategory(
|
|||||||
error: "Failed to create category",
|
error: "Failed to create category",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
logger.info(
|
logger.info(`Category created successfully ! (${body.slug_name})`);
|
||||||
`Category created successfully ! (${body.slug_name})`,
|
|
||||||
);
|
|
||||||
return res.status(201).json({
|
return res.status(201).json({
|
||||||
message: "Category created successfully",
|
message: "Category created successfully",
|
||||||
});
|
});
|
||||||
@ -55,10 +48,7 @@ async function createCategory(
|
|||||||
*
|
*
|
||||||
* @return {Promise<Response>} - A promise that will be resolved with the result of the update operation.
|
* @return {Promise<Response>} - A promise that will be resolved with the result of the update operation.
|
||||||
*/
|
*/
|
||||||
async function updateCategory(
|
async function updateCategory(req: Request, res: Response): Promise<Response> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const body: IDbCategory = req.body;
|
const body: IDbCategory = req.body;
|
||||||
const categoryId = req.params["categorySlug"];
|
const categoryId = req.params["categorySlug"];
|
||||||
if (!categoryId) {
|
if (!categoryId) {
|
||||||
@ -67,9 +57,7 @@ async function updateCategory(
|
|||||||
error: "Category slug is missing",
|
error: "Category slug is missing",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const doesExist = await CategoryService.getById(
|
const doesExist = await CategoryService.getById(`${categoryId}`);
|
||||||
`${categoryId}`,
|
|
||||||
);
|
|
||||||
if (!doesExist || !doesExist.id) {
|
if (!doesExist || !doesExist.id) {
|
||||||
logger.error("Category not found");
|
logger.error("Category not found");
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
@ -100,10 +88,7 @@ async function updateCategory(
|
|||||||
* @param {Response} res - The response object to send the result.
|
* @param {Response} res - The response object to send the result.
|
||||||
* @returns {Promise<Response>} A Promise that resolves to the response object.
|
* @returns {Promise<Response>} A Promise that resolves to the response object.
|
||||||
*/
|
*/
|
||||||
async function deleteCategory(
|
async function deleteCategory(req: Request, res: Response): Promise<Response> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const categorySlug = req.params["categorySlug"];
|
const categorySlug = req.params["categorySlug"];
|
||||||
if (!categorySlug) {
|
if (!categorySlug) {
|
||||||
logger.error("Category slug is missing");
|
logger.error("Category slug is missing");
|
||||||
@ -111,27 +96,21 @@ async function deleteCategory(
|
|||||||
error: "Category slug is missing",
|
error: "Category slug is missing",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const doesExist = await CategoryService.getBySlug(
|
const doesExist = await CategoryService.getBySlug(`${categorySlug}`);
|
||||||
`${categorySlug}`,
|
|
||||||
);
|
|
||||||
if (!doesExist || !doesExist.id) {
|
if (!doesExist || !doesExist.id) {
|
||||||
logger.error("Category not found");
|
logger.error("Category not found");
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
error: "Category not found",
|
error: "Category not found",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const deleteResult = await CategoryService.delete(
|
const deleteResult = await CategoryService.delete(`${doesExist.id}`);
|
||||||
`${doesExist.id}`,
|
|
||||||
);
|
|
||||||
if (!deleteResult) {
|
if (!deleteResult) {
|
||||||
logger.error("Failed to delete category");
|
logger.error("Failed to delete category");
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
error: "Failed to delete category",
|
error: "Failed to delete category",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
logger.info(
|
logger.info(`Category deleted successfully! (${categorySlug})`);
|
||||||
`Category deleted successfully! (${categorySlug})`,
|
|
||||||
);
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
message: "Category deleted successfully",
|
message: "Category deleted successfully",
|
||||||
});
|
});
|
||||||
@ -144,10 +123,7 @@ async function deleteCategory(
|
|||||||
* @param {Response} res - The response object.
|
* @param {Response} res - The response object.
|
||||||
* @return {Promise<Response>} - A promise that resolves to the response object.
|
* @return {Promise<Response>} - A promise that resolves to the response object.
|
||||||
*/
|
*/
|
||||||
async function getAllCategory(
|
async function getAllCategory(_req: Request, res: Response): Promise<Response> {
|
||||||
_req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const categories = await CategoryService.getAll();
|
const categories = await CategoryService.getAll();
|
||||||
if (!categories) {
|
if (!categories) {
|
||||||
logger.error("Failed to get categories");
|
logger.error("Failed to get categories");
|
||||||
@ -186,18 +162,14 @@ async function getBySlugCategory(
|
|||||||
error: "Category slug is missing",
|
error: "Category slug is missing",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const category = await CategoryService.getBySlug(
|
const category = await CategoryService.getBySlug(`${categorySlug}`);
|
||||||
`${categorySlug}`,
|
|
||||||
);
|
|
||||||
if (!category || !category.id) {
|
if (!category || !category.id) {
|
||||||
logger.error("Category not found");
|
logger.error("Category not found");
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
error: "Category not found",
|
error: "Category not found",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
logger.info(
|
logger.info(`Category retrieved successfully! (${categorySlug})`);
|
||||||
`Category retrieved successfully! (${categorySlug})`,
|
|
||||||
);
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
id: category.id,
|
id: category.id,
|
||||||
display_name: category.display_name,
|
display_name: category.display_name,
|
||||||
|
@ -9,14 +9,9 @@ const logger = new Logger({
|
|||||||
name: "ModelController",
|
name: "ModelController",
|
||||||
});
|
});
|
||||||
|
|
||||||
async function createModel(
|
async function createModel(req: Request, res: Response): Promise<Response> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const body: IDbModel = req.body;
|
const body: IDbModel = req.body;
|
||||||
const doesExist = await CategoryService.getBySlug(
|
const doesExist = await CategoryService.getBySlug(`${body.slug_name}`);
|
||||||
`${body.slug_name}`,
|
|
||||||
);
|
|
||||||
if (doesExist) {
|
if (doesExist) {
|
||||||
logger.error("Category already exists");
|
logger.error("Category already exists");
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
@ -38,22 +33,15 @@ async function createModel(
|
|||||||
error: "Failed to create category",
|
error: "Failed to create category",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
logger.info(
|
logger.info(`Category created successfully ! (${body.slug_name})`);
|
||||||
`Category created successfully ! (${body.slug_name})`,
|
|
||||||
);
|
|
||||||
return res.status(201).json({
|
return res.status(201).json({
|
||||||
message: "Category created successfully",
|
message: "Category created successfully",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateModel(
|
async function updateModel(req: Request, res: Response): Promise<Response> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const body: IDbModel = req.body;
|
const body: IDbModel = req.body;
|
||||||
const doesExist = await ModelService.getBySlug(
|
const doesExist = await ModelService.getBySlug(`${body.slug_name}`);
|
||||||
`${body.slug_name}`,
|
|
||||||
);
|
|
||||||
if (!doesExist) {
|
if (!doesExist) {
|
||||||
logger.error("Model does not exist");
|
logger.error("Model does not exist");
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
@ -76,9 +64,7 @@ async function updateModel(
|
|||||||
error: "Failed to update model",
|
error: "Failed to update model",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
logger.info(
|
logger.info(`Model updated successfully! (${body.slug_name})`);
|
||||||
`Model updated successfully! (${body.slug_name})`,
|
|
||||||
);
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
message: "Model updated successfully",
|
message: "Model updated successfully",
|
||||||
});
|
});
|
||||||
@ -99,10 +85,7 @@ async function getAllModel(res: Response): Promise<Response> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getModelBySlug(
|
async function getModelBySlug(req: Request, res: Response): Promise<Response> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const slug = req.params["modelSlug"];
|
const slug = req.params["modelSlug"];
|
||||||
if (!slug) {
|
if (!slug) {
|
||||||
logger.error("Invalid slug");
|
logger.error("Invalid slug");
|
||||||
@ -122,10 +105,7 @@ async function getModelBySlug(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteModel(
|
async function deleteModel(req: Request, res: Response): Promise<Response> {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
): Promise<Response> {
|
|
||||||
const modelSlug = req.params["modelSlug"];
|
const modelSlug = req.params["modelSlug"];
|
||||||
if (!modelSlug) {
|
if (!modelSlug) {
|
||||||
logger.error("Invalid model slug");
|
logger.error("Invalid model slug");
|
||||||
@ -141,9 +121,7 @@ async function deleteModel(
|
|||||||
error: "Failed to delete model",
|
error: "Failed to delete model",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
logger.info(
|
logger.info(`Model deleted successfully! (SLUG: ${modelSlug})`);
|
||||||
`Model deleted successfully! (SLUG: ${modelSlug})`,
|
|
||||||
);
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
message: "Model deleted successfully",
|
message: "Model deleted successfully",
|
||||||
});
|
});
|
||||||
|
@ -10,25 +10,16 @@ AuthRouter.route("/register").post(AuthController.register);
|
|||||||
|
|
||||||
// PATCH
|
// PATCH
|
||||||
//TODO - To test
|
//TODO - To test
|
||||||
AuthRouter.route("/me").patch(
|
AuthRouter.route("/me").patch(UserGuard, AuthController.editUser);
|
||||||
UserGuard,
|
|
||||||
AuthController.editUser,
|
|
||||||
);
|
|
||||||
|
|
||||||
// GET
|
// GET
|
||||||
AuthRouter.route("/me").get(UserGuard, AuthController.getSelf);
|
AuthRouter.route("/me").get(UserGuard, AuthController.getSelf);
|
||||||
|
|
||||||
// DELETE
|
// DELETE
|
||||||
AuthRouter.route("/me").delete(
|
AuthRouter.route("/me").delete(UserGuard, AuthController.deleteSelf);
|
||||||
UserGuard,
|
|
||||||
AuthController.deleteSelf,
|
|
||||||
);
|
|
||||||
|
|
||||||
// GET
|
// GET
|
||||||
AuthRouter.route("/all").get(
|
AuthRouter.route("/all").get(AdminGuard, AuthController.getAllUsers);
|
||||||
AdminGuard,
|
|
||||||
AuthController.getAllUsers,
|
|
||||||
);
|
|
||||||
|
|
||||||
// GET
|
// GET
|
||||||
AuthRouter.route("/user/:targetId")
|
AuthRouter.route("/user/:targetId")
|
||||||
|
@ -9,10 +9,7 @@ const CatalogRouter: Router = express.Router();
|
|||||||
|
|
||||||
//-- MODELS >>
|
//-- MODELS >>
|
||||||
|
|
||||||
CatalogRouter.route("/model/new").get(
|
CatalogRouter.route("/model/new").get(AdminGuard, ModelController.create);
|
||||||
AdminGuard,
|
|
||||||
ModelController.create,
|
|
||||||
);
|
|
||||||
|
|
||||||
CatalogRouter.route("/model/all").get(ModelController.getAll);
|
CatalogRouter.route("/model/all").get(ModelController.getAll);
|
||||||
|
|
||||||
@ -23,14 +20,9 @@ CatalogRouter.route("/model/:modelSlug")
|
|||||||
|
|
||||||
//-- CATEGORY >>
|
//-- CATEGORY >>
|
||||||
|
|
||||||
CatalogRouter.route("/category/new").get(
|
CatalogRouter.route("/category/new").get(AdminGuard, CategoryController.create);
|
||||||
AdminGuard,
|
|
||||||
CategoryController.create,
|
|
||||||
);
|
|
||||||
|
|
||||||
CatalogRouter.route("/category/all").get(
|
CatalogRouter.route("/category/all").get(CategoryController.getAll);
|
||||||
CategoryController.getAll,
|
|
||||||
);
|
|
||||||
|
|
||||||
CatalogRouter.route("/category/:categorySlug")
|
CatalogRouter.route("/category/:categorySlug")
|
||||||
.get(UserGuard, CategoryController.getBySlug)
|
.get(UserGuard, CategoryController.getBySlug)
|
||||||
@ -39,10 +31,7 @@ CatalogRouter.route("/category/:categorySlug")
|
|||||||
|
|
||||||
//-- BRAND >>
|
//-- BRAND >>
|
||||||
|
|
||||||
CatalogRouter.route("/brand/new").post(
|
CatalogRouter.route("/brand/new").post(AdminGuard, BrandController.create);
|
||||||
AdminGuard,
|
|
||||||
BrandController.create,
|
|
||||||
);
|
|
||||||
CatalogRouter.route("/brand/all").get(BrandController.getAll);
|
CatalogRouter.route("/brand/all").get(BrandController.getAll);
|
||||||
CatalogRouter.route("/brand/:brandSlug")
|
CatalogRouter.route("/brand/:brandSlug")
|
||||||
.get(UserGuard, BrandController.getBySlug)
|
.get(UserGuard, BrandController.getBySlug)
|
||||||
|
@ -27,19 +27,14 @@ async function createBrand(data: IDbBrand): Promise<unknown> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
const brandId = uuidv4();
|
const brandId = uuidv4();
|
||||||
const createdBrand = await MysqlService.Brand.insert(
|
const createdBrand = await MysqlService.Brand.insert(DbHandler, {
|
||||||
DbHandler,
|
id: brandId,
|
||||||
{
|
slug_name: `${data.slug_name}`,
|
||||||
id: brandId,
|
display_name: `${data.display_name}`,
|
||||||
slug_name: `${data.slug_name}`,
|
image_blob: data.image_blob,
|
||||||
display_name: `${data.display_name}`,
|
});
|
||||||
image_blob: data.image_blob,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
if (createdBrand) {
|
if (createdBrand) {
|
||||||
logger.info(
|
logger.info(`Brand created successfully (${data.slug_name})`);
|
||||||
`Brand created successfully (${data.slug_name})`,
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
brand: createdBrand,
|
brand: createdBrand,
|
||||||
@ -71,19 +66,14 @@ async function updateBrand(data: IDbBrand): Promise<boolean> {
|
|||||||
logger.error(`Brand already exists (${data.slug_name})`);
|
logger.error(`Brand already exists (${data.slug_name})`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const updatedBrand = await MysqlService.Brand.update(
|
const updatedBrand = await MysqlService.Brand.update(DbHandler, {
|
||||||
DbHandler,
|
id: data.id,
|
||||||
{
|
slug_name: `${data.slug_name}`,
|
||||||
id: data.id,
|
display_name: `${data.display_name}`,
|
||||||
slug_name: `${data.slug_name}`,
|
image_blob: data.image_blob,
|
||||||
display_name: `${data.display_name}`,
|
});
|
||||||
image_blob: data.image_blob,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
if (updatedBrand) {
|
if (updatedBrand) {
|
||||||
logger.info(
|
logger.info(`Brand updated successfully (${data.slug_name})`);
|
||||||
`Brand updated successfully (${data.slug_name})`,
|
|
||||||
);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
logger.error(`Failed to update brand (${data.slug_name})`);
|
logger.error(`Failed to update brand (${data.slug_name})`);
|
||||||
@ -100,9 +90,7 @@ async function getAllBrand(): Promise<Array<IDbBrand> | false> {
|
|||||||
logger.error("Failed to retrieve brands");
|
logger.error("Failed to retrieve brands");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
logger.info(
|
logger.info(`Retrieved all brands successfully (${brands.length})`);
|
||||||
`Retrieved all brands successfully (${brands.length})`,
|
|
||||||
);
|
|
||||||
return brands;
|
return brands;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,24 +100,17 @@ async function getAllBrand(): Promise<Array<IDbBrand> | false> {
|
|||||||
* @param {string} brandSlug - The slug of the brand.
|
* @param {string} brandSlug - The slug of the brand.
|
||||||
* @returns {Promise<IDbBrand|false>} - A promise that resolves to the retrieved brand object or false if the brand is not found.
|
* @returns {Promise<IDbBrand|false>} - A promise that resolves to the retrieved brand object or false if the brand is not found.
|
||||||
*/
|
*/
|
||||||
async function getBySlugBrand(
|
async function getBySlugBrand(brandSlug: string): Promise<IDbBrand | false> {
|
||||||
brandSlug: string,
|
|
||||||
): Promise<IDbBrand | false> {
|
|
||||||
if (!brandSlug) {
|
if (!brandSlug) {
|
||||||
logger.error("Brand slug is missing");
|
logger.error("Brand slug is missing");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const brand = await MysqlService.Brand.getBySlug(
|
const brand = await MysqlService.Brand.getBySlug(DbHandler, brandSlug);
|
||||||
DbHandler,
|
|
||||||
brandSlug,
|
|
||||||
);
|
|
||||||
if (!brand) {
|
if (!brand) {
|
||||||
logger.error(`Brand not found (${brandSlug})`);
|
logger.error(`Brand not found (${brandSlug})`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
logger.info(
|
logger.info(`Retrieved brand by slug successfully (${brandSlug})`);
|
||||||
`Retrieved brand by slug successfully (${brandSlug})`,
|
|
||||||
);
|
|
||||||
return brand;
|
return brand;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,9 +121,7 @@ async function getBySlugBrand(
|
|||||||
*
|
*
|
||||||
* @returns {Promise<IDbBrand | false>} A promise that resolves to the retrieved brand object, or false if the brand is not found or the ID is invalid.
|
* @returns {Promise<IDbBrand | false>} A promise that resolves to the retrieved brand object, or false if the brand is not found or the ID is invalid.
|
||||||
*/
|
*/
|
||||||
async function getByIdBrand(
|
async function getByIdBrand(brandId: string): Promise<IDbBrand | false> {
|
||||||
brandId: string,
|
|
||||||
): Promise<IDbBrand | false> {
|
|
||||||
if (!brandId) {
|
if (!brandId) {
|
||||||
logger.error("Brand ID is missing");
|
logger.error("Brand ID is missing");
|
||||||
return false;
|
return false;
|
||||||
@ -151,17 +130,12 @@ async function getByIdBrand(
|
|||||||
logger.error("Invalid brand ID");
|
logger.error("Invalid brand ID");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const brand = await MysqlService.Brand.getById(
|
const brand = await MysqlService.Brand.getById(DbHandler, brandId);
|
||||||
DbHandler,
|
|
||||||
brandId,
|
|
||||||
);
|
|
||||||
if (!brand) {
|
if (!brand) {
|
||||||
logger.error(`Brand not found (${brandId})`);
|
logger.error(`Brand not found (${brandId})`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
logger.info(
|
logger.info(`Retrieved brand by ID successfully (${brandId})`);
|
||||||
`Retrieved brand by ID successfully (${brandId})`,
|
|
||||||
);
|
|
||||||
return brand;
|
return brand;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,10 +159,7 @@ async function deleteBrand(brandId: string): Promise<boolean> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//TODO verify if as models linked
|
//TODO verify if as models linked
|
||||||
const deletedBrand = await MysqlService.Brand.delete(
|
const deletedBrand = await MysqlService.Brand.delete(DbHandler, brandId);
|
||||||
DbHandler,
|
|
||||||
brandId,
|
|
||||||
);
|
|
||||||
if (!deletedBrand) {
|
if (!deletedBrand) {
|
||||||
logger.error(`Failed to delete brand (${brandId})`);
|
logger.error(`Failed to delete brand (${brandId})`);
|
||||||
return false;
|
return false;
|
||||||
|
@ -15,12 +15,8 @@ const logger = new Logger({
|
|||||||
* @returns {Promise<boolean>} A promise that resolves with the created category.
|
* @returns {Promise<boolean>} A promise that resolves with the created category.
|
||||||
* If an error occurs, the promise will reject with the error.
|
* If an error occurs, the promise will reject with the error.
|
||||||
*/
|
*/
|
||||||
async function createCategory(
|
async function createCategory(data: IDbCategory): Promise<boolean> {
|
||||||
data: IDbCategory,
|
logger.info(`Creating a new category... (${data.display_name})`);
|
||||||
): Promise<boolean> {
|
|
||||||
logger.info(
|
|
||||||
`Creating a new category... (${data.display_name})`,
|
|
||||||
);
|
|
||||||
try {
|
try {
|
||||||
await MysqlService.Category.insert(DbHandler, {
|
await MysqlService.Category.insert(DbHandler, {
|
||||||
id: uuidv4(),
|
id: uuidv4(),
|
||||||
@ -69,9 +65,7 @@ async function updateCategory(data: IDbCategory) {
|
|||||||
*
|
*
|
||||||
* @returns {Promise<Array<IDbCategory>> | null} Promise that resolves to an array of IDbCategory objects or null if an error occurred.
|
* @returns {Promise<Array<IDbCategory>> | null} Promise that resolves to an array of IDbCategory objects or null if an error occurred.
|
||||||
*/
|
*/
|
||||||
async function getAll(): Promise<Promise<
|
async function getAll(): Promise<Promise<Array<IDbCategory>> | null> {
|
||||||
Array<IDbCategory>
|
|
||||||
> | null> {
|
|
||||||
try {
|
try {
|
||||||
logger.info("Getting all categories...");
|
logger.info("Getting all categories...");
|
||||||
return await MysqlService.Category.getAll(DbHandler);
|
return await MysqlService.Category.getAll(DbHandler);
|
||||||
@ -87,15 +81,10 @@ async function getAll(): Promise<Promise<
|
|||||||
* @param {string} slug - The slug of the category
|
* @param {string} slug - The slug of the category
|
||||||
* @return {Promise<IDbCategory|null>} - A promise that resolves to the category object or null if not found
|
* @return {Promise<IDbCategory|null>} - A promise that resolves to the category object or null if not found
|
||||||
*/
|
*/
|
||||||
async function getBySlug(
|
async function getBySlug(slug: string): Promise<IDbCategory | null> {
|
||||||
slug: string,
|
|
||||||
): Promise<IDbCategory | null> {
|
|
||||||
try {
|
try {
|
||||||
logger.info(`Getting category by slug... (${slug})`);
|
logger.info(`Getting category by slug... (${slug})`);
|
||||||
return await MysqlService.Category.getBySlug(
|
return await MysqlService.Category.getBySlug(DbHandler, slug);
|
||||||
DbHandler,
|
|
||||||
slug,
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Error getting category by slug: ${error}`);
|
logger.error(`Error getting category by slug: ${error}`);
|
||||||
return null;
|
return null;
|
||||||
@ -108,9 +97,7 @@ async function getBySlug(
|
|||||||
* @param {string} id - The id of the category to retrieve.
|
* @param {string} id - The id of the category to retrieve.
|
||||||
* @returns {Promise<IDbCategory | null>} - A Promise that resolves with the retrieved category object or null if not found.
|
* @returns {Promise<IDbCategory | null>} - A Promise that resolves with the retrieved category object or null if not found.
|
||||||
*/
|
*/
|
||||||
async function getById(
|
async function getById(id: string): Promise<IDbCategory | null> {
|
||||||
id: string,
|
|
||||||
): Promise<IDbCategory | null> {
|
|
||||||
try {
|
try {
|
||||||
logger.info(`Getting category by id... (${id})`);
|
logger.info(`Getting category by id... (${id})`);
|
||||||
return await MysqlService.Category.getById(DbHandler, id);
|
return await MysqlService.Category.getById(DbHandler, id);
|
||||||
|
@ -9,10 +9,7 @@ export async function getHashFromPassword(password: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//ToTest
|
//ToTest
|
||||||
export async function comparePassword(
|
export async function comparePassword(password: string, hash: string) {
|
||||||
password: string,
|
|
||||||
hash: string,
|
|
||||||
) {
|
|
||||||
return await Argon2id.verify(hash, password, {
|
return await Argon2id.verify(hash, password, {
|
||||||
secret: Buffer.from(`${process.env["HASH_SECRET"]}`),
|
secret: Buffer.from(`${process.env["HASH_SECRET"]}`),
|
||||||
algorithm: 2,
|
algorithm: 2,
|
||||||
|
@ -61,9 +61,7 @@ async function JwtSignService(
|
|||||||
.setIssuer(`${process.env["JWT_SECRET"]} - Mathis HERRIOT`)
|
.setIssuer(`${process.env["JWT_SECRET"]} - Mathis HERRIOT`)
|
||||||
.setAudience(audience)
|
.setAudience(audience)
|
||||||
.setExpirationTime(expTime)
|
.setExpirationTime(expTime)
|
||||||
.sign(
|
.sign(new TextEncoder().encode(`${process.env["JWT_SECRET"]}`));
|
||||||
new TextEncoder().encode(`${process.env["JWT_SECRET"]}`),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const JwtService = {
|
const JwtService = {
|
||||||
|
@ -72,18 +72,13 @@ async function updateModel(data: IDbModel): Promise<boolean> {
|
|||||||
* @param {string} modelSlug - The slug of the model to be deleted.
|
* @param {string} modelSlug - The slug of the model to be deleted.
|
||||||
* @return {Promise<boolean>} - A promise that resolves to true if the deletion is successful, else false.
|
* @return {Promise<boolean>} - A promise that resolves to true if the deletion is successful, else false.
|
||||||
*/
|
*/
|
||||||
async function deleteModel(
|
async function deleteModel(modelSlug: string): Promise<boolean> {
|
||||||
modelSlug: string,
|
|
||||||
): Promise<boolean> {
|
|
||||||
if (!modelSlug) {
|
if (!modelSlug) {
|
||||||
logger.error("Model slug is missing");
|
logger.error("Model slug is missing");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
logger.info(`Deleting model with ID: ${modelSlug}`);
|
logger.info(`Deleting model with ID: ${modelSlug}`);
|
||||||
const doesExist = await MysqlService.Model.getBySlug(
|
const doesExist = await MysqlService.Model.getBySlug(DbHandler, modelSlug);
|
||||||
DbHandler,
|
|
||||||
modelSlug,
|
|
||||||
);
|
|
||||||
if (!doesExist || !doesExist.id) {
|
if (!doesExist || !doesExist.id) {
|
||||||
logger.warn(`Model with slug ${modelSlug} not found`);
|
logger.warn(`Model with slug ${modelSlug} not found`);
|
||||||
return false;
|
return false;
|
||||||
@ -104,15 +99,10 @@ async function deleteModel(
|
|||||||
* @param {string} modelSlug - The slug of the model to be fetched.
|
* @param {string} modelSlug - The slug of the model to be fetched.
|
||||||
* @return {Promise<IDbModel | null>} - A promise that resolves to the model if found, else null.
|
* @return {Promise<IDbModel | null>} - A promise that resolves to the model if found, else null.
|
||||||
*/
|
*/
|
||||||
async function getBySlugModel(
|
async function getBySlugModel(modelSlug: string): Promise<IDbModel | null> {
|
||||||
modelSlug: string,
|
|
||||||
): Promise<IDbModel | null> {
|
|
||||||
logger.info(`Fetching model with slug: ${modelSlug}`);
|
logger.info(`Fetching model with slug: ${modelSlug}`);
|
||||||
try {
|
try {
|
||||||
const model = await MysqlService.Model.getBySlug(
|
const model = await MysqlService.Model.getBySlug(DbHandler, modelSlug);
|
||||||
DbHandler,
|
|
||||||
modelSlug,
|
|
||||||
);
|
|
||||||
if (!model) {
|
if (!model) {
|
||||||
logger.warn(`Model with slug ${modelSlug} not found`);
|
logger.warn(`Model with slug ${modelSlug} not found`);
|
||||||
return null;
|
return null;
|
||||||
|
@ -5,10 +5,7 @@ import type { IDbModel } from "@interfaces/database/IDbModel";
|
|||||||
import type { IDbStatusResult } from "@interfaces/database/IDbStatusResult";
|
import type { IDbStatusResult } from "@interfaces/database/IDbStatusResult";
|
||||||
import type { IDbUser } from "@interfaces/database/IDbUser";
|
import type { IDbUser } from "@interfaces/database/IDbUser";
|
||||||
import type { IDbVehicle } from "@interfaces/database/IDbVehicle";
|
import type { IDbVehicle } from "@interfaces/database/IDbVehicle";
|
||||||
import mysql, {
|
import mysql, { type Connection, type ConnectionOptions } from "mysql2";
|
||||||
type Connection,
|
|
||||||
type ConnectionOptions,
|
|
||||||
} from "mysql2";
|
|
||||||
import { Logger } from "tslog";
|
import { Logger } from "tslog";
|
||||||
|
|
||||||
const access: ConnectionOptions = {
|
const access: ConnectionOptions = {
|
||||||
@ -35,9 +32,7 @@ class MysqlHandler {
|
|||||||
this.Logger.error(`Error connecting to MySQL: ${err}`);
|
this.Logger.error(`Error connecting to MySQL: ${err}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
this.Logger.info(
|
this.Logger.info(`Connected to MySQL database (${access.database})`);
|
||||||
`Connected to MySQL database (${access.database})`,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
closeConnection() {
|
closeConnection() {
|
||||||
@ -65,10 +60,7 @@ class MysqlHandler {
|
|||||||
this.Connection.execute(
|
this.Connection.execute(
|
||||||
queryString,
|
queryString,
|
||||||
values,
|
values,
|
||||||
(
|
(err: mysql.QueryError | null, results: mysql.QueryResult) => {
|
||||||
err: mysql.QueryError | null,
|
|
||||||
results: mysql.QueryResult,
|
|
||||||
) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
this.Logger.error(`Error executing query: ${err}`);
|
this.Logger.error(`Error executing query: ${err}`);
|
||||||
reject(err);
|
reject(err);
|
||||||
@ -114,10 +106,7 @@ const MySqlService = {
|
|||||||
* @throws {Error} If an error occurs during the execution.
|
* @throws {Error} If an error occurs during the execution.
|
||||||
* @throws {string} If the `id` field is undefined or invalid.
|
* @throws {string} If the `id` field is undefined or invalid.
|
||||||
*/
|
*/
|
||||||
insert(
|
insert(handler: MysqlHandler, data: IDbUser): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
data: IDbUser,
|
|
||||||
): Promise<IDbStatusResult> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject("Id is undefined");
|
if (!data.id) return reject("Id is undefined");
|
||||||
if (data.id.length !== 36) return reject("Id invalid");
|
if (data.id.length !== 36) return reject("Id invalid");
|
||||||
@ -138,9 +127,7 @@ const MySqlService = {
|
|||||||
];
|
];
|
||||||
try {
|
try {
|
||||||
handler.execute(_sql, _values).then((result) => {
|
handler.execute(_sql, _values).then((result) => {
|
||||||
return resolve(
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
result as unknown as IDbStatusResult,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
@ -148,10 +135,7 @@ const MySqlService = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
update(
|
update(handler: MysqlHandler, data: IDbUser): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
data: IDbUser,
|
|
||||||
): Promise<IDbStatusResult> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject("Id is undefined");
|
if (!data.id) return reject("Id is undefined");
|
||||||
if (data.id.length !== 36) return reject("Id invalid");
|
if (data.id.length !== 36) return reject("Id invalid");
|
||||||
@ -163,11 +147,7 @@ const MySqlService = {
|
|||||||
${data.lastname ? "`lastname` = ?," : null}
|
${data.lastname ? "`lastname` = ?," : null}
|
||||||
${data.dob ? "`dob` = ?," : null}
|
${data.dob ? "`dob` = ?," : null}
|
||||||
${data.email ? "`email` = ?," : null}
|
${data.email ? "`email` = ?," : null}
|
||||||
${
|
${data.is_mail_verified ? "`is_mail_verified` = ?," : null}
|
||||||
data.is_mail_verified
|
|
||||||
? "`is_mail_verified` = ?,"
|
|
||||||
: null
|
|
||||||
}
|
|
||||||
${data.is_admin ? "`is_admin` = ?," : null}
|
${data.is_admin ? "`is_admin` = ?," : null}
|
||||||
${data.gdpr ? "`gdpr` = ?," : null}
|
${data.gdpr ? "`gdpr` = ?," : null}
|
||||||
${data.hash ? "`hash` = ?" : null}`;
|
${data.hash ? "`hash` = ?" : null}`;
|
||||||
@ -186,9 +166,7 @@ const MySqlService = {
|
|||||||
|
|
||||||
const _sql = `UPDATE "users" SET ${_template} WHERE 'id' = ?`;
|
const _sql = `UPDATE "users" SET ${_template} WHERE 'id' = ?`;
|
||||||
handler.execute(_sql, _values).then((result) => {
|
handler.execute(_sql, _values).then((result) => {
|
||||||
return resolve(
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
result as unknown as IDbStatusResult,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
@ -196,10 +174,7 @@ const MySqlService = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
getById(
|
getById(handler: MysqlHandler, userId: string): Promise<IDbUser> {
|
||||||
handler: MysqlHandler,
|
|
||||||
userId: string,
|
|
||||||
): Promise<IDbUser> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (userId.length !== 36) return reject("Id invalid");
|
if (userId.length !== 36) return reject("Id invalid");
|
||||||
const _sql = "SELECT * FROM `users` WHERE `id` = ?";
|
const _sql = "SELECT * FROM `users` WHERE `id` = ?";
|
||||||
@ -242,10 +217,7 @@ const MySqlService = {
|
|||||||
* @return {Promise<IDbUser>} - A promise that resolves to the retrieved user object.
|
* @return {Promise<IDbUser>} - A promise that resolves to the retrieved user object.
|
||||||
* @throws {Error} - If an error occurs while retrieving the user.
|
* @throws {Error} - If an error occurs while retrieving the user.
|
||||||
*/
|
*/
|
||||||
getByEmail(
|
getByEmail(handler: MysqlHandler, email: string): Promise<IDbUser> {
|
||||||
handler: MysqlHandler,
|
|
||||||
email: string,
|
|
||||||
): Promise<IDbUser> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!email) return reject("email is undefined");
|
if (!email) return reject("email is undefined");
|
||||||
const _sql = "SELECT * FROM `users` WHERE `email` = ?";
|
const _sql = "SELECT * FROM `users` WHERE `email` = ?";
|
||||||
@ -273,8 +245,7 @@ const MySqlService = {
|
|||||||
userId: string,
|
userId: string,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const _sql =
|
const _sql = "SELECT `is_admin` FROM `users` WHERE `id` = ?";
|
||||||
"SELECT `is_admin` FROM `users` WHERE `id` = ?";
|
|
||||||
const _values = [userId];
|
const _values = [userId];
|
||||||
try {
|
try {
|
||||||
const isAdmin = handler.execute(
|
const isAdmin = handler.execute(
|
||||||
@ -297,17 +268,16 @@ const MySqlService = {
|
|||||||
* @param {string} userId - The ID of the user to delete.
|
* @param {string} userId - The ID of the user to delete.
|
||||||
* @return {Promise<unknown>} - A Promise that resolves when the deletion is successful, or rejects with an error.
|
* @return {Promise<unknown>} - A Promise that resolves when the deletion is successful, or rejects with an error.
|
||||||
*/
|
*/
|
||||||
delete(
|
delete(handler: MysqlHandler, userId: string): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
userId: string,
|
|
||||||
): Promise<unknown> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!userId) return reject("Id is undefined");
|
if (!userId) return reject("Id is undefined");
|
||||||
if (userId.length !== 36) return reject("Id invalid");
|
if (userId.length !== 36) return reject("Id invalid");
|
||||||
const _sql = "DELETE FROM `users` WHERE `id` = ?";
|
const _sql = "DELETE FROM `users` WHERE `id` = ?";
|
||||||
const _values = [userId];
|
const _values = [userId];
|
||||||
try {
|
try {
|
||||||
resolve(handler.execute(_sql, _values));
|
handler.execute(_sql, _values).then((result) => {
|
||||||
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
|
});
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -323,10 +293,7 @@ const MySqlService = {
|
|||||||
* @returns {Promise<IDbStatusResult>} - A Promise that resolves to the status result of the insert operation.
|
* @returns {Promise<IDbStatusResult>} - A Promise that resolves to the status result of the insert operation.
|
||||||
* @throws {Error} - If an error occurs during the execution of the SQL query.
|
* @throws {Error} - If an error occurs during the execution of the SQL query.
|
||||||
*/
|
*/
|
||||||
insert(
|
insert(handler: MysqlHandler, data: IDbBrand): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
data: IDbBrand,
|
|
||||||
): Promise<IDbStatusResult> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject("Id is undefined");
|
if (!data.id) return reject("Id is undefined");
|
||||||
if (data.id.length !== 36) return reject("Id invalid");
|
if (data.id.length !== 36) return reject("Id invalid");
|
||||||
@ -340,12 +307,7 @@ const MySqlService = {
|
|||||||
data.image_blob,
|
data.image_blob,
|
||||||
];
|
];
|
||||||
try {
|
try {
|
||||||
resolve(
|
resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult);
|
||||||
handler.execute(
|
|
||||||
_sql,
|
|
||||||
_values,
|
|
||||||
) as unknown as IDbStatusResult,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -360,10 +322,7 @@ const MySqlService = {
|
|||||||
* @returns {Promise<IDbStatusResult>} - A promise that resolves to the status result of the update operation.
|
* @returns {Promise<IDbStatusResult>} - A promise that resolves to the status result of the update operation.
|
||||||
* @throws {Error} - If any error occurs during the process.
|
* @throws {Error} - If any error occurs during the process.
|
||||||
*/
|
*/
|
||||||
update(
|
update(handler: MysqlHandler, data: IDbBrand): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
data: IDbBrand,
|
|
||||||
): Promise<IDbStatusResult> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject("Id is undefined");
|
if (!data.id) return reject("Id is undefined");
|
||||||
if (data.id.length !== 36) return reject("Id invalid");
|
if (data.id.length !== 36) return reject("Id invalid");
|
||||||
@ -381,12 +340,9 @@ const MySqlService = {
|
|||||||
data.id,
|
data.id,
|
||||||
];
|
];
|
||||||
const _sql = `UPDATE "brands" SET ${_template} WHERE 'id' = ?`;
|
const _sql = `UPDATE "brands" SET ${_template} WHERE 'id' = ?`;
|
||||||
return resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbStatusResult,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -404,9 +360,9 @@ const MySqlService = {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const _sql = "SELECT * FROM `brands`";
|
const _sql = "SELECT * FROM `brands`";
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.query(_sql).then((result) => {
|
||||||
handler.query(_sql) as unknown as Array<IDbBrand>,
|
return resolve(result as unknown as Array<IDbBrand>);
|
||||||
);
|
});
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -422,22 +378,16 @@ const MySqlService = {
|
|||||||
* @throws {Error} - If an error occurs during execution
|
* @throws {Error} - If an error occurs during execution
|
||||||
* @throws {string} - If brandId is undefined or invalid
|
* @throws {string} - If brandId is undefined or invalid
|
||||||
*/
|
*/
|
||||||
getById(
|
getById(handler: MysqlHandler, brandId: string): Promise<IDbBrand> {
|
||||||
handler: MysqlHandler,
|
|
||||||
brandId: string,
|
|
||||||
): Promise<IDbBrand> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!brandId) return reject("Id is undefined");
|
if (!brandId) return reject("Id is undefined");
|
||||||
if (brandId.length !== 36) return reject("Id invalid");
|
if (brandId.length !== 36) return reject("Id invalid");
|
||||||
const _sql = "SELECT * FROM `brands` WHERE `id` = ?";
|
const _sql = "SELECT * FROM `brands` WHERE `id` = ?";
|
||||||
const _values = [brandId];
|
const _values = [brandId];
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbBrand);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbBrand,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
//TODO Reject with null and logger error
|
//TODO Reject with null and logger error
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
@ -452,22 +402,15 @@ const MySqlService = {
|
|||||||
* @param {string} brandSlug - The slug of the brand to retrieve.
|
* @param {string} brandSlug - The slug of the brand to retrieve.
|
||||||
* @returns {Promise<IDbBrand>} - A promise that resolves with the brand object if found, or rejects with an error message.
|
* @returns {Promise<IDbBrand>} - A promise that resolves with the brand object if found, or rejects with an error message.
|
||||||
*/
|
*/
|
||||||
getBySlug(
|
getBySlug(handler: MysqlHandler, brandSlug: string): Promise<IDbBrand> {
|
||||||
handler: MysqlHandler,
|
|
||||||
brandSlug: string,
|
|
||||||
): Promise<IDbBrand> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!brandSlug) return reject("slug is undefined");
|
if (!brandSlug) return reject("slug is undefined");
|
||||||
const _sql =
|
const _sql = "SELECT * FROM `brands` WHERE `slug_name` = ?";
|
||||||
"SELECT * FROM `brands` WHERE `slug_name` = ?";
|
|
||||||
const _values = [brandSlug];
|
const _values = [brandSlug];
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbBrand);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbBrand,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -482,10 +425,7 @@ const MySqlService = {
|
|||||||
* @returns {Promise<IDbStatusResult>} A promise that resolves to the database status result.
|
* @returns {Promise<IDbStatusResult>} A promise that resolves to the database status result.
|
||||||
* @throws {Error} If an error occurs while deleting the brand.
|
* @throws {Error} If an error occurs while deleting the brand.
|
||||||
*/
|
*/
|
||||||
delete(
|
delete(handler: MysqlHandler, brandId: string): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
brandId: string,
|
|
||||||
): Promise<IDbStatusResult> {
|
|
||||||
//TODO check if has models linked before actions
|
//TODO check if has models linked before actions
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!brandId) return reject("Id is undefined");
|
if (!brandId) return reject("Id is undefined");
|
||||||
@ -493,12 +433,9 @@ const MySqlService = {
|
|||||||
const _sql = "DELETE FROM `brands` WHERE `id` = ?";
|
const _sql = "DELETE FROM `brands` WHERE `id` = ?";
|
||||||
const _values = [brandId];
|
const _values = [brandId];
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbStatusResult,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -519,9 +456,9 @@ const MySqlService = {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const _sql = "SELECT * FROM `models`";
|
const _sql = "SELECT * FROM `models`";
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.query(_sql).then((result) => {
|
||||||
handler.query(_sql) as unknown as Array<IDbModel>,
|
return resolve(result as unknown as Array<IDbModel>);
|
||||||
);
|
});
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -536,21 +473,14 @@ const MySqlService = {
|
|||||||
* @return {Promise<IDbModel>} A promise that resolves with the retrieved model.
|
* @return {Promise<IDbModel>} A promise that resolves with the retrieved model.
|
||||||
* @throws {Error} If there was an error executing the query.
|
* @throws {Error} If there was an error executing the query.
|
||||||
*/
|
*/
|
||||||
getBySlug(
|
getBySlug(handler: MysqlHandler, modelSlug: string): Promise<IDbModel> {
|
||||||
handler: MysqlHandler,
|
|
||||||
modelSlug: string,
|
|
||||||
): Promise<IDbModel> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const _sql =
|
const _sql = "SELECT * FROM `models` WHERE `slug_name` = ?";
|
||||||
"SELECT * FROM `models` WHERE `slug_name` = ?";
|
|
||||||
const _values = [modelSlug];
|
const _values = [modelSlug];
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbModel);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbModel,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -564,20 +494,12 @@ const MySqlService = {
|
|||||||
* @param {string} modelId - The ID of the model to retrieve.
|
* @param {string} modelId - The ID of the model to retrieve.
|
||||||
* @return {Promise<IDbModel>} - A promise that resolves with the retrieved model, or rejects with an error.
|
* @return {Promise<IDbModel>} - A promise that resolves with the retrieved model, or rejects with an error.
|
||||||
*/
|
*/
|
||||||
getById(
|
getById(handler: MysqlHandler, modelId: string): Promise<IDbModel> {
|
||||||
handler: MysqlHandler,
|
|
||||||
modelId: string,
|
|
||||||
): Promise<IDbModel> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const _sql = "SELECT * FROM `models` WHERE `id` = ?";
|
const _sql = "SELECT * FROM `models` WHERE `id` = ?";
|
||||||
const _values = [modelId];
|
const _values = [modelId];
|
||||||
try {
|
try {
|
||||||
resolve(
|
resolve(handler.execute(_sql, _values) as unknown as IDbModel);
|
||||||
handler.execute(
|
|
||||||
_sql,
|
|
||||||
_values,
|
|
||||||
) as unknown as IDbModel,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -592,10 +514,7 @@ const MySqlService = {
|
|||||||
* @throws {string} - Throws an error message if the id is undefined or invalid.
|
* @throws {string} - Throws an error message if the id is undefined or invalid.
|
||||||
* @returns {Promise<IDbStatusResult>} - A promise that resolves to the status result of the insert operation.
|
* @returns {Promise<IDbStatusResult>} - A promise that resolves to the status result of the insert operation.
|
||||||
*/
|
*/
|
||||||
insert(
|
insert(handler: MysqlHandler, data: IDbModel): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
data: IDbModel,
|
|
||||||
): Promise<IDbStatusResult> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject("Id is undefined");
|
if (!data.id) return reject("Id is undefined");
|
||||||
if (data.id.length !== 36) return reject("Id invalid");
|
if (data.id.length !== 36) return reject("Id invalid");
|
||||||
@ -613,12 +532,9 @@ const MySqlService = {
|
|||||||
data.id,
|
data.id,
|
||||||
];
|
];
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbStatusResult,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -635,10 +551,7 @@ const MySqlService = {
|
|||||||
* @return {Promise<IDbStatusResult>} - A promise that resolves to the status result of the update operation.
|
* @return {Promise<IDbStatusResult>} - A promise that resolves to the status result of the update operation.
|
||||||
* @throws {Error} - If an error occurs during the update process.
|
* @throws {Error} - If an error occurs during the update process.
|
||||||
*/
|
*/
|
||||||
update(
|
update(handler: MysqlHandler, data: IDbModel): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
data: IDbModel,
|
|
||||||
): Promise<IDbStatusResult> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject("Id is undefined");
|
if (!data.id) return reject("Id is undefined");
|
||||||
if (data.id.length !== 36) return reject("Id invalid");
|
if (data.id.length !== 36) return reject("Id invalid");
|
||||||
@ -664,12 +577,9 @@ const MySqlService = {
|
|||||||
data.id,
|
data.id,
|
||||||
];
|
];
|
||||||
const _sql = `UPDATE "models" SET ${_template} WHERE 'id' = ?`;
|
const _sql = `UPDATE "models" SET ${_template} WHERE 'id' = ?`;
|
||||||
return resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbStatusResult,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -684,10 +594,7 @@ const MySqlService = {
|
|||||||
* @returns {Promise<IDbStatusResult>} A promise that resolves to the result of the delete operation.
|
* @returns {Promise<IDbStatusResult>} A promise that resolves to the result of the delete operation.
|
||||||
* @throws {Error} If an error occurs during the delete operation.
|
* @throws {Error} If an error occurs during the delete operation.
|
||||||
*/
|
*/
|
||||||
delete(
|
delete(handler: MysqlHandler, modelId: string): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
modelId: string,
|
|
||||||
): Promise<IDbStatusResult> {
|
|
||||||
//TODO check if has models linked before actions
|
//TODO check if has models linked before actions
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!modelId) return reject("Id is undefined");
|
if (!modelId) return reject("Id is undefined");
|
||||||
@ -695,12 +602,9 @@ const MySqlService = {
|
|||||||
const _sql = "DELETE FROM `models` WHERE `id` = ?";
|
const _sql = "DELETE FROM `models` WHERE `id` = ?";
|
||||||
const _values = [modelId];
|
const _values = [modelId];
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbStatusResult,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -715,10 +619,7 @@ const MySqlService = {
|
|||||||
* @throws Throws an error if the provided `data` object does not contain the `id` property or if the `id` is not a valid UUID.
|
* @throws Throws an error if the provided `data` object does not contain the `id` property or if the `id` is not a valid UUID.
|
||||||
* @returns {Promise<IDbStatusResult>} A promise that resolves to the result of the insert operation.
|
* @returns {Promise<IDbStatusResult>} A promise that resolves to the result of the insert operation.
|
||||||
*/
|
*/
|
||||||
insert(
|
insert(handler: MysqlHandler, data: IDbVehicle): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
data: IDbVehicle,
|
|
||||||
): Promise<IDbStatusResult> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject("Id is undefined");
|
if (!data.id) return reject("Id is undefined");
|
||||||
if (data.id.length !== 36) return reject("Id invalid");
|
if (data.id.length !== 36) return reject("Id invalid");
|
||||||
@ -733,12 +634,9 @@ const MySqlService = {
|
|||||||
data.id,
|
data.id,
|
||||||
];
|
];
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbStatusResult,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -753,10 +651,7 @@ const MySqlService = {
|
|||||||
* @throws {string} Throws an error if the id is undefined or invalid.
|
* @throws {string} Throws an error if the id is undefined or invalid.
|
||||||
* @returns {Promise<IDbStatusResult>} Returns a Promise that resolves to the status result of the update operation.
|
* @returns {Promise<IDbStatusResult>} Returns a Promise that resolves to the status result of the update operation.
|
||||||
*/
|
*/
|
||||||
update(
|
update(handler: MysqlHandler, data: IDbVehicle): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
data: IDbVehicle,
|
|
||||||
): Promise<IDbStatusResult> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject("Id is undefined");
|
if (!data.id) return reject("Id is undefined");
|
||||||
if (data.id.length !== 36) return reject("Id invalid");
|
if (data.id.length !== 36) return reject("Id invalid");
|
||||||
@ -766,9 +661,7 @@ const MySqlService = {
|
|||||||
${data.model_id ? "`model_id` = ?," : null}
|
${data.model_id ? "`model_id` = ?," : null}
|
||||||
${data.plate_number ? "`plate_number` = ?," : null}
|
${data.plate_number ? "`plate_number` = ?," : null}
|
||||||
${data.odometer ? "`odometer` = ?," : null}
|
${data.odometer ? "`odometer` = ?," : null}
|
||||||
${
|
${data.health_state ? "`health_state` = ?," : null}`;
|
||||||
data.health_state ? "`health_state` = ?," : null
|
|
||||||
}`;
|
|
||||||
|
|
||||||
const _values = [
|
const _values = [
|
||||||
data.model_id,
|
data.model_id,
|
||||||
@ -778,12 +671,9 @@ const MySqlService = {
|
|||||||
data.id,
|
data.id,
|
||||||
];
|
];
|
||||||
const _sql = `UPDATE "vehicles" SET ${_template} WHERE 'id' = ?`;
|
const _sql = `UPDATE "vehicles" SET ${_template} WHERE 'id' = ?`;
|
||||||
return resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbStatusResult,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -798,23 +688,16 @@ const MySqlService = {
|
|||||||
* @returns {Promise<IDbVehicle>} - A promise that resolves to the retrieved vehicle.
|
* @returns {Promise<IDbVehicle>} - A promise that resolves to the retrieved vehicle.
|
||||||
* @throws {Error} - If an error occurs while retrieving the vehicle.
|
* @throws {Error} - If an error occurs while retrieving the vehicle.
|
||||||
*/
|
*/
|
||||||
getById(
|
getById(handler: MysqlHandler, vehicleId: string): Promise<IDbVehicle> {
|
||||||
handler: MysqlHandler,
|
|
||||||
vehicleId: string,
|
|
||||||
): Promise<IDbVehicle> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!vehicleId) return reject("Id is undefined");
|
if (!vehicleId) return reject("Id is undefined");
|
||||||
if (vehicleId.length !== 36)
|
if (vehicleId.length !== 36) return reject("Id invalid");
|
||||||
return reject("Id invalid");
|
|
||||||
const _sql = "SELECT * FROM `vehicles` WHERE `id` = ?";
|
const _sql = "SELECT * FROM `vehicles` WHERE `id` = ?";
|
||||||
const _values = [vehicleId];
|
const _values = [vehicleId];
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbVehicle);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbVehicle,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -832,9 +715,9 @@ const MySqlService = {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const _sql = "SELECT * FROM `models`";
|
const _sql = "SELECT * FROM `models`";
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.query(_sql).then((result) => {
|
||||||
handler.query(_sql) as unknown as Array<IDbVehicle>,
|
return resolve(result as unknown as Array<IDbVehicle>);
|
||||||
);
|
});
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -848,16 +731,13 @@ const MySqlService = {
|
|||||||
* @returns {Promise<Array<IDbVehicle>>} A promise that resolves to an array of available vehicles.
|
* @returns {Promise<Array<IDbVehicle>>} A promise that resolves to an array of available vehicles.
|
||||||
* @throws {Error} If an error occurs while executing the query.
|
* @throws {Error} If an error occurs while executing the query.
|
||||||
*/
|
*/
|
||||||
getAvailable(
|
getAvailable(handler: MysqlHandler): Promise<Array<IDbVehicle>> {
|
||||||
handler: MysqlHandler,
|
|
||||||
): Promise<Array<IDbVehicle>> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const _sql =
|
const _sql = "SELECT * FROM `vehicles` WERE `isAvailable` = 1";
|
||||||
"SELECT * FROM `vehicles` WERE `isAvailable` = 1";
|
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.query(_sql).then((result) => {
|
||||||
handler.query(_sql) as unknown as Array<IDbVehicle>,
|
return resolve(result as unknown as Array<IDbVehicle>);
|
||||||
);
|
});
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -866,29 +746,25 @@ const MySqlService = {
|
|||||||
},
|
},
|
||||||
Category: {
|
Category: {
|
||||||
/**
|
/**
|
||||||
* Inserts a category into the database.
|
* Insert a category into the database.
|
||||||
*
|
*
|
||||||
* @param {MysqlHandler} handler - The MySQL handler object.
|
* @param {MysqlHandler} handler - The MySQL handler instance.
|
||||||
* @param {IDbCategory} data - The category data to be inserted.
|
* @param {IDbCategory} data - The category data to insert.
|
||||||
* @return {Promise<unknown>} - A promise that resolves if the insertion is successful, and rejects with an error if it fails.
|
* @returns {Promise<IDbStatusResult>} A promise that resolves to the status result of the insertion.
|
||||||
|
* @throws {Error} If an error occurs during the execution of the insertion.
|
||||||
*/
|
*/
|
||||||
insert(
|
insert(handler: MysqlHandler, data: IDbCategory): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
data: IDbCategory,
|
|
||||||
): Promise<unknown> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject("Id is undefined");
|
if (!data.id) return reject("Id is undefined");
|
||||||
if (data.id.length !== 36) return reject("Id invalid");
|
if (data.id.length !== 36) return reject("Id invalid");
|
||||||
|
|
||||||
const _sql =
|
const _sql =
|
||||||
"INSERT INTO `categories`(`id`,`slug_name`, `display_name`) VALUES (?, ?, ?)";
|
"INSERT INTO `categories`(`id`,`slug_name`, `display_name`) VALUES (?, ?, ?)";
|
||||||
const _values = [
|
const _values = [data.id, data.slug_name, data.display_name];
|
||||||
data.id,
|
|
||||||
data.slug_name,
|
|
||||||
data.display_name,
|
|
||||||
];
|
|
||||||
try {
|
try {
|
||||||
resolve(handler.execute(_sql, _values));
|
handler.execute(_sql, _values).then((result) => {
|
||||||
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
|
});
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -898,15 +774,11 @@ const MySqlService = {
|
|||||||
/**
|
/**
|
||||||
* Updates a category in the database.
|
* Updates a category in the database.
|
||||||
*
|
*
|
||||||
* @param {MysqlHandler} handler - The MySQL handler instance.
|
* @param {MysqlHandler} handler - The MySQL handler for executing database queries.
|
||||||
* @param {IDbCategory} data - The category data to update.
|
* @param {IDbCategory} data - The category data to be updated.
|
||||||
* @returns {Promise<number>} - A promise that resolves with the number of affected rows in the database.
|
* @returns {Promise<IDbStatusResult>} - A promise that resolves to the status result of the update operation.
|
||||||
* @throws {Error} - If an error occurs during execution.
|
|
||||||
*/
|
*/
|
||||||
update(
|
update(handler: MysqlHandler, data: IDbCategory): Promise<IDbStatusResult> {
|
||||||
handler: MysqlHandler,
|
|
||||||
data: IDbCategory,
|
|
||||||
): Promise<unknown> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!data.id) return reject("Id is undefined");
|
if (!data.id) return reject("Id is undefined");
|
||||||
if (data.id.length !== 36) return reject("Id invalid");
|
if (data.id.length !== 36) return reject("Id invalid");
|
||||||
@ -914,19 +786,13 @@ const MySqlService = {
|
|||||||
try {
|
try {
|
||||||
const _template = `
|
const _template = `
|
||||||
${data.slug_name ? "`slug_name` = ?," : null}
|
${data.slug_name ? "`slug_name` = ?," : null}
|
||||||
${
|
${data.display_name ? "`display_name` = ?," : null}`;
|
||||||
data.display_name ? "`display_name` = ?," : null
|
|
||||||
}`;
|
|
||||||
|
|
||||||
const _values = [
|
const _values = [data.slug_name, data.display_name, data.id];
|
||||||
data.slug_name,
|
|
||||||
data.display_name,
|
|
||||||
data.id,
|
|
||||||
];
|
|
||||||
const _sql = `UPDATE "categories" SET ${_template} WHERE 'id' = ?`;
|
const _sql = `UPDATE "categories" SET ${_template} WHERE 'id' = ?`;
|
||||||
return resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(_sql, _values) as unknown as number,
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
);
|
});
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -937,19 +803,16 @@ const MySqlService = {
|
|||||||
*
|
*
|
||||||
* @param {MysqlHandler} handler - The MySQL handler used for executing the query.
|
* @param {MysqlHandler} handler - The MySQL handler used for executing the query.
|
||||||
*
|
*
|
||||||
* @return {Promise<Array<IDbCategory>>} - A promise that resolves with an array of category objects from the database.
|
* @return {Promise<Array<IDbCategory>>}
|
||||||
* - The category objects are of type IDbCategory.
|
* - A promise that resolves with an array of category objects from the database.
|
||||||
* - If an error occurs, the promise will be rejected with an Error object.
|
* - The category objects are of type IDbCategory.
|
||||||
|
* - If an error occurs, the promise will be rejected with an Error object.
|
||||||
*/
|
*/
|
||||||
getAll(handler: MysqlHandler): Promise<Array<IDbCategory>> {
|
getAll(handler: MysqlHandler): Promise<Array<IDbCategory>> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const _sql = "SELECT * FROM `categories`";
|
const _sql = "SELECT * FROM `categories`";
|
||||||
try {
|
try {
|
||||||
resolve(
|
resolve(handler.query(_sql) as unknown as Array<IDbCategory>);
|
||||||
handler.query(
|
|
||||||
_sql,
|
|
||||||
) as unknown as Array<IDbCategory>,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -969,16 +832,12 @@ const MySqlService = {
|
|||||||
): Promise<IDbCategory> {
|
): Promise<IDbCategory> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!categorySlug) return reject("slug is undefined");
|
if (!categorySlug) return reject("slug is undefined");
|
||||||
const _sql =
|
const _sql = "SELECT * FROM `categories` WHERE `slug_name` = ?";
|
||||||
"SELECT * FROM `categories` WHERE `slug_name` = ?";
|
|
||||||
const _values = [categorySlug];
|
const _values = [categorySlug];
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbCategory);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbCategory,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
@ -992,38 +851,41 @@ const MySqlService = {
|
|||||||
* @returns {Promise<IDbCategory>} - A Promise that resolves with the category object.
|
* @returns {Promise<IDbCategory>} - A Promise that resolves with the category object.
|
||||||
* @throws {Error} - If an error occurs during execution.
|
* @throws {Error} - If an error occurs during execution.
|
||||||
*/
|
*/
|
||||||
getById(
|
getById(handler: MysqlHandler, categoryId: string): Promise<IDbCategory> {
|
||||||
handler: MysqlHandler,
|
|
||||||
categoryId: string,
|
|
||||||
): Promise<IDbCategory> {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!categoryId) return reject("slug is undefined");
|
if (!categoryId) return reject("slug is undefined");
|
||||||
if (categoryId.length !== 36)
|
if (categoryId.length !== 36) return reject("Id invalid");
|
||||||
return reject("Id invalid");
|
const _sql = "SELECT * FROM `categories` WHERE `id` = ?";
|
||||||
const _sql =
|
|
||||||
"SELECT * FROM `categories` WHERE `id` = ?";
|
|
||||||
const _values = [categoryId];
|
const _values = [categoryId];
|
||||||
try {
|
try {
|
||||||
resolve(
|
handler.execute(_sql, _values).then((result) => {
|
||||||
handler.execute(
|
return resolve(result as unknown as IDbCategory);
|
||||||
_sql,
|
});
|
||||||
_values,
|
|
||||||
) as unknown as IDbCategory,
|
|
||||||
);
|
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a category from the database based on the given categoryId.
|
||||||
|
*
|
||||||
|
* @param {MysqlHandler} handler - The MysqlHandler object for executing database queries.
|
||||||
|
* @param {string} categoryId - The ID of the category to be deleted.
|
||||||
|
*
|
||||||
|
* @returns {Promise<IDbStatusResult>} - A promise that resolves to the database status result after deletion.
|
||||||
|
* @throws {Error} - If an error occurs while executing the deletion query.
|
||||||
|
*/
|
||||||
delete(handler: MysqlHandler, categoryId: string) {
|
delete(handler: MysqlHandler, categoryId: string) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!categoryId) return reject("Id is undefined");
|
if (!categoryId) return reject("Id is undefined");
|
||||||
if (categoryId.length !== 36)
|
if (categoryId.length !== 36) return reject("Id invalid");
|
||||||
return reject("Id invalid");
|
|
||||||
const _sql = "DELETE FROM `categories` WHERE `id` = ?";
|
const _sql = "DELETE FROM `categories` WHERE `id` = ?";
|
||||||
const _values = [categoryId];
|
const _values = [categoryId];
|
||||||
try {
|
try {
|
||||||
resolve(handler.execute(_sql, _values));
|
handler.execute(_sql, _values).then((result) => {
|
||||||
|
return resolve(result as unknown as IDbStatusResult);
|
||||||
|
});
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
reject(err as Error);
|
reject(err as Error);
|
||||||
}
|
}
|
||||||
|
@ -18,13 +18,8 @@ const DbHandler = new MySqlService.Handler("UserService");
|
|||||||
* @param {string} username - The username of the user to retrieve.
|
* @param {string} username - The username of the user to retrieve.
|
||||||
* @returns {Promise<Object | null>} - The user object if found, or null if not found.
|
* @returns {Promise<Object | null>} - The user object if found, or null if not found.
|
||||||
*/
|
*/
|
||||||
async function getUserFromUsername(
|
async function getUserFromUsername(username: string): Promise<object | null> {
|
||||||
username: string,
|
const dbUser = await MySqlService.User.getByUsername(DbHandler, username);
|
||||||
): Promise<object | null> {
|
|
||||||
const dbUser = await MySqlService.User.getByUsername(
|
|
||||||
DbHandler,
|
|
||||||
username,
|
|
||||||
);
|
|
||||||
if (dbUser === undefined) return null;
|
if (dbUser === undefined) return null;
|
||||||
return dbUser;
|
return dbUser;
|
||||||
}
|
}
|
||||||
@ -37,22 +32,16 @@ async function getUserFromIdService(id: string | undefined) {
|
|||||||
|
|
||||||
async function register(ReqData: IReqRegister) {
|
async function register(ReqData: IReqRegister) {
|
||||||
if (ReqData.password.length < 6) {
|
if (ReqData.password.length < 6) {
|
||||||
logger.info(
|
logger.info(`REGISTER :> Invalid password (${ReqData.username})`);
|
||||||
`REGISTER :> Invalid password (${ReqData.username})`,
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
error: "invalidPassword",
|
error: "invalidPassword",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const passwordHash = await CredentialService.hash(
|
const passwordHash = await CredentialService.hash(ReqData.password);
|
||||||
ReqData.password,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Does the new user has accepted GDPR ?
|
// Does the new user has accepted GDPR ?
|
||||||
if (ReqData.gdpr !== true) {
|
if (ReqData.gdpr !== true) {
|
||||||
logger.info(
|
logger.info(`REGISTER :> GDPR not validated (${ReqData.username})`);
|
||||||
`REGISTER :> GDPR not validated (${ReqData.username})`,
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
error: "gdprNotApproved",
|
error: "gdprNotApproved",
|
||||||
};
|
};
|
||||||
@ -60,9 +49,7 @@ async function register(ReqData: IReqRegister) {
|
|||||||
|
|
||||||
// Check if exist and return
|
// Check if exist and return
|
||||||
|
|
||||||
const dbUserIfExist = await getUserFromUsername(
|
const dbUserIfExist = await getUserFromUsername(ReqData.username);
|
||||||
ReqData.username,
|
|
||||||
);
|
|
||||||
if (dbUserIfExist) {
|
if (dbUserIfExist) {
|
||||||
logger.info(
|
logger.info(
|
||||||
`REGISTER :> User exist (${dbUserIfExist.username})\n ID:${dbUserIfExist.id}`,
|
`REGISTER :> User exist (${dbUserIfExist.username})\n ID:${dbUserIfExist.id}`,
|
||||||
@ -110,9 +97,7 @@ async function register(ReqData: IReqRegister) {
|
|||||||
};
|
};
|
||||||
logger.info(userData);
|
logger.info(userData);
|
||||||
await Db.collection("users").insertOne(NewUser);
|
await Db.collection("users").insertOne(NewUser);
|
||||||
logger.info(
|
logger.info(`REGISTER :> Inserted new user (${NewUser.username})`);
|
||||||
`REGISTER :> Inserted new user (${NewUser.username})`,
|
|
||||||
);
|
|
||||||
return userData;
|
return userData;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,18 +108,14 @@ async function login(ReqData: IReqLogin) {
|
|||||||
ReqData.username,
|
ReqData.username,
|
||||||
);
|
);
|
||||||
if (!dbUser) {
|
if (!dbUser) {
|
||||||
console.log(
|
console.log(`LoginService :> User does not exist (${ReqData.username})`);
|
||||||
`LoginService :> User does not exist (${ReqData.username})`,
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
error: "userNotFound",
|
error: "userNotFound",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (ReqData.password.length < 6) {
|
if (ReqData.password.length < 6) {
|
||||||
console.log("X");
|
console.log("X");
|
||||||
console.log(
|
console.log(`LoginService :> Invalid password (${ReqData.username})`);
|
||||||
`LoginService :> Invalid password (${ReqData.username})`,
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
error: "invalidPassword",
|
error: "invalidPassword",
|
||||||
};
|
};
|
||||||
@ -145,9 +126,7 @@ async function login(ReqData: IReqLogin) {
|
|||||||
);
|
);
|
||||||
if (!isPasswordValid) {
|
if (!isPasswordValid) {
|
||||||
console.log(isPasswordValid);
|
console.log(isPasswordValid);
|
||||||
console.log(
|
console.log(`LoginService :> Invalid password (${ReqData.username})`);
|
||||||
`LoginService :> Invalid password (${ReqData.username})`,
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
error: "invalidPassword",
|
error: "invalidPassword",
|
||||||
};
|
};
|
||||||
@ -213,16 +192,12 @@ async function getAllUsersService() {
|
|||||||
*/
|
*/
|
||||||
async function editUserService(targetId, sanitizedData) {
|
async function editUserService(targetId, sanitizedData) {
|
||||||
if (sanitizedData.password) {
|
if (sanitizedData.password) {
|
||||||
const passwordHash = await getHashFromPassword(
|
const passwordHash = await getHashFromPassword(sanitizedData.password);
|
||||||
sanitizedData.password,
|
|
||||||
);
|
|
||||||
delete sanitizedData.password;
|
delete sanitizedData.password;
|
||||||
logger.info(`Changing password for user "${targetId}"`);
|
logger.info(`Changing password for user "${targetId}"`);
|
||||||
sanitizedData.passwordHash = passwordHash;
|
sanitizedData.passwordHash = passwordHash;
|
||||||
}
|
}
|
||||||
const updatedUserResult = await Db.collection(
|
const updatedUserResult = await Db.collection("users").updateOne(
|
||||||
"users",
|
|
||||||
).updateOne(
|
|
||||||
{
|
{
|
||||||
id: targetId,
|
id: targetId,
|
||||||
},
|
},
|
||||||
|
@ -13,14 +13,9 @@ const UNAUTHORIZED = 401;
|
|||||||
const FORBIDDEN = 403;
|
const FORBIDDEN = 403;
|
||||||
const UNAUTH_MESSAGE = "Missing Authorization Header";
|
const UNAUTH_MESSAGE = "Missing Authorization Header";
|
||||||
const INVALID_TOKEN_MESSAGE = "Invalid or expired token.";
|
const INVALID_TOKEN_MESSAGE = "Invalid or expired token.";
|
||||||
const PERMISSON_NOT_VALID =
|
const PERMISSON_NOT_VALID = "You are missing the required permission.";
|
||||||
"You are missing the required permission.";
|
|
||||||
|
|
||||||
async function AdminGuard(
|
async function AdminGuard(req: Request, res: Response, next: NextFunction) {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction,
|
|
||||||
) {
|
|
||||||
const authHeader = req.headers.authorization;
|
const authHeader = req.headers.authorization;
|
||||||
if (!authHeader) {
|
if (!authHeader) {
|
||||||
logger.warn(`Invalid header (${req.ip})`);
|
logger.warn(`Invalid header (${req.ip})`);
|
||||||
@ -40,11 +35,10 @@ async function AdminGuard(
|
|||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const isSourceAdmin =
|
const isSourceAdmin = await MysqlService.User.getAdminStateForId(
|
||||||
await MysqlService.User.getAdminStateForId(
|
DbHandler,
|
||||||
DbHandler,
|
token.sub,
|
||||||
token.sub,
|
);
|
||||||
);
|
|
||||||
if (isSourceAdmin === true) next();
|
if (isSourceAdmin === true) next();
|
||||||
return res.status(FORBIDDEN).json({
|
return res.status(FORBIDDEN).json({
|
||||||
message: PERMISSON_NOT_VALID,
|
message: PERMISSON_NOT_VALID,
|
||||||
|
@ -14,11 +14,7 @@ const UNAUTH_MESSAGE = "Missing Authorization Header";
|
|||||||
const INVALID_TOKEN_MESSAGE = "Invalid or expired token.";
|
const INVALID_TOKEN_MESSAGE = "Invalid or expired token.";
|
||||||
const USER_NOT_EXIST = "You dont exist anymore";
|
const USER_NOT_EXIST = "You dont exist anymore";
|
||||||
|
|
||||||
async function UserGuard(
|
async function UserGuard(req: Request, res: Response, next: NextFunction) {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction,
|
|
||||||
) {
|
|
||||||
const authHeader = req.headers.authorization;
|
const authHeader = req.headers.authorization;
|
||||||
if (!authHeader) {
|
if (!authHeader) {
|
||||||
return res.status(UNAUTHORIZED).json({
|
return res.status(UNAUTHORIZED).json({
|
||||||
@ -44,10 +40,7 @@ async function UserGuard(
|
|||||||
message: USER_NOT_EXIST,
|
message: USER_NOT_EXIST,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const user = await MySqlService.User.getById(
|
const user = await MySqlService.User.getById(DbHandler, userId);
|
||||||
DbHandler,
|
|
||||||
userId,
|
|
||||||
);
|
|
||||||
if (user) {
|
if (user) {
|
||||||
logger.info(`An user do a request. (${user?.username})`);
|
logger.info(`An user do a request. (${user?.username})`);
|
||||||
next();
|
next();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user