feat(users, db): users access test used to clean db access flow

This commit is contained in:
2024-10-26 13:20:46 +02:00
parent 0379fd4e28
commit 3327ae5996
4 changed files with 45 additions and 19 deletions

View File

@@ -9,9 +9,7 @@ const db_pool = mysql.createPool({
database: "path",
});
async function DB_Query<T>(
query: string,
): Promise<Partial<T>[] | ResponseError> {
async function DB_Query<T>(query: string): Promise<Partial<T>[]> {
try {
const [results, _fields] = await db_pool.query(query);
return results as T[];

View File

@@ -1,20 +1,34 @@
import { Router } from "express";
import UserService from "./users.service";
import { ResponseError } from "../../utils/error/response-error.model";
export const userRoutes = Router();
const userService = new UserService();
userRoutes.get("/", async (req, res) => {
//TODO: remove info from these Users and block access to NON-admins
userRoutes.get("/", async (_, res) => {
try {
const response = await userService.getAllUsers();
// console.log("__DB_DATA", data);
res.status(200);
res.send(response);
} catch (e) {
// const response_error = new ResponseError(e);
res.status(400);
res.status(500);
res.send(e);
}
});
userRoutes.get("/:username", async (req, res) => {
try {
const response = await userService.getUserByUsername(req.params.username);
if (response) {
res.status(200);
res.send(response);
} else {
res.status(400);
res.send("error");
}
} catch (e) {
res.status(500);
res.send(e);
}
});

View File

@@ -1,20 +1,20 @@
import { db_connection, db_query } from "../../db";
import { db_query } from "../../db";
import { User } from "./users.types";
class UserService {
constructor() {}
async getTestUsers() {
const data = await db_connection.execute("select * from user");
return data;
async getAllUsers(): Promise<User[]> {
const data = await db_query("select * from user where id == 1");
return data as User[];
}
async getAllUsers() {
try {
const data = await db_query("select * from user where user.id == 3");
return { value: data, example: "algo" };
} catch (e) {
return e;
}
async getUserByUsername(username: string): Promise<User | null> {
const data = await db_query(
`select * from user as user WHERE LOWER(username) = LOWER('${username}');`,
);
console.log("Data:", data);
return data.length ? (data[0] as User) : null;
}
// getUserById(id: number) {

View File

@@ -0,0 +1,14 @@
export enum Role {
Public = "public",
User = "user",
Manager = "manager",
Admin = "admin",
}
export type User = {
id: number;
username: string;
password: string;
roles: Role[];
picture: string;
};