diff --git a/CHANGELOG.md b/CHANGELOG.md index 9989edc98..b4623fc53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Filter, search and sort the event log #376 #455 - Dark mode! #420 #453 - Support multiple wallets: WalletConnect, Trust, Rainbow etc #424 - Export the user list as csv #402 #450 diff --git a/packages/api/src/eventlog/controllers.ts b/packages/api/src/eventlog/controllers.ts index 57918cb6a..fe6116cbc 100644 --- a/packages/api/src/eventlog/controllers.ts +++ b/packages/api/src/eventlog/controllers.ts @@ -1,5 +1,6 @@ import { getQuerySort } from '@shared/functions'; import { + EventLogsQueryInputParsedQs, PaginatedResponseBody, QueryInputParsedQs, TypedRequestQuery, @@ -7,22 +8,40 @@ import { } from '@shared/types'; import { BadRequestError } from '@error/errors'; import { StatusCodes } from 'http-status-codes'; -import { EventLogModel } from './entities'; -import { EventLogDto } from './types'; -import { eventLogListTransformer } from './transformers'; +import { EventLogModel, EventLogTypeModel } from './entities'; +import { EventLogDto, EventLogInput, EventLogTypeDto } from './types'; +import { + eventLogListTransformer, + eventLogTypeListTransformer, +} from './transformers'; +import mongoose from 'mongoose'; /** * Fetch a paginated list of EventLogs */ export const all = async ( - req: TypedRequestQuery, + req: TypedRequestQuery, res: TypedResponse> ): Promise => { if (!req.query.limit || !req.query.page) throw new BadRequestError('limit and page are required'); + const query: EventLogInput = {}; + if (req.query.type) { + const typesArray = req.query.type.split(','); + const types = await EventLogTypeModel.find({ key: { $in: typesArray } }); + query.type = types.map((item) => new mongoose.Types.ObjectId(item.id)); + } + + if (req.query.search && req.query.search !== '') { + query.description = { + $regex: `${req.query.search.toString()}`, + $options: 'i', + }; + } + const paginateQuery = { - query: {}, + query, limit: parseInt(req.query.limit), page: parseInt(req.query.page), sort: getQuerySort(req.query), @@ -43,3 +62,22 @@ export const all = async ( docs: docsTransfomed, }); }; + +/** + * Fetch a list of EventLogsTypes + */ +export const types = async ( + req: TypedRequestQuery, + res: TypedResponse> +): Promise => { + const response = await EventLogTypeModel.find(); + + if (!response) throw new BadRequestError('Failed to query event log types'); + + const docsTransfomed = await eventLogTypeListTransformer(response); + + res.status(StatusCodes.OK).json({ + ...response, + docs: docsTransfomed, + }); +}; diff --git a/packages/api/src/eventlog/routes.ts b/packages/api/src/eventlog/routes.ts index b470e754f..00c6e04b2 100644 --- a/packages/api/src/eventlog/routes.ts +++ b/packages/api/src/eventlog/routes.ts @@ -5,5 +5,6 @@ import * as controller from './controllers'; const eventLogRouter = Router(); eventLogRouter.getAsync('/all', controller.all); +eventLogRouter.getAsync('/types', controller.types); export { eventLogRouter }; diff --git a/packages/api/src/eventlog/transformers.ts b/packages/api/src/eventlog/transformers.ts index 8c573188f..aca22f75f 100644 --- a/packages/api/src/eventlog/transformers.ts +++ b/packages/api/src/eventlog/transformers.ts @@ -23,6 +23,16 @@ const eventLogTypeTransformer = ( } as EventLogTypeDto; }; +export const eventLogTypeListTransformer = async ( + eventLogTypes: EventLogTypeDocument[] +): Promise => { + const eventLogTypeDtos = await Promise.all( + eventLogTypes.map((eventLogType) => eventLogTypeTransformer(eventLogType)) + ); + + return eventLogTypeDtos; +}; + export const eventLogTransformer = async ( eventLog: EventLogDocument, currentUserRoles: UserRole[] = [UserRole.USER] diff --git a/packages/api/src/eventlog/types.ts b/packages/api/src/eventlog/types.ts index 044309564..b4e5cbc8b 100644 --- a/packages/api/src/eventlog/types.ts +++ b/packages/api/src/eventlog/types.ts @@ -47,3 +47,8 @@ export interface EventLogTypeDto { label: string; description: string; } + +export interface EventLogInput { + type?: Types.ObjectId[]; + description?: Object; +} diff --git a/packages/api/src/shared/types.ts b/packages/api/src/shared/types.ts index de25e45a4..7ba643dc2 100644 --- a/packages/api/src/shared/types.ts +++ b/packages/api/src/shared/types.ts @@ -20,6 +20,15 @@ export interface SearchQueryInput extends QueryInput { export interface SearchQueryInputParsedQs extends SearchQueryInput, Query {} +export interface EventLogsQueryInput extends QueryInput { + search?: string; + type?: string; +} + +export interface EventLogsQueryInputParsedQs + extends EventLogsQueryInput, + Query {} + export interface TypedRequest extends Request { body: U; query: T; diff --git a/packages/frontend/public/index.html b/packages/frontend/public/index.html index 62443c692..edf1db805 100644 --- a/packages/frontend/public/index.html +++ b/packages/frontend/public/index.html @@ -60,7 +60,7 @@ Praise - +