From c236e8169b20604b04ea0b04adb59d8d6f3dc5cc Mon Sep 17 00:00:00 2001 From: Ramesh Mane <101566080+rameshmane7218@users.noreply.github.com> Date: Mon, 24 Jun 2024 08:28:51 +0000 Subject: [PATCH] feat(nc-gui): add filter support in base audit list api --- .../src/controllers/audits.controller.ts | 5 +- packages/nocodb/src/models/Audit.ts | 67 ++++++++++++++++--- packages/nocodb/src/schema/swagger-v2.json | 56 +++++++++++++++- packages/nocodb/src/schema/swagger.json | 56 +++++++++++++++- .../nocodb/src/services/audits.service.ts | 10 +-- 5 files changed, 176 insertions(+), 18 deletions(-) diff --git a/packages/nocodb/src/controllers/audits.controller.ts b/packages/nocodb/src/controllers/audits.controller.ts index d08fd67e94..d875159448 100644 --- a/packages/nocodb/src/controllers/audits.controller.ts +++ b/packages/nocodb/src/controllers/audits.controller.ts @@ -58,7 +58,10 @@ export class AuditsController { baseId, }), { - count: await this.auditsService.auditCount({ baseId }), + count: await this.auditsService.auditCount({ + query: req.query, + baseId, + }), ...req.query, }, ); diff --git a/packages/nocodb/src/models/Audit.ts b/packages/nocodb/src/models/Audit.ts index 21d76f0722..cba937f7b6 100644 --- a/packages/nocodb/src/models/Audit.ts +++ b/packages/nocodb/src/models/Audit.ts @@ -141,10 +141,23 @@ export default class Audit implements AuditType { limit = 25, offset = 0, sourceId, + user, + type, + startDate, + endDate, + orderBy, }: { limit?: number; offset?: number; sourceId?: string; + user?: string; + type?: string; + startDate?: string; + endDate?: string; + orderBy?: { + created_at?: 'asc' | 'desc'; + user?: 'asc' | 'desc'; + }; }, ) { return await Noco.ncMeta.metaList2( @@ -155,9 +168,22 @@ export default class Audit implements AuditType { condition: { base_id: baseId, ...(sourceId ? { source_id: sourceId } : {}), + ...(user ? { user: user } : {}), + ...(type ? { op_type: type } : {}), }, orderBy: { - created_at: 'desc', + ...(orderBy?.created_at + ? { created_at: orderBy?.created_at } + : !orderBy?.user + ? { created_at: 'desc' } + : {}), + ...(orderBy?.user ? { user: orderBy?.user } : {}), + }, + xcCondition: { + _and: [ + ...(startDate ? [{ created_at: { ge: startDate } }] : []), + ...(endDate ? [{ created_at: { le: endDate } }] : []), + ], }, limit, offset, @@ -167,18 +193,39 @@ export default class Audit implements AuditType { static async baseAuditCount( baseId: string, - sourceId?: string, + { + sourceId, + user, + type, + startDate, + endDate, + }: { + sourceId?: string; + user?: string; + type?: string; + startDate?: string; + endDate?: string; + }, ): Promise { - return ( - await Noco.ncMeta - .knex(MetaTable.AUDIT) - .where({ + return await Noco.ncMeta.metaCount( + RootScopes.ROOT, + RootScopes.ROOT, + MetaTable.AUDIT, + { + condition: { base_id: baseId, + ...(user ? { user: user } : {}), ...(sourceId ? { source_id: sourceId } : {}), - }) - .count('id', { as: 'count' }) - .first() - )?.count; + ...(type ? { op_type: type } : {}), + }, + xcCondition: { + _and: [ + ...(startDate ? [{ created_at: { ge: startDate } }] : []), + ...(endDate ? [{ created_at: { le: endDate } }] : []), + ], + }, + }, + ); } static async sourceAuditList(sourceId: string, { limit = 25, offset = 0 }) { diff --git a/packages/nocodb/src/schema/swagger-v2.json b/packages/nocodb/src/schema/swagger-v2.json index 6413cc6174..687b35f1f0 100644 --- a/packages/nocodb/src/schema/swagger-v2.json +++ b/packages/nocodb/src/schema/swagger-v2.json @@ -9015,7 +9015,6 @@ "list": { "type": "array", "uniqueItems": true, - "minItems": 1, "items": { "$ref": "#/components/schemas/Audit" } @@ -9061,6 +9060,61 @@ "in": "query", "name": "sourceId" }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "user" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "type" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "startDate" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "endDate" + }, + { + "schema": { + "properties": { + "created_at": { + "type": "string", + "description": "Sort direction", + "enum": [ + "asc", + "desc" + ], + "example": "desc" + }, + "user": { + "type": "string", + "description": "Sort direction", + "enum": [ + "asc", + "desc" + ], + "example": "desc" + } + }, + "type": "object" + }, + "in": "query", + "name": "orderBy" + }, { "$ref": "#/components/parameters/xc-auth" } diff --git a/packages/nocodb/src/schema/swagger.json b/packages/nocodb/src/schema/swagger.json index a9c8bd2369..9dac0da3e5 100644 --- a/packages/nocodb/src/schema/swagger.json +++ b/packages/nocodb/src/schema/swagger.json @@ -14110,7 +14110,6 @@ "list": { "type": "array", "uniqueItems": true, - "minItems": 1, "items": { "$ref": "#/components/schemas/Audit" } @@ -14156,6 +14155,61 @@ "in": "query", "name": "sourceId" }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "user" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "type" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "startDate" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "endDate" + }, + { + "schema": { + "properties": { + "created_at": { + "type": "string", + "description": "Sort direction", + "enum": [ + "asc", + "desc" + ], + "example": "desc" + }, + "user": { + "type": "string", + "description": "Sort direction", + "enum": [ + "asc", + "desc" + ], + "example": "desc" + } + }, + "type": "object" + }, + "in": "query", + "name": "orderBy" + }, { "$ref": "#/components/parameters/xc-auth" } diff --git a/packages/nocodb/src/services/audits.service.ts b/packages/nocodb/src/services/audits.service.ts index fccd57ae5a..057e7ad3c1 100644 --- a/packages/nocodb/src/services/audits.service.ts +++ b/packages/nocodb/src/services/audits.service.ts @@ -74,14 +74,14 @@ export class AuditsService { } async auditCount(param: { query?: any; baseId: string }) { - return await Audit.baseAuditCount(param.baseId, param.query?.sourceId); + return await Audit.baseAuditCount(param.baseId, param.query); } - async baseAuditList(param: { query: any; sourceId: any }) { - return await Audit.baseAuditList(param.sourceId, param.query); + async sourceAuditList(param: { query: any; sourceId: any }) { + return await Audit.sourceAuditList(param.sourceId, param.query); } - async baseAuditCount(param: { sourceId: string }) { - return await Audit.baseAuditCount(param.sourceId); + async sourceAuditCount(param: { query: any; sourceId: string }) { + return await Audit.sourceAuditCount(param.sourceId); } }