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
@@ -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) => { export const getWorkspaceLogs = async (req: Request, res: Response) => {
let logs let logs
try { try {
@@ -44,6 +50,7 @@ export const getWorkspaceLogs = async (req: Request, res: Response) => {
const offset: number = parseInt(req.query.offset as string); const offset: number = parseInt(req.query.offset as string);
const limit: number = parseInt(req.query.limit 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 userId: string = req.query.userId as string;
const actionNames: string = req.query.actionNames 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) .skip(offset)
.limit(limit) .limit(limit)
.populate('actions') .populate('actions')
+3
View File
@@ -33,6 +33,9 @@ router.get(
param('workspaceId').exists().trim(), param('workspaceId').exists().trim(),
query('offset').exists().isInt(), query('offset').exists().isInt(),
query('limit').exists().isInt(), query('limit').exists().isInt(),
query('sortBy'),
query('userId'),
query('actionNames'),
validateRequest, validateRequest,
workspaceController.getWorkspaceLogs workspaceController.getWorkspaceLogs
); );
+5 -1
View File
@@ -25,6 +25,7 @@ const getProjectLogs = async ({ workspaceId, offset, limit, userId, actionNames
payload = { payload = {
offset: String(offset), offset: String(offset),
limit: String(limit), limit: String(limit),
sortBy: 'recent',
userId: JSON.stringify(userId), userId: JSON.stringify(userId),
actionNames: actionNames actionNames: actionNames
} }
@@ -32,18 +33,21 @@ const getProjectLogs = async ({ workspaceId, offset, limit, userId, actionNames
payload = { payload = {
offset: String(offset), offset: String(offset),
limit: String(limit), limit: String(limit),
sortBy: 'recent',
userId: JSON.stringify(userId) userId: JSON.stringify(userId)
} }
} else if (actionNames != "") { } else if (actionNames != "") {
payload = { payload = {
offset: String(offset), offset: String(offset),
limit: String(limit), limit: String(limit),
sortBy: 'recent',
actionNames: actionNames actionNames: actionNames
} }
} else { } else {
payload = { payload = {
offset: String(offset), offset: String(offset),
limit: String(limit) limit: String(limit),
sortBy: 'recent'
} }
} }