Skip to content

Commit

Permalink
ORV2-1700 Changes for Pagination, API Specification And Permit Endpoi…
Browse files Browse the repository at this point in the history
…nt (#929)

Co-authored-by: Krishnan Subramanian <[email protected]>
  • Loading branch information
gchauhan-aot and krishnan-aot authored Dec 20, 2023
1 parent efb7216 commit 83f1e5c
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 393 deletions.
33 changes: 25 additions & 8 deletions frontend/src/common/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ export type ORBC_FormTypes =
* The options for pagination.
*/
export type PaginationOptions = {
/**
* The page number to fetch.
*/
page: number;
limit: number;
}
/**
* The number of items in the current page.
*/
take: number;
};

/**
* A generic paginated response structure for all the paginated responses from APIs.
Expand All @@ -56,12 +62,23 @@ export type PaginatedResponse<T> = {
* The metadata containing info about a page in the paginated response.
*/
export type PageMetadataInResponse = {
currentPage: number;
currentItemCount: number;
itemsPerPage: number;
totalPages?: number;
totalItems?: number;
};
/**
* The total items matching the query in the database.
*/
totalItems: number;
/**
* The total number of pages.
*/
pageCount: number;
/**
* Is there a previous page?
*/
hasPreviousPage: boolean;
/**
* Is there a next page?
*/
hasNextPage: boolean;
} & PaginationOptions;

export type Optional<T> = T | undefined;
export type RequiredOrNull<T> = T | null;
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/features/idir/search/api/idirSearch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { VEHICLES_URL } from "../../../../common/apiManager/endpoints/endpoints";
import { httpGETRequest } from "../../../../common/apiManager/httpRequestHandler";
import { PaginatedResponse, PaginationOptions } from "../../../../common/types/common";
import {
PaginatedResponse,
PaginationOptions,
} from "../../../../common/types/common";
import { Permit } from "../../../permits/types/permit";
import { SearchFields } from "../types/types";

