Skip to content

Commit

Permalink
refactor(api): Made events central to workspace (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Mar 9, 2024
1 parent e09ae60 commit 9bc00ae
Show file tree
Hide file tree
Showing 25 changed files with 1,233 additions and 1,217 deletions.
48 changes: 3 additions & 45 deletions apps/api/src/api-key/service/api-key.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { addHoursToDate } from '../../common/add-hours-to-date'
import { generateApiKey } from '../../common/api-key-generator'
import { toSHA256 } from '../../common/to-sha256'
import { UpdateApiKey } from '../dto/update.api-key/update.api-key'
import { ApiKey, EventSource, EventType, User } from '@prisma/client'
import createEvent from '../../common/create-event'
import { User } from '@prisma/client'

@Injectable()
export class ApiKeyService {
Expand Down Expand Up @@ -43,21 +42,6 @@ export class ApiKeyService {
}
})

createEvent(
{
triggeredBy: user,
entity: apiKey as ApiKey,
type: EventType.API_KEY_ADDED,
source: EventSource.API_KEY,
title: `API key created`,
metadata: {
apiKeyId: apiKey.id,
name: apiKey.name
}
},
this.prisma
)

this.logger.log(`User ${user.id} created API key ${apiKey.id}`)

return {
Expand Down Expand Up @@ -93,46 +77,20 @@ export class ApiKeyService {
}
})

createEvent(
{
triggeredBy: user,
entity: updatedApiKey as ApiKey,
type: EventType.API_KEY_UPDATED,
source: EventSource.API_KEY,
title: `API key updated`,
metadata: {
apiKeyId: updatedApiKey.id,
name: updatedApiKey.name
}
},
this.prisma
)

this.logger.log(`User ${user.id} updated API key ${apiKeyId}`)

return updatedApiKey
}

