From a0ecb36064b57126343c8c5ecb49c7bbefa7b314 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Thu, 9 Jan 2020 18:20:10 +0100 Subject: [PATCH] Fix deletePost --- src/graphql/resolvers.ts | 7 ++++++- src/lib/dataAccess.ts | 11 ++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/graphql/resolvers.ts b/src/graphql/resolvers.ts index c6459e0..032edb8 100644 --- a/src/graphql/resolvers.ts +++ b/src/graphql/resolvers.ts @@ -258,7 +258,12 @@ export function resolver(req: any, res: any): any { }]}); const isAdmin = (await models.User.findOne({where: {id: req.session.userId}})).isAdmin; if (post.rAuthor.id === req.session.userId || isAdmin) { - return await dataaccess.deletePost(post.id); + try { + return await dataaccess.deletePost(post.id); + } catch (err) { + res.status(status.BAD_REQUEST); + return err.graphqlError ?? new GraphQLError(err.message); + } } else { res.status(status.FORBIDDEN); return new GraphQLError("User is not author of the post."); diff --git a/src/lib/dataAccess.ts b/src/lib/dataAccess.ts index 25ff897..48c0f60 100644 --- a/src/lib/dataAccess.ts +++ b/src/lib/dataAccess.ts @@ -197,10 +197,15 @@ namespace dataaccess { const post = await models.Post.findByPk(postId, {include: [{model: Activity}, {association: "rAuthor"}]}); const activity = await post.activity(); const author = await post.author(); - author.rankpoints -= activity.points; - await author.save(); + if (activity && author) { + author.rankpoints -= activity.points; + await author.save(); + } + await post.destroy(); } catch (err) { - return new PostNotFoundGqlError(postId); + globals.logger.error(err.message); + globals.logger.debug(err.stack); + throw new PostNotFoundGqlError(postId); } return true; }