Skip to content

Commit

Permalink
feat: added a route to delete al lof user data
Browse files Browse the repository at this point in the history
  • Loading branch information
wajeht committed Sep 9, 2022
1 parent ada8f20 commit c160e80
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/apps/api/v1/users/users.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ export async function patchUpdateAccountInformation(req, res) {
});
}

/**
* It updates the profile picture of a user
* @param req - The request object.
* @param res - The response object.
* @param next - The next middleware function in the stack.
*/
export async function postUpdateProfilePicture(req, res, next) {
const { path: profile_picture_path } = req.file;
const profile_picture_url = req.file.path.split('public')[1];
Expand All @@ -178,3 +184,24 @@ export async function postUpdateProfilePicture(req, res, next) {
data: updated,
});
}

/**
* It deletes all of the user's data from the database.
* @param req - The request object.
* @param res - The response object.
*/
export async function postDeleteUserData(req, res) {
const { user_id } = req.params;
const deleted = await UsersQueries.deleteUserData(user_id);

logger.info(
`User id ${user_id} has deleted all of comments, videos, variables, sets, logs, sessions and blocks`,
);

res.status(StatusCodes.OK).json({
status: 'success',
request_url: req.originalUrl,
message: 'The resource was updated successfully!',
data: deleted,
});
}
22 changes: 22 additions & 0 deletions src/apps/api/v1/users/users.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,32 @@ export function updateAccountInformation(id, body) {
.returning('*');
}

/**
* Update the profile picture path and url in the user_details table where the user_id matches the
* user_id passed in
* @returns The updated user_details row.
*/
export function updateProfilePicture({ profile_picture_url, profile_picture_path, user_id }) {
return db
.update({ profile_picture_path, profile_picture_url })
.from('user_details')
.where({ user_id })
.returning('*');
}

/**
* It deletes all the data associated with a user
* @param user_id - The user's id
* @returns An array of promises.
*/
export function deleteUserData(user_id) {
return Promise.all([
db.update({ deleted: true }).from('comments').where({ user_id }).returning('*'),
db.update({ deleted: true }).from('videos').where({ user_id }).returning('*'),
db.update({ deleted: true }).from('variables').where({ user_id }).returning('*'),
db.update({ deleted: true }).from('sets').where({ user_id }).returning('*'),
db.update({ deleted: true }).from('logs').where({ user_id }).returning('*'),
db.update({ deleted: true }).from('sessions').where({ user_id }).returning('*'),
db.update({ deleted: true }).from('blocks').where({ user_id }).returning('*'),
]);
}
12 changes: 12 additions & 0 deletions src/apps/api/v1/users/users.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ users.patch(
catchAsyncErrors(UsersController.patchUpdateAccountInformation),
);

/**
* DELETE /api/v1/users/{user_id}/data
* @tags users
* @summary delete a user data from the database
* @param {number} user_id.path.required - user id
*/
users.delete(
'/:user_id/data',
validator(UsersValidation.postDeleteUserData),
catchAsyncErrors(UsersController.postDeleteUserData),
);

/**
* DELETE /api/v1/users/{id}
* @tags users
Expand Down
16 changes: 16 additions & 0 deletions src/apps/api/v1/users/users.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,19 @@ export const postUpdateProfilePicture = [
return true;
}),
];

export const postDeleteUserData = [
param('user_id')
.trim()
.notEmpty()
.withMessage('The user_id must not be empty!')
.bail()
.isInt()
.withMessage('The user_id must be an ID!')
.bail()
.custom(async (value) => {
const user = await UserQueries.findUserById(value);
if (user.length === 0) throw new Error('User does not exist!');
})
.toInt(),
];

0 comments on commit c160e80

Please sign in to comment.