Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Add pagination metadata to Event module #394

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/api/src/environment/environment.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ describe('Environment Controller Tests', () => {
EventSource.ENVIRONMENT
)

const event = response[0]
const event = response.items[0]

expect(event.source).toBe(EventSource.ENVIRONMENT)
expect(event.triggerer).toBe(EventTriggerer.USER)
Expand Down Expand Up @@ -344,7 +344,7 @@ describe('Environment Controller Tests', () => {
EventSource.ENVIRONMENT
)

const event = response[0]
const event = response.items[0]

expect(event.source).toBe(EventSource.ENVIRONMENT)
expect(event.triggerer).toBe(EventTriggerer.USER)
Expand Down Expand Up @@ -459,7 +459,7 @@ describe('Environment Controller Tests', () => {
EventSource.ENVIRONMENT
)

const event = response[0]
const event = response.items[0]

expect(event.source).toBe(EventSource.ENVIRONMENT)
expect(event.triggerer).toBe(EventTriggerer.USER)
Expand Down
153 changes: 145 additions & 8 deletions apps/api/src/event/event.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { EnvironmentModule } from '../environment/environment.module'
import createEvent from '../common/create-event'
import { VariableService } from '../variable/service/variable.service'
import { VariableModule } from '../variable/variable.module'
import { QueryTransformPipe } from '../common/query.transform.pipe'

describe('Event Controller Tests', () => {
let app: NestFastifyApplication
Expand Down Expand Up @@ -80,6 +81,8 @@ describe('Event Controller Tests', () => {
secretService = moduleRef.get(SecretService)
variableService = moduleRef.get(VariableService)

app.useGlobalPipes(new QueryTransformPipe())

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

Expand Down Expand Up @@ -115,9 +118,8 @@ describe('Event Controller Tests', () => {
'x-e2e-user-email': user.email
}
})

expect(response.statusCode).toBe(200)
const event = response.json()[0]
const event = response.json().items[0]

expect(event.id).toBeDefined()
expect(event.title).toBeDefined()
Expand All @@ -129,6 +131,21 @@ describe('Event Controller Tests', () => {
expect(event.itemId).toBe(newWorkspace.id)
expect(event.userId).toBe(user.id)
expect(event.workspaceId).toBe(newWorkspace.id)

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toEqual(1)
expect(metadata.links.self).toEqual(
`/event/${newWorkspace.id}?source=WORKSPACE&page=0&limit=10&search=`
)
expect(metadata.links.first).toEqual(
`/event/${newWorkspace.id}?source=WORKSPACE&page=0&limit=10&search=`
)
expect(metadata.links.previous).toBeNull()
expect(metadata.links.next).toBeNull()
expect(metadata.links.last).toEqual(
`/event/${newWorkspace.id}?source=WORKSPACE&page=0&limit=10&search=`
)
})

it('should be able to fetch a project event', async () => {
Expand All @@ -152,7 +169,7 @@ describe('Event Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
const event = response.json()[0]
const event = response.json().items[0]

expect(event.id).toBeDefined()
expect(event.title).toBeDefined()
Expand All @@ -164,6 +181,21 @@ describe('Event Controller Tests', () => {
expect(event.itemId).toBe(newProject.id)
expect(event.userId).toBe(user.id)
expect(event.workspaceId).toBe(workspace.id)

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toEqual(1)
expect(metadata.links.self).toEqual(
`/event/${workspace.id}?source=PROJECT&page=0&limit=10&search=`
)
expect(metadata.links.first).toEqual(
`/event/${workspace.id}?source=PROJECT&page=0&limit=10&search=`
)
expect(metadata.links.previous).toBeNull()
expect(metadata.links.next).toBeNull()
expect(metadata.links.last).toEqual(
`/event/${workspace.id}?source=PROJECT&page=0&limit=10&search=`
)
})

it('should be able to fetch an environment event', async () => {
Expand All @@ -188,7 +220,7 @@ describe('Event Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
const event = response.json()[0]
const event = response.json().items[0]

expect(event.id).toBeDefined()
expect(event.title).toBeDefined()
Expand All @@ -200,6 +232,21 @@ describe('Event Controller Tests', () => {
expect(event.itemId).toBe(newEnvironment.id)
expect(event.userId).toBe(user.id)
expect(event.workspaceId).toBe(workspace.id)

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toEqual(1)
expect(metadata.links.self).toEqual(
`/event/${workspace.id}?source=ENVIRONMENT&page=0&limit=10&search=`
)
expect(metadata.links.first).toEqual(
`/event/${workspace.id}?source=ENVIRONMENT&page=0&limit=10&search=`
)
expect(metadata.links.previous).toBeNull()
expect(metadata.links.next).toBeNull()
expect(metadata.links.last).toEqual(
`/event/${workspace.id}?source=ENVIRONMENT&page=0&limit=10&search=`
)
})

it('should be able to fetch a secret event', async () => {
Expand Down Expand Up @@ -230,7 +277,7 @@ describe('Event Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
const event = response.json()[0]
const event = response.json().items[0]

expect(event.id).toBeDefined()
expect(event.title).toBeDefined()
Expand All @@ -242,6 +289,21 @@ describe('Event Controller Tests', () => {
expect(event.itemId).toBe(newSecret.id)
expect(event.userId).toBe(user.id)
expect(event.workspaceId).toBe(workspace.id)

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toEqual(1)
expect(metadata.links.self).toEqual(
`/event/${workspace.id}?source=SECRET&page=0&limit=10&search=`
)
expect(metadata.links.first).toEqual(
`/event/${workspace.id}?source=SECRET&page=0&limit=10&search=`
)
expect(metadata.links.previous).toBeNull()
expect(metadata.links.next).toBeNull()
expect(metadata.links.last).toEqual(
`/event/${workspace.id}?source=SECRET&page=0&limit=10&search=`
)
})

it('should be able to fetch a variable event', async () => {
Expand Down Expand Up @@ -272,7 +334,7 @@ describe('Event Controller Tests', () => {

expect(response.statusCode).toBe(200)
// expect(response.json()).toBe({})
const event = response.json()[0]
const event = response.json().items[0]

expect(event.id).toBeDefined()
expect(event.title).toBeDefined()
Expand All @@ -284,6 +346,21 @@ describe('Event Controller Tests', () => {
expect(event.itemId).toBe(newVariable.id)
expect(event.userId).toBe(user.id)
expect(event.workspaceId).toBe(workspace.id)

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toEqual(1)
expect(metadata.links.self).toEqual(
`/event/${workspace.id}?source=VARIABLE&page=0&limit=10&search=`
)
expect(metadata.links.first).toEqual(
`/event/${workspace.id}?source=VARIABLE&page=0&limit=10&search=`
)
expect(metadata.links.previous).toBeNull()
expect(metadata.links.next).toBeNull()
expect(metadata.links.last).toEqual(
`/event/${workspace.id}?source=VARIABLE&page=0&limit=10&search=`
)
})

it('should be able to fetch a workspace role event', async () => {
Expand All @@ -310,7 +387,7 @@ describe('Event Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
const event = response.json()[0]
const event = response.json().items[0]

expect(event.id).toBeDefined()
expect(event.title).toBeDefined()
Expand All @@ -322,6 +399,21 @@ describe('Event Controller Tests', () => {
expect(event.itemId).toBe(newWorkspaceRole.id)
expect(event.userId).toBe(user.id)
expect(event.workspaceId).toBe(workspace.id)

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toEqual(1)
expect(metadata.links.self).toEqual(
`/event/${workspace.id}?source=WORKSPACE_ROLE&page=0&limit=10&search=`
)
expect(metadata.links.first).toEqual(
`/event/${workspace.id}?source=WORKSPACE_ROLE&page=0&limit=10&search=`
)
expect(metadata.links.previous).toBeNull()
expect(metadata.links.next).toBeNull()
expect(metadata.links.last).toEqual(
`/event/${workspace.id}?source=WORKSPACE_ROLE&page=0&limit=10&search=`
)
})

it('should be able to fetch all events', async () => {
Expand All @@ -334,7 +426,52 @@ describe('Event Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
expect(response.json()).toHaveLength(6)
expect(response.json().items).toHaveLength(6)

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toEqual(6)
expect(metadata.links.self).toEqual(
`/event/${workspace.id}?page=0&limit=10&search=`
)
expect(metadata.links.first).toEqual(
`/event/${workspace.id}?page=0&limit=10&search=`
)
expect(metadata.links.previous).toBeNull()
expect(metadata.links.next).toBeNull()
expect(metadata.links.last).toEqual(
`/event/${workspace.id}?page=0&limit=10&search=`
)
})

it('should be able to fetch 2nd page of all events', async () => {
const response = await app.inject({
method: 'GET',
url: `/event/${workspace.id}?page=1&limit=3&`,
headers: {
'x-e2e-user-email': user.email
}
})

expect(response.statusCode).toBe(200)
expect(response.json().items).toHaveLength(3)

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toEqual(6)
expect(metadata.links.self).toEqual(
`/event/${workspace.id}?page=1&limit=3&search=`
)
expect(metadata.links.first).toEqual(
`/event/${workspace.id}?page=0&limit=3&search=`
)
expect(metadata.links.previous).toEqual(
`/event/${workspace.id}?page=0&limit=3&search=`
)
expect(metadata.links.next).toBeNull()
expect(metadata.links.last).toEqual(
`/event/${workspace.id}?page=1&limit=3&search=`
)
})

it('should throw an error with wrong severity value', async () => {
Expand Down
21 changes: 20 additions & 1 deletion apps/api/src/event/service/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BadRequestException, Injectable } from '@nestjs/common'
import { Authority, EventSeverity, EventSource, User } from '@prisma/client'
import { PrismaService } from '../../prisma/prisma.service'
import { AuthorityCheckerService } from '../../common/authority-checker.service'
import { paginate } from '../../common/paginate'

@Injectable()
export class EventService {
Expand Down Expand Up @@ -54,6 +55,24 @@ export class EventService {
}

// @ts-expect-error - Prisma does not have a type for severity
return await this.prisma.event.findMany(query)
const items = await this.prisma.event.findMany(query)

//calculate metadata for pagination
const totalCount = await this.prisma.event.count({
where: query.where
})

const metadata = paginate(
totalCount,
`/event/${workspaceId}`,
{
page,
limit,
search
},
{ source }
)

return { items, metadata }
}
}
6 changes: 3 additions & 3 deletions apps/api/src/project/project.e2e.spec.ts
muntaxir4 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ describe('Project Controller Tests', () => {
EventSource.PROJECT
)

const event = response[0]
const event = response.items[0]

expect(event.source).toBe(EventSource.PROJECT)
expect(event.triggerer).toBe(EventTriggerer.USER)
Expand Down Expand Up @@ -401,7 +401,7 @@ describe('Project Controller Tests', () => {
EventSource.PROJECT
)

const event = response[0]
const event = response.items[0]

expect(event.source).toBe(EventSource.PROJECT)
expect(event.triggerer).toBe(EventTriggerer.USER)
Expand Down Expand Up @@ -650,7 +650,7 @@ describe('Project Controller Tests', () => {
EventSource.PROJECT
)

const event = response[0]
const event = response.items[0]

expect(event.source).toBe(EventSource.PROJECT)
expect(event.triggerer).toBe(EventTriggerer.USER)
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/secret/secret.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ describe('Secret Controller Tests', () => {
EventSource.SECRET
)

const event = response[0]
const event = response.items[0]

expect(event.source).toBe(EventSource.SECRET)
expect(event.triggerer).toBe(EventTriggerer.USER)
Expand Down Expand Up @@ -410,7 +410,7 @@ describe('Secret Controller Tests', () => {
EventSource.SECRET
)

const event = response[0]
const event = response.items[0]

expect(event.source).toBe(EventSource.SECRET)
expect(event.triggerer).toBe(EventTriggerer.USER)
Expand Down Expand Up @@ -911,7 +911,7 @@ describe('Secret Controller Tests', () => {
EventSource.SECRET
)

const event = response[0]
const event = response.items[0]

expect(event.source).toBe(EventSource.SECRET)
expect(event.triggerer).toBe(EventTriggerer.USER)
Expand Down
Loading
Loading