Expand All @@ -11,12 +14,12 @@ import { SearchFields } from "../types/types";
*/
export const getDataBySearch = (
{ searchEntity, searchByFilter, searchValue }: SearchFields,
{ page = 0, limit = 10 }: PaginationOptions,
{ page = 0, take = 10 }: PaginationOptions,
): Promise<PaginatedResponse<Permit>> => {
const searchURL = new URL(`${VEHICLES_URL}/${searchEntity}/ppc/search`);
searchURL.searchParams.set("searchColumn", searchByFilter);
searchURL.searchParams.set("searchString", searchValue);
searchURL.searchParams.set("page", (page + 1).toString());
searchURL.searchParams.set("limit", (limit).toString());
searchURL.searchParams.set("take", take.toString());
return httpGETRequest(searchURL.toString()).then((response) => response.data);
};
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const IDIRSearchResults = memo(
searchEntity,
searchValue,
},
{ page: pagination.pageIndex, limit: pagination.pageSize },
{ page: pagination.pageIndex, take: pagination.pageSize },
),
{
retry: 1, // retry once.
Expand Down Expand Up @@ -140,7 +140,7 @@ export const IDIRSearchResults = memo(
autoResetPageIndex: false,
manualPagination: true,
rowCount: data?.meta?.totalItems ?? 0,
pageCount: data?.meta?.totalPages ?? 0,
pageCount: data?.meta?.pageCount ?? 0,
onPaginationChange: setPagination,
enablePagination: true,
enableTopToolbar: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const APPLICATIONS_API_ROUTES = {
export const PERMITS_API_ROUTES = {
BASE: PERMITS_API_BASE,
GET: PERMITS_API_BASE,
GET_LIST: `${PERMITS_API_BASE}/user`,
ISSUE: `${APPLICATIONS_API_BASE}/issue`,
HISTORY: `${PERMITS_API_BASE}/history`,
AMEND: APPLICATIONS_API_ROUTES.CREATE,
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/features/permits/apiManager/permitsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,18 @@ export const getCurrentAmendmentApplication = async (
*/
export const getPermits = async (
{ expired = false } = {},
{ page = 0, limit = 10 }: PaginationOptions,
{ page = 0, take = 10 }: PaginationOptions,
): Promise<PaginatedResponse<Permit>> => {
const companyId = getDefaultRequiredVal("", getCompanyIdFromSession());
const permitsURL = new URL(PERMITS_API_ROUTES.GET_LIST);
const permitsURL = new URL(PERMITS_API_ROUTES.GET);
if (companyId) {
permitsURL.searchParams.set("companyId", companyId);
}
if (expired) {
permitsURL.searchParams.set("expired", "true");
}
permitsURL.searchParams.set("page", (page + 1).toString());
permitsURL.searchParams.set("limit", limit.toString());
permitsURL.searchParams.set("take", take.toString());
const permits = await httpGETRequest(permitsURL.toString())
.then((response) => {
const paginatedResponseObject = getDefaultRequiredVal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const BasePermitList = ({
queryFn: () =>
getPermits(
{ expired: isExpired },
{ page: pagination.pageIndex, limit: pagination.pageSize },
{ page: pagination.pageIndex, take: pagination.pageSize },
),
keepPreviousData: true,
staleTime: FIVE_MINUTES,
Expand Down Expand Up @@ -87,7 +87,7 @@ export const BasePermitList = ({
autoResetPageIndex: false,
manualPagination: true,
rowCount: data?.meta?.totalItems ?? 0,
pageCount: data?.meta?.totalPages ?? 0,
pageCount: data?.meta?.pageCount ?? 0,
onPaginationChange: setPagination,
enablePagination: true,
enableBottomToolbar: true,
Expand Down
14 changes: 0 additions & 14 deletions vehicles/src/common/class/pagination.ts

This file was deleted.

27 changes: 27 additions & 0 deletions vehicles/src/common/decorator/api-paginate-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { applyDecorators, Type } from '@nestjs/common';
import { ApiExtraModels, ApiOkResponse, getSchemaPath } from '@nestjs/swagger';
import { PaginationDto } from '../dto/paginate/pagination';

export const ApiPaginatedResponse = <TModel extends Type<unknown>>(
model: TModel,
) => {
return applyDecorators(
ApiExtraModels(PaginationDto),
ApiOkResponse({
description: 'Successfully received model list',
schema: {
allOf: [
{ $ref: getSchemaPath(PaginationDto) },
{
properties: {
items: {
type: 'array',
items: { $ref: getSchemaPath(model) },
},
},
},
],
},
}),
);
};
31 changes: 31 additions & 0 deletions vehicles/src/common/dto/paginate/page-meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ApiProperty } from '@nestjs/swagger';
import { PageMetaDtoParameters } from '../../interface/pagination.interface';

export class PageMetaDto {
@ApiProperty()
readonly page: number;

@ApiProperty()
readonly take: number;

@ApiProperty()
readonly totalItems: number;

@ApiProperty()
readonly pageCount: number;

@ApiProperty()
readonly hasPreviousPage: boolean;

@ApiProperty()
readonly hasNextPage: boolean;

constructor({ pageOptionsDto, totalItems }: PageMetaDtoParameters) {
this.page = pageOptionsDto.page;
this.take = pageOptionsDto.take;
this.totalItems = totalItems;
this.pageCount = Math.ceil(this.totalItems / this.take);
this.hasPreviousPage = this.page > 1;
this.hasNextPage = this.page < this.pageCount;
}
}
31 changes: 31 additions & 0 deletions vehicles/src/common/dto/paginate/page-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsInt, IsOptional, Max, Min } from 'class-validator';

export class PageOptionsDto {
@ApiPropertyOptional({
minimum: 1,
default: 1,
})
@Type(() => Number)
@IsInt()
@Min(1)
@IsOptional()
readonly page?: number = 1;

@ApiPropertyOptional({
minimum: 1,
maximum: 25,
default: 10,
})
@Type(() => Number)
@IsInt()
@Min(1)
@Max(25)
@IsOptional()
readonly take?: number = 10;

get skip(): number {
return (this.page - 1) * this.take;
}
}
17 changes: 17 additions & 0 deletions vehicles/src/common/dto/paginate/pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsArray } from 'class-validator';
import { PageMetaDto } from './page-meta';

export class PaginationDto<T> {
@IsArray()
@ApiProperty({ isArray: true })
readonly items: T[];

@ApiProperty({ type: () => PageMetaDto })
readonly meta: PageMetaDto;

constructor(items: T[], meta: PageMetaDto) {
this.items = items;
this.meta = meta;
}
}
Loading

0 comments on commit 83f1e5c

Please sign in to comment.