Relevant source files
The following files were used as context for generating this wiki page:
- apps/api/src/framework-editor/task-template/dto/create-task-template.dto.ts
- apps/api/src/framework-editor/task-template/schemas/task-template-operations.ts
- apps/api/src/framework-editor/task-template/dto/update-task-template.dto.ts
- apps/api/src/framework-editor/task-template/task-template.controller.ts
- apps/api/src/framework-editor/task-template/task-template.service.ts
- apps/api/src/framework-editor/task-template/task-template.module.ts
The Framework Editor module provides an API for managing "Task Templates". These templates define recurring tasks with properties such as name, description, frequency, and responsible department. The module offers standard CRUD (Create, Read, Update, Delete) operations for these task templates, exposed via a RESTful API.
This module is built using the NestJS framework, adhering to its architectural patterns of controllers, services, and DTOs (Data Transfer Objects) to ensure a clear separation of concerns and maintainability. It integrates with an authentication system and uses database interactions for persistence.
Architecture Overview
The Framework Editor's Task Template component follows a typical NestJS modular architecture, consisting of a module, controller, and service. This structure facilitates organized code and clear responsibilities for handling API requests, business logic, and data persistence.
Sources: apps/api/src/framework-editor/task-template/task-template.module.ts:1-7, apps/api/src/framework-editor/task-template/task-template.controller.ts:20-22, apps/api/src/framework-editor/task-template/task-template.service.ts:1-4
TaskTemplateModule
The TaskTemplateModule is the entry point for this part of the Framework Editor. It imports the AuthModule for authentication capabilities and registers TaskTemplateController and TaskTemplateService. The TaskTemplateService is also exported, making it available for use by other modules if needed.
@Module({
imports: [AuthModule],
controllers: [TaskTemplateController],
providers: [TaskTemplateService],
exports: [TaskTemplateService],
})
export class TaskTemplateModule {}Sources: apps/api/src/framework-editor/task-template/task-template.module.ts:1-7
TaskTemplateController
The TaskTemplateController handles incoming HTTP requests related to task templates. It defines the API endpoints, applies authentication guards (HybridAuthGuard), and uses DTOs for request body validation. It delegates business logic to the TaskTemplateService. All endpoints are secured and require an X-Organization-Id header.
Sources: apps/api/src/framework-editor/task-template/task-template.controller.ts:1-87
TaskTemplateService
The TaskTemplateService encapsulates the business logic for managing task templates. It interacts directly with the database via db.frameworkEditorTaskTemplate (presumably a Prisma client or similar ORM) to perform CRUD operations. It includes error handling and logging for database interactions and throws NotFoundException for non-existent resources.
Sources: apps/api/src/framework-editor/task-template/task-template.service.ts:6-96
Data Structures
The module defines specific Data Transfer Objects (DTOs) for creating and updating task templates, ensuring data integrity and clear API contracts.
CreateTaskTemplateDto
This DTO defines the required fields for creating a new task template. All fields are mandatory and include validation rules.
export class CreateTaskTemplateDto {
name: string; // Task template name
description: string; // Detailed description
frequency: Frequency; // Frequency of the task (enum)
department: Departments; // Department responsible (enum)
}The Frequency and Departments enums are imported from @trycompai/db, indicating predefined categories for these fields.
| Field | Type | Description | Validation | Example |
|---|---|---|---|---|
name | string | Task template name | @IsString(), @IsNotEmpty() | "Monthly Security Review" |
description | string | Detailed description of the task template | @IsString(), @IsNotEmpty() | "Review and update..." |
frequency | Frequency | Frequency of the task (e.g., monthly, weekly) | @IsEnum(Frequency) | Frequency.monthly |
department | Departments | Department responsible for the task (e.g., IT) | @IsEnum(Departments) | Departments.it |
| Sources: apps/api/src/framework-editor/task-template/dto/create-task-template.dto.ts:1-30 |
UpdateTaskTemplateDto
The UpdateTaskTemplateDto is based on CreateTaskTemplateDto but uses PartialType from @nestjs/swagger. This makes all fields optional, allowing for partial updates of a task template.
import { PartialType } from '@nestjs/swagger';
import { CreateTaskTemplateDto } from './create-task-template.dto';
export class UpdateTaskTemplateDto extends PartialType(CreateTaskTemplateDto) {}Sources: apps/api/src/framework-editor/task-template/dto/update-task-template.dto.ts:1-4
API Endpoints
The TaskTemplateController exposes a set of RESTful API endpoints for managing task templates. All endpoints are versioned under /v1/framework-editor/task-template and require authentication.
| Method | Path | Description | Service Method Called | Request Body |
|---|---|---|---|---|
GET | / | Retrieve all task templates | findAll() | N/A |
GET | /:id | Retrieve a specific task template by ID | findById(id) | N/A |
PATCH | /:id | Update an existing task template by ID | updateById(id, dto) | UpdateTaskTemplateDto |
DELETE | /:id | Delete a task template by ID | deleteById(id) | N/A |
| Sources: apps/api/src/framework-editor/task-template/task-template.controller.ts:31-87, apps/api/src/framework-editor/task-template/schemas/task-template-operations.ts:1-16 |
Authentication and Authorization
All endpoints in the TaskTemplateController are protected by HybridAuthGuard. This guard enforces authentication, which can be either session-based or API key-based. The X-Organization-Id header is required for session authentication and optional for API key authentication. The AuthContext decorator is used to inject authentication details (e.g., userId, userEmail, authType) into controller methods.
Sources: apps/api/src/framework-editor/task-template/task-template.controller.ts:14-19, apps/api/src/framework-editor/task-template/task-template.controller.ts:50-51
Request Validation
The module employs several validation mechanisms:
ValidateIdPipe: Ensures that theidparameter in the URL is valid before it reaches the service layer.ValidationPipe: Applied to theupdateTaskTemplateendpoint, it validates theUpdateTaskTemplateDtorequest body against the rules defined in the DTO (e.g.,@IsString,@IsEnum). It is configured towhitelistandforbidNonWhitelistedproperties, ensuring only expected data is processed. Sources: apps/api/src/framework-editor/task-template/task-template.controller.ts:12, apps/api/src/framework-editor/task-template/task-template.controller.ts:63-68
Service Logic Details
The TaskTemplateService implements the core logic for interacting with the database. It uses a Logger for operational insights and robust error handling.
findAll()
Retrieves all task templates from the database, ordered alphabetically by name. Logs the number of retrieved templates.
Sources: apps/api/src/framework-editor/task-template/task-template.service.ts:9-23
findById(id: string)
Fetches a single task template by its unique id. If no template is found, it throws a NotFoundException.
Sources: apps/api/src/framework-editor/task-template/task-template.service.ts:25-47
updateById(id: string, updateDto: UpdateTaskTemplateDto)
Updates an existing task template. It first calls findById to ensure the template exists. If found, it proceeds with the update operation using the provided updateDto.
Sources: apps/api/src/framework-editor/task-template/task-template.service.ts:49-69
deleteById(id: string)
Deletes a task template by its id. Similar to updateById, it first verifies the template's existence using findById before performing the deletion. It returns a confirmation message and details of the deleted template.
Sources: apps/api/src/framework-editor/task-template/task-template.service.ts:71-96
On This Page
Architecture OverviewTaskTemplateModuleTaskTemplateControllerTaskTemplateServiceData StructuresCreateTaskTemplateDtoUpdateTaskTemplateDtoAPI EndpointsAuthentication and AuthorizationRequest ValidationService Logic DetailsfindAll()findById(id: string)updateById(id: string, updateDto: UpdateTaskTemplateDto)deleteById(id: string)