Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete user #6

Merged
merged 1 commit into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion cookenu/src/Routes/user.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UserDatabase } from "../data/UserDataBase";
import { Authenticator } from "../services/Authenticator";
import { CustomError } from "../Util/CustomError";
import moment from "moment";
import { RefreshTokenDataBase } from "../data/RefreshTokenDataBase";

const getProfileEndingPoint = async (request: Request, response: Response) => {
const token = request.headers.authorization as string;
Expand Down Expand Up @@ -99,7 +100,7 @@ const getFeeedEndpoint = async (request: Request, response: Response) => {
recipeDatabase.getRecipesByUserId(followingUsers[i].id_following),
]);

userRecipes.forEach((recipe) =>
userRecipes.forEach((recipe: Object) =>
recipes.push({ ...recipe, userName: user.name })
);
}
Expand All @@ -113,12 +114,34 @@ const getFeeedEndpoint = async (request: Request, response: Response) => {
response.status(200).send(recipes);
};

const deleteUserEndpoint = async (request: Request, response: Response) => {
const token = request.headers.authorization as string;
const userInfo = new Authenticator().getData(token);
const { id } = request.params;

const userDatabase = new UserDatabase();

if (userInfo.role !== "admin")
throw new CustomError("Usuário is not Admin", 402);

if (!(await userDatabase.getUserById(id)))
throw new CustomError("User does not exist", 400);

await new RefreshTokenDataBase().deleteUserRefreshToken(id);
await new FollowDatabase().deleteAllUserRelations(id);
await new RecipeDataBase().deleteAllRecipesFromUser(id);
await userDatabase.deleteUser(id);

response.status(200).send({ message: "User successfully deleted" });
};

const userRoute = Router();

userRoute.get("/profile", getProfileEndingPoint);
userRoute.get("/feed", getFeeedEndpoint);
userRoute.get("/:id", getOtherUserProfileEndpoint);
userRoute.post("/follow", followUserEndpoint);
userRoute.post("/unfollow", unfollowUserEndpoint);
userRoute.delete("/:id", deleteUserEndpoint);

export default userRoute;
8 changes: 8 additions & 0 deletions cookenu/src/data/FollowDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ export class FollowDatabase extends ServerDataBase {
.where({ id_follower })
.into(FollowDatabase.TABLE_NAME);
}

public async deleteAllUserRelations(id: string) {
await this.getConnection()
.into(FollowDatabase.TABLE_NAME)
.delete()
.where({ id_follower: id })
.orWhere({ id_following: id });
}
}
25 changes: 16 additions & 9 deletions cookenu/src/data/RecipeDataBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,27 @@ export class RecipeDataBase extends ServerDataBase {
.select("*")
.from(RecipeDataBase.TABLE_NAME)
.where({ user_id });
return recipe[0]
return recipe[0];
}

public async editRecipe (
recipe_id : string,
recipe_title : string,
recipe_description : string
){
public async editRecipe(
recipe_id: string,
recipe_title: string,
recipe_description: string
) {
await this.getConnection()
.from(RecipeDataBase.TABLE_NAME)
.where({id: recipe_id})
.where({ id: recipe_id })
.update({
title: recipe_title,
description: recipe_description
})
description: recipe_description,
});
}

public async deleteAllRecipesFromUser(user_id: string) {
this.getConnection()
.into(RecipeDataBase.TABLE_NAME)
.delete()
.where({ user_id });
}
}
131 changes: 65 additions & 66 deletions cookenu/src/data/RefreshTokenDataBase.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,75 @@
import { ServerDataBase } from "./ServerDataBase"
import { ServerDataBase } from "./ServerDataBase";

export class RefreshTokenDataBase extends ServerDataBase {
private static TABLE_NAME = "refresh_token";

export class RefreshTokenDataBase extends ServerDataBase{
public async storeRefreshToken(
token: string,
device: string,
isActive: boolean,
userId: string
): Promise<void> {
await this.getConnection()
.insert({
refresh_token: token,
device: device,
is_active: Number(isActive) === 1 ? true : false,
user_id: userId,
})
.into(RefreshTokenDataBase.TABLE_NAME);
}

private static TABLE_NAME = "refresh_token";
public async getRefreshToken(token: string): Promise<any> {
const tokenInfo = await this.getConnection()
.select("*")
.from(RefreshTokenDataBase.TABLE_NAME)
.where({
refresh_token: token,
});

public async storeRefreshToken(
token : string,
device : string,
isActive : boolean,
userId : string
) : Promise<void> {
const retrievedToken = tokenInfo[0][0];

await this
.getConnection()
.insert({
refresh_token: token,
device: device,
is_active: (Number(isActive) === 1 ? true : false),
user_id: userId
})
.into(RefreshTokenDataBase.TABLE_NAME)
}
return {
token: retrievedToken.refresh_token,
device: retrievedToken.device,
isActive: Number(retrievedToken.is_active) === 1 ? true : false,
userId: retrievedToken.user_id,
};
}

public async getRefreshToken( token : string ) : Promise<any>{
public async getRefreshTokenByIdAndDevice(
id: string,
device: string
): Promise<any> {
const tokenInfo = await this.getConnection()
.select("*")
.from(RefreshTokenDataBase.TABLE_NAME)
.where({
user_id: id,
device: device,
});

const tokenInfo = await this
.getConnection()
.select('*')
.from(RefreshTokenDataBase.TABLE_NAME)
.where({
refresh_token: token
})
const retrievedToken = tokenInfo[0];

const retrievedToken = tokenInfo[0][0]
return {
token: retrievedToken.refresh_token,
device: retrievedToken.device,
isActive: Number(retrievedToken.is_active) === 1 ? true : false,
userId: retrievedToken.user_id,
};
}

return{
token: retrievedToken.refresh_token,
device: retrievedToken.device,
isActive: (Number(retrievedToken.is_active) === 1 ? true : false),
userId: retrievedToken.user_id
}
}
public async deleteRefreshToken(token: string) {
await this.getConnection()
.del()
.from(RefreshTokenDataBase.TABLE_NAME)
.where({ refresh_token: token });
}

public async getRefreshTokenByIdAndDevice(
id: string,
device : string
) : Promise<any>{
const tokenInfo = await this
.getConnection()
.select('*')
.from(RefreshTokenDataBase.TABLE_NAME)
.where({
user_id: id,
device: device
})

const retrievedToken = tokenInfo[0]

return{
token: retrievedToken.refresh_token,
device: retrievedToken.device,
isActive: (Number(retrievedToken.is_active) === 1 ? true : false),
userId: retrievedToken.user_id
}
}

public async deleteRefreshToken( token : string ){
await this
.getConnection()
.del()
.from(RefreshTokenDataBase.TABLE_NAME)
.where({ refresh_token: token })
}
}
public async deleteUserRefreshToken(user_id: string) {
await this.getConnection()
.delete()
.where({ user_id })
.into(RefreshTokenDataBase.TABLE_NAME);
}
}