-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add create & edit permission admin mutation & connect frontend …
…to API
- Loading branch information
1 parent
d363f50
commit db73fcd
Showing
26 changed files
with
689 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/backend/src/core/admin/plugins/permissions-admin/create-edit/create-edit.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ArgsType, Field } from '@nestjs/graphql'; | ||
|
||
@ArgsType() | ||
export class CreateEditAdminPermissionsAdminPluginsArgs { | ||
@Field(() => String) | ||
id: string; | ||
|
||
@Field(() => String, { nullable: true }) | ||
old_id?: string; | ||
|
||
@Field(() => String, { nullable: true }) | ||
parent_id?: string; | ||
|
||
@Field(() => String) | ||
plugin_code: string; | ||
} |
23 changes: 23 additions & 0 deletions
23
...ages/backend/src/core/admin/plugins/permissions-admin/create-edit/create-edit.resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { AdminAuthGuards, OnlyForDevelopment } from '@/utils'; | ||
import { UseGuards } from '@nestjs/common'; | ||
import { Args, Mutation, Resolver } from '@nestjs/graphql'; | ||
|
||
import { ShowAdminPermissionsAdminPluginsObj } from '../show/show.dto'; | ||
import { CreateEditAdminPermissionsAdminPluginsArgs } from './create-edit.dto'; | ||
import { CreateEditAdminPermissionsAdminPluginsService } from './create-edit.service'; | ||
|
||
@Resolver() | ||
export class CreateEditAdminPermissionsAdminPluginsResolver { | ||
constructor( | ||
private readonly service: CreateEditAdminPermissionsAdminPluginsService, | ||
) {} | ||
|
||
@Mutation(() => ShowAdminPermissionsAdminPluginsObj) | ||
@UseGuards(AdminAuthGuards) | ||
@UseGuards(OnlyForDevelopment) | ||
async admin__core_plugins__permissions_admin__create_edit( | ||
@Args() args: CreateEditAdminPermissionsAdminPluginsArgs, | ||
): Promise<ShowAdminPermissionsAdminPluginsObj> { | ||
return await this.service.createEdit(args); | ||
} | ||
} |
172 changes: 172 additions & 0 deletions
172
packages/backend/src/core/admin/plugins/permissions-admin/create-edit/create-edit.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import { | ||
ABSOLUTE_PATHS_BACKEND, | ||
ConfigPlugin, | ||
CustomError, | ||
InternalServerError, | ||
NotFoundError, | ||
} from '@/index'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { existsSync } from 'fs'; | ||
import { readFile, writeFile } from 'fs/promises'; | ||
|
||
import { ShowAdminPermissionsAdminPluginsObj } from '../show/show.dto'; | ||
import { CreateEditAdminPermissionsAdminPluginsArgs } from './create-edit.dto'; | ||
|
||
@Injectable() | ||
export class CreateEditAdminPermissionsAdminPluginsService { | ||
async createEdit({ | ||
id, | ||
old_id, | ||
plugin_code, | ||
parent_id, | ||
}: CreateEditAdminPermissionsAdminPluginsArgs): Promise<ShowAdminPermissionsAdminPluginsObj> { | ||
const pathConfig = ABSOLUTE_PATHS_BACKEND.plugin({ | ||
code: plugin_code, | ||
}).config; | ||
if (!existsSync(pathConfig)) { | ||
throw new NotFoundError('Plugin'); | ||
} | ||
|
||
const config: ConfigPlugin = JSON.parse(await readFile(pathConfig, 'utf8')); | ||
|
||
const parent = config.permissions_admin?.find( | ||
permission => permission.id === parent_id, | ||
); | ||
|
||
if (!parent && parent_id) { | ||
throw new NotFoundError('Parent permission for the plugin'); | ||
} | ||
|
||
// Check if the id already exists | ||
if (old_id !== id) { | ||
const existsPermission = parent | ||
? parent.children.find(child => child.id === id) | ||
: config.permissions_admin?.find(permission => permission.id === id); | ||
|
||
if (existsPermission) { | ||
throw new CustomError({ | ||
message: 'Permission already exists', | ||
code: 'PERMISSION_ALREADY_EXISTS', | ||
}); | ||
} | ||
} | ||
|
||
// Edit | ||
if (old_id) { | ||
const oldPermission = parent | ||
? parent.children.find(child => child.id === old_id) | ||
: config.permissions_admin?.find( | ||
permission => permission.id === old_id, | ||
); | ||
|
||
if (!oldPermission) { | ||
throw new NotFoundError('Permission with the old id for the plugin'); | ||
} | ||
|
||
let newConfig: ConfigPlugin; | ||
|
||
if (parent) { | ||
newConfig = { | ||
...config, | ||
permissions_admin: config.permissions_admin?.map(permission => { | ||
if (permission.id === parent.id) { | ||
return { | ||
...permission, | ||
children: permission.children.map(child => { | ||
if (child.id === old_id) { | ||
return { | ||
id, | ||
}; | ||
} | ||
|
||
return child; | ||
}), | ||
}; | ||
} | ||
|
||
return permission; | ||
}), | ||
}; | ||
} else { | ||
newConfig = { | ||
...config, | ||
permissions_admin: config.permissions_admin?.map(permission => { | ||
if (permission.id === old_id) { | ||
return { | ||
id, | ||
children: permission.children, | ||
}; | ||
} | ||
|
||
return permission; | ||
}), | ||
}; | ||
} | ||
|
||
await writeFile(pathConfig, JSON.stringify(newConfig, null, 2)); | ||
|
||
const returnValue = parent | ||
? newConfig.permissions_admin?.find( | ||
permission => permission.id === parent.id, | ||
) | ||
: newConfig.permissions_admin?.find(permission => permission.id === id); | ||
|
||
if (!returnValue) { | ||
throw new InternalServerError(); | ||
} | ||
|
||
return { | ||
id, | ||
children: [], | ||
}; | ||
} | ||
|
||
let newConfig: ConfigPlugin; | ||
if (parent) { | ||
newConfig = { | ||
...config, | ||
permissions_admin: (config.permissions_admin ?? []).map(permission => { | ||
if (permission.id === parent.id) { | ||
return { | ||
...permission, | ||
children: [ | ||
...permission.children, | ||
{ | ||
id, | ||
children: [], | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
return permission; | ||
}), | ||
}; | ||
} else { | ||
newConfig = { | ||
...config, | ||
permissions_admin: [ | ||
...(config.permissions_admin ?? []), | ||
{ | ||
id, | ||
children: [], | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
await writeFile(pathConfig, JSON.stringify(newConfig, null, 2)); | ||
|
||
const returnValue = parent | ||
? newConfig.permissions_admin?.find( | ||
permission => permission.id === parent.id, | ||
) | ||
: newConfig.permissions_admin?.find(permission => permission.id === id); | ||
|
||
if (!returnValue) { | ||
throw new InternalServerError(); | ||
} | ||
|
||
return returnValue; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/backend/src/core/admin/plugins/permissions-admin/permissions-admin.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { CreateEditAdminPermissionsAdminPluginsResolver } from './create-edit/create-edit.resolver'; | ||
import { CreateEditAdminPermissionsAdminPluginsService } from './create-edit/create-edit.service'; | ||
import { ShowAdminPermissionsAdminPluginsResolver } from './show/show.resolver'; | ||
import { ShowAdminPermissionsAdminPluginsService } from './show/show.service'; | ||
|
||
@Module({ | ||
providers: [ | ||
ShowAdminPermissionsAdminPluginsResolver, | ||
ShowAdminPermissionsAdminPluginsService, | ||
CreateEditAdminPermissionsAdminPluginsService, | ||
CreateEditAdminPermissionsAdminPluginsResolver, | ||
], | ||
}) | ||
export class AdminPermissionsAdminPluginsModule {} |
1 change: 1 addition & 0 deletions
1
packages/backend/src/core/admin/plugins/permissions-admin/show/show.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.