Skip to content
Snippets Groups Projects
Commit 0fa1bd58 authored by Agustin Ruiz Diaz Cambon's avatar Agustin Ruiz Diaz Cambon
Browse files

listAudit

now AuditController can listAudit with paginator through a post method.
userListById added to UserAPI
missing token check
commented authChecker
parent 971f09bc
No related branches found
No related tags found
No related merge requests found
import {
Request, Response, Router,
} from 'express';
import AuditDTO from '../DTOs/AuditDTO';
import logger from '../Logger/logger';
import Auditor from '../Services/Auditor';
const router = Router();
const auditGet = async (req: Request, res: Response) => {
try {
const audits = await Auditor.getAudit(Number(req.query.cant), Number(req.query.page));
return res.status(200).send(audits);
} catch (error) {
const e = error as Error;
logger.info(e.message);
return res.status(400).json({ error: e.message });
}
};
router.get('/', auditGet);
......
type AuditDTO = {
id: number;
email: string;
user_name: string;
organization_name: string;
date: string;
action: string;
};
export default AuditDTO;
......@@ -50,7 +50,7 @@ function initParameterDataBase(): void {
console.log(err);
});
});
Auditor.sync({ force: true });
Auditor.sync({ force: false });
}
export default { initParameterDataBase };
import { Request, Response } from 'express';
import AgeGroup from '../DTOs/AgeGroupDTO';
import AuditDTO from '../DTOs/AuditDTO';
import Paginator from '../interfaces/paginator.interface';
import Auditor from '../Models/Auditor';
import UserAPI from './UserAPI';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const audit = (request: any, action: string): void => {
......@@ -6,6 +11,62 @@ export const audit = (request: any, action: string): void => {
Auditor.create({ user_id: userId, action });
};
const listAudits = async (limit: number, offset: number): Promise<Paginator<Auditor>> => {
let options = {};
if (limit >= 1 && offset >= 0) {
options = {
limit,
offset,
};
}
const res = await Auditor.findAndCountAll({
attributes: [
'id', 'user_id', 'action', 'createdAt',
],
...options,
});
return res;
};
const getAudit = async (cant: number, page: number) => {
const offset = cant * (page - 1);
const audits: Paginator<Auditor> = await listAudits(cant, offset);
const ids: number[] = [];
audits.rows.forEach((auditor: Auditor) => {
if (!ids.includes(auditor.user_id)) {
ids.push(auditor.user_id);
}
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const users = await UserAPI.listUsersById({ userIds: ids }) as any[];
const usersMap: Map<number, any> = new Map();
users.forEach((user) => {
usersMap.set(user.id, user);
});
const userAudits: AuditDTO[] = [];
audits.rows.forEach((auditor) => {
const user = usersMap.get(auditor.user_id);
const userAudit: AuditDTO = {
id: auditor.id,
email: user.email,
user_name: user.name,
organization_name: user.organization,
date: auditor.time,
action: auditor.action,
};
userAudits.push(userAudit);
});
return {
count: audits.rows.length,
list: userAudits,
};
};
// {
// count:number,
// list:[{id,user_name,full_name, organization_name, date, action}]
// }
// export const getAudit = (cant: number, page: number) => {
// const init = (cant * page) - cant;
// const end = cant * page;
......@@ -19,3 +80,7 @@ export const audit = (request: any, action: string): void => {
// };
// BD: UUID // USER ID // ACTION // TIME
export default {
getAudit,
};
......@@ -84,6 +84,12 @@ const removeAdminPermission = async (idUser: string, token: string) => {
return res.data;
};
const listUsersById = async (userIds: any) => {
const url = '/usersById';
const res = await instance.post(url, userIds);
return res.data;
};
export default {
create,
login,
......@@ -94,4 +100,5 @@ export default {
cancel,
giveAdminPermission,
removeAdminPermission,
listUsersById,
};
export default interface Paginator<T> {
count: number;
rows: T[];
}
......@@ -14,7 +14,7 @@ router.get('/', (req: Request, res: Response): void => {
router.use('/users', UserController);
// From this line on a auth verification will be taken
router.use(authChecker);
// router.use(authChecker);
router.use('/sheetParser', SheetController);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment