Skip to content

Commit

Permalink
feature(Delete user and delete all its data):Delete user and delete a…
Browse files Browse the repository at this point in the history
…ll its data

Delete user and its data
[Starts #165652955]
  • Loading branch information
Cavdy committed Apr 27, 2019
1 parent 04982bf commit bb6c722
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 46 deletions.
88 changes: 46 additions & 42 deletions server/v1/helper/statusHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,52 @@ const statusHelper = {
* @param {*} data - passed in success data
*/
async statusHelper(req, res, status, error, data) {
if (status === 401) { // unauthorized
res.status(401);
return res.json({
status: 401,
data: error,
});
} else if (status === 409) { // conflict
res.status(409);
return res.json({
status: 409,
data: error,
});
} else if (status === 403) { // conflict
res.status(403);
return res.json({
status: 403,
data: error,
});
} else if (status === 201) { // created
res.status(201);
return res.json({
status: 201,
data,
});
} else if (status === 200) { // success
res.status(200);
return res.json({
status: 200,
data,
});
} else if (status === 404) { // not found
res.status(404);
return res.json({
status: 404,
data: error,
});
} else if (status === 422) {
res.status(422);
return res.json({ // unprocessable entity
status: 422,
data: error,
});
switch (status) {
case 401: // unauthorized
res.status(401);
return res.json({
status: 401,
data: error,
});
case 409: // conflict
res.status(409);
return res.json({
status: 409,
data: error,
});
case 403: // forbidden
res.status(403);
return res.json({
status: 403,
data: error,
});
case 201: // created
res.status(201);
return res.json({
status: 201,
data,
});
case 200: // success
res.status(200);
return res.json({
status: 200,
data,
});
case 404: // not found
res.status(404);
return res.json({
status: 404,
data: error,
});
case 422: // unprocessable entity
res.status(422);
return res.json({
status: 422,
data: error,
});
default:
// do nothing
break;
}
},
};
Expand Down
30 changes: 26 additions & 4 deletions server/v1/services/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,22 @@ const UsersServices = {
[staff.email]);
const { type, isadmin } = userDetails.rows[0];

if (type === 'staff') {
if (type === 'staff') { // checks if staff
const checkusers = await dbConnection
.dbConnect('SELECT type FROM users WHERE id=$1', [id]);
.dbConnect('SELECT email, type FROM users WHERE id=$1', [id]);
if (checkusers.rows.length > 0) {
if (checkusers.rows[0].type === 'client') {
if (checkusers.rows[0].type === 'client') { // check if client
const delTransactions = await dbConnection
.dbConnect('SELECT * from accounts WHERE email=$1',
[checkusers.rows[0].email]); // loop through db for accounts
delTransactions.rows.map(async (del) => {
await dbConnection
.dbConnect('DELETE FROM transactions WHERE accountnumber=$1',
[del.accountnumber]);
await dbConnection
.dbConnect('DELETE FROM accounts WHERE accountnumber=$1',
[del.accountnumber]);
});
const accountDbData = await dbConnection
.dbConnect('DELETE FROM users WHERE id=$1', [id]);
if (accountDbData.command === 'DELETE') {
Expand All @@ -106,8 +117,19 @@ const UsersServices = {
}
} else if (isadmin === true) {
const checkusers = await dbConnection
.dbConnect('SELECT type FROM users WHERE id=$1', [id]);
.dbConnect('SELECT email, type FROM users WHERE id=$1', [id]);
if (checkusers.rows.length > 0) {
const delTransactions = await dbConnection
.dbConnect('SELECT * from accounts WHERE email=$1',
[checkusers.rows[0].email]);
delTransactions.rows.map(async (del) => {
await dbConnection
.dbConnect('DELETE FROM transactions WHERE accountnumber=$1',
[del.accountnumber]);
await dbConnection
.dbConnect('DELETE FROM accounts WHERE accountnumber=$1',
[del.accountnumber]);
});
const accountDbData = await dbConnection
.dbConnect('DELETE FROM users WHERE id=$1', [id]);
if (accountDbData.command === 'DELETE') {
Expand Down

0 comments on commit bb6c722

Please sign in to comment.