Skip to content

Commit fe7622c

Browse files
abstract transactions
1 parent 2d62a26 commit fe7622c

File tree

7 files changed

+195
-170
lines changed

7 files changed

+195
-170
lines changed

src/controllers/booking.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pool, db } from '../database/db.js';
1+
import { Client } from '../database/db.js';
22
import sql from '../../sql.js';
33
import { add } from '../utils/handler.js';
44
import auth from '../middleware/authentication.js';
@@ -9,27 +9,28 @@ const insert = async (req, res) => {
99
const bookedById = req.user.id;
1010
const isAdmin = req.user.isAdmin;
1111
const organisationId = req.user.organisationId;
12-
const client = await pool.connect();
12+
const db = new Client();
13+
await db.connect();
1314
try {
14-
await client.query('begin');
15-
const promises = shifts.map(s => db.rowCount(sql.bookings.insert, [
16-
s.shiftId,
17-
s.shiftRoleId,
15+
await db.begin();
16+
const promises = shifts.map(s => db.rowCount(sql.bookings.insert, {
17+
shiftId: s.shiftId,
18+
shiftRoleId: s.shiftRoleId,
1819
userId,
1920
bookedById,
2021
isAdmin,
2122
organisationId
22-
], client));
23+
}));
2324
const results = await Promise.all(promises);
24-
await client.query('commit');
25+
await db.commit();
2526
return res.json({ rowCount: results.reduce((a, c) => a + c) });
2627
}
2728
catch (e) {
28-
await client.query('rollback');
29+
await db.rollback();
2930
return res.sendStatus(500);
3031
}
3132
finally {
32-
client.release();
33+
db.release();
3334
}
3435
}
3536

src/controllers/field.js

+27-24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { pool, db } from '../database/db.js';
1+
import { Client } from '../database/db.js';
22
import auth from '../middleware/authentication.js';
33
import { admin } from '../middleware/permission.js';
44
import sql from '../../sql.js';
55
import { add, params } from '../utils/handler.js';
66

7-
const insertSelect = async (params, organisationId, selectItems, client) => {
8-
const fieldId = await db.value(sql.fields.insert, params, client);
7+
const insertSelect = async (params, organisationId, selectItems, db) => {
8+
const fieldId = await db.value(sql.fields.insert, params);
99
if (fieldId === null) {
1010
return 0;
1111
}
@@ -17,7 +17,7 @@ const insertSelect = async (params, organisationId, selectItems, client) => {
1717
name,
1818
itemNumber,
1919
organisationId
20-
}, client);
20+
});
2121
promises.push(promise);
2222
}
2323
await Promise.all(promises);
@@ -27,45 +27,48 @@ const insertSelect = async (params, organisationId, selectItems, client) => {
2727
const insert = async (req, res) => {
2828
const { selectItems, ...field } = req.body;
2929
const organisationId = req.user.organisationId;
30-
const params = { ...field, organisationId };
31-
const client = await pool.connect();
30+
const params = {...field, organisationId };
31+
const db = new Client();
32+
await db.connect();
3233
try {
33-
await client.query('begin');
34+
await db.begin();
3435
let rowCount;
3536
if (selectItems) {
36-
rowCount = await insertSelect(params, organisationId, selectItems, client);
37+
rowCount = await insertSelect(params, organisationId, selectItems, db);
3738
}
3839
else {
39-
rowCount = await db.rowCount(sql.fields.insert, params, client);
40+
rowCount = await db.rowCount(sql.fields.insert, params);
4041
}
41-
await client.query('commit');
42+
await db.commit();
4243
return res.json({ rowCount });
4344
}
4445
catch (e) {
45-
await client.query('rollback');
46+
await db.rollback();
4647
return res.sendStatus(500);
4748
}
4849
finally {
49-
client.release();
50+
db.release();
5051
}
5152
}
5253

5354
const update = async (req, res) => {
5455
const { fieldId, name, existingName, itemsToDelete, itemsToAdd, itemsToUpdate } = req.body;
5556
const organisationId = req.user.organisationId;
56-
const client = await pool.connect();
57+
const db = new Client();
58+
await db.connect();
5759
try {
58-
await client.query('begin');
60+
await db.begin();
5961
let totalRowCount = 0;
60-
const query = sql.fields.getById;
61-
const params = { fieldId, organisationId };
62-
const field = await db.first(query, params, client);
62+
const field = await db.first(sql.fields.getById, {
63+
fieldId,
64+
organisationId
65+
});
6366
if (name !== existingName) {
6467
const rowCount = await db.rowCount(sql.fields.update, {
6568
fieldId,
6669
name,
6770
organisationId
68-
}, client);
71+
});
6972
totalRowCount += rowCount;
7073
}
7174
if (field.fieldType === 'Select') {
@@ -77,7 +80,7 @@ const update = async (req, res) => {
7780
const promise = db.rowCount(sql.fieldItems.remove, {
7881
id,
7982
organisationId
80-
}, client);
83+
});
8184
promises.push(promise);
8285
}
8386
}
@@ -88,7 +91,7 @@ const update = async (req, res) => {
8891
name,
8992
fieldId,
9093
organisationId
91-
}, client);
94+
});
9295
promises.push(promise);
9396
}
9497
for (const item of itemsToUpdate) {
@@ -98,7 +101,7 @@ const update = async (req, res) => {
98101
id,
99102
name,
100103
organisationId
101-
}, client);
104+
});
102105
promises.push(promise);
103106
}
104107
}
@@ -107,15 +110,15 @@ const update = async (req, res) => {
107110
totalRowCount += rowCount;
108111
}
109112
}
110-
await client.query('commit');
113+
await db.commit();
111114
return res.json({ rowCount: totalRowCount });
112115
}
113116
catch (e) {
114-
await client.query('rollback');
117+
await db.rollback();
115118
return res.sendStatus(500);
116119
}
117120
finally {
118-
client.release();
121+
db.release();
119122
}
120123
}
121124

