1
- import { pool , db } from '../database/db.js' ;
1
+ import { pool , db , Client } from '../database/db.js' ;
2
2
import sql from '../../sql.js' ;
3
3
import { add , params } from '../utils/handler.js' ;
4
4
import auth from '../middleware/authentication.js' ;
@@ -8,9 +8,10 @@ const insert = async (req, res) => {
8
8
const { series, shift, shiftRoles } = req . body ;
9
9
const userId = req . user . id ;
10
10
const organisationId = req . user . organisationId ;
11
- const client = await pool . connect ( ) ;
11
+ const db = new Client ( ) ;
12
+ await db . connect ( ) ;
12
13
try {
13
- await client . query ( ' begin' ) ;
14
+ await db . begin ( ) ;
14
15
const { notes, questionGroupId } = series ;
15
16
const { areaId, times, breakMinutes } = shift ;
16
17
const seriesId = await db . value ( sql . shiftSeries . insert , {
@@ -19,29 +20,29 @@ const insert = async (req, res) => {
19
20
questionGroupId,
20
21
userId,
21
22
organisationId
22
- } , client ) ;
23
+ } ) ;
23
24
await Promise . all ( times . map ( t => db . empty ( sql . shifts . insert , {
24
25
areaId,
25
26
startTime : t . startTime ,
26
27
endTime : t . endTime ,
27
28
breakMinutes,
28
29
seriesId,
29
30
organisationId
30
- } , client ) ) ) ;
31
+ } ) ) ) ;
31
32
await Promise . all ( shiftRoles . map ( shiftRole => db . empty ( sql . shiftRoles . insert , {
32
33
...shiftRole ,
33
34
seriesId,
34
35
organisationId
35
- } , client ) ) ) ;
36
- await client . query ( ' commit' ) ;
36
+ } ) ) ) ;
37
+ await db . commit ( ) ;
37
38
return res . json ( { rowCount : 1 } ) ;
38
39
}
39
40
catch ( e ) {
40
- await client . query ( ' rollback' ) ;
41
+ await db . rollback ( ) ;
41
42
return res . sendStatus ( 500 ) ;
42
43
}
43
44
finally {
44
- client . release ( ) ;
45
+ db . release ( ) ;
45
46
}
46
47
}
47
48
@@ -57,36 +58,37 @@ const update = async (req, res) => {
57
58
detach
58
59
} = req . body ;
59
60
const organisationId = req . user . organisationId ;
60
- const client = await pool . connect ( ) ;
61
+ const db = new Client ( ) ;
62
+ await db . connect ( ) ;
61
63
try {
62
- await client . query ( ' begin' ) ;
64
+ await db . begin ( ) ;
63
65
const promises = [ ] ;
64
66
let seriesId = initialSeries . id ;
65
67
if ( detach ) {
66
68
seriesId = await db . value ( sql . shiftSeries . copy , {
67
69
seriesId : updatedSeries . id ,
68
70
organisationId
69
- } , client ) ;
71
+ } ) ;
70
72
await db . empty ( sql . shiftRoles . copy , {
71
73
fromSeriesId : initialSeries . id ,
72
74
toSeriesId : seriesId ,
73
75
organisationId
74
- } , client ) ;
76
+ } ) ;
75
77
await db . empty ( sql . shifts . updateSeriesId , {
76
78
shiftId : initialShift . id ,
77
79
seriesId,
78
80
organisationId
79
- } , client ) ;
81
+ } ) ;
80
82
await db . empty ( sql . bookings . transfer , {
81
83
fromSeriesId : initialShift . id ,
82
84
toSeriesId : seriesId ,
83
85
organisationId
84
- } , client ) ;
86
+ } ) ;
85
87
remove = remove . map ( sr => ( { ...sr , id : null , seriesId } ) ) ;
86
88
update = update . map ( sr => ( { ...sr , id : null , seriesId } ) ) ;
87
89
}
88
90
if ( initialSeries . notes !== updatedSeries . notes ) {
89
- const promise = db . empty ( sql . shiftSeries . update , { ...updatedSeries , organisationId } , client ) ;
91
+ const promise = db . empty ( sql . shiftSeries . update , { ...updatedSeries , organisationId } ) ;
90
92
promises . push ( promise ) ;
91
93
}
92
94
if (
@@ -101,41 +103,41 @@ const update = async (req, res) => {
101
103
updatedEndTime : updatedShift . endTime ,
102
104
breakMinutes : updatedShift . breakMinutes ,
103
105
organisationId
104
- } , client ) ;
106
+ } ) ;
105
107
promises . push ( promise ) ;
106
108
}
107
109
for ( const shiftRole of remove ) {
108
110
const { id, seriesId, roleId } = shiftRole ;
109
111
const query = id ? sql . shiftRoles . remove : sql . shiftRoles . removeBySeriesId ;
110
112
const params = id ? { id, organisationId } : { seriesId, roleId, organisationId } ;
111
- const promise = db . empty ( query , params , client ) ;
113
+ const promise = db . empty ( query , params ) ;
112
114
promises . push ( promise ) ;
113
115
}
114
116
for ( const shiftRole of add ) {
115
117
const promise = db . empty ( sql . shiftRoles . insert , {
116
118
...shiftRole ,
117
119
seriesId : initialSeries . id ,
118
120
organisationId
119
- } , client ) ;
121
+ } ) ;
120
122
promises . push ( promise ) ;
121
123
}
122
124
for ( const shiftRole of update ) {
123
125
const { id, seriesId, roleId, capacity } = shiftRole ;
124
126
const query = id ? sql . shiftRoles . update : sql . shiftRoles . updateBySeriesId ;
125
127
const params = id ? { id, capacity, organisationId } : { seriesId, roleId, capacity, organisationId } ;
126
- const promise = db . empty ( query , params , client ) ;
128
+ const promise = db . empty ( query , params ) ;
127
129
promises . push ( promise ) ;
128
130
}
129
131
await Promise . all ( promises ) ;
130
- await client . query ( ' commit' ) ;
132
+ await db . commit ( ) ;
131
133
return res . json ( { rowCount : 1 } ) ;
132
134
}
133
135
catch ( e ) {
134
- await client . query ( ' rollback' ) ;
136
+ await db . rollback ( ) ;
135
137
return res . sendStatus ( 500 ) ;
136
138
}
137
139
finally {
138
- client . release ( ) ;
140
+ db . release ( ) ;
139
141
}
140
142
}
141
143
0 commit comments