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(
|
||||
|
||||
@@ -55,6 +55,15 @@ export class CreateProjectInput {
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
isPublic?: boolean;
|
||||
|
||||
@Field(() => ID, {
|
||||
nullable: true,
|
||||
description:
|
||||
'A project you own or administer to start from: its statuses, transitions, types, areas, custom fields and automation rules are copied',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsUUID()
|
||||
templateProjectId?: string | null;
|
||||
}
|
||||
|
||||
@InputType()
|
||||
|
||||
@@ -151,7 +151,11 @@ export class ProjectResolver {
|
||||
@CurrentUser() me: User,
|
||||
@Args('input') input: CreateProjectInput,
|
||||
) {
|
||||
return this.projects.create(me.id, input);
|
||||
const { templateProjectId, ...fields } = input;
|
||||
return this.projects.create(me.id, {
|
||||
...fields,
|
||||
templateId: templateProjectId,
|
||||
});
|
||||
}
|
||||
|
||||
@Mutation(() => CommandResultGql)
|
||||
|
||||
@@ -87,10 +87,21 @@ export class CreateProjectDto {
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
isPublic?: boolean;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
type: String,
|
||||
nullable: true,
|
||||
example: 'API',
|
||||
description:
|
||||
'A project you own or administer (id or key) to start from: its statuses, transitions, types, areas, custom fields and automation rules are copied',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
template?: string | null;
|
||||
}
|
||||
|
||||
export class UpdateProjectDto extends PartialType(
|
||||
OmitType(CreateProjectDto, ['key'] as const),
|
||||
OmitType(CreateProjectDto, ['key', 'template'] as const),
|
||||
) {
|
||||
@ApiPropertyOptional({
|
||||
description:
|
||||
|
||||
@@ -83,8 +83,12 @@ export class ProjectsController {
|
||||
@ApiOperation({ summary: 'Create a project (you become its owner)' })
|
||||
@ApiCreatedResponse({ type: CommandResultDto })
|
||||
@ApiCommandErrors()
|
||||
create(@CurrentUser() me: User, @Body() body: CreateProjectDto) {
|
||||
return this.projects.create(me.id, body);
|
||||
async create(@CurrentUser() me: User, @Body() body: CreateProjectDto) {
|
||||
const { template, ...fields } = body;
|
||||
return this.projects.create(me.id, {
|
||||
...fields,
|
||||
templateId: template ? await this.refs.projectId(me.id, template) : null,
|
||||
});
|
||||
}
|
||||
|
||||
@Get(':project')
|
||||
|
||||
Reference in New Issue
Block a user