Skip to content

Commit

Permalink
feat: 🎸 add getIncidentById api and searchText in getIncidents
Browse files Browse the repository at this point in the history
  • Loading branch information
yeukfei02 committed Jan 2, 2023
1 parent 922b073 commit 5b05f16
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

incident management system

documentation: <>
documentation: <https://documenter.getpostman.com/view/3827865/2s8Z6zzX4z>

features:

Expand Down
27 changes: 24 additions & 3 deletions apps/api/src/incident/incident.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Controller, Post, Body, Get, Query } from '@nestjs/common';
import { Controller, Post, Body, Get, Query, Param } from '@nestjs/common';
import { CreateIncidentDto } from './dto/createIncident.dto';
import { IncidentService } from './incident.service';
import { CreateIncidentRes } from './interface/createIncident.interface';
import { GetIncidentsRes } from './interface/getIncidents.interface';
import { GetIncidentRes } from './interface/getIncident.interface';
import { UserRole } from '@prisma/client';

@Controller('incidents')
Expand Down Expand Up @@ -41,11 +42,16 @@ export class IncidentController {
@Get('/list')
async getIncidents(
@Query('userRole') userRole: UserRole,
@Query('userId') userId: string
@Query('userId') userId: string,
@Query('searchText') searchText?: string
) {
let response: GetIncidentsRes;

const incidents = await this.incidentService.getIncidents(userRole, userId);
const incidents = await this.incidentService.getIncidents(
userRole,
userId,
searchText
);
if (incidents) {
response = {
message: 'get incidents',
Expand All @@ -55,4 +61,19 @@ export class IncidentController {

return response;
}

@Get('/:id')
async getIncident(@Param('id') id: string) {
let response: GetIncidentRes;

const incident = await this.incidentService.getIncidentById(id);
if (incident) {
response = {
message: 'get incident',
incident: incident,
};
}

return response;
}
}
29 changes: 28 additions & 1 deletion apps/api/src/incident/incident.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,27 @@ export class IncidentRepository {
return incident;
}

async getIncidents(userRole: UserRole, userId: string) {
async getIncidents(userRole: UserRole, userId: string, searchText?: string) {
let incidents: Incident[];

if (userRole === UserRole.ADMIN) {
incidents = await this.prisma.incident.findMany({
where: {
creator_id: userId,
...(searchText && {
OR: [
{
title: {
contains: searchText,
},
},
{
description: {
contains: searchText,
},
},
],
}),
},
include: {
creator: true,
Expand All @@ -56,4 +70,17 @@ export class IncidentRepository {

return incidents;
}

async getIncident(id: string) {
const incident = await this.prisma.incident.findUnique({
where: {
id: id,
},
include: {
creator: true,
assignee: true,
},
});
return incident;
}
}
10 changes: 8 additions & 2 deletions apps/api/src/incident/incident.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ export class IncidentService {
return incident;
}

async getIncidents(userRole: UserRole, userId: string) {
async getIncidents(userRole: UserRole, userId: string, searchText?: string) {
const incidents = await this.incidentRepository.getIncidents(
userRole,
userId
userId,
searchText
);
return incidents;
}

async getIncidentById(id: string) {
const incident = await this.incidentRepository.getIncident(id);
return incident;
}
}
6 changes: 6 additions & 0 deletions apps/api/src/incident/interface/getIncident.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Incident } from '@prisma/client';

export interface GetIncidentRes {
message: string;
incident: Incident;
}
11 changes: 7 additions & 4 deletions apps/web/src/components/incidentCardList/IncidentCardList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ function IncidentCardList({ token, userRole, userId, searchText }: Props) {
const [snackbarOpen, setSnackbarOpen] = useState(false);

useEffect(() => {
getIncidents();
}, []);
if (token && userRole && userId) {
getIncidents(searchText);
}
}, [token, userRole, userId, searchText]);

const getIncidents = async () => {
const getIncidents = async (searchText?: string) => {
if (token && userRole && userId) {
const response = await incidentService.getIncidents(
token,
userRole as UserRole,
userId
userId,
searchText
);
console.log('response = ', response);

Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/services/incidentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ export const createIncident = async (
export const getIncidents = async (
token: string,
userRole: UserRole,
userId: string
userId: string,
searchText?: string
) => {
const params = {
userRole: userRole,
userId: userId,
...(searchText && { searchText: searchText }),
};

const response = await axios.get(`${rootUrl}/incidents/list`, {
Expand Down

0 comments on commit 5b05f16

Please sign in to comment.