feat: keep deletions restorable and confirm a pipeline's deals before it is removed
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -69,6 +69,15 @@ export class PipelinesController {
|
||||
return this.pipelinesService.merge(user.organizationId, pipelineId, dto);
|
||||
}
|
||||
|
||||
@Delete(':pipelineId')
|
||||
@Roles('owner', 'admin')
|
||||
remove(
|
||||
@CurrentUser() user: { organizationId: string },
|
||||
@Param('pipelineId') pipelineId: string,
|
||||
) {
|
||||
return this.pipelinesService.remove(user.organizationId, pipelineId);
|
||||
}
|
||||
|
||||
@Post(':pipelineId/stages')
|
||||
@Roles('owner', 'admin')
|
||||
createStage(
|
||||
|
||||
@@ -107,7 +107,18 @@ export class PipelinesService {
|
||||
include: { stages: { orderBy: { position: 'asc' } } },
|
||||
orderBy: [{ isDefault: 'desc' }, { name: 'asc' }],
|
||||
});
|
||||
return rows.map((row) => this.toPipelineDto(row));
|
||||
const counts = await this.prisma.deal.groupBy({
|
||||
by: ['pipelineId'],
|
||||
where: { organizationId },
|
||||
_count: { _all: true },
|
||||
});
|
||||
const dealCountByPipeline = new Map(
|
||||
counts.map((row) => [row.pipelineId, row._count._all]),
|
||||
);
|
||||
return rows.map((row) => ({
|
||||
...this.toPipelineDto(row),
|
||||
dealCount: dealCountByPipeline.get(row.id) ?? 0,
|
||||
}));
|
||||
}
|
||||
|
||||
async create(
|
||||
@@ -192,6 +203,29 @@ export class PipelinesService {
|
||||
return this.getOwnedPipelineDto(organizationId, pipeline.id);
|
||||
}
|
||||
|
||||
async remove(organizationId: string, pipelineId: string): Promise<void> {
|
||||
const pipeline = await this.getOwnedPipeline(organizationId, pipelineId);
|
||||
await this.prisma.$transaction(async (tx) => {
|
||||
await tx.deal.deleteMany({
|
||||
where: { organizationId, pipelineId: pipeline.id },
|
||||
});
|
||||
if (pipeline.isDefault) {
|
||||
const next = await tx.pipeline.findFirst({
|
||||
where: { organizationId, id: { not: pipeline.id } },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
select: { id: true },
|
||||
});
|
||||
if (next) {
|
||||
await tx.pipeline.update({
|
||||
where: { id: next.id },
|
||||
data: { isDefault: true },
|
||||
});
|
||||
}
|
||||
}
|
||||
await tx.pipeline.delete({ where: { id: pipeline.id } });
|
||||
});
|
||||
}
|
||||
|
||||
async deleteStage(
|
||||
organizationId: string,
|
||||
pipelineId: string,
|
||||
|
||||
Reference in New Issue
Block a user