Skip to content

Commit

Permalink
fix(frontend): Revalidate date when edit admin permissions in AdminCP
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Oct 16, 2024
1 parent 19477e9 commit bbd76fe
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 164 deletions.
2 changes: 1 addition & 1 deletion apps/backend/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ type Mutation {
admin__core_plugins__permissions_admin__delete(id: String!, parent_id: String, plugin_code: String!): String!
admin__core_plugins__upload(code: String, file: Upload!): String!
admin__core_security__captcha__edit(secret_key: String!, site_key: String!, type: CaptchaTypeEnum!): ShowAdminCaptchaSecurityObj!
admin__core_staff_administrators__create_edit(group_id: Int, permissions: [PermissionsStaffArgs!], user_id: Int): ShowAdminStaffAdministrators!
admin__core_staff_administrators__create_edit(group_id: Int, id: Int, permissions: [PermissionsStaffArgs!], user_id: Int): ShowAdminStaffAdministrators!
admin__core_staff_administrators__delete(id: Int!): String!
admin__core_staff_moderators__create(group_id: Int, user_id: Int): ShowAdminStaffModerators!
admin__core_staff_moderators__delete(id: Int!): String!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export class CreateEditAdminStaffAdministratorsArgs {
@Field(() => Int, { nullable: true })
group_id: null | number;

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

@Field(() => [PermissionsStaffArgs], { nullable: true })
permissions: PermissionsStaffArgs[];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { core_admin_permissions } from '@/database/schema/admins';
import { CustomError, NotFoundError } from '@/errors';
import { CustomError, InternalServerError, NotFoundError } from '@/errors';
import { getUser } from '@/utils/database/helpers/get-user';
import { InternalDatabaseService } from '@/utils/database/internal_database.service';
import { Injectable } from '@nestjs/common';
import { eq } from 'drizzle-orm';

import { ShowAdminStaffAdministrators } from '../show/show.dto';
import { CreateEditAdminStaffAdministratorsArgs } from './create-edit.dto';
Expand All @@ -15,7 +16,75 @@ export class CreateEditAdminStaffAdministratorsService {
group_id,
user_id,
permissions,
id,
}: CreateEditAdminStaffAdministratorsArgs): Promise<ShowAdminStaffAdministrators> {
// Edit
if (id) {
const permission =
await this.databaseService.db.query.core_admin_permissions.findFirst({
where: (table, { eq }) => eq(table.id, id),
});

if (!permission) {
throw new NotFoundError('Permission');
}

await this.databaseService.db
.update(core_admin_permissions)
.set({
permissions,
})
.where(eq(core_admin_permissions.id, id));

const data =
await this.databaseService.db.query.core_admin_permissions.findFirst({
where: (table, { eq }) => eq(table.id, id),
with: {
group: {
columns: {
id: true,
color: true,
},
},
},
});

if (!data) {
throw new InternalServerError();
}

if (data.user_id) {
const user = await getUser({
id: data.user_id,
db: this.databaseService.db,
});

return {
...data,
user_or_group: {
...user,
},
permissions:
data.permissions as ShowAdminStaffAdministrators['permissions'],
};
}

if (!data.group) {
throw new InternalServerError();
}

return {
...data,
user_or_group: {
...data.group,
group_name: [],
},
permissions:
data.permissions as ShowAdminStaffAdministrators['permissions'],
};
}

// Create
if (!group_id && !user_id) {
throw new CustomError({
code: 'BAD_REQUEST',
Expand All @@ -38,8 +107,7 @@ export class CreateEditAdminStaffAdministratorsService {
message: 'This user or group already has moderator permissions.',
});
}

const permission = await this.databaseService.db
const [permission] = await this.databaseService.db
.insert(core_admin_permissions)
.values({
user_id,
Expand All @@ -50,7 +118,7 @@ export class CreateEditAdminStaffAdministratorsService {

const data =
await this.databaseService.db.query.core_admin_permissions.findFirst({
where: (table, { eq }) => eq(table.id, permission[0].id),
where: (table, { eq }) => eq(table.id, permission.id),
with: {
group: {
columns: {
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/utils/guards/admin-auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export class AdminAuthGuards implements CanActivate {
if (!plugin) throw new AccessDeniedError();
const group = plugin.groups.find(group => group.id === permission.group);
if (!group) throw new AccessDeniedError();
if (group.permissions.length === 0) return !!authorization;

const permissionObj = group.permissions.find(
item => item === permission.permission,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/components/ui/carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React from 'react';
import { cn } from '../../helpers/classnames';
import { Button } from './button';

type CarouselApi = UseEmblaCarouselType[1];
export type CarouselApi = UseEmblaCarouselType[1];
type UseCarouselParameters = Parameters<typeof useEmblaCarousel>;
type CarouselOptions = UseCarouselParameters[0];
type CarouselPlugin = UseCarouselParameters[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ export type Admin__Core_Staff_Administrators__Create_EditMutationVariables = Typ
groupId?: Types.InputMaybe<Types.Scalars['Int']['input']>;
userId?: Types.InputMaybe<Types.Scalars['Int']['input']>;
permissions?: Types.InputMaybe<Array<Types.PermissionsStaffArgs> | Types.PermissionsStaffArgs>;
id?: Types.InputMaybe<Types.Scalars['Int']['input']>;
}>;


export type Admin__Core_Staff_Administrators__Create_EditMutation = { __typename?: 'Mutation', admin__core_staff_administrators__create_edit: { __typename?: 'ShowAdminStaffAdministrators', id: number } };


export const Admin__Core_Staff_Administrators__Create_Edit = gql`
mutation Admin__core_staff_administrators__create_edit($groupId: Int, $userId: Int, $permissions: [PermissionsStaffArgs!]) {
mutation Admin__core_staff_administrators__create_edit($groupId: Int, $userId: Int, $permissions: [PermissionsStaffArgs!], $id: Int) {
admin__core_staff_administrators__create_edit(
group_id: $groupId
user_id: $userId
permissions: $permissions
id: $id
) {
id
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ mutation Admin__core_staff_administrators__create_edit(
$groupId: Int
$userId: Int
$permissions: [PermissionsStaffArgs!]
$id: Int
) {
admin__core_staff_administrators__create_edit(
group_id: $groupId
user_id: $userId
permissions: $permissions
id: $id
) {
id
}
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/src/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ export type MutationAdmin__Core_Security__Captcha__EditArgs = {

export type MutationAdmin__Core_Staff_Administrators__Create_EditArgs = {
group_id?: InputMaybe<Scalars['Int']['input']>;
id?: InputMaybe<Scalars['Int']['input']>;
permissions?: InputMaybe<Array<PermissionsStaffArgs>>;
user_id?: InputMaybe<Scalars['Int']['input']>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export const mutationApi = async (
query: Admin__Core_Staff_Administrators__Create_Edit,
variables,
});

revalidatePath('/', 'layout');
} catch (error) {
const e = error as Error;

return { error: e.message };
}

revalidatePath('/admin', 'layout');
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const useFormCreateEditFormGroupsMembersAdmin = ({
}: {
data?: Admin__Core_Staff_Administrators__ShowQuery['admin__core_staff_administrators__show']['edges'][0];
}) => {
const t = useTranslations('admin.members.staff.administrators.add');
const t = useTranslations('admin.members.staff.administrators');
const tShared = useTranslations('admin.members.staff.shared');
const tCore = useTranslations('core.global');
const { convertText } = useTextLang();
Expand Down Expand Up @@ -75,15 +75,18 @@ export const useFormCreateEditFormGroupsMembersAdmin = ({
form: UseFormReturn<z.infer<typeof formSchema>>,
) => {
const mutation = await mutationApi({
groupId:
values.type === 'group' && values.group?.[0].key
groupId: data
? undefined
: values.type === 'group' && values.group?.[0].key
? +values.group[0].key
: undefined,
userId:
values.type === 'user' && values.user?.[0].key
userId: data
? undefined
: values.type === 'user' && values.user?.[0].key
? +values.user[0].key
: undefined,
permissions: values.unrestricted ? [] : values.permissions,
id: data?.id,
});

if (mutation?.error) {
Expand All @@ -104,7 +107,7 @@ export const useFormCreateEditFormGroupsMembersAdmin = ({
}

setOpen?.(false);
toast.success(t('success'), {
toast.success(t(data ? 'edit.success' : 'add.success'), {
description:
values.type === 'group' && Array.isArray(values.group?.[0].value)
? convertText(values.group[0].value)
Expand Down
Loading

0 comments on commit bbd76fe

Please sign in to comment.