From fe52c266897801234307d65263d2ad1f510151ec Mon Sep 17 00:00:00 2001 From: Ramesh Mane <101566080+rameshmane7218@users.noreply.github.com> Date: Mon, 24 Jun 2024 08:28:52 +0000 Subject: [PATCH] feat(nc-gui): add project audit list api --- .../src/controllers/audits.controller.ts | 15 ++ packages/nocodb/src/models/Audit.ts | 91 ++++++++++++ packages/nocodb/src/schema/swagger-v2.json | 136 ++++++++++++++++++ packages/nocodb/src/schema/swagger.json | 136 ++++++++++++++++++ .../nocodb/src/services/audits.service.ts | 8 ++ 5 files changed, 386 insertions(+) diff --git a/packages/nocodb/src/controllers/audits.controller.ts b/packages/nocodb/src/controllers/audits.controller.ts index d875159448..13f02b5a25 100644 --- a/packages/nocodb/src/controllers/audits.controller.ts +++ b/packages/nocodb/src/controllers/audits.controller.ts @@ -66,4 +66,19 @@ export class AuditsController { }, ); } + @Get(['/api/v1/db/meta/projects/audits/', '/api/v2/meta/projects/audits/']) + @Acl('projectAuditList') + async projectAuditList(@Req() req: NcRequest) { + return new PagedResponseImpl( + await this.auditsService.projectAuditList({ + query: req.query, + }), + { + count: await this.auditsService.projectAuditCount({ + query: req.query, + }), + ...req.query, + }, + ); + } } diff --git a/packages/nocodb/src/models/Audit.ts b/packages/nocodb/src/models/Audit.ts index cba937f7b6..c3528e6d71 100644 --- a/packages/nocodb/src/models/Audit.ts +++ b/packages/nocodb/src/models/Audit.ts @@ -253,4 +253,95 @@ export default class Audit implements AuditType { .first() )?.count; } + + static async projectAuditList({ + limit = 25, + offset = 0, + sourceId, + user, + baseId, + type, + startDate, + endDate, + orderBy, + }: { + limit?: number; + offset?: number; + sourceId?: string; + user?: string; + baseId?: string; + type?: string; + startDate?: string; + endDate?: string; + orderBy?: { + created_at?: 'asc' | 'desc'; + user?: 'asc' | 'desc'; + }; + }) { + return await Noco.ncMeta.metaList2( + RootScopes.ROOT, + RootScopes.ROOT, + MetaTable.AUDIT, + { + limit, + offset, + condition: { + ...(user ? { user: user } : {}), + ...(baseId ? { base_id: baseId } : {}), + ...(sourceId ? { source_id: sourceId } : {}), + ...(type ? { op_type: type } : {}), + }, + orderBy: { + ...(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 } }] : []), + ], + }, + }, + ); + } + + static async projectAuditCount({ + user, + baseId, + sourceId, + type, + startDate, + endDate, + }: { + user?: string; + baseId?: string; + sourceId?: string; + type?: string; + startDate?: string; + endDate?: string; + }): Promise { + return await Noco.ncMeta.metaCount( + RootScopes.ROOT, + RootScopes.ROOT, + MetaTable.AUDIT, + { + condition: { + ...(user ? { user: user } : {}), + ...(baseId ? { base_id: baseId } : {}), + ...(sourceId ? { source_id: sourceId } : {}), + ...(type ? { op_type: type } : {}), + }, + xcCondition: { + _and: [ + ...(startDate ? [{ created_at: { ge: startDate } }] : []), + ...(endDate ? [{ created_at: { le: endDate } }] : []), + ], + }, + }, + ); + } } diff --git a/packages/nocodb/src/schema/swagger-v2.json b/packages/nocodb/src/schema/swagger-v2.json index 687b35f1f0..55cb575271 100644 --- a/packages/nocodb/src/schema/swagger-v2.json +++ b/packages/nocodb/src/schema/swagger-v2.json @@ -8987,6 +8987,142 @@ ] } }, + "/api/v2/meta/projects/audits": { + "parameters": [ + { + "$ref": "#/components/parameters/xc-auth" + } + ], + "get": { + "summary": "List Audits in Project", + "operationId": "project-audit-list", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "list": { + "type": "array", + "uniqueItems": true, + "items": { + "$ref": "#/components/schemas/Audit" + } + }, + "pageInfo": { + "$ref": "#/components/schemas/Paginated" + } + }, + "required": [ + "list", + "pageInfo" + ] + } + } + } + }, + "400": { + "$ref": "#/components/responses/BadRequest" + } + }, + "description": "List all audit data in the given project", + "parameters": [ + { + "schema": { + "type": "integer", + "minimum": 0 + }, + "in": "query", + "name": "offset" + }, + { + "schema": { + "type": "integer", + "minimum": 1 + }, + "in": "query", + "name": "limit" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "user" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "type" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "baseId" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "sourceId" + }, + { + "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" + } + ], + "tags": [ + "Utils" + ] + } + } "/api/v2/meta/bases/{baseId}/audits": { "parameters": [ { diff --git a/packages/nocodb/src/schema/swagger.json b/packages/nocodb/src/schema/swagger.json index 9dac0da3e5..75888919c0 100644 --- a/packages/nocodb/src/schema/swagger.json +++ b/packages/nocodb/src/schema/swagger.json @@ -14082,6 +14082,142 @@ ] } }, + "/api/v1/db/meta/projects/audits": { + "parameters": [ + { + "$ref": "#/components/parameters/xc-auth" + } + ], + "get": { + "summary": "List Audits in Project", + "operationId": "project-audit-list", + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "list": { + "type": "array", + "uniqueItems": true, + "items": { + "$ref": "#/components/schemas/Audit" + } + }, + "pageInfo": { + "$ref": "#/components/schemas/Paginated" + } + }, + "required": [ + "list", + "pageInfo" + ] + } + } + } + }, + "400": { + "$ref": "#/components/responses/BadRequest" + } + }, + "description": "List all audit data in the given project", + "parameters": [ + { + "schema": { + "type": "integer", + "minimum": 0 + }, + "in": "query", + "name": "offset" + }, + { + "schema": { + "type": "integer", + "minimum": 1 + }, + "in": "query", + "name": "limit" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "user" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "type" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "baseId" + }, + { + "schema": { + "type": "string" + }, + "in": "query", + "name": "sourceId" + }, + { + "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" + } + ], + "tags": [ + "Utils" + ] + } + } "/api/v1/db/meta/projects/{baseId}/audits": { "parameters": [ { diff --git a/packages/nocodb/src/services/audits.service.ts b/packages/nocodb/src/services/audits.service.ts index 057e7ad3c1..49a1e34434 100644 --- a/packages/nocodb/src/services/audits.service.ts +++ b/packages/nocodb/src/services/audits.service.ts @@ -84,4 +84,12 @@ export class AuditsService { async sourceAuditCount(param: { query: any; sourceId: string }) { return await Audit.sourceAuditCount(param.sourceId); } + + async projectAuditList(param: { query: any }) { + return await Audit.projectAuditList(param.query); + } + + async projectAuditCount(param: { query?: any }) { + return await Audit.projectAuditCount(param.query); + } }