Skip to content

Commit

Permalink
feat(forum): Block delete forum if has children
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Feb 28, 2024
1 parent 956d79e commit 7871c14
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 76 deletions.
33 changes: 6 additions & 27 deletions backend/src/admin/forum/forums/delete/delete.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import { forum_topics } from "../../database/schema/topics";
export class DeleteForumForumsService {
constructor(private databaseService: DatabaseService) {}

async delete({
id,
move_forums_to,
move_topics_to
}: DeleteForumForumsArgs): Promise<string> {
async delete({ id, move_topics_to }: DeleteForumForumsArgs): Promise<string> {
const forum = await this.databaseService.db.query.forum_forums.findFirst({
where: (table, { eq }) => eq(table.id, id)
});
Expand All @@ -31,28 +27,11 @@ export class DeleteForumForumsService {
});

if (children.length > 0) {
if (!move_forums_to) {
throw new CustomError({
code: "FORUM_HAS_CHILDREN",
message: "Forum has children and no move_forums_to provided"
});
}

const moveForumsToForum =
await this.databaseService.db.query.forum_forums.findFirst({
where: (table, { eq }) => eq(table.id, move_forums_to)
});

if (!moveForumsToForum) {
throw new NotFoundError("Forum");
}

await this.databaseService.db
.update(forum_forums)
.set({
parent_id: move_forums_to
})
.where(eq(forum_forums.parent_id, id));
throw new CustomError({
code: "FORUM_HAS_CHILDREN",
message:
"Forum has children and cannot be deleted. Please delete children first."
});
}

const topics = await this.databaseService.db.query.forum_topics.findMany({
Expand Down
3 changes: 0 additions & 3 deletions backend/src/admin/forum/forums/delete/dto/delete.args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ export class DeleteForumForumsArgs {
@Field(() => Int)
id: number;

@Field(() => Int, { nullable: true })
move_forums_to: number | null;

@Field(() => Int, { nullable: true })
move_topics_to: number | null;
}
2 changes: 1 addition & 1 deletion backend/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type Mutation {
admin__core_themes__upload(file: Upload!): String!
admin__forum_forums__change_position(id: Int!, index_to_move: Int!, parent_id: Int): String!
admin__forum_forums__create(description: [TextLanguageInput!]!, name: [TextLanguageInput!]!, parent_id: Int, permissions: PermissionsCreateForumForums!): CreateForumForumsObj!
admin__forum_forums__delete(id: Int!, move_forums_to: Int, move_topics_to: Int): String!
admin__forum_forums__delete(id: Int!, move_topics_to: Int): String!
admin__forum_forums__edit(description: [TextLanguageInput!]!, id: Int!, name: [TextLanguageInput!]!, parent_id: Int, permissions: PermissionsCreateForumForums!): CreateForumForumsObj!
admin__install__create_database: String!
admin_sessions__sign_out: String!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,6 @@ export const ContentDeleteActionForumAdmin = ({
</AlertDialogDescription>
</AlertDialogHeader>

{_count.children > 0 && (
<FormField
control={form.control}
name="move_forums_to"
render={({ field }) => (
<FormItem>
<FormLabel>{t("move_forums_to")}</FormLabel>
<FormControl>
<ForumsSelect {...field} exclude={[id]} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
)}

{_count.topics > 0 && (
<FormField
control={form.control}
Expand Down Expand Up @@ -90,7 +74,6 @@ export const ContentDeleteActionForumAdmin = ({
type="submit"
disabled={
!form.formState.isValid ||
(_count.children > 0 && !form.watch("move_forums_to")) ||
(_count.topics > 0 && !form.watch("move_topics_to"))
}
loading={form.formState.isSubmitting}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export const DeleteActionForumAdmin = (
return (
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="destructiveGhost" size="icon" tooltip={t("delete")}>
<Button
variant="destructiveGhost"
size="icon"
tooltip={t("delete")}
disabled={props._count.children > 0}
>
<Trash2 />
</Button>
</AlertDialogTrigger>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ export const useDeleteForumAdmin = ({
const { convertText } = useTextLang();
const { setOpen } = useAlertDialog();
const formSchema = z.object({
move_forums_to: z
.object({
id: z.number().min(1),
name: zodInput.languageInput
})
.optional(),
move_topics_to: z
.object({
id: z.number().min(1),
Expand All @@ -40,7 +34,6 @@ export const useDeleteForumAdmin = ({
const onSubmit = async (values: z.infer<typeof formSchema>) => {
const mutation = await mutationApi({
id,
moveForumsTo: values.move_forums_to?.id,
moveTopicsTo: values.move_topics_to?.id
});
if (mutation.error) {
Expand Down
10 changes: 2 additions & 8 deletions frontend/graphql/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ export type MutationAdmin__Forum_Forums__CreateArgs = {

export type MutationAdmin__Forum_Forums__DeleteArgs = {
id: Scalars['Int']['input'];
move_forums_to?: InputMaybe<Scalars['Int']['input']>;
move_topics_to?: InputMaybe<Scalars['Int']['input']>;
};

Expand Down Expand Up @@ -1310,7 +1309,6 @@ export type Admin__Forum_Forums__CreateMutation = { __typename?: 'Mutation', adm

export type Admin__Forum_Forums__DeleteMutationVariables = Exact<{
id: Scalars['Int']['input'];
moveForumsTo?: InputMaybe<Scalars['Int']['input']>;
moveTopicsTo?: InputMaybe<Scalars['Int']['input']>;
}>;

Expand Down Expand Up @@ -1868,12 +1866,8 @@ export const Admin__Forum_Forums__Create = gql`
}
`;
export const Admin__Forum_Forums__Delete = gql`
mutation Admin__forum_forums__delete($id: Int!, $moveForumsTo: Int, $moveTopicsTo: Int) {
admin__forum_forums__delete(
id: $id
move_forums_to: $moveForumsTo
move_topics_to: $moveTopicsTo
)
mutation Admin__forum_forums__delete($id: Int!, $moveTopicsTo: Int) {
admin__forum_forums__delete(id: $id, move_topics_to: $moveTopicsTo)
}
`;
export const Admin__Forum_Forums__Edit = gql`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
mutation Admin__forum_forums__delete(
$id: Int!
$moveForumsTo: Int
$moveTopicsTo: Int
) {
admin__forum_forums__delete(
id: $id
move_forums_to: $moveForumsTo
move_topics_to: $moveTopicsTo
)
mutation Admin__forum_forums__delete($id: Int!, $moveTopicsTo: Int) {
admin__forum_forums__delete(id: $id, move_topics_to: $moveTopicsTo)
}
1 change: 0 additions & 1 deletion frontend/langs/en/forum.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
},
"delete": {
"desc": "Are you sure you want to delete <name></name> forum? This action cannot be undone.",
"move_forums_to": "Move children forums to",
"move_topics_to": "Move topics to",
"success": "Forum has been deleted.",
"submit": "Delete Forum"
Expand Down
1 change: 0 additions & 1 deletion frontend/langs/pl/forum.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
},
"delete": {
"desc": "Czy na pewno chcesz usunąć forum <name></name>? Ta akcja nie może być cofnięta.",
"move_forums_to": "Przenieś sub-fora do",
"move_topics_to": "Przenieś posty do",
"success": "Forum zostało usunięte.",
"submit": "Usuń forum"
Expand Down

0 comments on commit 7871c14

Please sign in to comment.