Skip to content

Commit

Permalink
fix: 🐛 fix page bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yeukfei02 committed Jan 2, 2023
1 parent 9b4cbd7 commit 77b6a41
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
14 changes: 10 additions & 4 deletions apps/api/src/incident/incident.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ export class IncidentController {
page,
perPage
);
const allIncidents = await this.incidentService.getAllIncidents();
const allIncidents =
await this.incidentService.getAllIncidentsByUserRoleAndUserId(
userRole,
userId
);
console.log('allIncidents.length = ', allIncidents.length);

const pageInt = page ? parseInt(page, 10) : 1;
const perPageInt = perPage ? parseInt(perPage, 10) : 10;
Expand All @@ -82,9 +87,10 @@ export class IncidentController {
total: incidents.length,
page: pageInt,
perPage: perPageInt,
totalPageCount: allIncidents
? Math.floor(allIncidents.length / perPageInt)
: 0,
totalPageCount:
allIncidents && allIncidents.length > 0
? Math.floor(allIncidents.length / perPageInt) || 1
: 1,
};
}

Expand Down
16 changes: 14 additions & 2 deletions apps/api/src/incident/incident.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,20 @@ export class IncidentRepository {
return incidents;
}

async findAllIncidents() {
const allIncidents = await this.prisma.incident.findMany({});
async findAllIncidentsByUserRoleAndUserId(
userRole: UserRole,
userId: string
) {
const allIncidents = await this.prisma.incident.findMany({
where: {
...(userRole === UserRole.ADMIN && {
creator_id: userId,
}),
...(userRole === UserRole.NORMAL_USER && {
assignee_id: userId,
}),
},
});
return allIncidents;
}

Expand Down
8 changes: 6 additions & 2 deletions apps/api/src/incident/incident.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ export class IncidentService {
return incidents;
}

async getAllIncidents() {
const allIncidents = await this.incidentRepository.findAllIncidents();
async getAllIncidentsByUserRoleAndUserId(userRole: UserRole, userId: string) {
const allIncidents =
await this.incidentRepository.findAllIncidentsByUserRoleAndUserId(
userRole,
userId
);
return allIncidents;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function Dashboard() {
}
};

const handlePageChange = (e: React.ChangeEvent<unknown>, page: number) => {
const handlePageChange = (e: React.ChangeEvent<unknown>, page: number) => {
setPage(page);
};

Expand Down

0 comments on commit 77b6a41

Please sign in to comment.