From 00fe2475337e703f28f3adf213950b02b62b5e3f Mon Sep 17 00:00:00 2001 From: rajdip-b Date: Fri, 9 Feb 2024 17:27:24 +0530 Subject: [PATCH] refactor(api): made minor adjustments to event --- .github/workflows/api.yaml | 11 +- apps/api/src/common/create-event.ts | 212 ++++++++++---------- apps/api/src/event/service/event.service.ts | 8 +- codecov.yml | 9 - package.json | 2 +- 5 files changed, 119 insertions(+), 123 deletions(-) diff --git a/.github/workflows/api.yaml b/.github/workflows/api.yaml index fb35f0b0..6d083634 100644 --- a/.github/workflows/api.yaml +++ b/.github/workflows/api.yaml @@ -4,7 +4,7 @@ on: tags: - '*' pull_request: - types: [opened] + paths: ['apps/api/**', '.github/workflows/api.yaml'] jobs: validate: @@ -61,15 +61,6 @@ jobs: docker compose up -d pnpm run e2e:api docker compose down - ls -l - - - name: Upload unit test coverage reports to Codecov - uses: codecov/codecov-action@v3 - env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} - with: - flags: api-unit-tests - files: /coverage/api/coverage-final.json - name: Upload e2e test coverage reports to Codecov uses: codecov/codecov-action@v3 diff --git a/apps/api/src/common/create-event.ts b/apps/api/src/common/create-event.ts index 6f524bd1..2e622954 100644 --- a/apps/api/src/common/create-event.ts +++ b/apps/api/src/common/create-event.ts @@ -41,115 +41,125 @@ export default async function createEvent( } const baseData = { - triggerer: data.triggerer ? data.triggerer : EventTriggerer.USER, - severity: data.severity ? data.severity : EventSeverity.INFO, + triggerer: data.triggerer ?? EventTriggerer.USER, + severity: data.severity ?? EventSeverity.INFO, type: data.type, source: data.source, title: data.title, description: data.description, metadata: data.metadata, - sourceUser: { - connect: { - id: data.triggeredBy.id - } - } + sourceUser: data.triggeredBy.id + ? { + connect: { + id: data.triggeredBy.id + } + } + : undefined } - switch (data.source) { - case EventSource.WORKSPACE: { - const entity = data.entity as Workspace - await prisma.event.create({ - data: { - ...baseData, - sourceWorkspace: data.entity - ? { - connect: { - id: entity.id + try { + switch (data.source) { + case EventSource.WORKSPACE: { + const entity = data.entity as Workspace + await prisma.event.create({ + data: { + ...baseData, + sourceWorkspace: data.entity + ? { + connect: { + id: entity.id + } } - } - : undefined - } - }) - break - } - case EventSource.PROJECT: { - const entity = data.entity as Project - await prisma.event.create({ - data: { - ...baseData, - sourceProject: data.entity - ? { connect: { id: entity.id } } - : undefined - } - }) - break - } - case EventSource.ENVIRONMENT: { - const entity = data.entity as Environment - await prisma.event.create({ - data: { - ...baseData, - sourceEnvironment: data.entity - ? { connect: { id: entity.id } } - : undefined - } - }) - break - } - case EventSource.WORKSPACE_ROLE: { - const entity = data.entity as WorkspaceRole - await prisma.event.create({ - data: { - ...baseData, - sourceWorkspaceRole: data.entity - ? { connect: { id: entity.id } } - : undefined - } - }) - break - } - case EventSource.WORKSPACE_MEMBER: { - const entity = data.entity as WorkspaceMember - await prisma.event.create({ - data: { - ...baseData, - sourceWorkspaceMembership: data.entity - ? { connect: { id: entity.id } } - : undefined - } - }) - break - } - case EventSource.API_KEY: { - const entity = data.entity as ApiKey - await prisma.event.create({ - data: { - ...baseData, - sourceApiKey: data.entity ? { connect: { id: entity.id } } : undefined - } - }) - break - } - case EventSource.SECRET: { - const entity = data.entity as Secret - await prisma.event.create({ - data: { - ...baseData, - sourceSecret: data.entity ? { connect: { id: entity.id } } : undefined - } - }) - break - } - case EventSource.USER: { - await prisma.event.create({ - data: { - ...baseData - } - }) - break - } - default: { - throw new Error('Invalid event source') + : undefined + } + }) + break + } + case EventSource.PROJECT: { + const entity = data.entity as Project + await prisma.event.create({ + data: { + ...baseData, + sourceProject: data.entity + ? { connect: { id: entity.id } } + : undefined + } + }) + break + } + case EventSource.ENVIRONMENT: { + const entity = data.entity as Environment + await prisma.event.create({ + data: { + ...baseData, + sourceEnvironment: data.entity + ? { connect: { id: entity.id } } + : undefined + } + }) + break + } + case EventSource.WORKSPACE_ROLE: { + const entity = data.entity as WorkspaceRole + await prisma.event.create({ + data: { + ...baseData, + sourceWorkspaceRole: data.entity + ? { connect: { id: entity.id } } + : undefined + } + }) + break + } + case EventSource.WORKSPACE_MEMBER: { + const entity = data.entity as WorkspaceMember + await prisma.event.create({ + data: { + ...baseData, + sourceWorkspaceMembership: data.entity + ? { connect: { id: entity.id } } + : undefined + } + }) + break + } + case EventSource.API_KEY: { + const entity = data.entity as ApiKey + await prisma.event.create({ + data: { + ...baseData, + sourceApiKey: data.entity + ? { connect: { id: entity.id } } + : undefined + } + }) + break + } + case EventSource.SECRET: { + const entity = data.entity as Secret + await prisma.event.create({ + data: { + ...baseData, + sourceSecret: data.entity + ? { connect: { id: entity.id } } + : undefined + } + }) + break + } + case EventSource.USER: { + await prisma.event.create({ + data: { + ...baseData + } + }) + break + } + default: { + throw new Error('Invalid event source') + } } + } catch (error) { + console.error('Error creating event', error) } } diff --git a/apps/api/src/event/service/event.service.ts b/apps/api/src/event/service/event.service.ts index 798c6082..d83a0dbc 100644 --- a/apps/api/src/event/service/event.service.ts +++ b/apps/api/src/event/service/event.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common' +import { BadRequestException, Injectable } from '@nestjs/common' import { Authority, EventSeverity, User } from '@prisma/client' import getWorkspaceWithAuthority from '../../common/get-workspace-with-authority' import getProjectWithAuthority from '../../common/get-project-with-authority' @@ -26,8 +26,12 @@ export class EventService { search: string, severity?: EventSeverity ) { + if (severity && !Object.values(EventSeverity).includes(severity)) { + throw new BadRequestException('Invalid "severity" value') + } + const whereCondition = { - severity: severity ? severity : undefined, + severity: severity, title: { contains: search } diff --git a/codecov.yml b/codecov.yml index 906c7b56..2f3a6b9b 100644 --- a/codecov.yml +++ b/codecov.yml @@ -30,12 +30,3 @@ flag_management: target: 95% - type: patch target: 98% - - name: api-unit-tests - paths: - - apps/api/ - carryforward: true - statuses: - - type: project - target: 80% - - type: patch - target: 90% diff --git a/package.json b/package.json index f6d1b7bd..a38d06a3 100644 --- a/package.json +++ b/package.json @@ -99,7 +99,7 @@ "build:web": "nx run web:build --configuration=production", "build:workspace": "nx run workspace:build", "test": "nx run-many -t test --parallel", - "test:api": "nx run api:test -- --coverage --coverageDirectory=coverage/api --coverageReporters=json", + "test:api": "nx run api:test", "e2e:api:prepare": "NODE_ENV='e2e' DATABASE_URL='postgresql://prisma:prisma@localhost:5433/tests' pnpm run db:deploy-migrations", "e2e:api": "pnpm run e2e:api:prepare && NODE_ENV='e2e' DATABASE_URL='postgresql://prisma:prisma@localhost:5433/tests' nx run api:test:e2e -- --coverage --coverageDirectory=coverage-e2e/api --coverageReporters=json", "test:web": "nx run web:test",