diff --git a/packages/api-client/src/controllers/event/event.ts b/packages/api-client/src/controllers/event/event.ts new file mode 100644 index 00000000..19005022 --- /dev/null +++ b/packages/api-client/src/controllers/event/event.ts @@ -0,0 +1,23 @@ +import { GetEventsRequest, GetEventsResponse } from '../../types/event.types' +import { APIClient } from '../../core/client' +import { ClientResponse } from '../../types/index.types' +import { parseResponse } from '../../core/response-parser' + +export default class EventController { + private apiClient: APIClient + + constructor(private readonly backendUrl: string) { + this.apiClient = new APIClient(this.backendUrl) + } + + async getEvents( + request: GetEventsRequest, + headers?: Record + ): Promise> { + const response = await this.apiClient.get( + `/api/event/${request.workspaceId}?source=${request.source}`, + headers + ) + return await parseResponse(response) + } +} diff --git a/packages/api-client/src/types/event.types.d.ts b/packages/api-client/src/types/event.types.d.ts new file mode 100644 index 00000000..30eee490 --- /dev/null +++ b/packages/api-client/src/types/event.types.d.ts @@ -0,0 +1,82 @@ +import { Page } from '../../../../apps/cli/src/types/index.types' + +export enum EventSource { + SECRET, + VARIABLE, + ENVIRONMENT, + PROJECT, + WORKSPACE, + WORKSPACE_ROLE, + INTEGRATION +} + +export enum EventTriggerer { + USER, + SYSTEM +} + +export enum EventSeverity { + INFO, + WARN, + ERROR +} + +export enum EventType { + INVITED_TO_WORKSPACE, + REMOVED_FROM_WORKSPACE, + ACCEPTED_INVITATION, + DECLINED_INVITATION, + CANCELLED_INVITATION, + LEFT_WORKSPACE, + WORKSPACE_MEMBERSHIP_UPDATED, + WORKSPACE_UPDATED, + WORKSPACE_CREATED, + WORKSPACE_ROLE_CREATED, + WORKSPACE_ROLE_UPDATED, + WORKSPACE_ROLE_DELETED, + PROJECT_CREATED, + PROJECT_UPDATED, + PROJECT_DELETED, + SECRET_UPDATED, + SECRET_DELETED, + SECRET_ADDED, + VARIABLE_UPDATED, + VARIABLE_DELETED, + VARIABLE_ADDED, + ENVIRONMENT_UPDATED, + ENVIRONMENT_DELETED, + ENVIRONMENT_ADDED, + INTEGRATION_ADDED, + INTEGRATION_UPDATED, + INTEGRATION_DELETED +} + +export interface GetEventsRequest { + workspaceId: string + source: string +} + +export interface GetEventsResponse + extends Page<{ + id: string + source: EventSource + triggerer: EventTriggerer + severity: EventSeverity + type: EventType + timestamp: string + metadata: { + name: string + projectName: string + projectId?: string + variableId?: string + environmentId?: string + secretId?: string + workspaceId?: string + workspaceName?: string + } + title: string + description: string + itemId: string + userId: string + workspaceId: string + }> {} diff --git a/packages/api-client/tests/event.spec.ts b/packages/api-client/tests/event.spec.ts new file mode 100644 index 00000000..3d0b0120 --- /dev/null +++ b/packages/api-client/tests/event.spec.ts @@ -0,0 +1,156 @@ +import { APIClient } from '../src/core/client' +import EventController from '../src/controllers/event/event' +export enum EventSource { + SECRET = 'SECRET', + VARIABLE = 'VARIABLE', + ENVIRONMENT = 'ENVIRONMENT', + PROJECT = 'PROJECT', + WORKSPACE = 'WORKSPACE', + WORKSPACE_ROLE = 'WORKSPACE_ROLE', + INTEGRATION = 'INTEGRATION' +} + +describe('Get Event Controller', () => { + const backendUrl = process.env.BACKEND_URL + + const client = new APIClient(backendUrl) + const eventController = new EventController(backendUrl) + const email = 'johndoe@example.com' + let projectId: string | null + let workspaceId: string | null + let secret: any + let variable: any + let environment: any + + beforeAll(async () => { + //Create the user's workspace + const workspaceResponse = (await ( + await client.post( + '/api/workspace', + { + name: 'Event Workspace' + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any + console.log(workspaceResponse) + workspaceId = workspaceResponse.id + }) + + afterAll(async () => { + // Delete the workspace + await client.delete(`/api/workspace/${workspaceId}`, { + 'x-e2e-user-email': email + }) + }) + + it('should fetch a Project Event', async () => { + const projectResponse = (await ( + await client.post( + `/api/project/${workspaceId}`, + { + name: 'Project', + storePrivateKey: true + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any + + projectId = projectResponse.id + const events = await eventController.getEvents( + { workspaceId, source: 'PROJECT' }, + { 'x-e2e-user-email': email } + ) + console.log(events.data.items) + expect(events.data.items[0].source).toBe(EventSource.PROJECT) + expect(events.data.items[0].metadata.projectId).toBe(projectId) + expect(events.data.items[0].metadata.name).toBe('Project') + }) + + it('should fetch a Environment Event', async () => { + const environmentResponse = (await ( + await client.post( + `/api/environment/${projectId}`, + { + name: 'Dev' + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any + const events = await eventController.getEvents( + { workspaceId, source: EventSource.ENVIRONMENT }, + { 'x-e2e-user-email': email } + ) + expect(events.data.items[0].source).toBe('ENVIRONMENT') + expect(events.data.items[0].metadata.environmentId).toBe( + environmentResponse.id + ) + expect(events.data.items[0].metadata.name).toBe('Dev') + environment = environmentResponse + }) + + it('should fetch a Secret Event', async () => { + const secretRepsonse = (await ( + await client.post( + `/api/secret/${projectId}`, + { + name: 'My secret', + entries: [ + { + value: 'My value', + environmentId: environment.id + } + ], + note: 'Some note', + rotateAfter: '720' + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any + const events = await eventController.getEvents( + { workspaceId, source: EventSource.SECRET }, + { 'x-e2e-user-email': email } + ) + expect(events.data.items[0].source).toBe('SECRET') + expect(events.data.items[0].metadata.secretId).toBe(secretRepsonse.id) + expect(events.data.items[0].metadata.name).toBe('My secret') + secret = secretRepsonse + }) + + it('should fetch a Variable Event', async () => { + const variableResponse = (await ( + await client.post( + `/api/variable/${projectId}`, + { + name: 'My variable', + entries: [ + { + value: 'My value', + environmentId: environment.id + } + ], + note: 'Some note' + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any + const events = await eventController.getEvents( + { workspaceId, source: EventSource.VARIABLE }, + { 'x-e2e-user-email': email } + ) + expect(events.data.items[0].source).toBe('VARIABLE') + expect(events.data.items[0].metadata.variableId).toBe(variableResponse.id) + expect(events.data.items[0].metadata.name).toBe('My variable') + variable = variableResponse + }) +})