feat: start a project from another one's configuration
Creating a project can name one the creator owns or administers: its statuses, transitions, types, areas, custom fields and automation rules are copied once (paused rules stay paused; rules assigning to someone who is not in the new project are left out). GraphQL templateProjectId, REST template (id or key), MCP create_project template, and a select in the web's new-project form. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,8 +2,10 @@ import { GetShowcaseQuery, Showcase } from './queries/get-showcase.query';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { CommandBus, QueryBus } from '@nestjs/cqrs';
|
||||
import {
|
||||
BusinessRuleError,
|
||||
ForbiddenError,
|
||||
NotFoundError,
|
||||
ValidationError,
|
||||
assertThat,
|
||||
} from '@task-stream/api/shared/domain';
|
||||
import {
|
||||
@@ -36,6 +38,12 @@ import { GetProjectStatsQuery } from './queries/get-project-stats.query';
|
||||
import { GetPublicProjectsQuery } from './queries/get-public-projects.query';
|
||||
import { GetProjectQuery } from './queries/get-project.query';
|
||||
import { GetReleasesQuery } from './queries/get-releases.query';
|
||||
import {
|
||||
CreateAutomationCommand,
|
||||
GetAutomationsQuery,
|
||||
UpdateAutomationCommand,
|
||||
} from './automations/automations';
|
||||
import { Automation } from './ports/automations.repository';
|
||||
|
||||
@Injectable()
|
||||
export class ProjectsService {
|
||||
@@ -108,16 +116,28 @@ export class ProjectsService {
|
||||
return this.queryBus.execute(new GetShowcaseQuery(project));
|
||||
}
|
||||
|
||||
create(
|
||||
/**
|
||||
* A new project. With `templateId`, it starts with a copy of that
|
||||
* project's configuration (statuses, transitions, types, areas and custom
|
||||
* fields) and automation rules, taken once: later changes to either one
|
||||
* stay there. Only from a project the actor manages (rules are only shown
|
||||
* to managers); a rule that names someone who is not in the new project
|
||||
* is left out.
|
||||
*/
|
||||
async create(
|
||||
actorId: string,
|
||||
input: {
|
||||
key: string;
|
||||
name: string;
|
||||
description?: string | null;
|
||||
isPublic?: boolean;
|
||||
templateId?: string | null;
|
||||
},
|
||||
): Promise<CommandResult> {
|
||||
return this.commandBus.execute(
|
||||
const template = input.templateId
|
||||
? await this.template(actorId, input.templateId)
|
||||
: null;
|
||||
const created: CommandResult = await this.commandBus.execute(
|
||||
new CreateProjectCommand(
|
||||
actorId,
|
||||
input.key,
|
||||
@@ -126,6 +146,61 @@ export class ProjectsService {
|
||||
input.isPublic ?? false,
|
||||
),
|
||||
);
|
||||
if (!template) return created;
|
||||
const configured: CommandResult = await this.commandBus.execute(
|
||||
new UpdateProjectSettingsCommand(
|
||||
actorId,
|
||||
created.id,
|
||||
structuredClone(template.settings),
|
||||
),
|
||||
);
|
||||
for (const rule of template.automations)
|
||||
await this.copyRule(actorId, created.id, rule);
|
||||
return { ...configured, key: created.key };
|
||||
}
|
||||
|
||||
private async template(
|
||||
actorId: string,
|
||||
projectId: string,
|
||||
): Promise<{
|
||||
settings: ProjectSettings;
|
||||
automations: Automation[];
|
||||
}> {
|
||||
const project = await this.access.project(projectId);
|
||||
project.assertCanManage(actorId);
|
||||
const automations: Automation[] = await this.queryBus.execute(
|
||||
new GetAutomationsQuery(actorId, projectId),
|
||||
);
|
||||
return { settings: project.settings, automations };
|
||||
}
|
||||
|
||||
private async copyRule(
|
||||
actorId: string,
|
||||
projectId: string,
|
||||
rule: Automation,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const copy: Automation = await this.commandBus.execute(
|
||||
new CreateAutomationCommand(actorId, projectId, {
|
||||
name: rule.name,
|
||||
trigger: rule.trigger,
|
||||
// Stored without conditions when it had none.
|
||||
conditions: rule.conditions ?? {},
|
||||
action: rule.action,
|
||||
}),
|
||||
);
|
||||
if (!rule.active)
|
||||
await this.commandBus.execute(
|
||||
new UpdateAutomationCommand(actorId, copy.id, { active: false }),
|
||||
);
|
||||
} catch (error) {
|
||||
// Someone it assigns to is not a member here: that rule stays behind.
|
||||
if (
|
||||
!(error instanceof ValidationError) &&
|
||||
!(error instanceof BusinessRuleError)
|
||||
)
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
update(
|
||||
|
||||
Reference in New Issue
Block a user