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')
|
||||
|
||||
@@ -11,6 +11,8 @@ export interface CreateProjectInput {
|
||||
name: string;
|
||||
description?: string | null;
|
||||
isPublic?: boolean;
|
||||
/** A project I manage whose configuration and rules it starts with. */
|
||||
templateProjectId?: string | null;
|
||||
}
|
||||
|
||||
export interface UpdateProjectInput {
|
||||
|
||||
@@ -116,6 +116,8 @@ export type CreateProjectInput = {
|
||||
/** 2-10 chars, e.g. "TS" → tickets TS-1, TS-2... */
|
||||
key: string;
|
||||
name: string;
|
||||
/** A project you own or administer to start from: its statuses, transitions, types, areas, custom fields and automation rules are copied */
|
||||
templateProjectId?: string | null | undefined;
|
||||
};
|
||||
|
||||
export type CreateReleaseInput = {
|
||||
|
||||
@@ -20,12 +20,15 @@ import {
|
||||
PiCheckboxComponent,
|
||||
PiEmptyStateComponent,
|
||||
PiLabelDirective,
|
||||
PiSelectFieldComponent,
|
||||
PiSkeletonComponent,
|
||||
PiTextareaComponent,
|
||||
PiTextFieldComponent,
|
||||
type PiSelectFieldOption,
|
||||
} from '@pi/ui';
|
||||
import { DrawerService } from '@task-stream/web/shared/ui';
|
||||
import { ProjectsFacade } from '@task-stream/web/tracker/application';
|
||||
import { ProjectRole } from '@task-stream/web/tracker/domain';
|
||||
import { AuthFacade } from '@task-stream/web/identity/application';
|
||||
import { GoToComponent } from '../ui/go-to';
|
||||
import { ProjectCardComponent } from '../ui/project-card';
|
||||
@@ -46,6 +49,7 @@ import { MyWorkComponent } from './my-work';
|
||||
PiTextFieldComponent,
|
||||
PiTextareaComponent,
|
||||
PiLabelDirective,
|
||||
PiSelectFieldComponent,
|
||||
],
|
||||
template: `
|
||||
<header class="ts-page-header">
|
||||
@@ -125,6 +129,18 @@ import { MyWorkComponent } from './my-work';
|
||||
>
|
||||
<pi-label>Descripción</pi-label>
|
||||
</pi-textarea>
|
||||
@if (templateOptions().length > 1) {
|
||||
<pi-select-field
|
||||
[formField]="f.template"
|
||||
[options]="templateOptions()"
|
||||
>
|
||||
<pi-label>Empezar con la configuración de</pi-label>
|
||||
</pi-select-field>
|
||||
<p class="ts-hint">
|
||||
Copia sus estados, transiciones, tipos, áreas, campos y
|
||||
automatizaciones. Después, cada proyecto cambia por su cuenta.
|
||||
</p>
|
||||
}
|
||||
<div class="ts-public-check">
|
||||
<pi-checkbox label="Proyecto público" [formField]="f.isPublic" />
|
||||
<p>
|
||||
@@ -164,6 +180,11 @@ import { MyWorkComponent } from './my-work';
|
||||
font-weight: 400;
|
||||
}
|
||||
}
|
||||
.ts-hint {
|
||||
margin: -0.5rem 0 0;
|
||||
color: var(--ts-muted);
|
||||
font-size: 0.8125rem;
|
||||
}
|
||||
.ts-public-check {
|
||||
p {
|
||||
/* Under the checkbox's text, past the box. */
|
||||
@@ -194,7 +215,28 @@ export class ProjectsPage implements OnInit {
|
||||
name: '',
|
||||
description: '',
|
||||
isPublic: false,
|
||||
template: '',
|
||||
});
|
||||
|
||||
/** Projects I own or administer: the ones whose configuration I can copy. */
|
||||
protected readonly templateOptions = computed<PiSelectFieldOption<string>[]>(
|
||||
() => {
|
||||
const me = this.auth.user()?.id;
|
||||
return [
|
||||
{ value: '', label: 'La de por defecto' },
|
||||
...this.facade
|
||||
.sorted()
|
||||
.filter((p) =>
|
||||
p.members.some(
|
||||
(m) =>
|
||||
m.user.id === me &&
|
||||
(m.role === ProjectRole.OWNER || m.role === ProjectRole.ADMIN),
|
||||
),
|
||||
)
|
||||
.map((p) => ({ value: p.id, label: `${p.key} · ${p.name}` })),
|
||||
];
|
||||
},
|
||||
);
|
||||
protected readonly f = form(this.model, (p) => {
|
||||
required(p.key);
|
||||
pattern(p.key, /^[A-Za-z][A-Za-z0-9]{1,9}$/, {
|
||||
@@ -211,18 +253,25 @@ export class ProjectsPage implements OnInit {
|
||||
}
|
||||
|
||||
protected openCreate(): void {
|
||||
this.model.set({ key: '', name: '', description: '', isPublic: false });
|
||||
this.model.set({
|
||||
key: '',
|
||||
name: '',
|
||||
description: '',
|
||||
isPublic: false,
|
||||
template: '',
|
||||
});
|
||||
this.drawer.open('Nuevo proyecto', this.createForm());
|
||||
}
|
||||
|
||||
protected create(): void {
|
||||
if (this.f().invalid()) return;
|
||||
const { key, name, description, isPublic } = this.model();
|
||||
const { key, name, description, isPublic, template } = this.model();
|
||||
this.facade.create({
|
||||
key: key.toUpperCase(),
|
||||
name,
|
||||
description: description || null,
|
||||
isPublic,
|
||||
templateProjectId: template || null,
|
||||
});
|
||||
this.drawer.closeAll();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user