Skip to content

Commit

Permalink
update : adaptation for new organisation in BDD
Browse files Browse the repository at this point in the history
  • Loading branch information
frapuks committed Feb 23, 2023
1 parent bfa88c5 commit 9c58f67
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 35 deletions.
19 changes: 4 additions & 15 deletions app/controllers/familyController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { familyDatamapper } from "../datamappers/index.js";
import { userDatamapper } from "../datamappers/index.js";
import jwt from 'jsonwebtoken';

const familyController = {
async create(req, res) {
Expand All @@ -17,20 +15,11 @@ const familyController = {
if(!family) throw new Error('Impossible to create family');

// link user & family
const link = await familyDatamapper.createLink(user.id, family.id);
const isParent = true;
const link = await familyDatamapper.createLink(user.id, family.id, isParent);
if(!link) throw new Error('Impossible to create link');

// update role to "parent"
const roleIdParent = 2;
const userUpdated = await userDatamapper.updateRole(user.id, roleIdParent);

// save update in session
req.session.user = userUpdated;

// generate new token
const token = jwt.sign(userUpdated, process.env.SESSION_SECRET, {expiresIn: '7 days'});

return res.json({family, "user": userUpdated, token});
return res.json(family);
} catch (error) {
return res.status(500).json(error.message);
}
Expand All @@ -46,7 +35,7 @@ const familyController = {
if(!family) throw new Error(`Cannot get family with id = ${id}`);

// verif if link exist
const link = family.members.find(member => member.id === user.id);
const link = family.members?.find(member => member.id === user.id);
if(!link) throw new Error(`Access Denied`);

return res.json(family);
Expand Down
10 changes: 5 additions & 5 deletions app/datamappers/familyDatamapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const familyDatamapper = {
'lastname', lastname,
'pseudo', pseudo,
'email', email,
'role_id', role_id,
'isParent', "isParent",
'credit', credit
)
) AS members
Expand All @@ -70,12 +70,12 @@ const familyDatamapper = {
return result.rows[0];
},

async createLink(userId, familyId){
async createLink(userId, familyId, isParent){
const sql = `
INSERT INTO user_has_family (user_id, family_id)
VALUES ($1, $2)
INSERT INTO user_has_family (user_id, family_id, "isParent")
VALUES ($1, $2, $3)
RETURNING *;`
const values = [userId, familyId];
const values = [userId, familyId, isParent];
const result = await client.query(sql, values);
return result.rows[0];
},
Expand Down
7 changes: 4 additions & 3 deletions app/datamappers/userDatamapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ const userDatamapper = {

async findById(id){
const sql = `
SELECT id, firstname, lastname, pseudo, email, role_id, (
SELECT id, firstname, lastname, pseudo, email, (
SELECT json_agg(
json_build_object(
'id', id,
'name', name,
'credit', credit
'credit', credit,
'isParent', "isParent"
)
) AS families
FROM family
Expand All @@ -40,7 +41,7 @@ const userDatamapper = {
const sql = `
INSERT INTO "user"(firstname, lastname, pseudo, email, password)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, firstname, lastname, pseudo, email, role_id;`
RETURNING id, firstname, lastname, pseudo, email;`
const values = [form.firstname, form.lastname, form.pseudo, form.email, form.password];
const result = await client.query(sql, values);
return result.rows[0];
Expand Down
12 changes: 6 additions & 6 deletions data/seed_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ INSERT INTO "role"(label) VALUES
('child');

INSERT INTO "user"(firstname, lastname, pseudo, email, password, role_id) VALUES
('François', 'GRUNERT', 'Frapuks', '[email protected]', 'adrienLeBg', 3),
('Adrien', 'BLANC', 'AdriBG', '[email protected]', 'JeSuisLePlusBeau', 2),
('Adrien', 'Haggani', 'AdriH', '[email protected]', 'FautQueJeVousRaconteUnTruc', 3),
('Charlotte', 'Rose', 'Zerossama', '[email protected]', 'OclockOuaisOuais', 2),
('Marwane', 'Ben Tekaya', 'Marwenn', '[email protected]', '1234', 3),
('Benjamin', 'Nougadère', 'Benjam', '[email protected]', 'QuandJeBossaisChezMicrosoft', 1);
('François', 'GRUNERT', 'Frapuks', '[email protected]', '$2b$10$SakdfubdCIaWkkC5iunBqek39tW7ZDecTsoQNXfUxViKJ5K9FRo6C', 3),
('Adrien', 'BLANC', 'AdriBG', '[email protected]', '$2b$10$.Ger.B0cbGvX87B2desn.elNVYHrcT2ZmzOllSl6fbNOYT.zYLIVG', 2),
('Adrien', 'Haggani', 'AdriH', '[email protected]', '$2b$10$4hCI/G/YLkEyrnJ7C3PsTOZz2bNG9uuE1JB.HL4ApC5CP/3ro3A6a', 3),
('Charlotte', 'Rose', 'Zerossama', '[email protected]', '$2b$10$tquH8BH.o3zuZMkeGQp1UesFJrcFLgkt/UE5Xvu92ZT2EZ8oZ.nKW', 2),
('Marwane', 'Ben Tekaya', 'Marwenn', '[email protected]', '$2b$10$VjDhncJ6nHx3bq3Tk.3ruO3ISYpk23X5ptXuxLD29eeToyLPvxalC', 3),
('Benjamin', 'Nougadère', 'Benjam', '[email protected]', '$2b$10$S9lILcczF9Hy6GyzQoYNJO8trCZkQgG8WL473Zwz.qIGr/xvZWiy.', 1);

INSERT INTO "family"(name) VALUES
('Moaï'),
Expand Down
4 changes: 2 additions & 2 deletions migration/deploy/3.move_table_role.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
BEGIN;

ALTER TABLE user_has_family
ADD COLUMN isParent BOOLEAN NOT NULL DEFAULT false;
ADD COLUMN "isParent" BOOLEAN NOT NULL DEFAULT false;

UPDATE user_has_family
SET isParent = true
SET "isParent" = true
WHERE user_id IN (
SELECT id
FROM "user"
Expand Down
6 changes: 3 additions & 3 deletions migration/revert/3.move_table_role.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ SET "role_id" = 3
WHERE id IN (
SELECT user_id
FROM user_has_family
WHERE isParent = false
WHERE "isParent" = false
);

UPDATE "user"
SET "role_id" = 2
WHERE id IN (
SELECT user_id
FROM user_has_family
WHERE isParent = true
WHERE "isParent" = true
);

ALTER TABLE user_has_family
DROP COLUMN isParent;
DROP COLUMN "isParent";

COMMIT;
2 changes: 1 addition & 1 deletion migration/verify/3.move_table_role.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

BEGIN;

SELECT isParent
SELECT "isParent"
FROM user_has_family;

ROLLBACK;

0 comments on commit 9c58f67

Please sign in to comment.