Skip to content

Commit

Permalink
Merge pull request #8619 from ever-co/feat/dashboard-api
Browse files Browse the repository at this point in the history
[Feature] Dashboard API
  • Loading branch information
rahul-rocket authored Dec 25, 2024
2 parents 2b0d82f + 8bf5c86 commit 21bd1c4
Show file tree
Hide file tree
Showing 33 changed files with 512 additions and 43 deletions.
11 changes: 6 additions & 5 deletions packages/contracts/src/lib/base-entity.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ export enum ActorTypeEnum {

export enum BaseEntityEnum {
Candidate = 'Candidate',
Comment = 'Comment',
Contact = 'Contact',
Currency = 'Currency',
DailyPlan = 'DailyPlan',
Dashboard = 'Dashboard',
Employee = 'Employee',
Expense = 'Expense',
DailyPlan = 'DailyPlan',
Invoice = 'Invoice',
Income = 'Income',
Language = 'Language',
Expand All @@ -89,13 +91,12 @@ export enum BaseEntityEnum {
OrganizationTeam = 'OrganizationTeam',
OrganizationProjectModule = 'OrganizationProjectModule',
OrganizationSprint = 'OrganizationSprint',
ResourceLink = 'ResourceLink',
OrganizationVendor = 'OrganizationVendor',
Task = 'Task',
ResourceLink = 'ResourceLink',
ScreeningTask = 'ScreeningTask',
TaskView = 'TaskView',
Task = 'Task',
TaskLinkedIssue = 'TaskLinkedIssue',
TaskView = 'TaskView',
User = 'User',
Comment = 'Comment',
Tenant = 'Tenant'
}
7 changes: 6 additions & 1 deletion packages/contracts/src/lib/dashboard.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ export interface IDashboard extends IBasePerTenantAndOrganizationEntityModel, IH
/**
* Input interface for creating a Dashboard entity.
*/
export interface IDashboardCreateInput extends ExcludeCreatorFields<IDashboard> {}
export interface IDashboardCreateInput extends ExcludeCreatorFields<IDashboard>, Omit<IDashboard, 'isDefault'> {}

/**
* Input interface for updating a Dashboard entity.
*/
export interface IDashboardUpdateInput extends Partial<IDashboardCreateInput> {}
15 changes: 13 additions & 2 deletions packages/contracts/src/lib/role-permission.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,12 @@ export enum PermissionsEnum {
PROJECT_MODULE_DELETE = 'PROJECT_MODULE_DELETE',
/** API Call Log */
API_CALL_LOG_READ = 'API_CALL_LOG_READ',
API_CALL_LOG_DELETE = 'API_CALL_LOG_DELETE'
API_CALL_LOG_DELETE = 'API_CALL_LOG_DELETE',
/** Dashboard */
DASHBOARD_CREATE = 'DASHBOARD_CREATE',
DASHBOARD_READ = 'DASHBOARD_READ',
DASHBOARD_UPDATE = 'DASHBOARD_UPDATE',
DASHBOARD_DELETE = 'DASHBOARD_DELETE'
}

export const PermissionGroups = {
Expand Down Expand Up @@ -325,7 +330,13 @@ export const PermissionGroups = {
PermissionsEnum.PROJECT_MODULE_READ,
PermissionsEnum.PROJECT_MODULE_UPDATE,
PermissionsEnum.PROJECT_MODULE_DELETE,
/** Project Module Permissions start */
/** Project Module Permissions end */
/** Dashboard Permissions Start */
PermissionsEnum.DASHBOARD_CREATE,
PermissionsEnum.DASHBOARD_READ,
PermissionsEnum.DASHBOARD_UPDATE,
PermissionsEnum.DASHBOARD_DELETE,
/** Dashboard Permissions End */
/** Organization Team Permissions Start */
PermissionsEnum.ORG_TEAM_ADD,
PermissionsEnum.ORG_TEAM_VIEW,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/lib/core/entities/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export * from '../../activity-log/activity-log.subscriber';
export * from '../../api-call-log/api-call-log.subscriber';
export * from '../../candidate/candidate.subscriber';
export * from '../../custom-smtp/custom-smtp.subscriber';
export * from '../../dashboard/dashboard.subscriber';
export * from '../../email-reset/email-reset.subscriber';
export * from '../../email-template/email-template.subscriber';
export * from '../../employee-setting/employee-setting.subscriber';
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/lib/core/entities/subscribers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ApiCallLogSubscriber,
CandidateSubscriber,
CustomSmtpSubscriber,
DashboardSubscriber,
EmailResetSubscriber,
EmailTemplateSubscriber,
EmployeeSettingSubscriber,
Expand Down Expand Up @@ -63,6 +64,7 @@ export const coreSubscribers = [
ApiCallLogSubscriber,
CandidateSubscriber,
CustomSmtpSubscriber,
DashboardSubscriber,
EmailResetSubscriber,
EmailTemplateSubscriber,
EmployeeSettingSubscriber,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ICommand } from '@nestjs/cqrs';
import { IDashboardCreateInput } from '@gauzy/contracts';

export class DashboardCreateCommand implements ICommand {
static readonly type = '[Dashboard] Create';

constructor(public readonly input: IDashboardCreateInput) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ICommand } from '@nestjs/cqrs';
import { ID, IDashboardUpdateInput } from '@gauzy/contracts';

export class DashboardUpdateCommand implements ICommand {
static readonly type = '[Dashboard] Update';

constructor(public readonly id: ID, public readonly input: IDashboardUpdateInput) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { IDashboard } from '@gauzy/contracts';
import { DashboardService } from '../../dashboard.service';
import { DashboardCreateCommand } from '../dashboard.create.command';

@CommandHandler(DashboardCreateCommand)
export class DashboardCreateHandler implements ICommandHandler<DashboardCreateCommand> {
constructor(private readonly dashboardService: DashboardService) {}

/**
* Handles the DashboardCreateCommand to create a new dashboard.
*
* @param command - The command containing the input data for dashboard creation.
* @returns A promise that resolves to the created dashboard.
*/
public async execute(command: DashboardCreateCommand): Promise<IDashboard> {
const { input } = command;
return this.dashboardService.create(input);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { IDashboard } from '@gauzy/contracts';
import { DashboardService } from '../../dashboard.service';
import { DashboardUpdateCommand } from '../dashboard.update.command';

@CommandHandler(DashboardUpdateCommand)
export class DashboardUpdateHandler implements ICommandHandler<DashboardUpdateCommand> {
constructor(private readonly dashboardService: DashboardService) {}

/**
* Handles the DashboardUpdateCommand to update an existing dashboard.
*
* @param command - The command containing the id and input data for dashboard update.
* @returns A promise that resolves to the updated dashboard.
*/
public async execute(command: DashboardUpdateCommand): Promise<IDashboard> {
const { id, input } = command;
return this.dashboardService.update(id, input);
}
}
4 changes: 4 additions & 0 deletions packages/core/src/lib/dashboard/commands/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { DashboardCreateHandler } from './dashboard.create.handler';
import { DashboardUpdateHandler } from './dashboard.update.handler';

export const CommandHandlers = [DashboardCreateHandler, DashboardUpdateHandler];
2 changes: 2 additions & 0 deletions packages/core/src/lib/dashboard/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './dashboard.create.command';
export * from './dashboard.update.command';
119 changes: 119 additions & 0 deletions packages/core/src/lib/dashboard/dashboard.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { CommandBus } from '@nestjs/cqrs';
import {
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
Param,
Post,
Put,
Query,
UseGuards
} from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ID, IPagination, PermissionsEnum } from '@gauzy/contracts';
import { PermissionGuard, TenantPermissionGuard } from '../shared/guards';
import { Permissions } from '../shared/decorators';
import { UseValidationPipe, UUIDValidationPipe } from '../shared/pipes';
import { CrudController, PaginationParams } from '../core/crud';
import { Dashboard } from './dashboard.entity';
import { DashboardService } from './dashboard.service';
import { DashboardCreateCommand, DashboardUpdateCommand } from './commands';
import { CreateDashboardDTO, UpdateDashboardDTO } from './dto';
import { DeleteResult } from 'typeorm';

@ApiTags('Dashboard')
@UseGuards(TenantPermissionGuard, PermissionGuard)
@Permissions(PermissionsEnum.DASHBOARD_READ)
@Controller('dashboard')
export class DashboardController extends CrudController<Dashboard> {
constructor(private readonly dashboardService: DashboardService, private readonly commandBus: CommandBus) {
super(dashboardService);
}

@ApiOperation({ summary: 'Get dashboards.' })
@ApiResponse({
status: HttpStatus.OK,
description: 'Found dashboards',
type: Dashboard
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: 'Records not found'
})
@Permissions(PermissionsEnum.ALL_ORG_VIEW, PermissionsEnum.DASHBOARD_READ)
@Get()
async findAll(@Query() params: PaginationParams<Dashboard>): Promise<IPagination<Dashboard>> {
return this.dashboardService.findAll(params);
}

@ApiOperation({ summary: 'Find by id.' })
@ApiResponse({
status: HttpStatus.OK,
description: 'Found dashboard',
type: Dashboard
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: 'Record not found'
})
@Permissions(PermissionsEnum.ALL_ORG_VIEW, PermissionsEnum.DASHBOARD_READ)
@Get(':id')
@UseValidationPipe()
async findById(
@Param('id', UUIDValidationPipe) id: ID,
@Query() params: PaginationParams<Dashboard>
): Promise<Dashboard> {
return this.dashboardService.findOneByIdString(id, params);
}

@ApiOperation({ summary: 'Create dashboard.' })
@ApiResponse({
status: HttpStatus.CREATED,
description: 'The record has been successfully created.'
})
@Permissions(PermissionsEnum.ALL_ORG_EDIT, PermissionsEnum.DASHBOARD_CREATE)
@Post()
@UseValidationPipe({ whitelist: true })
async create(@Body() entity: CreateDashboardDTO): Promise<Dashboard> {
return await this.commandBus.execute(new DashboardCreateCommand(entity));
}

@ApiOperation({ summary: 'Update dashboard.' })
@ApiResponse({
status: HttpStatus.CREATED,
description: 'The record has been successfully updated.'
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: 'Record not found'
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: 'Invalid input, The response body may contain clues as to what went wrong'
})
@HttpCode(HttpStatus.ACCEPTED)
@Permissions(PermissionsEnum.ALL_ORG_EDIT, PermissionsEnum.DASHBOARD_UPDATE)
@Put(':id')
@UseValidationPipe({ whitelist: true })
async update(@Param('id', UUIDValidationPipe) id: ID, @Body() entity: UpdateDashboardDTO): Promise<Dashboard> {
return await this.commandBus.execute(new DashboardUpdateCommand(id, entity));
}

@ApiOperation({ summary: 'Delete dashboard.' })
@ApiResponse({
status: HttpStatus.OK,
description: 'The record has been successfully deleted.'
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: 'Record not found'
})
@Permissions(PermissionsEnum.ALL_ORG_EDIT, PermissionsEnum.DASHBOARD_DELETE)
@Delete(':id')
async delete(@Param('id', UUIDValidationPipe) id: ID): Promise<DeleteResult> {
return await this.dashboardService.delete(id);
}
}
14 changes: 11 additions & 3 deletions packages/core/src/lib/dashboard/dashboard.module.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { CqrsModule } from '@nestjs/cqrs';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Module } from '@nestjs/common';
import { RolePermissionModule } from '../role-permission/role-permission.module';
import { CommandHandlers } from './commands/handlers';
import { Dashboard } from './dashboard.entity';
import { DashboardService } from './dashboard.service';
import { DashboardController } from './dashboard.controller';
import { TypeOrmDashboardRepository } from './repository/type-orm-dashboard.repository';

@Module({
imports: [
TypeOrmModule.forFeature([Dashboard]),
MikroOrmModule.forFeature([Dashboard]),
RolePermissionModule,
CqrsModule
],
controllers: [],
providers: [],
exports: []
controllers: [DashboardController],
providers: [DashboardService, TypeOrmDashboardRepository, ...CommandHandlers],
exports: [TypeOrmModule, DashboardService, TypeOrmDashboardRepository]
})
export class DashboardModule {}
Loading

0 comments on commit 21bd1c4

Please sign in to comment.