async deleteApiKey(user: User, apiKeyId: string) {
const apiKey = await this.prisma.apiKey.delete({
await this.prisma.apiKey.delete({
where: {
id: apiKeyId,
userId: user.id
}
})

createEvent(
{
triggeredBy: user,
type: EventType.API_KEY_DELETED,
source: EventSource.API_KEY,
title: `API key deleted`,
metadata: {
name: apiKey.name
}
},
this.prisma
)
this.logger.log(`User ${user.id} deleted API key ${apiKeyId}`)
}

async getApiKeyById(user: User, apiKeyId: string) {
Expand Down
102 changes: 101 additions & 1 deletion apps/api/src/approval/approval.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import {
ApprovalStatus,
Authority,
Environment,
EventSeverity,
EventSource,
EventTriggerer,
EventType,
Project,
Secret,
User,
Expand All @@ -36,6 +40,9 @@ import { VariableModule } from '../variable/variable.module'
import { UserModule } from '../user/user.module'
import { WorkspaceRoleService } from '../workspace-role/service/workspace-role.service'
import { WorkspaceRoleModule } from '../workspace-role/workspace-role.module'
import { EventService } from '../event/service/event.service'
import { EventModule } from '../event/event.module'
import fetchEvents from '../common/fetch-events'

describe('Approval Controller Tests', () => {
let app: NestFastifyApplication
Expand All @@ -47,6 +54,7 @@ describe('Approval Controller Tests', () => {
let secretService: SecretService
let variableService: VariableService
let workspaceRoleService: WorkspaceRoleService
let eventService: EventService

let workspace1: Workspace
let project1: Project
Expand All @@ -67,7 +75,8 @@ describe('Approval Controller Tests', () => {
EnvironmentModule,
SecretModule,
VariableModule,
WorkspaceRoleModule
WorkspaceRoleModule,
EventModule
]
})
.overrideProvider(MAIL_SERVICE)
Expand All @@ -84,6 +93,7 @@ describe('Approval Controller Tests', () => {
secretService = moduleRef.get(SecretService)
variableService = moduleRef.get(VariableService)
workspaceRoleService = moduleRef.get(WorkspaceRoleService)
eventService = moduleRef.get(EventService)

await app.init()
await app.getHttpAdapter().getInstance().ready()
Expand Down Expand Up @@ -157,6 +167,24 @@ describe('Approval Controller Tests', () => {
})
})

it('should have created a APPROVAL_CREATED event', async () => {
const response = await fetchEvents(
eventService,
user1,
workspace1.id,
EventSource.APPROVAL
)

const event = response[0]

expect(event.source).toBe(EventSource.APPROVAL)
expect(event.triggerer).toBe(EventTriggerer.USER)
expect(event.severity).toBe(EventSeverity.INFO)
expect(event.type).toBe(EventType.APPROVAL_CREATED)
expect(event.workspaceId).toBe(workspace1.id)
expect(event.itemId).toBeDefined()
})

it('should allow user with WORKSPACE_ADMIN to view the approval', async () => {
const approval = await prisma.approval.findFirst({
where: {
Expand Down Expand Up @@ -295,6 +323,24 @@ describe('Approval Controller Tests', () => {
expect(response.json().reason).toBe('updated')
})

it('should have created a APPROVAL_UPDATED event', async () => {
const response = await fetchEvents(
eventService,
user1,
workspace1.id,
EventSource.APPROVAL
)

const event = response[0]

expect(event.source).toBe(EventSource.APPROVAL)
expect(event.triggerer).toBe(EventTriggerer.USER)
expect(event.severity).toBe(EventSeverity.INFO)
expect(event.type).toBe(EventType.APPROVAL_UPDATED)
expect(event.workspaceId).toBe(workspace1.id)
expect(event.itemId).toBeDefined()
})

it('should update the workspace if the approval is approved', async () => {
let approval = await prisma.approval.findFirst({
where: {
Expand Down Expand Up @@ -334,6 +380,24 @@ describe('Approval Controller Tests', () => {
workspace1 = updatedWorkspace
})

it('should have created a APPROVAL_APPROVED event', async () => {
const response = await fetchEvents(
eventService,
user1,
workspace1.id,
EventSource.APPROVAL
)

const event = response[0]

expect(event.source).toBe(EventSource.APPROVAL)
expect(event.triggerer).toBe(EventTriggerer.USER)
expect(event.severity).toBe(EventSeverity.INFO)
expect(event.type).toBe(EventType.APPROVAL_APPROVED)
expect(event.workspaceId).toBe(workspace1.id)
expect(event.itemId).toBeDefined()
})

it('should not be able to approve an already approved approval', async () => {
const approval = await prisma.approval.findFirst({
where: {
Expand Down Expand Up @@ -442,6 +506,24 @@ describe('Approval Controller Tests', () => {
expect(approval).toBeNull()
})

it('should have created a APPROVAL_DELETED event', async () => {
const response = await fetchEvents(
eventService,
user1,
workspace1.id,
EventSource.APPROVAL
)

const event = response[0]

expect(event.source).toBe(EventSource.APPROVAL)
expect(event.triggerer).toBe(EventTriggerer.USER)
expect(event.severity).toBe(EventSeverity.INFO)
expect(event.type).toBe(EventType.APPROVAL_DELETED)
expect(event.workspaceId).toBe(workspace1.id)
expect(event.itemId).toBeDefined()
})

it('should allow creating project with the same name till it is not approved', async () => {
const result1 = (await projectService.createProject(
user1,
Expand Down Expand Up @@ -1530,6 +1612,24 @@ describe('Approval Controller Tests', () => {
expect(response.statusCode).toBe(200)
})

it('should have created a APPROVAL_REJECTED event', async () => {
const response = await fetchEvents(
eventService,
user1,
workspace1.id,
EventSource.APPROVAL
)

const event = response[0]

expect(event.source).toBe(EventSource.APPROVAL)
expect(event.triggerer).toBe(EventTriggerer.USER)
expect(event.severity).toBe(EventSeverity.INFO)
expect(event.type).toBe(EventType.APPROVAL_REJECTED)
expect(event.workspaceId).toBe(workspace1.id)
expect(event.itemId).toBeDefined()
})

it('should delete the item if the approval is rejected', async () => {
// Create a new project
const result = (await projectService.createProject(
Expand Down
21 changes: 13 additions & 8 deletions apps/api/src/approval/service/approval.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class ApprovalService {

this.logger.log(`Approval with id ${approvalId} updated by ${user.id}`)

createEvent(
await createEvent(
{
triggeredBy: user,
entity: approval,
Expand All @@ -73,7 +73,8 @@ export class ApprovalService {
title: `Approval with id ${approvalId} updated`,
metadata: {
approvalId
}
},
workspaceId: approval.workspaceId
},
this.prisma
)
Expand Down Expand Up @@ -102,15 +103,17 @@ export class ApprovalService {

this.logger.log(`Approval with id ${approvalId} deleted by ${user.id}`)

createEvent(
await createEvent(
{
triggeredBy: user,
entity: approval,
type: EventType.APPROVAL_DELETED,
source: EventSource.APPROVAL,
title: `Approval with id ${approvalId} deleted`,
metadata: {
approvalId
}
},
workspaceId: approval.workspaceId
},
this.prisma
)
Expand Down Expand Up @@ -145,7 +148,7 @@ export class ApprovalService {

this.logger.log(`Approval with id ${approvalId} rejected by ${user.id}`)

createEvent(
await createEvent(
{
triggeredBy: user,
entity: approval,
Expand All @@ -154,7 +157,8 @@ export class ApprovalService {
title: `Approval with id ${approvalId} rejected`,
metadata: {
approvalId
}
},
workspaceId: approval.workspaceId
},
this.prisma
)
Expand Down Expand Up @@ -360,7 +364,7 @@ export class ApprovalService {

this.logger.log(`Approval with id ${approvalId} approved by ${user.id}`)

createEvent(
await createEvent(
{
triggeredBy: user,
entity: approval,
Expand All @@ -369,7 +373,8 @@ export class ApprovalService {
title: `Approval with id ${approvalId} approved`,
metadata: {
approvalId
}
},
workspaceId: approval.workspaceId
},
this.prisma
)
Expand Down
7 changes: 7 additions & 0 deletions apps/api/src/auth/auth.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { MAIL_SERVICE } from '../mail/services/interface.service'
import { MockMailService } from '../mail/services/mock.service'
import { AppModule } from '../app/app.module'
import { Otp } from '@prisma/client'
import cleanUp from '../common/cleanup'

describe('Auth Controller Tests', () => {
let app: NestFastifyApplication
Expand All @@ -31,6 +32,8 @@ describe('Auth Controller Tests', () => {

await app.init()
await app.getHttpAdapter().getInstance().ready()

await cleanUp(prisma)
})

it('should be defined', async () => {
Expand Down Expand Up @@ -122,4 +125,8 @@ describe('Auth Controller Tests', () => {

expect(response.statusCode).toBe(401)
})

afterAll(async () => {
await cleanUp(prisma)
})
})
5 changes: 3 additions & 2 deletions apps/api/src/common/create-approval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default async function createApproval(
`Approval for ${data.itemType} with id ${data.itemId} created by ${data.user.id}`
)

createEvent(
await createEvent(
{
triggeredBy: data.user,
entity: approval,
Expand All @@ -74,7 +74,8 @@ export default async function createApproval(
itemId: data.itemId,
action: data.action,
reason: data.reason
}
},
workspaceId: data.workspaceId
},
prisma
)
Expand Down
Loading

0 comments on commit 9bc00ae

Please sign in to comment.