diff --git a/server/Entity relations.drawio b/server/Entity relations.drawio index 605d3dd..caf84c3 100644 --- a/server/Entity relations.drawio +++ b/server/Entity relations.drawio @@ -1,484 +1,488 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/server/src/migrations/index.ts b/server/src/migrations/index.ts index b753201..1145b0f 100644 --- a/server/src/migrations/index.ts +++ b/server/src/migrations/index.ts @@ -4,16 +4,20 @@ dotenv.config(); import { client } from "../db"; client.query(` +DROP TABLE IF EXISTS public.orders; +DROP TABLE IF EXISTS public.reserved_timeslots; +DROP TABLE IF EXISTS public.weekly_timeslots; DROP TABLE IF EXISTS public.trainers; DROP TABLE IF EXISTS public.centers; +DROP TABLE IF EXISTS public.admins; DROP TABLE IF EXISTS public.users; -DROP TYPE IF EXISTS public."UserType"; +DROP TYPE IF EXISTS public."OrderStatus"; -CREATE TYPE public."UserType" AS ENUM +CREATE TYPE public."OrderStatus" AS ENUM ( - 'User', - 'Trainer', - 'Admin' + 'Processing', + 'Payed', + 'Cancelled' ); CREATE TABLE public.users @@ -23,10 +27,14 @@ CREATE TABLE public.users last_name text NOT NULL, email text NOT NULL UNIQUE, password_hash text NOT NULL, - user_type "UserType" NOT NULL DEFAULT 'User', email_verified boolean NOT NULL DEFAULT false ); +CREATE TABLE public.admins +( + user_id int REFERENCES users(id) NOT NULL PRIMARY KEY +); + CREATE TABLE public.centers ( id serial NOT NULL PRIMARY KEY, @@ -43,6 +51,33 @@ CREATE TABLE public.trainers center_id int REFERENCES centers(id) NOT NULL, hourly_price int NOT NULL ); + +CREATE TABLE public.weekly_timeslots +( + id serial NOT NULL PRIMARY KEY, + trainer_id int REFERENCES trainers(id) NOT NULL, + day_of_week int NOT NULL, + start_time time NOT NULL, + end_time time NOT NULL +); + +CREATE TABLE public.reserved_timeslots +( + id serial NOT NULL PRIMARY KEY, + trainer_id int REFERENCES trainers(id) NOT NULL, + start_time timestamp NOT NULL, + end_time timestamp NOT NULL +); + +CREATE TABLE public.orders +( + id serial NOT NULL PRIMARY KEY, + timeslot_id int REFERENCES reserved_timeslots(id) NOT NULL, + user_id int REFERENCES users(id) NOT NULL, + order_status "OrderStatus" NOT NULL DEFAULT 'Processing', + price int NOT NULL, + create_at timestamp NOT NULL DEFAULT NOW() +); `) .then(()=>{ return client.end(); diff --git a/server/src/migrations/populate.ts b/server/src/migrations/populate.ts index 044a83a..cf7326c 100644 --- a/server/src/migrations/populate.ts +++ b/server/src/migrations/populate.ts @@ -7,16 +7,26 @@ async function main() { const users = await client.query(` -INSERT INTO users (first_name, last_name, email, password_hash, user_type) VALUES -('Filip', 'B P', 'fbp@example.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'Admin'), -('User1', 'Lastname', 'u1@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'User'), -('User2', 'Lastname', 'u2@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'User'), -('Trainer1', 'Lastname', 't1@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'Trainer'), -('Trainer2', 'Lastname', 't2@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'Trainer'), -('Admin1', 'Lastname', 'a1@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'Admin') +INSERT INTO users (first_name, last_name, email, password_hash, email_verified) VALUES +('Filip', 'B P', 'fbp@example.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', true), +('User1', 'Lastname', 'u1@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', true), +('User2', 'Lastname', 'u2@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', true), +('User3', 'Lastname', 'u3@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', true), +('Trainer1', 'Lastname', 't1@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', true), +('Trainer2', 'Lastname', 't2@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', true), +('Admin1', 'Lastname', 'a1@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', true) RETURNING id, email; `); + const admins = await client.query(` +INSERT INTO admins (user_id) VALUES +($1), +($2); + `, [ + users.rows.find(user => user.email === "a1@test.com").id, + users.rows.find(user => user.email === "fbp@example.com").id + ]); + const centers = await client.query(` INSERT INTO centers (name, city, zip_code, address) VALUES ('Herning Fitness', 'Herning', '7400', 'Vej 123'), @@ -36,6 +46,51 @@ RETURNING id, user_id; centers.rows[1].id, ]); + const weekly_timeslots = await client.query(` +INSERT INTO weekly_timeslots (trainer_id, day_of_week, start_time, end_time) VALUES +($1, 0, '11:00:00', '12:00:00'), +($1, 1, '10:00:00', '13:00:00'), +($1, 2, '10:00:00', '15:00:00'), +($2, 0, '10:00:00', '11:00:00'), +($2, 0, '11:00:00', '12:00:00'), +($2, 0, '12:00:00', '13:00:00') +RETURNING id; + `, [ + trainers.rows[0].id, + trainers.rows[1].id + ]); + + const reserved_timeslots = await client.query(` +INSERT INTO reserved_timeslots (trainer_id, start_time, end_time) VALUES +($1, '2023-04-17 11:00:00+2', '2023-04-17 12:00:00+2'), +($1, '2023-04-18 12:00:00+2', '2023-04-17 13:00:00+2'), +($1, '2023-04-24 11:00:00+2', '2023-04-24 12:00:00+2'), +($2, '2023-04-17 11:00:00+2', '2023-04-17 12:00:00+2'), +($2, '2023-04-10 12:00:00+2', '2023-04-10 13:00:00+2') +RETURNING id, trainer_id; + `, [ + trainers.rows[0].id, + trainers.rows[1].id + ]); + + const orders = await client.query(` +INSERT INTO orders (timeslot_id, user_id, order_status, price) VALUES +($3, $1, 'Cancelled', 10000), +($4, $1, 'Payed', 20000), +($5, $2, 'Processing', 20000), +($6, $1, 'Payed', 20000), +($7, $1, 'Payed', 20000) +RETURNING id; + `, [ + users.rows.find(user => user.email === "u1@test.com").id, + users.rows.find(user => user.email === "u2@test.com").id, + reserved_timeslots.rows[0].id, + reserved_timeslots.rows[1].id, + reserved_timeslots.rows[2].id, + reserved_timeslots.rows[3].id, + reserved_timeslots.rows[4].id, + ]); + client.end(); }