@@ -4,18 +4,12 @@ import { Knex } from "knex";
44import { isArray } from "lodash" ;
55
66export class IncidentService {
7- async getAll (
8- email : string ,
9- where : ( query : Knex . QueryBuilder ) => Knex . QueryBuilder
10- ) : Promise < Incident [ ] > {
7+ async getAll ( email : string , where : ( query : Knex . QueryBuilder ) => Knex . QueryBuilder ) : Promise < Incident [ ] > {
118 return db < Incident > ( "incidents" )
129 . innerJoin ( "incident_types" , "incident_types.id" , "incidents.incident_type_id" )
1310 . innerJoin ( "incident_statuses" , "incident_statuses.code" , "incidents.status_code" )
1411 . innerJoin ( "departments" , "departments.code" , "incidents.department_code" )
15- . whereRaw (
16- `"incidents"."id" IN (SELECT "incident_id" FROM "incident_users_view" WHERE "user_email" = ?)` ,
17- [ email ]
18- )
12+ . whereRaw ( `"incidents"."id" IN (SELECT "incident_id" FROM "incident_users_view" WHERE "user_email" = ?)` , [ email ] )
1913 . whereNot ( "incident_types.name" , "inspection" )
2014 . modify ( where )
2115 . select (
@@ -27,18 +21,12 @@ export class IncidentService {
2721 )
2822 . orderBy ( "incidents.created_at" , "desc" ) ;
2923 }
30- async getCount (
31- email : string ,
32- where : ( query : Knex . QueryBuilder ) => Knex . QueryBuilder
33- ) : Promise < { count : number } > {
24+ async getCount ( email : string , where : ( query : Knex . QueryBuilder ) => Knex . QueryBuilder ) : Promise < { count : number } > {
3425 return db < Incident > ( "incidents" )
3526 . innerJoin ( "incident_types" , "incident_types.id" , "incidents.incident_type_id" )
3627 . innerJoin ( "incident_statuses" , "incident_statuses.code" , "incidents.status_code" )
3728 . innerJoin ( "departments" , "departments.code" , "incidents.department_code" )
38- . whereRaw (
39- `"incidents"."id" IN (SELECT "incident_id" FROM "incident_users_view" WHERE "user_email" = ?)` ,
40- [ email ]
41- )
29+ . whereRaw ( `"incidents"."id" IN (SELECT "incident_id" FROM "incident_users_view" WHERE "user_email" = ?)` , [ email ] )
4230 . whereNot ( "incident_types.name" , "inspection" )
4331 . modify ( where )
4432 . count ( "* as count" )
@@ -58,10 +46,7 @@ export class IncidentService {
5846 . innerJoin ( "incident_statuses" , "incident_statuses.code" , "incidents.status_code" )
5947 . innerJoin ( "departments" , "departments.code" , "incidents.department_code" )
6048 . innerJoin ( "locations" , "incidents.location_code" , "locations.code" )
61- . whereRaw (
62- `"incidents"."id" IN (SELECT "incident_id" FROM "incident_users_view" WHERE "user_email" = ?)` ,
63- [ email ]
64- )
49+ . whereRaw ( `"incidents"."id" IN (SELECT "incident_id" FROM "incident_users_view" WHERE "user_email" = ?)` , [ email ] )
6550 . whereNot ( "incident_types.name" , "inspection" )
6651 . select < Incident > (
6752 "incidents.*" ,
@@ -77,21 +62,10 @@ export class IncidentService {
7762
7863 item . attachments = await db ( "incident_attachments" )
7964 . where ( { incident_id : item . id } )
80- . select (
81- "id" ,
82- "incident_id" ,
83- "added_by_email" ,
84- "file_name" ,
85- "file_type" ,
86- "file_size" ,
87- "added_date"
88- ) ;
65+ . select ( "id" , "incident_id" , "added_by_email" , "file_name" , "file_type" , "file_size" , "added_date" ) ;
8966
9067 item . steps = await db ( "incident_steps" ) . where ( { incident_id : item . id } ) . orderBy ( "order" ) ;
91- item . actions = await db ( "actions" )
92- . where ( { incident_id : item . id } )
93- . orderBy ( "due_date" )
94- . orderBy ( "id" ) ;
68+ item . actions = await db ( "actions" ) . where ( { incident_id : item . id } ) . orderBy ( "due_date" ) . orderBy ( "id" ) ;
9569 item . investigation = await db ( "investigations" ) . where ( { incident_id : item . id } ) . first ( ) ;
9670 item . access = await db ( "incident_users_view" ) . where ( {
9771 incident_id : item . id ,
@@ -104,23 +78,15 @@ export class IncidentService {
10478
10579 item . hazards = await db ( "incident_hazards" )
10680 . where ( { incident_id : item . id } )
107- . innerJoin (
108- "incident_hazard_types" ,
109- "incident_hazards.incident_hazard_type_code" ,
110- "incident_hazard_types.code"
111- )
81+ . innerJoin ( "incident_hazard_types" , "incident_hazards.incident_hazard_type_code" , "incident_hazard_types.code" )
11282 . select ( "incident_hazards.*" , "incident_hazard_types.name as incident_hazard_type_name" ) ;
11383
11484 for ( let hazard of item . hazards ?? [ ] ) {
11585 hazard . hazard = await db ( "hazards" )
11686 . where ( { "hazards.id" : hazard . hazard_id } )
11787 . innerJoin ( "hazard_types" , "hazards.hazard_type_id" , "hazard_types.id" )
11888 . innerJoin ( "locations" , "hazards.location_code" , "locations.code" )
119- . select (
120- "hazards.*" ,
121- "hazard_types.name as hazard_type_name" ,
122- "locations.name as location_name"
123- )
89+ . select ( "hazards.*" , "hazard_types.name as hazard_type_name" , "locations.name as location_name" )
12490 . first ( ) ;
12591 }
12692
@@ -130,24 +96,21 @@ export class IncidentService {
13096 await db ( "role_types" ) . where ( { id : action . actor_role_type_id } ) . first ( )
13197 ) . description ;
13298 } else if ( action . actor_user_id ) {
133- action . actor_display_name = (
134- await db ( "users" ) . where ( { id : action . actor_user_id } ) . first ( )
135- ) . display_name ;
99+ action . actor_display_name = ( await db ( "users" ) . where ( { id : action . actor_user_id } ) . first ( ) ) . display_name ;
136100 } else if ( action . actor_user_email ) {
137101 action . actor_display_name = action . actor_user_email ;
138102 }
139103
140104 action . categories = action . categories ?? [ ] ;
141- if ( ! isArray ( action . categories ) )
142- action . categories = action . categories . split ( "," ) . filter ( ( c ) => c ) ;
105+ if ( ! isArray ( action . categories ) ) action . categories = action . categories . split ( "," ) . filter ( ( c ) => c ) ;
143106 }
144107
145108 return item ;
146109 }
147110
148111 async getByReportingEmail ( email : string ) : Promise < Incident [ ] > {
149112 return db < Incident > ( "incidents" )
150- . where ( "incident_users_view.user_email" , email )
113+ . whereILike ( "incident_users_view.user_email" , email . toLowerCase ( ) )
151114 . where ( "incident_users_view.reason" , "reporter" )
152115 . whereNotIn ( "status_code" , [ "Closed" , "Dup" , "NoAct" ] )
153116 . whereNot ( "incident_types.name" , "inspection" )
@@ -166,7 +129,7 @@ export class IncidentService {
166129
167130 async getBySupervisorEmail ( email : string ) : Promise < Incident [ ] > {
168131 return db < Incident > ( "incidents" )
169- . where ( "incident_users_view.user_email" , email )
132+ . whereILike ( "incident_users_view.user_email" , email . toLowerCase ( ) )
170133 . where ( "incident_users_view.reason" , "supervisor" )
171134 . whereNot ( "incident_types.name" , "inspection" )
172135 . whereNotIn ( "status_code" , [ "Closed" , "Dup" , "NoAct" ] )
0 commit comments