Skip to content

Commit 2d62a26

Browse files
use objects instead of arrays
1 parent 087333f commit 2d62a26

File tree

8 files changed

+130
-120
lines changed

8 files changed

+130
-120
lines changed

src/controllers/booking.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ add([
4343
},
4444
{
4545
sql: sql.bookings.remove,
46-
params: (req) => [...Object.values(req.body), req.user.isAdmin, req.user.organisationId],
46+
params: (req) => ({...req.body, isAdmin: req.user.isAdmin, organisationId: req.user.organisationId }),
4747
route: '/bookings/remove',
4848
middleware
4949
}

src/controllers/field.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ const insertSelect = async (params, organisationId, selectItems, client) => {
1212
const promises = [];
1313
for (const item of selectItems) {
1414
const { name, itemNumber } = item;
15-
const promise = db.rowCount(sql.fieldItems.insert, [
15+
const promise = db.rowCount(sql.fieldItems.insert, {
1616
fieldId,
1717
name,
1818
itemNumber,
1919
organisationId
20-
], client);
20+
}, client);
2121
promises.push(promise);
2222
}
2323
await Promise.all(promises);
@@ -27,7 +27,7 @@ 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 = [...Object.values(field), organisationId];
30+
const params = { ...field, organisationId };
3131
const client = await pool.connect();
3232
try {
3333
await client.query('begin');
@@ -58,14 +58,14 @@ const update = async (req, res) => {
5858
await client.query('begin');
5959
let totalRowCount = 0;
6060
const query = sql.fields.getById;
61-
const params = [fieldId, organisationId];
61+
const params = { fieldId, organisationId };
6262
const field = await db.first(query, params, client);
6363
if (name !== existingName) {
64-
const rowCount = await db.rowCount(sql.fields.update, [
64+
const rowCount = await db.rowCount(sql.fields.update, {
6565
fieldId,
6666
name,
6767
organisationId
68-
], client);
68+
}, client);
6969
totalRowCount += rowCount;
7070
}
7171
if (field.fieldType === 'Select') {
@@ -74,31 +74,31 @@ const update = async (req, res) => {
7474
for (const item of itemsToDelete) {
7575
const { id } = item;
7676
if (existingItemIds.includes(id)) {
77-
const promise = db.rowCount(sql.fieldItems.remove, [
77+
const promise = db.rowCount(sql.fieldItems.remove, {
7878
id,
7979
organisationId
80-
], client);
80+
}, client);
8181
promises.push(promise);
8282
}
8383
}
8484
for (const item of itemsToAdd) {
8585
const { id, name } = item;
86-
const promise = db.rowCount(sql.fieldItems.insert, [
86+
const promise = db.rowCount(sql.fieldItems.insert, {
8787
id,
8888
name,
8989
fieldId,
9090
organisationId
91-
], client);
91+
}, client);
9292
promises.push(promise);
9393
}
9494
for (const item of itemsToUpdate) {
9595
const { id, name } = item;
9696
if (existingItemIds.includes(id)) {
97-
const promise = db.rowCount(sql.fieldItems.update, [
97+
const promise = db.rowCount(sql.fieldItems.update, {
9898
id,
9999
name,
100100
organisationId
101-
], client);
101+
}, client);
102102
promises.push(promise);
103103
}
104104
}
@@ -150,14 +150,14 @@ const routes = [
150150
},
151151
{
152152
sql: fields.getByType,
153-
params: (req) => [['Short', 'Standard', 'Number'], req.user.organisationId],
153+
params: (req) => ({ types: ['Short', 'Standard', 'Number'], organisationId: req.user.organisationId }),
154154
route: '/fields/getFilenameFields',
155155
middleware,
156156
wrap
157157
},
158158
{
159159
sql: fields.getByType,
160-
params: (req) => [['Short', 'Standard', 'Number', 'Select'], req.user.organisationId],
160+
params: (req) => ({ types: ['Short', 'Standard', 'Number', 'Select'], organisationId: req.user.organisationId }),
161161
route: '/fields/getCsvFields',
162162
middleware,
163163
wrap

src/controllers/file.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { files, photos } from '../middleware/upload.js';
77
const uploadFiles = async (req, res) => {
88
const promises = [];
99
for (const file of req.files) {
10-
const promise = db.empty(sql.files.insert, [
11-
...Object.values(file),
12-
req.user.id,
13-
req.user.organisationId
14-
]);
10+
const promise = db.empty(sql.files.insert, {
11+
...file,
12+
userId: req.user.id,
13+
organisationId: req.user.organisationId
14+
});
1515
promises.push(promise);
1616
}
1717
await Promise.all(promises);

src/controllers/shift.js

+41-41
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ const insert = async (req, res) => {
1313
await client.query('begin');
1414
const { notes, questionGroupId } = series;
1515
const { areaId, times, breakMinutes } = shift;
16-
const seriesId = await db.value(sql.shiftSeries.insert, [
17-
times.length === 1,
16+
const seriesId = await db.value(sql.shiftSeries.insert, {
17+
isSingle: times.length === 1,
1818
notes,
1919
questionGroupId,
2020
userId,
2121
organisationId
22-
], client);
23-
await Promise.all(times.map(t => db.empty(sql.shifts.insert, [
22+
}, client);
23+
await Promise.all(times.map(t => db.empty(sql.shifts.insert, {
2424
areaId,
25-
t.startTime,
26-
t.endTime,
25+
startTime: t.startTime,
26+
endTime: t.endTime,
2727
breakMinutes,
2828
seriesId,
2929
organisationId
30-
], client)));
31-
await Promise.all(shiftRoles.map(sr => db.empty(sql.shiftRoles.insert, [
32-
...Object.values(sr),
30+
}, client)));
31+
await Promise.all(shiftRoles.map(shiftRole => db.empty(sql.shiftRoles.insert, {
32+
...shiftRole,
3333
seriesId,
3434
organisationId
35-
], client)));
35+
}, client)));
3636
await client.query('commit');
3737
return res.json({ rowCount: 1 });
3838
}
@@ -63,66 +63,66 @@ const update = async (req, res) => {
6363
const promises = [];
6464
let seriesId = initialSeries.id;
6565
if (detach) {
66-
seriesId = await db.value(sql.shiftSeries.copy, [
67-
updatedSeries.id,
66+
seriesId = await db.value(sql.shiftSeries.copy, {
67+
seriesId: updatedSeries.id,
6868
organisationId
69-
], client);
70-
await db.empty(sql.shiftRoles.copy, [
71-
initialSeries.id,
72-
seriesId,
69+
}, client);
70+
await db.empty(sql.shiftRoles.copy, {
71+
fromSeriesId: initialSeries.id,
72+
toSeriesId: seriesId,
7373
organisationId
74-
], client);
75-
await db.empty(sql.shifts.updateSeriesId, [
76-
initialShift.id,
74+
}, client);
75+
await db.empty(sql.shifts.updateSeriesId, {
76+
shiftId: initialShift.id,
7777
seriesId,
7878
organisationId
79-
], client);
80-
await db.empty(sql.bookings.transfer, [
81-
initialShift.id,
82-
seriesId,
79+
}, client);
80+
await db.empty(sql.bookings.transfer, {
81+
fromSeriesId: initialShift.id,
82+
toSeriesId: seriesId,
8383
organisationId
84-
], client);
84+
}, client);
8585
remove = remove.map(sr => ({...sr, id: null, seriesId }));
8686
update = update.map(sr => ({...sr, id: null, seriesId }));
8787
}
8888
if (initialSeries.notes !== updatedSeries.notes) {
89-
const promise = db.empty(sql.shiftSeries.update, [...Object.values(updatedSeries), organisationId], client);
89+
const promise = db.empty(sql.shiftSeries.update, {...updatedSeries, organisationId }, client);
9090
promises.push(promise);
9191
}
9292
if (
9393
initialShift.startTime !== updatedShift.startTime ||
9494
initialShift.endTime !== updatedShift.endTime ||
9595
initialShift.breakMinutes !== updatedShift.breakMinutes) {
96-
const promise = db.empty(sql.shifts.update, [
96+
const promise = db.empty(sql.shifts.update, {
9797
seriesId,
98-
initialShift.startTime,
99-
initialShift.endTime,
100-
updatedShift.startTime,
101-
updatedShift.endTime,
102-
updatedShift.breakMinutes,
98+
initialStartTime: initialShift.startTime,
99+
initialEndTime: initialShift.endTime,
100+
updatedStartTime: updatedShift.startTime,
101+
updatedEndTime: updatedShift.endTime,
102+
breakMinutes: updatedShift.breakMinutes,
103103
organisationId
104-
], client);
104+
}, client);
105105
promises.push(promise);
106106
}
107107
for (const shiftRole of remove) {
108108
const { id, seriesId, roleId } = shiftRole;
109109
const query = id ? sql.shiftRoles.remove : sql.shiftRoles.removeBySeriesId;
110-
const params = id ? [id, organisationId] : [seriesId, roleId, organisationId];
110+
const params = id ? { id, organisationId } : { seriesId, roleId, organisationId };
111111
const promise = db.empty(query, params, client);
112112
promises.push(promise);
113113
}
114114
for (const shiftRole of add) {
115-
const promise = db.empty(sql.shiftRoles.insert, [
116-
...Object.values(shiftRole),
117-
initialSeries.id,
115+
const promise = db.empty(sql.shiftRoles.insert, {
116+
...shiftRole,
117+
seriesId: initialSeries.id,
118118
organisationId
119-
], client);
119+
}, client);
120120
promises.push(promise);
121121
}
122122
for (const shiftRole of update) {
123123
const { id, seriesId, roleId, capacity } = shiftRole;
124124
const query = id ? sql.shiftRoles.update : sql.shiftRoles.updateBySeriesId;
125-
const params = id ? [id, capacity, organisationId] : [seriesId, roleId, capacity, organisationId];
125+
const params = id ? { id, capacity, organisationId } : { seriesId, roleId, capacity, organisationId };
126126
const promise = db.empty(query, params, client);
127127
promises.push(promise);
128128
}
@@ -143,17 +143,17 @@ const find = async (req, res) => {
143143
const organisationId = req.user.organisationId;
144144
let { areaId, startTime, endTime } = req.body;
145145
if (!areaId) {
146-
const { id, timeZone } = await db.first(sql.shifts.getFirstArea, [organisationId]);
146+
const { id, timeZone } = await db.first(sql.shifts.getFirstArea, { organisationId });
147147
areaId = id;
148148
startTime += timeZone;
149149
endTime += timeZone;
150150
}
151-
const shifts = await db.text(sql.shifts.find, [
151+
const shifts = await db.text(sql.shifts.find, {
152152
areaId,
153153
startTime,
154154
endTime,
155155
organisationId
156-
]);
156+
});
157157
return res.send(shifts);
158158
}
159159

0 commit comments

Comments
 (0)