Skip to content

Commit

Permalink
feat: 🎸 add assignIncident, updateIncidentStatus api
Browse files Browse the repository at this point in the history
  • Loading branch information
yeukfei02 committed Jan 2, 2023
1 parent 2c33875 commit 988559b
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 8 deletions.
3 changes: 3 additions & 0 deletions apps/api/src/incident/dto/assignIncidentStatus.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class AssignIncidentStatusDto {
assigneeId: string;
}
5 changes: 5 additions & 0 deletions apps/api/src/incident/dto/updateIncidentStatus.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Status } from '@prisma/client';

export class UpdateIncidentStatusDto {
status: Status;
}
56 changes: 53 additions & 3 deletions apps/api/src/incident/incident.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import {
Post,
Body,
Get,
Patch,
Delete,
Query,
Param,
} from '@nestjs/common';
import { CreateIncidentDto } from './dto/createIncident.dto';
import { UpdateIncidentStatusDto } from './dto/updateIncidentStatus.dto';
import { AssignIncidentStatusDto } from './dto/assignIncidentStatus.dto';
import { IncidentService } from './incident.service';
import { CreateIncidentRes } from './interface/createIncident.interface';
import { GetIncidentsRes } from './interface/getIncidents.interface';
import { GetIncidentByIdRes } from './interface/getIncidentById.interface';
import { DeleteIncidentByIdRes } from './interface/deleteIncidentById.interface';
import { UpdateIncidentStatusRes } from './interface/updateIncidentStatus.interface';
import { AssignIncidentRes } from './interface/assignIncident.interface';
import { UserRole } from '@prisma/client';

@Controller('incidents')
Expand Down Expand Up @@ -53,7 +58,7 @@ export class IncidentController {
@Query('userRole') userRole: UserRole,
@Query('userId') userId: string,
@Query('searchText') searchText?: string
) {
): Promise<GetIncidentsRes> {
let response: GetIncidentsRes;

const incidents = await this.incidentService.getIncidents(
Expand All @@ -72,7 +77,7 @@ export class IncidentController {
}

@Get('/:id')
async getIncidentById(@Param('id') id: string) {
async getIncidentById(@Param('id') id: string): Promise<GetIncidentByIdRes> {
let response: GetIncidentByIdRes;

const incident = await this.incidentService.getIncidentById(id);
Expand All @@ -86,8 +91,53 @@ export class IncidentController {
return response;
}

@Patch('/:id/assignIncident')
async assignIncident(
@Param('id') id: string,
@Body() assignIncidentStatusDto: AssignIncidentStatusDto
): Promise<AssignIncidentRes> {
let response: AssignIncidentRes;

const assigneeId = assignIncidentStatusDto.assigneeId;

const incident = await this.incidentService.assignIncident(id, assigneeId);
if (incident) {
response = {
message: 'update incident status',
incident: incident,
};
}

return response;
}

@Patch('/:id/updateIncidentStatus')
async updateIncidentStatus(
@Param('id') id: string,
@Body() updateIncidentStatusDto: UpdateIncidentStatusDto
): Promise<UpdateIncidentStatusRes> {
let response: UpdateIncidentStatusRes;

const status = updateIncidentStatusDto.status;

const incident = await this.incidentService.updateIncidentStatus(
id,
status
);
if (incident) {
response = {
message: 'update incident status',
incident: incident,
};
}

return response;
}

@Delete('/:id')
async deleteIncidentById(@Param('id') id: string) {
async deleteIncidentById(
@Param('id') id: string
): Promise<DeleteIncidentByIdRes> {
let response: DeleteIncidentByIdRes;

const incident = await this.incidentService.deleteIncidentById(id);
Expand Down
34 changes: 33 additions & 1 deletion apps/api/src/incident/incident.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { Incident, IncidentType, UserRole } from '@prisma/client';
import { Incident, IncidentType, UserRole, Status } from '@prisma/client';
import { PrismaService } from '../prisma.service';

@Injectable()
Expand Down Expand Up @@ -102,6 +102,38 @@ export class IncidentRepository {
return incident;
}

async assignIncident(id: string, assigneeId: string) {
const incident = await this.prisma.incident.update({
where: {
id: id,
},
data: {
assignee_id: assigneeId,
},
include: {
creator: true,
assignee: true,
},
});
return incident;
}

async updateIncidentStatus(id: string, status: Status) {
const incident = await this.prisma.incident.update({
where: {
id: id,
},
data: {
status: status,
},
include: {
creator: true,
assignee: true,
},
});
return incident;
}

async deleteIncidentById(id: string) {
const incident = await this.prisma.incident.delete({
where: {
Expand Down
18 changes: 17 additions & 1 deletion apps/api/src/incident/incident.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { IncidentType, UserRole } from '@prisma/client';
import { IncidentType, UserRole, Status } from '@prisma/client';
import { IncidentRepository } from './incident.repository';

@Injectable()
Expand Down Expand Up @@ -37,6 +37,22 @@ export class IncidentService {
return incident;
}

async assignIncident(id: string, assigneeId: string) {
const incident = await this.incidentRepository.assignIncident(
id,
assigneeId
);
return incident;
}

async updateIncidentStatus(id: string, status: Status) {
const incident = await this.incidentRepository.updateIncidentStatus(
id,
status
);
return incident;
}

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

export interface AssignIncidentRes {
message: string;
incident: Incident;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Incident } from '@prisma/client';

export interface UpdateIncidentStatusRes {
message: string;
incident: Incident;
}
3 changes: 3 additions & 0 deletions apps/web/src/components/dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ function Dashboard() {
margin="normal"
required
fullWidth
multiline
rows={5}
maxRows={10}
name="description"
label="Description"
type="text"
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/components/incidentCardList/IncidentCardList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
import Box from '@mui/material/Box';
import Grid from '@mui/material/Grid';
import CardView from '../cardView/CardView';
import { Incident } from '@prisma/client';
import { Incident, UserRole } from '@prisma/client';
import * as userService from '../../services/userService';

interface Props {
Expand All @@ -19,7 +19,8 @@ function IncidentCardList({ incidents, getIncidents }: Props) {

const getNormalUsers = async () => {
const token = localStorage.getItem('token');
if (token) {
const userRole = localStorage.getItem('userRole');
if (token && userRole && userRole === UserRole.ADMIN) {
const response = await userService.getNormalUsers(token);
console.log('response = ', response);

Expand Down
42 changes: 41 additions & 1 deletion apps/web/src/services/incidentService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { UserRole, IncidentType } from '@prisma/client';
import { UserRole, IncidentType, Status } from '@prisma/client';
import { getRootUrl } from '../helper/helper';

const rootUrl = getRootUrl();
Expand Down Expand Up @@ -58,6 +58,46 @@ export const getIncidentById = async (token: string, id: string) => {
return response;
};

export const assignIncident = async (
token: string,
id: string,
assigneeId: string
) => {
const data = {
assigneeId: assigneeId,
};
const response = await axios.patch(
`${rootUrl}/incidents/${id}/assignIncident`,
data,
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
return response;
};

export const updateIncidentStatus = async (
token: string,
id: string,
status: Status
) => {
const data = {
status: status,
};
const response = await axios.patch(
`${rootUrl}/incidents/${id}/updateIncidentStatus`,
data,
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
return response;
};

export const deleteIncidentById = async (token: string, id: string) => {
const response = await axios.delete(`${rootUrl}/incidents/${id}`, {
headers: {
Expand Down

0 comments on commit 988559b

Please sign in to comment.