Skip to content

Commit 20c1033

Browse files
committed
9.12 - 9.13
1 parent 71b844f commit 20c1033

10 files changed

Lines changed: 187 additions & 52 deletions

File tree

part9/patientor-backend/data/patients.json

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Patient } from "../src/types";
2+
import toNewPatient from "../src/util";
3+
4+
const data = [
5+
{
6+
id: "d2773336-f723-11e9-8f0b-362b9e155667",
7+
name: "John McClane",
8+
dateOfBirth: "1986-07-09",
9+
ssn: "090786-122X",
10+
gender: "male",
11+
occupation: "New york city cop",
12+
},
13+
{
14+
id: "d2773598-f723-11e9-8f0b-362b9e155667",
15+
name: "Martin Riggs",
16+
dateOfBirth: "1979-01-30",
17+
ssn: "300179-77A",
18+
gender: "male",
19+
occupation: "Cop",
20+
},
21+
{
22+
id: "d27736ec-f723-11e9-8f0b-362b9e155667",
23+
name: "Hans Gruber",
24+
dateOfBirth: "1970-04-25",
25+
ssn: "250470-555L",
26+
gender: "male",
27+
occupation: "Technician",
28+
},
29+
{
30+
id: "d2773822-f723-11e9-8f0b-362b9e155667",
31+
name: "Dana Scully",
32+
dateOfBirth: "1974-01-05",
33+
ssn: "050174-432N",
34+
gender: "female",
35+
occupation: "Forensic Pathologist",
36+
},
37+
{
38+
id: "d2773c6e-f723-11e9-8f0b-362b9e155667",
39+
name: "Matti Luukkainen",
40+
dateOfBirth: "1971-04-09",
41+
ssn: "090471-8890",
42+
gender: "male",
43+
occupation: "Digital evangelist",
44+
},
45+
];
46+
47+
const patients: Patient[] = data.map((obj) => {
48+
const object = toNewPatient(obj) as Patient;
49+
object.id = obj.id;
50+
return object;
51+
});
52+
53+
export default patients;

