From 5b05f16889c970176419fc265da305e99a2e48c4 Mon Sep 17 00:00:00 2001 From: Donald Wu Date: Mon, 2 Jan 2023 12:29:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20getIncidentById=20?= =?UTF-8?q?api=20and=20searchText=20in=20getIncidents?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- apps/api/src/incident/incident.controller.ts | 27 +++++++++++++++-- apps/api/src/incident/incident.repository.ts | 29 ++++++++++++++++++- apps/api/src/incident/incident.service.ts | 10 +++++-- .../interface/getIncident.interface.ts | 6 ++++ .../incidentCardList/IncidentCardList.tsx | 11 ++++--- apps/web/src/services/incidentService.ts | 4 ++- 7 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 apps/api/src/incident/interface/getIncident.interface.ts diff --git a/README.md b/README.md index 178f7b7..b9d111b 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ incident management system -documentation: <> +documentation: features: diff --git a/apps/api/src/incident/incident.controller.ts b/apps/api/src/incident/incident.controller.ts index ca4e890..bcd19c4 100644 --- a/apps/api/src/incident/incident.controller.ts +++ b/apps/api/src/incident/incident.controller.ts @@ -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') @@ -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', @@ -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; + } } diff --git a/apps/api/src/incident/incident.repository.ts b/apps/api/src/incident/incident.repository.ts index 251c684..123d2e8 100644 --- a/apps/api/src/incident/incident.repository.ts +++ b/apps/api/src/incident/incident.repository.ts @@ -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, @@ -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; + } } diff --git a/apps/api/src/incident/incident.service.ts b/apps/api/src/incident/incident.service.ts index bf05807..9d1398c 100644 --- a/apps/api/src/incident/incident.service.ts +++ b/apps/api/src/incident/incident.service.ts @@ -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; + } } diff --git a/apps/api/src/incident/interface/getIncident.interface.ts b/apps/api/src/incident/interface/getIncident.interface.ts new file mode 100644 index 0000000..f67b419 --- /dev/null +++ b/apps/api/src/incident/interface/getIncident.interface.ts @@ -0,0 +1,6 @@ +import { Incident } from '@prisma/client'; + +export interface GetIncidentRes { + message: string; + incident: Incident; +} diff --git a/apps/web/src/components/incidentCardList/IncidentCardList.tsx b/apps/web/src/components/incidentCardList/IncidentCardList.tsx index e297a38..c2275e0 100644 --- a/apps/web/src/components/incidentCardList/IncidentCardList.tsx +++ b/apps/web/src/components/incidentCardList/IncidentCardList.tsx @@ -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); diff --git a/apps/web/src/services/incidentService.ts b/apps/web/src/services/incidentService.ts index 0f394c4..52e7a5e 100644 --- a/apps/web/src/services/incidentService.ts +++ b/apps/web/src/services/incidentService.ts @@ -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`, {