Skip to content

Commit

Permalink
feat(api): Add max page size (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
DhairyaMajmudar authored and rajdip-b committed Sep 5, 2024
1 parent 4c6f3f8 commit ed18eb0
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 21 deletions.
3 changes: 2 additions & 1 deletion apps/api/src/api-key/service/api-key.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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, User } from '@prisma/client'
import { limitMaxItemsPerPage } from '../../common/limit-max-items-per-page'

@Injectable()
export class ApiKeyService {
Expand Down Expand Up @@ -146,7 +147,7 @@ export class ApiKeyService {
}
},
skip: page * limit,
take: limit,
take: limitMaxItemsPerPage(limit),
orderBy: {
[sort]: order
},
Expand Down
6 changes: 6 additions & 0 deletions apps/api/src/common/limit-max-items-per-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function limitMaxItemsPerPage(
limit: number,
maxlimit: number = 30
): number {
return Math.min(limit, maxlimit)
}
5 changes: 3 additions & 2 deletions apps/api/src/environment/service/environment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { PrismaService } from '../../prisma/prisma.service'
import createEvent from '../../common/create-event'
import { AuthorityCheckerService } from '../../common/authority-checker.service'
import { paginate } from '../../common/paginate'
import { limitMaxItemsPerPage } from '../../common/limit-max-items-per-page'

