Skip to content

Commit

Permalink
refactor(api): made minor adjustments to event
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Feb 9, 2024
1 parent 4c6c376 commit 00fe247
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 123 deletions.
11 changes: 1 addition & 10 deletions .github/workflows/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
tags:
- '*'
pull_request:
types: [opened]
paths: ['apps/api/**', '.github/workflows/api.yaml']

jobs:
validate:
Expand Down Expand Up @@ -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
Expand Down
212 changes: 111 additions & 101 deletions apps/api/src/common/create-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
8 changes: 6 additions & 2 deletions apps/api/src/event/service/event.service.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 0 additions & 9 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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%
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 00fe247

Please sign in to comment.