Skip to content

Commit

Permalink
Merge branch 'lebihae/1025' into 'main'
Browse files Browse the repository at this point in the history
fix(posts): make delete button actually do something (closes #1025)

Closes #1025

See merge request churros/churros!283
  • Loading branch information
LeGmask committed Nov 19, 2024
2 parents 13f22da + bba3e47 commit ab7a9a9
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 590 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-hairs-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@churros/app': major
---

Remove /posts/:group/create - the URL was not used anywhere in the UI anymore
5 changes: 5 additions & 0 deletions .changeset/fifty-cars-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@churros/api': major
---

change return type and argument type for Mutation.deleteArticle
5 changes: 5 additions & 0 deletions .changeset/odd-horses-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@churros/app': patch
---

make delete button on posts work
43 changes: 20 additions & 23 deletions packages/api/src/modules/posts/resolvers/mutation.delete-article.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import { builder, log, prisma, publish } from '#lib';
import { builder, ensureGlobalId, log, prisma, publish } from '#lib';
import { LocalID } from '#modules/global';
import { ArticleType, canEditArticle } from '#modules/posts';

builder.mutationField('deleteArticle', (t) =>
t.field({
type: 'Boolean',
args: { id: t.arg.id() },
t.prismaField({
type: ArticleType,
errors: {},
description: 'Supprimer un post',
args: { id: t.arg({ type: LocalID }) },
async authScopes(_, { id }, { user }) {
if (!user) return false;
if (user.canEditGroups) return true;

const article = await prisma.article.findUniqueOrThrow({ where: { id } });

// Who can delete this article?
return (
// Admins
user.admin ||
// The author
user.id === article.authorId ||
// Other authors of the group
user.groups.some(
({ groupId, canEditArticles }) => canEditArticles && groupId === article.groupId,
)
);
const post = await prisma.article.findUniqueOrThrow({
where: { id: ensureGlobalId(id, 'Article') },
include: canEditArticle.prismaIncludes,
});
return canEditArticle(post, { authorId: null, group: null }, user);
},
async resolve(_, { id }, { user }) {
await prisma.article.delete({ where: { id } });
async resolve(query, _, { id }, { user }) {
id = ensureGlobalId(id, 'Article');

await log('article', 'delete', { message: `Article ${id} deleted` }, id, user);

const result = await prisma.article.delete({ ...query, where: { id } });

publish(id, 'deleted', id);
return true;

return result;
},
}),
);
11 changes: 10 additions & 1 deletion packages/app/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2371,7 +2371,10 @@ type Mutation @rateLimit(limit: 1200, duration: 600) {
"""
deduplicateBookings(event: LocalID!): MutationDeduplicateBookingsResult!
deleteAnnouncement(id: ID!): Boolean!
deleteArticle(id: ID!): Boolean!
"""
Supprimer un post
"""
deleteArticle(id: LocalID!): MutationDeleteArticleResult!
deleteArticlePicture(id: ID!): Boolean!
deleteComment(id: ID!): Comment!
"""
Expand Down Expand Up @@ -3343,6 +3346,12 @@ type MutationDeduplicateBookingsSuccess {
data: Int!
}

union MutationDeleteArticleResult = Error | MutationDeleteArticleSuccess | ZodError

type MutationDeleteArticleSuccess {
data: Article!
}

union MutationDeleteContributionResult = Error | MutationDeleteContributionSuccess | ZodError

type MutationDeleteContributionSuccess {
Expand Down
4 changes: 0 additions & 4 deletions packages/app/src/lib/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,6 @@ const PAGES = {
'/logout': `/logout`,
'/logs': `/logs`,
'/notifications': `/notifications`,
'/posts/create': (params?: { group?: string | number }) => {
return `/posts${params?.group ? `/${params?.group}` : ''}/create`;
},
'/posts/[id]': (id: string | number, params?: {}) => {
return `/posts/${id}`;
},
Expand Down Expand Up @@ -579,7 +576,6 @@ export type KIT_ROUTES = {
'/logout': never;
'/logs': never;
'/notifications': never;
'/posts/create': 'group';
'/posts/[id]': 'id';
'/posts/[id]/edit': 'id';
'/posts/[id]/edit/body': 'id';
Expand Down
Loading

0 comments on commit ab7a9a9

Please sign in to comment.