Skip to content

Commit

Permalink
feat(api): Add pagination metadata to Variable module (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
muntaxir4 committed Jul 29, 2024
1 parent 8ad6932 commit 7207952
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
23 changes: 22 additions & 1 deletion apps/api/src/variable/service/variable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ChangeNotification,
ChangeNotificationEvent
} from 'src/socket/socket.types'
import { paginate } from '../../common/paginate'

@Injectable()
export class VariableService {
Expand Down Expand Up @@ -601,7 +602,27 @@ export class VariableService {
}
}

return Array.from(variablesWithEnvironmentalValues.values())
const items = Array.from(variablesWithEnvironmentalValues.values())

//calculate metadata
const totalCount = await this.prisma.variable.count({
where: {
projectId,
name: {
contains: search
}
}
})

const metadata = paginate(totalCount, `/variable/${projectId}`, {
page,
limit,
sort,
order,
search
})

return { items, metadata }
}

private async variableExists(
Expand Down
24 changes: 21 additions & 3 deletions apps/api/src/variable/variable.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { mockDeep } from 'jest-mock-extended'
import { RedisClientType } from 'redis'
import { UserService } from '../user/service/user.service'
import { UserModule } from '../user/user.module'
import { QueryTransformPipe } from '../common/query.transform.pipe'

describe('Variable Controller Tests', () => {
let app: NestFastifyApplication
Expand Down Expand Up @@ -83,6 +84,8 @@ describe('Variable Controller Tests', () => {
eventService = moduleRef.get(EventService)
userService = moduleRef.get(UserService)

app.useGlobalPipes(new QueryTransformPipe())

await app.init()
await app.getHttpAdapter().getInstance().ready()
})
Expand Down Expand Up @@ -575,23 +578,38 @@ describe('Variable Controller Tests', () => {
it('should be able to fetch all variables', async () => {
const response = await app.inject({
method: 'GET',
url: `/variable/${project1.id}`,
url: `/variable/${project1.id}?page=0&limit=10`,
headers: {
'x-e2e-user-email': user1.email
}
})

expect(response.statusCode).toBe(200)
expect(response.json().length).toBe(1)
expect(response.json().items.length).toBe(1)

const { variable, values } = response.json()[0]
const { variable, values } = response.json().items[0]
expect(variable).toBeDefined()
expect(values).toBeDefined()
expect(values.length).toBe(1)
expect(values[0].value).toBe('Variable 1 value')
expect(values[0].environment.id).toBe(environment1.id)
expect(variable.id).toBe(variable1.id)
expect(variable.name).toBe('Variable 1')

//check metadata
const metadata = response.json().metadata
expect(metadata.totalCount).toEqual(1)
expect(metadata.links.self).toEqual(
`/variable/${project1.id}?page=0&limit=10&sort=name&order=asc&search=`
)
expect(metadata.links.first).toEqual(
`/variable/${project1.id}?page=0&limit=10&sort=name&order=asc&search=`
)
expect(metadata.links.previous).toBeNull()
expect(metadata.links.next).toBeNull()
expect(metadata.links.last).toEqual(
`/variable/${project1.id}?page=0&limit=10&sort=name&order=asc&search=`
)
})

it('should not be able to fetch all variables if the user has no access to the project', async () => {
Expand Down

0 comments on commit 7207952

Please sign in to comment.