Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down
1 change: 0 additions & 1 deletion prisma/meta-seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export class MetaSeed {
}catch(error){
throw new Error("Failed during the seeded ❌");
}

}
}

80 changes: 78 additions & 2 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ const user_x_token: user_x_token[] = [
}
]


const roles_seed: roles[] = [
{
rol_id: 1,
Expand Down Expand Up @@ -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[] = [
Expand Down Expand Up @@ -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>("departments", department_seed);
Expand Down
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
52 changes: 38 additions & 14 deletions src/controllers/IncidentControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
},

};
81 changes: 81 additions & 0 deletions src/interfaces/incidentsInterface.ts
Original file line number Diff line number Diff line change
@@ -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[];
}
13 changes: 13 additions & 0 deletions src/interfaces/userInterfaces.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
10 changes: 8 additions & 2 deletions src/repositories/IIncidentsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { incidents } from "@prisma/client";
export interface IIncidentsRepository {
findAll(): Promise<incidents[]>;
findById(incident_id: string): Promise<incidents | null>;
create(incident: incidents): Promise<incidents>
create(Incident: incidents): Promise<incidents>
update(incident_id: string, user: Partial<incidents>): Promise<incidents>;
delete(incident_id: string): Promise<void>;
delete(incident_id: string): Promise<incidents | null>;
findByUserId(user_dni: string): Promise<incidents[] | null>;
findByRiskId(risk_id:number): Promise<incidents[] | null>;
findByCategoryId(category_id:number): Promise<incidents[] | null>;
findByPriorityId(priority_id:number):Promise<incidents[] | null>;
findByStatusId(status_id:number): Promise<incidents[] | null>;
findByRecordDate(record_date:Date): Promise<incidents[] | null>;
}
51 changes: 49 additions & 2 deletions src/repositories/IncidentRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PrismaClient, incidents } from "@prisma/client";
import { IIncidentsRepository } from "./IIncidentsRepository";
import { Incident } from "../interfaces/incidentsInterface";

const prisma = new PrismaClient();

Expand All @@ -20,7 +21,53 @@ export class IncidentRepository implements IIncidentsRepository{
return prisma.incidents.update({ where: { incident_id }, data: incident });
}

async delete(incident_id: string): Promise<void> {
await prisma.incidents.delete({ where: { incident_id } });
async delete(incident_id: string): Promise<incidents | null> {
return prisma.incidents.delete({ where: { incident_id } });
}

async findByUserId(user_dni: string): Promise<incidents[] | null> {
return prisma.incidents.findMany({
where:{user_dni:user_dni},
});
}

async findByRiskId(risk_id: number): Promise<incidents[] | null> {
return prisma.incidents.findMany({
where:{
risk_id:risk_id
}
})
}

async findByCategoryId(category_id: number): Promise<incidents[] | null> {
return prisma.incidents.findMany({
where:{
category_id:category_id
}
})
}

async findByPriorityId(priority_id: number): Promise<incidents[] | null> {
return prisma.incidents.findMany({
where:{
priority_id:priority_id
}
})
}

async findByStatusId(status_id: number): Promise<incidents[] | null> {
return prisma.incidents.findMany({
where:{
status_id:status_id
}
})
}

async findByRecordDate(record_date: Date): Promise<incidents[] | null> {
return prisma.incidents.findMany({
where:{
record_date:record_date
}
})
}
}
Loading