Skip to content
This repository was archived by the owner on Sep 19, 2019. It is now read-only.

Commit

Permalink
Clean up of database structor
Browse files Browse the repository at this point in the history
  • Loading branch information
7imbrook committed Feb 12, 2016
1 parent 8bdc9a8 commit c6d114a
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.DS_Store
docker-compose.override.yml
5 changes: 4 additions & 1 deletion app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ COPY ./ /app

RUN npm install
RUN npm run build
RUN ls dist
RUN mkdir -p /usr/share/nginx/html
RUN cp -avr ./dist/ /usr/share/nginx/html/

CMD /bin/bash -c "exit 0;"
CMD /bin/bash -c "while true; do sleep 60; done;"
5 changes: 4 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ gateway:
ports:
- "80:80"
volumes:
- ./app/dist:/usr/share/nginx/html
- ./app/dist:/usr/share/nginx/html:ro
links:
- pgapi
- server
Expand All @@ -30,6 +30,9 @@ db:

server:
build: ./server
environment:
- MAILTOKEN=''
- SERVERURL=192.168.99.100
links:
- cache

Expand Down
Binary file added erm.mwb
Binary file not shown.
Binary file added erm.mwb.bak
Binary file not shown.
3 changes: 3 additions & 0 deletions server/mailer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ module.exports = {
to, message: message.toString('ascii')
};
mailer.messages().sendMime(dataToSend, function (sendError, body) {
if (sendError) {
console.error(sendError);
}
console.log(body);
});
});
Expand Down
12 changes: 12 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ router.options('/v1/request', (req, res) => {
res.send(schemas.request);
});


