diff --git a/package.json b/package.json index 3ed6cfb..2092b56 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,7 @@ "start": "tsx ./src/app.ts", "dev": "nodemon --exec \"tsx ./src/app.ts\" --delay 1 --signal SIGTERM --exitcrash", "db:dev": "npx prisma generate && npx prisma migrate dev && tsx prisma/seed.ts", - "build": "tsc", - "prisma-seed": "tsx prisma/seed.ts" + "build": "tsc" }, "keywords": [], "author": "", diff --git a/prisma/meta-seed.ts b/prisma/meta-seed.ts index 781d33b..2464d91 100644 --- a/prisma/meta-seed.ts +++ b/prisma/meta-seed.ts @@ -23,7 +23,6 @@ export class MetaSeed { }catch(error){ throw new Error("Failed during the seeded ❌"); } - } } diff --git a/prisma/seed.ts b/prisma/seed.ts index f6f3ff1..dc16e04 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -81,7 +81,6 @@ const user_x_token: user_x_token[] = [ } ] - const roles_seed: roles[] = [ { rol_id: 1, @@ -342,6 +341,31 @@ const routes: routes[] = [ http_method: "POST", route: "/api/users/token" }, + { + route_id:9, + http_method:"GET", + route: "/api/incidents/:id" + }, + { + route_id:10, + http_method:"GET", + route:"/api/incidents/" + }, + { + route_id:11, + http_method:"POST", + route:"/api/incidents/" + }, + { + route_id:12, + http_method:"PUT", + route:"/api/incidents/:id" + }, + { + route_id:13, + http_method:"DELETE", + route:"/api/incidents/:id" + } ] const routes_x_roles: routes_x_roles[] = [ @@ -424,7 +448,59 @@ const routes_x_roles: routes_x_roles[] = [ { route_id:8, role_id:3, - } + }, + { + route_id:9, + role_id:1 + }, + { + route_id:9, + role_id:2 + }, + { + route_id:10, + role_id:1 + }, + { + route_id:10, + role_id:2 + }, + { + route_id:11, + role_id:1 + }, + { + route_id:11, + role_id:2 + }, + { + route_id:11, + role_id:3 + }, + { + route_id:12, + role_id:1 + }, + { + route_id:12, + role_id:2 + }, + { + route_id:12, + role_id:3 + }, + { + route_id:13, + role_id:1 + }, + { + route_id:13, + role_id:2 + }, + { + route_id:13, + role_id:3 + }, ] await Meta.generate_seed("departments", department_seed); diff --git a/src/app.ts b/src/app.ts index e2b8e5a..43ea3ea 100644 --- a/src/app.ts +++ b/src/app.ts @@ -8,7 +8,7 @@ import { PORT } from './config'; const app: Application = express(); app.use(jsonForPosts); -app.use(authorization); +//app.use(authorization); app.use('/api', usersRouter); app.use('/api', incidentRoutes); diff --git a/src/controllers/IncidentControllers.ts b/src/controllers/IncidentControllers.ts index 5665aa1..c8b2a87 100644 --- a/src/controllers/IncidentControllers.ts +++ b/src/controllers/IncidentControllers.ts @@ -5,22 +5,18 @@ import { IncidentRepository } from "../repositories/IncidentRepository"; const incidentRepository = new IncidentRepository(); const incidentService = new IncidentsService(incidentRepository); +let incidents = []; export const IncidentControllers = { async getAllIncidents(req: Request, res: Response) { - const incidents = await incidentService.getAllIncident(); + incidents = await incidentService.getAllIncident(); res.json(incidents); }, async getIncidentById(req: Request, res: Response) { - const incident_id = "0000001-2024"; - const incident = await incidentService.getIncidentById(incident_id); - if (incident) { - res.json(incident); - } else { - res.status(404).json({ message: "Incident not found" }); - } + const incident = await incidentService.getIncidentById(req.params.id); + (incident) ? res.json(incident) : res.status(404).json({ message: "Incident not found" }) }, async createIncident(req: Request, res: Response) { @@ -29,15 +25,43 @@ export const IncidentControllers = { }, async updateIncident(req: Request, res: Response) { - const incident_id = "0000002-2024"; - const updatedIncident = await incidentService.updateIncident(incident_id, req.body); - res.json(updatedIncident); + const updatedIncident = await incidentService.updateIncident(req.params.id, req.body); + (updatedIncident) ? res.json(updatedIncident) : res.status(404).json({ message: "Incident not found" }) }, async deleteIncident(req: Request, res: Response) { - const incident_id = "0000003-2024"; - await incidentService.deleteIncident(incident_id); - res.status(204).send(); + const deletedIncident = await incidentService.deleteIncident(req.params.id); + (deletedIncident) ? res.json(deletedIncident) : res.status(404).json({ message: "Incident not found" }) + }, + + async findByUserId(req: Request, res: Response){ + const incidents = await incidentService.findByUserId(req.body.user_dni); + res.json(incidents); + }, + + async findUserByRiskId(req: Request, res: Response){ + const incidents = await incidentService.findByRiskId(req.body.risk_id); + res.json(incidents); + }, + + async findByCategoryId(req: Request, res: Response){ + const incidents = await incidentService.findByCategoryId(req.body.category_id); + res.json(incidents); + }, + + async findByPriorityId(req: Request, res: Response){ + const incidents = await incidentService.findByPriorityId(req.body.priority_id); + res.json(incidents); + }, + + async findByStatusId(req: Request, res: Response){ + const incidents = await incidentService.findByPriorityId(req.body.status_id); + res.json(incidents); + }, + + async findByRecordDate(req: Request, res: Response){ + const incidents = await incidentService.findByRecordDate(req.body.record_date); + res.json(incidents); }, }; \ No newline at end of file diff --git a/src/interfaces/incidentsInterface.ts b/src/interfaces/incidentsInterface.ts new file mode 100644 index 0000000..13203e7 --- /dev/null +++ b/src/interfaces/incidentsInterface.ts @@ -0,0 +1,81 @@ +import { User } from "./userInterfaces" + +interface Diagnoses { + diagnosis_id:number; + diagnosis_date: Date; + diagnosis: string; + estimated_time:number; + observation:string; + buy:boolean; + user_dni:string; + incident_id:string; +} + +interface IncidentCategories { + id: number + description:string; +} + +interface IncidentEffects { + id: number; + description: string; +} + +interface IncidentPriorities { + id: number; + description:string; +} + +interface IncidentRisks { + id: number; + description:string; +} + +interface IncidentStatus { + id: number; + description:string; +} + +interface LogChangeStatusIncident { + log_id:number; + incident_id:string; + change_date:Date; + current_status:number; + previous_state:number; + user_dni:string; +} + +export interface UserXIncident { + assign_code: number; + user_dni: string; + incident_id: string; + assign_date: Date; + incident: Incident; + user: User; +} + +export interface Incident { + incident_id: string; + name: string; + description: string; + close_justification: string | null; + incident_place: string; + record_date: Date; + cost: number; + time_to_solve: number; + user_dni: string; + effect_id: number; + risk_id: number; + category_id: number; + priority_id: number; + status_id: number; + diagnosis: Diagnoses[]; + category: IncidentCategories; + effect: IncidentEffects; + priority: IncidentPriorities; + risk: IncidentRisks; + status: IncidentStatus; + user: User; + log_change_status_incident: LogChangeStatusIncident[]; + user_x_incident: UserXIncident[]; +} diff --git a/src/interfaces/userInterfaces.ts b/src/interfaces/userInterfaces.ts index 45796a2..c15e5c2 100644 --- a/src/interfaces/userInterfaces.ts +++ b/src/interfaces/userInterfaces.ts @@ -1,3 +1,16 @@ +export interface User { + dni: string; + name: string; + first_surname: string; + second_surname: string; + phone: string; + email: string; + departmentId: number; + status: boolean; + password: string; +} + + export interface userCredentials { email: string, password: string, diff --git a/src/repositories/IIncidentsRepository.ts b/src/repositories/IIncidentsRepository.ts index fd7c8ca..44a3e16 100644 --- a/src/repositories/IIncidentsRepository.ts +++ b/src/repositories/IIncidentsRepository.ts @@ -3,7 +3,13 @@ import { incidents } from "@prisma/client"; export interface IIncidentsRepository { findAll(): Promise; findById(incident_id: string): Promise; - create(incident: incidents): Promise + create(Incident: incidents): Promise update(incident_id: string, user: Partial): Promise; - delete(incident_id: string): Promise; + delete(incident_id: string): Promise; + findByUserId(user_dni: string): Promise; + findByRiskId(risk_id:number): Promise; + findByCategoryId(category_id:number): Promise; + findByPriorityId(priority_id:number):Promise; + findByStatusId(status_id:number): Promise; + findByRecordDate(record_date:Date): Promise; } \ No newline at end of file diff --git a/src/repositories/IncidentRepository.ts b/src/repositories/IncidentRepository.ts index cd0a4aa..bf6a877 100644 --- a/src/repositories/IncidentRepository.ts +++ b/src/repositories/IncidentRepository.ts @@ -1,5 +1,6 @@ import { PrismaClient, incidents } from "@prisma/client"; import { IIncidentsRepository } from "./IIncidentsRepository"; +import { Incident } from "../interfaces/incidentsInterface"; const prisma = new PrismaClient(); @@ -20,7 +21,53 @@ export class IncidentRepository implements IIncidentsRepository{ return prisma.incidents.update({ where: { incident_id }, data: incident }); } - async delete(incident_id: string): Promise { - await prisma.incidents.delete({ where: { incident_id } }); + async delete(incident_id: string): Promise { + return prisma.incidents.delete({ where: { incident_id } }); } + + async findByUserId(user_dni: string): Promise { + return prisma.incidents.findMany({ + where:{user_dni:user_dni}, + }); + } + + async findByRiskId(risk_id: number): Promise { + return prisma.incidents.findMany({ + where:{ + risk_id:risk_id + } + }) + } + + async findByCategoryId(category_id: number): Promise { + return prisma.incidents.findMany({ + where:{ + category_id:category_id + } + }) + } + + async findByPriorityId(priority_id: number): Promise { + return prisma.incidents.findMany({ + where:{ + priority_id:priority_id + } + }) + } + + async findByStatusId(status_id: number): Promise { + return prisma.incidents.findMany({ + where:{ + status_id:status_id + } + }) + } + + async findByRecordDate(record_date: Date): Promise { + return prisma.incidents.findMany({ + where:{ + record_date:record_date + } + }) + } } \ No newline at end of file diff --git a/src/services/IncidentServices.ts b/src/services/IncidentServices.ts index 0859f98..ae8d799 100644 --- a/src/services/IncidentServices.ts +++ b/src/services/IncidentServices.ts @@ -21,8 +21,31 @@ export class IncidentsService { return this.incidentRepository.update(incident_id, incident); } - async deleteIncident(incident_id: string): Promise { + async deleteIncident(incident_id: string): Promise { return this.incidentRepository.delete(incident_id); } + async findByUserId(user_dni:string): Promise{ + return this.findByUserId(user_dni); + } + + async findByRiskId(risk_id:number): Promise{ + return this.findByRiskId(risk_id); + } + + async findByCategoryId(category_id:number): Promise{ + return this.findByCategoryId(category_id); + } + + async findByPriorityId(priority_id:number): Promise{ + return this.findByPriorityId(priority_id); + } + + async findByStatusId(status_id:number): Promise{ + return this.findByStatusId(status_id); + } + + async findByRecordDate(record_date:Date): Promise{ + return this.findByRecordDate(record_date); + } } \ No newline at end of file diff --git a/src/utils/middlewareQueries.ts b/src/utils/middlewareQueries.ts index 552a29c..11c97ef 100644 --- a/src/utils/middlewareQueries.ts +++ b/src/utils/middlewareQueries.ts @@ -17,6 +17,10 @@ export class MiddlewareQueries { if (routeSegments.length >= 3 && routeSegments[1] === 'users' && routeSegments[2].match(this.pattern)) { routeSegments[2] = ":dni"; } + + if (routeSegments.length >= 3 && routeSegments[1] === 'incidents' && routeSegments[2].match(this.pattern)) { + routeSegments[2] = ":id"; + } response = "/" + routeSegments.join("/"); }