303 lines
11 KiB
JavaScript
303 lines
11 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.TaskService = void 0;
|
|
const sdk_1 = require("@directus/sdk");
|
|
const directus_client_1 = require("../../../scripts/utils/directus-client");
|
|
class TaskService {
|
|
constructor() {
|
|
this.initializeClient();
|
|
}
|
|
async initializeClient() {
|
|
this.client = await (0, directus_client_1.getAuthenticatedClient)();
|
|
}
|
|
/**
|
|
* Create a single task
|
|
*/
|
|
async createTask(data) {
|
|
try {
|
|
if (!this.client)
|
|
await this.initializeClient();
|
|
const task = await this.client.request((0, sdk_1.createItem)('tasks', {
|
|
...data,
|
|
created_at: new Date().toISOString(),
|
|
updated_at: new Date().toISOString()
|
|
}));
|
|
return task;
|
|
}
|
|
catch (error) {
|
|
throw new Error(`Failed to create task: ${error.message}`);
|
|
}
|
|
}
|
|
/**
|
|
* Get all tasks with pagination and filtering
|
|
*/
|
|
async getTasks(query) {
|
|
try {
|
|
if (!this.client)
|
|
await this.initializeClient();
|
|
const { page, limit, sort, filter, search, fields } = query;
|
|
const offset = (page - 1) * limit;
|
|
// Build query parameters
|
|
const queryParams = {
|
|
limit,
|
|
offset,
|
|
meta: 'total_count'
|
|
};
|
|
if (sort)
|
|
queryParams.sort = sort.split(',');
|
|
if (filter)
|
|
queryParams.filter = filter;
|
|
if (search)
|
|
queryParams.search = search;
|
|
if (fields)
|
|
queryParams.fields = fields;
|
|
const response = await this.client.request((0, sdk_1.readItems)('tasks', queryParams));
|
|
// Calculate pagination metadata
|
|
const totalPages = Math.ceil(response.meta.total_count / limit);
|
|
return {
|
|
data: response.data,
|
|
meta: {
|
|
total: response.meta.total_count,
|
|
page,
|
|
limit,
|
|
totalPages
|
|
}
|
|
};
|
|
}
|
|
catch (error) {
|
|
throw new Error(`Failed to fetch tasks: ${error.message}`);
|
|
}
|
|
}
|
|
/**
|
|
* Get a single task by ID
|
|
*/
|
|
async getTaskById(id) {
|
|
try {
|
|
if (!this.client)
|
|
await this.initializeClient();
|
|
const task = await this.client.request((0, sdk_1.readItem)('tasks', id));
|
|
if (!task) {
|
|
throw new Error('Task not found');
|
|
}
|
|
return task;
|
|
}
|
|
catch (error) {
|
|
throw new Error(`Failed to fetch task: ${error.message}`);
|
|
}
|
|
}
|
|
/**
|
|
* Update a task
|
|
*/
|
|
async updateTask(id, data) {
|
|
try {
|
|
if (!this.client)
|
|
await this.initializeClient();
|
|
const task = await this.client.request((0, sdk_1.updateItem)('tasks', id, {
|
|
...data,
|
|
updated_at: new Date().toISOString()
|
|
}));
|
|
return task;
|
|
}
|
|
catch (error) {
|
|
throw new Error(`Failed to update task: ${error.message}`);
|
|
}
|
|
}
|
|
/**
|
|
* Delete a task
|
|
*/
|
|
async deleteTask(id) {
|
|
try {
|
|
if (!this.client)
|
|
await this.initializeClient();
|
|
await this.client.request((0, sdk_1.deleteItem)('tasks', id));
|
|
}
|
|
catch (error) {
|
|
throw new Error(`Failed to delete task: ${error.message}`);
|
|
}
|
|
}
|
|
/**
|
|
* Bulk create tasks
|
|
*/
|
|
async bulkCreateTasks(data) {
|
|
try {
|
|
if (!this.client)
|
|
await this.initializeClient();
|
|
const tasksWithTimestamps = data.tasks.map(task => ({
|
|
...task,
|
|
created_at: new Date().toISOString(),
|
|
updated_at: new Date().toISOString()
|
|
}));
|
|
const tasks = await this.client.request((0, sdk_1.createItems)('tasks', tasksWithTimestamps));
|
|
return tasks;
|
|
}
|
|
catch (error) {
|
|
throw new Error(`Failed to bulk create tasks: ${error.message}`);
|
|
}
|
|
}
|
|
/**
|
|
* Bulk update tasks
|
|
*/
|
|
async bulkUpdateTasks(data) {
|
|
try {
|
|
if (!this.client)
|
|
await this.initializeClient();
|
|
const updates = data.ids.map(id => ({
|
|
id,
|
|
...data.updates,
|
|
updated_at: new Date().toISOString()
|
|
}));
|
|
const tasks = await this.client.request((0, sdk_1.updateItems)('tasks', updates));
|
|
return tasks;
|
|
}
|
|
catch (error) {
|
|
throw new Error(`Failed to bulk update tasks: ${error.message}`);
|
|
}
|
|
}
|
|
/**
|
|
* Bulk delete tasks
|
|
*/
|
|
async bulkDeleteTasks(data) {
|
|
try {
|
|
if (!this.client)
|
|
await this.initializeClient();
|
|
for (const id of data.ids) {
|
|
await this.client.request((0, sdk_1.deleteItem)('tasks', id));
|
|
}
|
|
}
|
|
catch (error) {
|
|
throw new Error(`Failed to bulk delete tasks: ${error.message}`);
|
|
}
|
|
}
|
|
/**
|
|
* Duplicate a task
|
|
*/
|
|
async duplicateTask(data) {
|
|
try {
|
|
if (!this.client)
|
|
await this.initializeClient();
|
|
// Fetch the original task
|
|
const originalTask = await this.getTaskById(data.taskId);
|
|
// Remove fields that shouldn't be duplicated
|
|
const { id, created_at, updated_at, created_by, actual_hours, completed_at, ...taskData } = originalTask;
|
|
// Apply overrides
|
|
const newTaskData = {
|
|
...taskData,
|
|
...data.overrides,
|
|
title: data.overrides?.title || `${taskData.title} (Copy)`,
|
|
created_at: new Date().toISOString(),
|
|
updated_at: new Date().toISOString()
|
|
};
|
|
// Create the duplicated task
|
|
const newTask = await this.client.request((0, sdk_1.createItem)('tasks', newTaskData));
|
|
// Handle subtasks if requested
|
|
if (data.includeSubtasks) {
|
|
const subtasks = await this.client.request((0, sdk_1.readItems)('tasks', {
|
|
filter: {
|
|
parent_task: {
|
|
_eq: data.taskId
|
|
}
|
|
}
|
|
}));
|
|
if (subtasks.length > 0) {
|
|
const newSubtasks = subtasks.map((subtask) => {
|
|
const { id, created_at, updated_at, created_by, ...subtaskData } = subtask;
|
|
return {
|
|
...subtaskData,
|
|
parent_task: newTask.id,
|
|
created_at: new Date().toISOString(),
|
|
updated_at: new Date().toISOString()
|
|
};
|
|
});
|
|
await this.client.request((0, sdk_1.createItems)('tasks', newSubtasks));
|
|
}
|
|
}
|
|
return newTask;
|
|
}
|
|
catch (error) {
|
|
throw new Error(`Failed to duplicate task: ${error.message}`);
|
|
}
|
|
}
|
|
/**
|
|
* Create a template from a task
|
|
*/
|
|
async createTemplateFromTask(data) {
|
|
try {
|
|
if (!this.client)
|
|
await this.initializeClient();
|
|
// Fetch the task
|
|
const task = await this.getTaskById(data.taskId);
|
|
// Remove fields that shouldn't be in template
|
|
const excludeDefault = [
|
|
'id', 'created_at', 'updated_at', 'created_by',
|
|
'actual_hours', 'completed_at', 'progress_percentage',
|
|
...(data.excludeFields || [])
|
|
];
|
|
const templateData = {};
|
|
Object.keys(task).forEach(key => {
|
|
if (!excludeDefault.includes(key)) {
|
|
templateData[key] = task[key];
|
|
}
|
|
});
|
|
// Create template
|
|
const template = await this.client.request((0, sdk_1.createItem)('task_templates', {
|
|
name: data.templateName,
|
|
description: data.templateDescription,
|
|
title_template: templateData.title,
|
|
description_template: templateData.description,
|
|
acceptance_criteria_template: templateData.acceptance_criteria,
|
|
default_priority: templateData.priority,
|
|
default_complexity: templateData.complexity,
|
|
estimated_hours: templateData.estimated_hours,
|
|
template_type: 'feature',
|
|
workflow_stage: 'development',
|
|
usage_count: 0,
|
|
created_at: new Date().toISOString(),
|
|
updated_at: new Date().toISOString()
|
|
}));
|
|
return template;
|
|
}
|
|
catch (error) {
|
|
throw new Error(`Failed to create template from task: ${error.message}`);
|
|
}
|
|
}
|
|
/**
|
|
* Apply a template to create a new task
|
|
*/
|
|
async applyTemplate(data) {
|
|
try {
|
|
if (!this.client)
|
|
await this.initializeClient();
|
|
// Fetch the template
|
|
const template = await this.client.request((0, sdk_1.readItem)('task_templates', data.templateId));
|
|
if (!template) {
|
|
throw new Error('Template not found');
|
|
}
|
|
// Create task from template
|
|
const taskData = {
|
|
title: template.title_template,
|
|
description: template.description_template,
|
|
acceptance_criteria: template.acceptance_criteria_template,
|
|
priority: template.default_priority,
|
|
complexity: template.default_complexity,
|
|
estimated_hours: template.estimated_hours,
|
|
project: data.projectId,
|
|
ai_generated: true,
|
|
...data.overrides,
|
|
created_at: new Date().toISOString(),
|
|
updated_at: new Date().toISOString()
|
|
};
|
|
const task = await this.client.request((0, sdk_1.createItem)('tasks', taskData));
|
|
// Update template usage count
|
|
await this.client.request((0, sdk_1.updateItem)('task_templates', data.templateId, {
|
|
usage_count: template.usage_count + 1,
|
|
last_used: new Date().toISOString()
|
|
}));
|
|
return task;
|
|
}
|
|
catch (error) {
|
|
throw new Error(`Failed to apply template: ${error.message}`);
|
|
}
|
|
}
|
|
}
|
|
exports.TaskService = TaskService;
|
|
//# sourceMappingURL=task.service.js.map
|