This repository has been archived by the owner on May 3, 2024. It is now read-only.
/
server.js
160 lines (135 loc) · 3.72 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const express = require("express");
const { json, urlencoded } = require("body-parser");
const cors = require("cors");
const { sequelize } = require("./models");
const { Op } = require("sequelize");
/**
* @type {import('sequelize').Sequelize['models']}
*/
const models = sequelize.models;
const app = express();
app.use(json());
app.use(urlencoded({ extended: true }));
app.use(cors());
/**
* @param {(req: Request, res: Response) => Promise<void>} callback
*/
function handler(callback) {
return (req, res, next) => {
try {
callback(req, res).catch((error) => next(error));
} catch (error) {
next(error);
}
};
}
// A helper function to assert the request ID param is valid
// and convert it to a number (since it comes as a string by default)
function getIdParam(req) {
const id = req.params.id;
if (/^\d+$/.test(id)) {
return Number.parseInt(id, 10);
}
throw new TypeError(`Invalid ':id' param: "${id}"`);
}
app.get(
"/api/patients",
handler(async (req, res) => {
let order = undefined;
/**
* @type {import('sequelize').WhereOptions}
*/
let where = {};
if (req.query.sortBy) {
order = [
[req.query.sortBy, req.query.direction === "asc" ? "ASC" : "DESC"],
];
}
if (req.query.fullNameTerm) {
where['full_name'] = { [Op.or]: [{ [Op.like]: req.query.fullNameTerm }, { [Op.substring]: req.query.fullNameTerm }] }
}
if (req.query.emailTerm) {
where['email'] = { [Op.or]: [{ [Op.like]: req.query.emailTerm }, { [Op.substring]: req.query.emailTerm }] }
}
if (req.query.medicationsTerm) {
where['medications'] = { [Op.or]: [{ [Op.like]: req.query.medicationsTerm }, { [Op.substring]: req.query.medicationsTerm }] }
}
if (req.query.allergiesTerm) {
where['allergies'] = { [Op.or]: [{ [Op.like]: req.query.allergiesTerm }, { [Op.substring]: req.query.allergiesTerm }] }
}
const patients = await models.patient.findAll({
order,
where
});
res.status(200).json(patients);
})
);
app.post(
"/api/patients",
handler(async (req, res) => {
if (req.body.id) {
res
.status(400)
.send(
`Bad request: ID should not be provided, since it is determined automatically by the database.`
);
} else {
const { id } = await models.patient.create(req.body);
res.status(201).json({ id });
}
})
);
app.get(
"/api/patient/:id",
handler(async (req, res) => {
const id = getIdParam(req);
const patient =
"includeInstruments" in req.query
? await models.patient.findByPk(id, { include: models.instrument })
: await models.patient.findByPk(id);
if (patient) {
res.status(200).json(patient);
} else {
res.status(404).send("404 - Not found");
}
})
);
app.put(
"/api/patient/:id",
handler(async (req, res) => {
const id = getIdParam(req);
// We only accept an UPDATE request if the `:id` param matches the body `id`
if (req.body.id === id) {
await models.patient.update(req.body, {
where: {
id: id,
},
});
res.status(200).end();
} else {
res
.status(400)
.send(
`Bad request: param ID (${id}) does not match body ID (${req.body.id}).`
);
}
})
);
app.delete(
"/api/patient/:id",
handler(async (req, res) => {
const id = getIdParam(req);
await models.patient.destroy({
where: {
id: id,
},
});
res.status(200).end();
})
);
app.use(express.static(`${__dirname}/frontend/dist`));
app.get("/*", (_req, res) =>
res.sendFile(__dirname + "/frontend/dist/index.html")
);
const port = process.env.PORT || 3000;
app.listen(port, () => console.log("Listening on " + port));