src/controllers/shift.js

+25-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pool, db } from '../database/db.js';
1+
import { pool, db, Client } from '../database/db.js';
22
import sql from '../../sql.js';
33
import { add, params } from '../utils/handler.js';
44
import auth from '../middleware/authentication.js';
@@ -8,9 +8,10 @@ const insert = async (req, res) => {
88
const { series, shift, shiftRoles } = req.body;
99
const userId = req.user.id;
1010
const organisationId = req.user.organisationId;
11-
const client = await pool.connect();
11+
const db = new Client();
12+
await db.connect();
1213
try {
13-
await client.query('begin');
14+
await db.begin();
1415
const { notes, questionGroupId } = series;
1516
const { areaId, times, breakMinutes } = shift;
1617
const seriesId = await db.value(sql.shiftSeries.insert, {
@@ -19,29 +20,29 @@ const insert = async (req, res) => {
1920
questionGroupId,
2021
userId,
2122
organisationId
22-
}, client);
23+
});
2324
await Promise.all(times.map(t => db.empty(sql.shifts.insert, {
2425
areaId,
2526
startTime: t.startTime,
2627
endTime: t.endTime,
2728
breakMinutes,
2829
seriesId,
2930
organisationId
30-
}, client)));
31+
})));
3132
await Promise.all(shiftRoles.map(shiftRole => db.empty(sql.shiftRoles.insert, {
3233
...shiftRole,
3334
seriesId,
3435
organisationId
35-
}, client)));
36-
await client.query('commit');
36+
})));
37+
await db.commit();
3738
return res.json({ rowCount: 1 });
3839
}
3940
catch (e) {
40-
await client.query('rollback');
41+
await db.rollback();
4142
return res.sendStatus(500);
4243
}
4344
finally {
44-
client.release();
45+
db.release();
4546
}
4647
}
4748

@@ -57,36 +58,37 @@ const update = async (req, res) => {
5758
detach
5859
} = req.body;
5960
const organisationId = req.user.organisationId;
60-
const client = await pool.connect();
61+
const db = new Client();
62+
await db.connect();
6163
try {
62-
await client.query('begin');
64+
await db.begin();
6365
const promises = [];
6466
let seriesId = initialSeries.id;
6567
if (detach) {
6668
seriesId = await db.value(sql.shiftSeries.copy, {
6769
seriesId: updatedSeries.id,
6870
organisationId
69-
}, client);
71+
});
7072
await db.empty(sql.shiftRoles.copy, {
7173
fromSeriesId: initialSeries.id,
7274
toSeriesId: seriesId,
7375
organisationId
74-
}, client);
76+
});
7577
await db.empty(sql.shifts.updateSeriesId, {
7678
shiftId: initialShift.id,
7779
seriesId,
7880
organisationId
79-
}, client);
81+
});
8082
await db.empty(sql.bookings.transfer, {
8183
fromSeriesId: initialShift.id,
8284
toSeriesId: seriesId,
8385
organisationId
84-
}, client);
86+
});
8587
remove = remove.map(sr => ({...sr, id: null, seriesId }));
8688
update = update.map(sr => ({...sr, id: null, seriesId }));
8789
}
8890
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 });
9092
promises.push(promise);
9193
}
9294
if (
@@ -101,41 +103,41 @@ const update = async (req, res) => {
101103
updatedEndTime: updatedShift.endTime,
102104
breakMinutes: updatedShift.breakMinutes,
103105
organisationId
104-
}, client);
106+
});
105107
promises.push(promise);
106108
}
107109
for (const shiftRole of remove) {
108110
const { id, seriesId, roleId } = shiftRole;
109111
const query = id ? sql.shiftRoles.remove : sql.shiftRoles.removeBySeriesId;
110112
const params = id ? { id, organisationId } : { seriesId, roleId, organisationId };
111-
const promise = db.empty(query, params, client);
113+
const promise = db.empty(query, params);
112114
promises.push(promise);
113115
}
114116
for (const shiftRole of add) {
115117
const promise = db.empty(sql.shiftRoles.insert, {
116118
...shiftRole,
117119
seriesId: initialSeries.id,
118120
organisationId
119-
}, client);
121+
});
120122
promises.push(promise);
121123
}
122124
for (const shiftRole of update) {
123125
const { id, seriesId, roleId, capacity } = shiftRole;
124126
const query = id ? sql.shiftRoles.update : sql.shiftRoles.updateBySeriesId;
125127
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);
127129
promises.push(promise);
128130
}
129131
await Promise.all(promises);
130-
await client.query('commit');
132+
await db.commit();
131133
return res.json({ rowCount: 1 });
132134
}
133135
catch (e) {
134-
await client.query('rollback');
136+
await db.rollback();
135137
return res.sendStatus(500);
136138
}
137139
finally {
138-
client.release();
140+
db.release();
139141
}
140142
}
141143

0 commit comments

Comments
 (0)