@Injectable()
export class EnvironmentService {
Expand Down Expand Up @@ -196,7 +197,7 @@ export class EnvironmentService {
}
},
skip: page * limit,
take: limit,
take: limitMaxItemsPerPage(limit),
orderBy: {
[sort]: order
}
Expand All @@ -212,7 +213,7 @@ export class EnvironmentService {
})
const metadata = paginate(totalCount, `/environment/all/${projectId}`, {
page,
limit,
limit: limitMaxItemsPerPage(limit),
sort,
order,
search
Expand Down
6 changes: 4 additions & 2 deletions apps/api/src/event/service/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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'
import { limitMaxItemsPerPage } from '../../common/limit-max-items-per-page'

@Injectable()
export class EventService {
Expand Down Expand Up @@ -44,7 +45,8 @@ export class EventService {
}
},
skip: page * limit,
take: limit,
take: limitMaxItemsPerPage(limit),

orderBy: {
timestamp: 'desc'
}
Expand All @@ -67,7 +69,7 @@ export class EventService {
`/event/${workspaceId}`,
{
page,
limit,
limit: limitMaxItemsPerPage(limit),
search
},
{ source }
Expand Down
6 changes: 4 additions & 2 deletions apps/api/src/integration/service/integration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { AuthorityCheckerService } from '../../common/authority-checker.service'
import createEvent from '../../common/create-event'
import IntegrationFactory from '../plugins/factory/integration.factory'
import { paginate } from '../../common/paginate'
import { limitMaxItemsPerPage } from '../../common/limit-max-items-per-page'

@Injectable()
export class IntegrationService {
Expand Down Expand Up @@ -297,7 +298,8 @@ export class IntegrationService {
]
},
skip: page * limit,
take: limit,
take: limitMaxItemsPerPage(limit),

orderBy: {
[sort]: order
}
Expand All @@ -324,7 +326,7 @@ export class IntegrationService {
})
const metadata = paginate(totalCount, `/integration/all/${workspaceId}`, {
page,
limit,
limit: limitMaxItemsPerPage(limit),
sort,
order,
search
Expand Down
6 changes: 4 additions & 2 deletions apps/api/src/project/service/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { ProjectWithSecrets } from '../project.types'
import { AuthorityCheckerService } from '../../common/authority-checker.service'
import { ForkProject } from '../dto/fork.project/fork.project'
import { paginate } from '../../common/paginate'
import { limitMaxItemsPerPage } from '../../common/limit-max-items-per-page'

@Injectable()
export class ProjectService {
Expand Down Expand Up @@ -629,7 +630,7 @@ export class ProjectService {
`/project/${projectId}/forks`,
{
page,
limit
limit: limitMaxItemsPerPage(limit)
}
)

Expand Down Expand Up @@ -670,7 +671,8 @@ export class ProjectService {
const items = (
await this.prisma.project.findMany({
skip: page * limit,
take: limit,
take: limitMaxItemsPerPage(limit),

orderBy: {
[sort]: order
},
Expand Down
8 changes: 5 additions & 3 deletions apps/api/src/secret/service/secret.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
ChangeNotificationEvent
} from 'src/socket/socket.types'
import { paginate } from '../../common/paginate'
import { limitMaxItemsPerPage } from '../../common/limit-max-items-per-page'

@Injectable()
export class SecretService {
Expand Down Expand Up @@ -524,7 +525,7 @@ export class SecretService {
environmentId: environmentId
},
skip: page * limit,
take: limit,
take: limitMaxItemsPerPage(limit),
orderBy: {
version: sortOrder
}
Expand Down Expand Up @@ -570,7 +571,8 @@ export class SecretService {
}
},
skip: page * limit,
take: limit,
take: limitMaxItemsPerPage(limit),

orderBy: {
[sort]: order
}
Expand Down Expand Up @@ -672,7 +674,7 @@ export class SecretService {
`/secret/${projectId}`,
{
page,
limit,
limit: limitMaxItemsPerPage(limit),
sort,
order,
search
Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/user/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import createUser from '../../common/create-user'
import generateOtp from '../../common/generate-otp'
import { EnvSchema } from '../../common/env/env.schema'
import { limitMaxItemsPerPage } from '../../common/limit-max-items-per-page'

@Injectable()
export class UserService {
Expand Down Expand Up @@ -216,7 +217,7 @@ export class UserService {
): Promise<User[]> {
return this.prisma.user.findMany({
skip: (page - 1) * limit,
take: limit,
take: limitMaxItemsPerPage(limit),
orderBy: {
[sort]: order
},
Expand Down
9 changes: 6 additions & 3 deletions apps/api/src/variable/service/variable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
ChangeNotificationEvent
} from 'src/socket/socket.types'
import { paginate } from '../../common/paginate'
import { limitMaxItemsPerPage } from '../../common/limit-max-items-per-page'

@Injectable()
export class VariableService {
Expand Down Expand Up @@ -521,7 +522,8 @@ export class VariableService {
}
},
skip: page * limit,
take: limit,
take: limitMaxItemsPerPage(limit),

orderBy: {
[sort]: order
}
Expand Down Expand Up @@ -616,7 +618,7 @@ export class VariableService {

const metadata = paginate(totalCount, `/variable/${projectId}`, {
page,
limit,
limit: limitMaxItemsPerPage(limit),
sort,
order,
search
Expand Down Expand Up @@ -653,7 +655,8 @@ export class VariableService {
environmentId: environmentId
},
skip: page * limit,
take: limit,
take: limitMaxItemsPerPage(limit),

orderBy: {
version: order
}
Expand Down
6 changes: 4 additions & 2 deletions apps/api/src/workspace-role/service/workspace-role.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { WorkspaceRoleWithProjects } from '../workspace-role.types'
import { v4 } from 'uuid'
import { AuthorityCheckerService } from '../../common/authority-checker.service'
import { paginate, PaginatedMetadata } from '../../common/paginate'
import { limitMaxItemsPerPage } from '../../common/limit-max-items-per-page'

@Injectable()
export class WorkspaceRoleService {
Expand Down Expand Up @@ -320,7 +321,8 @@ export class WorkspaceRoleService {
}
},
skip: page * limit,
take: limit,
take: limitMaxItemsPerPage(limit),

orderBy: {
[sort]: order
}
Expand All @@ -341,7 +343,7 @@ export class WorkspaceRoleService {
`/workspace-role/${workspaceId}/all`,
{
page,
limit,
limit: limitMaxItemsPerPage(limit),
sort,
order,
search
Expand Down
7 changes: 4 additions & 3 deletions apps/api/src/workspace/service/workspace.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import createEvent from '../../common/create-event'
import createWorkspace from '../../common/create-workspace'
import { AuthorityCheckerService } from '../../common/authority-checker.service'
import { paginate } from '../../common/paginate'
import { limitMaxItemsPerPage } from '../../common/limit-max-items-per-page'

@Injectable()
export class WorkspaceService {
Expand Down Expand Up @@ -539,7 +540,7 @@ export class WorkspaceService {

const metadata = paginate(totalCount, `/workspace/${workspaceId}/members`, {
page,
limit,
limit: limitMaxItemsPerPage(limit),
sort,
order,
search
Expand Down Expand Up @@ -813,8 +814,8 @@ export class WorkspaceService {

//calculate metadata for pagination
const metadata = paginate(totalCount, `/workspace`, {
page: Number(page),
limit: Number(limit),
page,
limit: limitMaxItemsPerPage(limit),
sort,
order,
search
Expand Down

0 comments on commit ed18eb0

Please sign in to comment.