Modify getWorkspaceLogs to accept sortBy query param

This commit is contained in:
Tuan Dang
2023-01-03 09:19:07 +07:00
parent 14286795e9
commit 0ff8194cf8
3 changed files with 16 additions and 2 deletions

View File

@ -37,6 +37,12 @@ import {
});
}
/**
* Return (audit) logs for workspace with id [workspaceId]
* @param req
* @param res
* @returns
*/
export const getWorkspaceLogs = async (req: Request, res: Response) => {
let logs
try {
@ -44,6 +50,7 @@ export const getWorkspaceLogs = async (req: Request, res: Response) => {
const offset: number = parseInt(req.query.offset as string);
const limit: number = parseInt(req.query.limit as string);
const sortBy: string = req.query.sortBy as string;
const userId: string = req.query.userId as string;
const actionNames: string = req.query.actionNames as string;
@ -59,7 +66,7 @@ export const getWorkspaceLogs = async (req: Request, res: Response) => {
} : {}
)
})
.sort({ createdAt: -1 })
.sort({ createdAt: sortBy === 'recent' ? -1 : 1 })
.skip(offset)
.limit(limit)
.populate('actions')

View File

@ -33,6 +33,9 @@ router.get(
param('workspaceId').exists().trim(),
query('offset').exists().isInt(),
query('limit').exists().isInt(),
query('sortBy'),
query('userId'),
query('actionNames'),
validateRequest,
workspaceController.getWorkspaceLogs
);

View File

@ -25,6 +25,7 @@ const getProjectLogs = async ({ workspaceId, offset, limit, userId, actionNames
payload = {
offset: String(offset),
limit: String(limit),
sortBy: 'recent',
userId: JSON.stringify(userId),
actionNames: actionNames
}
@ -32,18 +33,21 @@ const getProjectLogs = async ({ workspaceId, offset, limit, userId, actionNames
payload = {
offset: String(offset),
limit: String(limit),
sortBy: 'recent',
userId: JSON.stringify(userId)
}
} else if (actionNames != "") {
payload = {
offset: String(offset),
limit: String(limit),
sortBy: 'recent',
actionNames: actionNames
}
} else {
payload = {
offset: String(offset),
limit: String(limit)
limit: String(limit),
sortBy: 'recent'
}
}