Skip to content

Commit

Permalink
fix: 🐛 fix getIncidents api to post request
Browse files Browse the repository at this point in the history
  • Loading branch information
yeukfei02 committed Jan 3, 2023
1 parent ac73e2c commit b84b040
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 46 deletions.
12 changes: 12 additions & 0 deletions apps/api/src/incident/dto/getIncidents.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { UserRole, IncidentType } from '@prisma/client';

export class GetIncidentsDto {
userRole: UserRole;
userId: string;
searchText?: string;
incidentType?: IncidentType;
page?: number;
perPage?: number;
sortByCreatedAt?: boolean;
sortByUpdatedAt?: boolean;
}
32 changes: 17 additions & 15 deletions apps/api/src/incident/incident.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Param,
} from '@nestjs/common';
import { CreateIncidentDto } from './dto/createIncident.dto';
import { GetIncidentsDto } from './dto/getIncidents.dto';
import { UpdateIncidentStatusDto } from './dto/updateIncidentStatus.dto';
import { AssignIncidentStatusDto } from './dto/assignIncidentStatus.dto';
import { IncidentService } from './incident.service';
Expand Down Expand Up @@ -53,19 +54,23 @@ export class IncidentController {
return response;
}

@Get('/list')
@Post('/list')
async getIncidents(
@Query('userRole') userRole: UserRole,
@Query('userId') userId: string,
@Query('searchText') searchText?: string,
@Query('incidentType') incidentType?: IncidentType,
@Query('page') page?: string,
@Query('perPage') perPage?: string,
@Query('sortByCreatedAt') sortByCreatedAt?: string,
@Query('sortByUpdatedAt') sortByUpdatedAt?: string
@Body() getIncidentsDto: GetIncidentsDto
): Promise<GetIncidentsRes> {
let response: GetIncidentsRes;

const userRole = getIncidentsDto.userRole;
const userId = getIncidentsDto.userId;
const searchText = getIncidentsDto.searchText;
const incidentType = getIncidentsDto.incidentType;
const page = getIncidentsDto.page ? getIncidentsDto.page : 1;
const perPage = getIncidentsDto.perPage
? getIncidentsDto.perPage
: 10;
const sortByCreatedAt = getIncidentsDto.sortByCreatedAt ? 'true' : 'false';
const sortByUpdatedAt = getIncidentsDto.sortByUpdatedAt ? 'true' : 'false';

const incidents = await this.incidentService.getIncidents(
userRole,
userId,
Expand All @@ -83,19 +88,16 @@ export class IncidentController {
);
console.log('allIncidents.length = ', allIncidents.length);

const pageInt = page ? parseInt(page, 10) : 1;
const perPageInt = perPage ? parseInt(perPage, 10) : 10;

if (incidents) {
response = {
message: 'get incidents',
incidents: incidents,
total: incidents.length,
page: pageInt,
perPage: perPageInt,
page: page,
perPage: perPage,
totalPageCount:
allIncidents && allIncidents.length > 0
? Math.floor(allIncidents.length / perPageInt) || 1
? Math.floor(allIncidents.length / perPage) || 1
: 1,
};
}
Expand Down
8 changes: 4 additions & 4 deletions apps/api/src/incident/incident.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export class IncidentRepository {
userId: string,
searchText?: string,
incidentType?: IncidentType,
page?: string,
perPage?: string,
page?: number,
perPage?: number,
sortByCreatedAt?: string,
sortByUpdatedAt?: string
) {
const pageInt = page ? parseInt(page, 10) : 1;
const perPageInt = perPage ? parseInt(perPage, 10) : 10;
const pageInt = page ? page : 1;
const perPageInt = perPage ? perPage : 10;

const incidents = await this.prisma.incident.findMany({
where: {
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/incident/incident.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export class IncidentService {
userId: string,
searchText?: string,
incidentType?: IncidentType,
page?: string,
perPage?: string,
page?: number,
perPage?: number,
sortByCreatedAt?: string,
sortByUpdatedAt?: string
) {
Expand Down
17 changes: 7 additions & 10 deletions apps/web/src/components/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,19 @@ function Dashboard() {
sortByUpdatedAt?: boolean
) => {
if (token && userRole && userId) {
const pageStr = page ? page.toString() : '1';
const perPageStr = '10';

const sortByCreatedAtStr = sortByCreatedAt ? 'true' : 'false';
const sortByUpdatedAtStr = sortByUpdatedAt ? 'true' : 'false';
const pageInt = page ? page : 1;
const perPage = 10;

const response = await incidentService.getIncidents(
token,
userRole as UserRole,
userId,
searchText,
incidentType,
pageStr,
perPageStr,
sortByCreatedAtStr,
sortByUpdatedAtStr
pageInt,
perPage,
sortByCreatedAt,
sortByUpdatedAt
);
console.log('response = ', response);

Expand Down Expand Up @@ -166,7 +163,7 @@ function Dashboard() {

setTitle('');
setDescription('');
setIncidentType(IncidentType.MEDIUM);
setIncidentType(undefined);

await getIncidents(searchText);
}
Expand Down
29 changes: 14 additions & 15 deletions apps/web/src/services/incidentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,31 @@ export const getIncidents = async (
userId: string,
searchText?: string,
incidentType?: IncidentType,
pageStr?: string,
perPageStr?: string,
sortByCreatedAtStr?: string,
sortByUpdatedAtStr?: string
page?: number,
perPage?: number,
sortByCreatedAt?: boolean,
sortByUpdatedAt?: boolean
) => {
const params = {
const data = {
userRole: userRole,
userId: userId,
...(searchText && { searchText: searchText }),
...(incidentType && { incidentType: incidentType }),
...(pageStr && {
page: pageStr,
...(page && {
page: page,
}),
...(perPageStr && {
perPage: perPageStr,
...(perPage && {
perPage: perPage,
}),
...(sortByCreatedAtStr && {
sortByCreatedAt: sortByCreatedAtStr,
...(sortByCreatedAt && {
sortByCreatedAt: sortByCreatedAt,
}),
...(sortByUpdatedAtStr && {
sortByUpdatedAt: sortByUpdatedAtStr,
...(sortByUpdatedAt && {
sortByUpdatedAt: sortByUpdatedAt,
}),
};

const response = await axios.get(`${rootUrl}/incidents/list`, {
params: params,
const response = await axios.post(`${rootUrl}/incidents/list`, data, {
headers: {
Authorization: `Bearer ${token}`,
},
Expand Down

0 comments on commit b84b040

Please sign in to comment.