/**
{
"project": 1,
"part": "A new part",
"quantity": 1,
"unit_price": 0.50,
"shipping": 1.23,
"link": "https://google.com",
"purpose": "The Internet"
}
*/
router.post('/v1/request', (req, res, next) => {
let r = validator.validate(req.body, schemas.request);
if (r.errors.length > 0) {
Expand Down
25 changes: 16 additions & 9 deletions server/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ const pool = pg.Pool('postgres://admin:alpine@192.168.99.100/projects');
module.exports = {
createRequest (r) {

let activeUserId = 1;

return pool.query(sql`
WITH
approvers AS (
SELECT approver_id as id FROM defaultapprovers WHERE project_id=${r.project} LIMIT 1
),
approval AS (
INSERT INTO approval_status(approved_by) VALUES ((select id from approvers)) RETURNING id
) INSERT INTO requests(project, requester, part, unit_price, quantity, link, purpose, approval)
VALUES (${r.project}, 1, ${r.part}, ${r.unit_price}, ${r.quantity}, ${r.link}, ${r.purpose}, (select id from approval))
purchase AS (
INSERT INTO purchase(part, unit_price, shipping, quantity, link, purpose)
VALUES (${r.part}, ${r.unit_price}, ${r.shipping}, ${r.quantity}, ${r.link}, ${r.purpose})
RETURNING id
)
INSERT INTO requests(project, purchase, requester)
VALUES (${r.project}, (SELECT id FROM purchase), ${activeUserId})
RETURNING id;
`)
.then(data => {
Expand All @@ -33,12 +35,17 @@ module.exports = {
});
},
getRequest(id) {
return pool.query(sql`select requests.part, projects.title from requests join projects on requests.project=projects.id where requests.id=${id}`)
return pool.query(sql`
select purchase.part, projects.name
from requests
join projects on requests.project=projects.id
join purchase on requests.purchase=purchase.id
where requests.id=${id}`)
.then(data => data.rows[0]);
},
approve(id) {
return pool.query(sql`
UPDATE approval_status SET approved=TRUE WHERE id=${id}
UPDATE requests SET approved=TRUE WHERE id=${id}
`);
}
}
3 changes: 2 additions & 1 deletion server/utils/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ module.exports = {
part: {"type": "string"},
quantity: {"type": "integer", "minimum": 1},
unit_price: {"type": "number", "minimum": 0.01},
shipping: {"type": "number", "minimum": 0.01},
link: {"type": "string"},
purpose: {"type": "string"}
},
required: ["project", "part", "quantity", "unit_price", "link", "purpose"]
required: ["project", "part", "quantity", "unit_price", "link", "purpose", "shipping"]
}
};
54 changes: 14 additions & 40 deletions services/postgrest/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,42 @@ BEGIN;
CREATE TABLE accounts
(
id serial PRIMARY KEY,
prefered_name varchar(80),
email varchar(80),
name varchar(80),
email varchar(80) check ( email ~* '^.+@.+\..+$' ),
created date NOT NULL default CURRENT_DATE
);

CREATE TABLE projects
(
id serial PRIMARY KEY,
title varchar(80),
name varchar(80),
owner integer REFERENCES accounts,
approver integer REFERENCES accounts,
created date NOT NULL default CURRENT_DATE
);

CREATE TABLE account_manager_for_project
(
projectid integer,
accountid integer,
id serial PRIMARY KEY,
CONSTRAINT account_manager_for_project_accountid_fkey FOREIGN KEY (accountid)
REFERENCES public.accounts (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT account_manager_for_project_projectid_fkey FOREIGN KEY (projectid)
REFERENCES public.projects (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);

CREATE TABLE approval_status
(
id serial PRIMARY KEY,
approved boolean default FALSE,
approved_by integer REFERENCES accounts
);

CREATE TABLE purchase
(
id serial PRIMARY KEY,
purchaser integer REFERENCES accounts,
approved boolean default FALSE,
tracking_number varchar(40),
additional_info TEXT
);

CREATE TABLE requests
(
id serial PRIMARY KEY,
project integer REFERENCES projects,
requester integer REFERENCES accounts,
part varchar(80),
unit_price money,
shipping money,
quantity integer,
link varchar(255),
purpose TEXT,
approval integer REFERENCES approval_status,
purchase int, -- Nullable referance to a purchase
tracking_number varchar(40),
additional_info TEXT,
created date NOT NULL default CURRENT_DATE
);

CREATE TABLE notifications
CREATE TABLE requests
(
id serial PRIMARY KEY,
account integer REFERENCES accounts,
request integer REFERENCES requests,
ack BOOLEAN,
project integer REFERENCES projects,
purchase integer REFERENCES purchase,
purchaser integer REFERENCES accounts,
requester integer REFERENCES accounts,
approved boolean default FALSE,
created date NOT NULL default CURRENT_DATE
);

Expand Down
12 changes: 12 additions & 0 deletions services/postgrest/seeds.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
-- Make things in the projects DB
\c projects

BEGIN;

-- Create default accounts
INSERT INTO accounts (name, email) VALUES ('Timbrook', 'timbrook480@gmail.com');
INSERT INTO accounts (name, email) VALUES ('Jesse', 'jrj2307@rit.edu');

-- Create default projects
INSERT INTO projects (name, owner, approver) VALUES ('Holo Desk', 2, 1);


COMMIT;
32 changes: 9 additions & 23 deletions services/postgrest/views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,18 @@ BEGIN;
CREATE VIEW purchaselist AS
select
requests.id,
projects.title as project,
part,
link,
(unit_price * quantity) as price, -- TODO: make total
quantity,
purpose,
accounts.prefered_name as requester,
purchase.tracking_number as tracking,
purchase.approved as purchased,
requests.created as lastModified,
approval_status.approved
projects.name as project,
purchase.part,
(purchase.unit_price * purchase.quantity + purchase.shipping) as price,
requests.approved,
accounts.name as requester,
purchase.quantity,
purchase.link,
purchase.purpose
from requests
join projects on requests.project=projects.id
join purchase on requests.purchase=purchase.id
join accounts on requests.requester=accounts.id
join approval_status on requests.approval=approval_status.id
left join purchase on requests.purchase=purchase.id
;

CREATE VIEW defaultapprovers AS
select
accounts.id as approver_id,
projects.id as project_id
from account_manager_for_project
join accounts on
accounts.id=account_manager_for_project.accountid
join projects on
projects.id=account_manager_for_project.projectid
;
COMMIT;

0 comments on commit c6d114a

Please sign in to comment.