part9/patientor-backend/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
"dependencies": {
1313
"@types/cors": "^2.8.12",
1414
"cors": "^2.8.5",
15-
"express": "^4.17.2"
15+
"express": "^4.17.2",
16+
"uuid": "^8.3.2"
1617
},
1718
"devDependencies": {
1819
"@types/express": "^4.17.13",
20+
"@types/uuid": "^8.3.4",
1921
"@typescript-eslint/eslint-plugin": "^5.10.0",
2022
"@typescript-eslint/parser": "^5.10.0",
2123
"eslint": "^8.7.0",

part9/patientor-backend/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const app = express();
77
app.use(cors());
88
app.use(express.json());
99

10-
app.get("/ping", (_req, res) => {
10+
app.get("/api/ping", (_req, res) => {
1111
res.send("pong");
1212
});
1313

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
12
import Router from "express";
23
import patientService from "../services/patientService";
34
const router = Router();
45

5-
router.get("/", (_req, res) => {
6-
res.send(patientService.getPatients());
7-
});
6+
router
7+
.route("/")
8+
.get((_req, res) => {
9+
res.send(patientService.getPatients());
10+
})
11+
.post((req, res) => {
12+
const { name, dateOfBirth, occupation, ssn, gender } = req.body;
13+
const newPatient = patientService.addPatient({
14+
name,
15+
dateOfBirth,
16+
occupation,
17+
ssn,
18+
gender,
19+
});
20+
res.json(newPatient);
21+
});
822

923
export default router;

part9/patientor-backend/src/services/diagnoseService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import diagnoseData from "../../data/diagnoses.json";
2-
import { Diagnose } from "../../types";
2+
import { Diagnose } from "../types";
33

44
const getDiagnoses = (): Diagnose[] => {
55
return diagnoseData;
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
import patientData from "../../data/patients.json";
2-
import { Patient } from "../../types";
1+
import { v4 as uuid } from "uuid";
2+
import patientData from "../../data/patients";
3+
import { NewPatient, Patient } from "../types";
34

45
const patients: Patient[] = patientData;
56

67
const getPatients = (): Omit<Patient, "ssn">[] => {
7-
return patients.map(({ id, name, dateOfBirth, occupation }) => ({
8+
return patients.map(({ id, name, dateOfBirth, occupation, gender }) => ({
89
id,
910
name,
11+
gender,
1012
dateOfBirth,
1113
occupation,
1214
}));
1315
};
1416

15-
export default { getPatients };
17+
const addPatient = (patient: NewPatient): Patient => {
18+
const newPatient = {
19+
id: uuid(),
20+
...patient,
21+
};
22+
patients.push(newPatient);
23+
return newPatient;
24+
};
25+
26+
export default { getPatients, addPatient };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,13 @@ export interface Patient {
1010
dateOfBirth: string;
1111
ssn: string;
1212
occupation: string;
13+
gender: string;
1314
}
15+
16+
export enum Gender {
17+
MALE = "male",
18+
FEMALE = "female",
19+
OTHER = "other",
20+
}
21+
22+
export type NewPatient = Omit<Patient, "id">;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Gender, NewPatient } from "./types";
2+
3+
const isString = (value: unknown): value is string =>
4+
typeof value === "string" || value instanceof String;
5+
6+
const parseName = (name: unknown): string => {
7+
if (!name || !isString(name)) {
8+
throw new Error("Incorrect or missing name");
9+
}
10+
11+
return name;
12+
};
13+
14+
const parseSsn = (ssn: unknown): string => {
15+
if (!ssn || !isString(ssn)) {
16+
throw new Error("Incorrect or missing ssn");
17+
}
18+
19+
return ssn;
20+
};
21+
22+
const parseOccupation = (occupation: unknown): string => {
23+
if (!occupation || !isString(occupation)) {
24+
throw new Error("Incorrect or missing occupation");
25+
}
26+
27+
return occupation;
28+
};
29+
30+
const isDate = (date: string): boolean => {
31+
return Boolean(Date.parse(date));
32+
};
33+
34+
const parseDOB = (date: unknown): string => {
35+
if (!date || !isString(date) || !isDate(date)) {
36+
throw new Error("Incorrect or missing date: " + date);
37+
}
38+
return date;
39+
};
40+
41+
const isGender = (param: string): param is Gender =>
42+
["female", "male", "other"].includes(param);
43+
44+
const parseGender = (gender: unknown): Gender => {
45+
if (!gender || !isString(gender) || !isGender(gender)) {
46+
throw new Error("Incorrect or missing gender: " + gender);
47+
}
48+
return gender;
49+
};
50+
51+
type Fields = {
52+
id: unknown;
53+
name: unknown;
54+
dateOfBirth: unknown;
55+
ssn: unknown;
56+
occupation: unknown;
57+
gender: unknown;
58+
};
59+
60+
const toNewPatient = ({
61+
name,
62+
gender,
63+
dateOfBirth,
64+
ssn,
65+
occupation,
66+
}: Fields): NewPatient => {
67+
const newPatient: NewPatient = {
68+
name: parseName(name),
69+
gender: parseGender(gender),
70+
ssn: parseSsn(ssn),
71+
dateOfBirth: parseDOB(dateOfBirth),
72+
occupation: parseOccupation(occupation),
73+
};
74+
75+
return newPatient;
76+
};
77+
78+
export default toNewPatient;

part9/patientor-backend/yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@
134134
resolved "https://registry.yarnpkg.com/@types/strip-json-comments/-/strip-json-comments-0.0.30.tgz#9aa30c04db212a9a0649d6ae6fd50accc40748a1"
135135
integrity sha512-7NQmHra/JILCd1QqpSzl8+mJRc8ZHz3uDm8YV1Ks9IhK0epEiTw8aIErbvH9PI+6XbqhyIQy3462nEsn7UVzjQ==
136136

137+
"@types/uuid@^8.3.4":
138+
version "8.3.4"
139+
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.4.tgz#bd86a43617df0594787d38b735f55c805becf1bc"
140+
integrity sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==
141+
137142
"@typescript-eslint/eslint-plugin@^5.10.0":
138143
version "5.10.0"
139144
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.10.0.tgz#e90afea96dff8620892ad216b0e4ccdf8ee32d3a"
@@ -1441,6 +1446,11 @@ utils-merge@1.0.1:
14411446
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
14421447
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
14431448

1449+
uuid@^8.3.2:
1450+
version "8.3.2"
1451+
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
1452+
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
1453+
14441454
v8-compile-cache@^2.0.3:
14451455
version "2.3.0"
14461456
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz#2de19618c66dc247dcfb6f99338035d8245a2cee"

0 commit comments

Comments
 (0)