Skip to content

Commit

Permalink
API: Add pagination metadata to Secret module
Browse files Browse the repository at this point in the history
  • Loading branch information
muntaxir4 authored and rajdip-b committed Jul 29, 2024
1 parent 5959df4 commit 773a4f4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
42 changes: 36 additions & 6 deletions apps/api/src/secret/secret.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,16 +596,16 @@ describe('Secret Controller Tests', () => {
it('should be able to fetch all secrets', async () => {
const response = await app.inject({
method: 'GET',
url: `/secret/${project1.id}`,
url: `/secret/${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 { secret, values } = response.json()[0]
const { secret, values } = response.json().items[0]
expect(secret.id).toBeDefined()
expect(secret.name).toBeDefined()
expect(secret.note).toBeDefined()
Expand All @@ -615,21 +615,36 @@ describe('Secret Controller Tests', () => {
const value = values[0]
expect(value.environment).toBeDefined()
expect(value.value).not.toEqual('Secret 1 value')

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

it('should be able to fetch all secrets decrypted', async () => {
const response = await app.inject({
method: 'GET',
url: `/secret/${project1.id}?decryptValue=true`,
url: `/secret/${project1.id}?decryptValue=true&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 { secret, values } = response.json()[0]
const { secret, values } = response.json().items[0]
expect(secret.id).toBeDefined()
expect(secret.name).toBeDefined()
expect(secret.note).toBeDefined()
Expand All @@ -639,6 +654,21 @@ describe('Secret Controller Tests', () => {
const value = values[0]
expect(value.environment).toBeDefined()
expect(value.value).toEqual('Secret 1 value')

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

it('should not be able to fetch all secrets decrypted if the project does not store the private key', async () => {
Expand Down
30 changes: 28 additions & 2 deletions apps/api/src/secret/service/secret.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
ChangeNotification,
ChangeNotificationEvent
} from 'src/socket/socket.types'
import { paginate } from '../../common/paginate'

@Injectable()
export class SecretService {
Expand Down Expand Up @@ -530,7 +531,7 @@ export class SecretService {
}
},
skip: page * limit,
take: limit,
take: Number(limit),
orderBy: {
[sort]: order
}
Expand Down Expand Up @@ -615,7 +616,32 @@ export class SecretService {
}
}

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

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

const metadata = paginate(
totalCount,
`/secret/${projectId}`,
{
page: Number(page),
limit: Number(limit),
sort,
order,
search
},
{ decryptValue }
)

return { items, metadata }
}

private async secretExists(
Expand Down

0 comments on commit 773a4f4

Please sign in to comment.