mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
refactor(nestjs): wip
This commit is contained in:
@@ -4,6 +4,7 @@ import { DeleteRoleService } from './commands/DeleteRole.service';
|
||||
import { EditRoleService } from './commands/EditRole.service';
|
||||
import { GetRoleService } from './queries/GetRole.service';
|
||||
import { GetRolesService } from './queries/GetRoles.service';
|
||||
import { RolePermissionsSchema } from './queries/RolePermissionsSchema';
|
||||
|
||||
@Injectable()
|
||||
export class RolesApplication {
|
||||
@@ -13,6 +14,7 @@ export class RolesApplication {
|
||||
private readonly deleteRoleService: DeleteRoleService,
|
||||
private readonly getRoleService: GetRoleService,
|
||||
private readonly getRolesService: GetRolesService,
|
||||
private readonly getRolePermissionsSchemaService: RolePermissionsSchema,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -59,4 +61,12 @@ export class RolesApplication {
|
||||
async getRoles() {
|
||||
return this.getRolesService.getRoles();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the role permissions schema.
|
||||
* @returns The role permissions schema.
|
||||
*/
|
||||
async getRolePermissionsSchema() {
|
||||
return this.getRolePermissionsSchemaService.getRolePermissionsSchema();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,9 +72,7 @@ export class RolesController {
|
||||
status: HttpStatus.OK,
|
||||
description: 'Role deleted successfully',
|
||||
})
|
||||
async deleteRole(
|
||||
@Param('id', ParseIntPipe) roleId: number,
|
||||
) {
|
||||
async deleteRole(@Param('id', ParseIntPipe) roleId: number) {
|
||||
await this.rolesApp.deleteRole(roleId);
|
||||
|
||||
return {
|
||||
@@ -83,24 +81,34 @@ export class RolesController {
|
||||
};
|
||||
}
|
||||
|
||||
@Get('permissions/schema')
|
||||
@ApiOperation({ summary: 'Get role permissions schema' })
|
||||
@ApiResponse({
|
||||
status: HttpStatus.OK,
|
||||
description: 'Role permissions schema',
|
||||
})
|
||||
async getRolePermissionsSchema() {
|
||||
const schema = await this.rolesApp.getRolePermissionsSchema();
|
||||
|
||||
return schema;
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'Get all roles' })
|
||||
@ApiResponse({ status: HttpStatus.OK, description: 'List of all roles' })
|
||||
async getRoles() {
|
||||
const roles = await this.rolesApp.getRoles();
|
||||
|
||||
return { roles };
|
||||
return roles;
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get a specific role by ID' })
|
||||
@ApiParam({ name: 'id', description: 'Role ID' })
|
||||
@ApiResponse({ status: HttpStatus.OK, description: 'Role details' })
|
||||
async getRole(
|
||||
@Param('id', ParseIntPipe) roleId: number,
|
||||
) {
|
||||
async getRole(@Param('id', ParseIntPipe) roleId: number) {
|
||||
const role = await this.rolesApp.getRole(roleId);
|
||||
|
||||
return { role };
|
||||
return role;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import { Role } from './models/Role.model';
|
||||
import { RolePermission } from './models/RolePermission.model';
|
||||
import { RolesController } from './Roles.controller';
|
||||
import { RolesApplication } from './Roles.application';
|
||||
import { RolePermissionsSchema } from './queries/RolePermissionsSchema';
|
||||
|
||||
const models = [
|
||||
RegisterTenancyModel(Role),
|
||||
@@ -24,6 +25,7 @@ const models = [
|
||||
GetRoleService,
|
||||
GetRolesService,
|
||||
RolesApplication,
|
||||
RolePermissionsSchema
|
||||
],
|
||||
controllers: [RolesController],
|
||||
exports: [...models],
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { IRoleCreatedPayload } from '../Roles.types';
|
||||
import { Role } from './../models/Role.model';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { UnitOfWork } from '../../Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { events } from '@/common/events/events';
|
||||
import { CreateRoleDto } from '../dtos/Role.dto';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { validateInvalidPermissions } from '../utils';
|
||||
|
||||
@Injectable()
|
||||
|
||||
@@ -1,12 +1,42 @@
|
||||
import { AbilitySchema } from '../AbilitySchema';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { I18nService } from 'nestjs-i18n';
|
||||
import { cloneDeep } from 'lodash';
|
||||
import { ISubjectAbilitiesSchema } from '../Roles.types';
|
||||
|
||||
@Injectable()
|
||||
export class RolePermissionsSchema {
|
||||
constructor(private readonly i18nService: I18nService) {}
|
||||
|
||||
/**
|
||||
* Retrieve the role permissions schema.
|
||||
* Retrieve the role permissions schema with translated labels.
|
||||
* @returns {ISubjectAbilitiesSchema[]}
|
||||
*/
|
||||
getRolePermissionsSchema() {
|
||||
return AbilitySchema;
|
||||
getRolePermissionsSchema(): ISubjectAbilitiesSchema[] {
|
||||
// Clone the schema to avoid modifying the original
|
||||
const schema = cloneDeep(AbilitySchema);
|
||||
|
||||
// Apply translations to each subject and its abilities
|
||||
return schema.map((subject: ISubjectAbilitiesSchema) => {
|
||||
// Translate subject label
|
||||
subject.subjectLabel = this.i18nService.t(subject.subjectLabel);
|
||||
|
||||
// Translate abilities labels
|
||||
if (subject.abilities) {
|
||||
subject.abilities = subject.abilities.map((ability) => ({
|
||||
...ability,
|
||||
label: this.i18nService.t(ability.label),
|
||||
}));
|
||||
}
|
||||
// Translate extra abilities labels if they exist
|
||||
if (subject.extraAbilities) {
|
||||
subject.extraAbilities = subject.extraAbilities.map((ability) => ({
|
||||
...ability,
|
||||
label: this.i18nService.t(ability.label),
|
||||
}));
|
||||
}
|
||||
|
||||
return subject;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user