Skip to content

Commit 61919ac

Browse files
authored
fix(wf-def): disallow template editing (#2661)
1 parent 6900f0b commit 61919ac

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

services/workflows-service/src/workflow-defintion/workflow-definition.repository.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,19 @@ export class WorkflowDefinitionRepository {
122122
projectIds: TProjectIds,
123123
noValidate = false,
124124
): Promise<Prisma.BatchPayload> {
125+
const workflowDefinition = await this.prisma.workflowDefinition.findUnique({
126+
where: { id },
127+
select: { isPublic: true },
128+
});
129+
130+
if (workflowDefinition?.isPublic) {
131+
throw new Error('Cannot update public workflow definition templates');
132+
}
133+
125134
const scopedArgs = this.scopeService.scopeUpdateMany(
126135
{
127136
...args,
128-
where: { id },
137+
where: { id, isPublic: false },
129138
},
130139
projectIds,
131140
);
@@ -142,6 +151,14 @@ export class WorkflowDefinitionRepository {
142151
args: Prisma.SelectSubset<T, Omit<Prisma.WorkflowDefinitionDeleteArgs, 'where'>>,
143152
projectIds: TProjectIds,
144153
): Promise<WorkflowDefinition> {
154+
const workflowDefinition = await this.prisma.workflowDefinition.findUnique({
155+
where: { id },
156+
select: { isPublic: true },
157+
});
158+
159+
if (workflowDefinition?.isPublic) {
160+
throw new Error('Cannot delete public workflow definition templates');
161+
}
145162
return await this.prisma.workflowDefinition.delete(
146163
this.scopeService.scopeDelete(
147164
{

services/workflows-service/src/workflow-defintion/workflow-definition.service.intg.test.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { ClsService } from 'nestjs-cls';
1616
import { ApiKeyService } from '@/customer/api-key/api-key.service';
1717
import { ApiKeyRepository } from '@/customer/api-key/api-key.repository';
1818

19-
const buildWorkflowDefinition = (sequenceNum: number, projectId?: string) => {
19+
const buildWorkflowDefinition = (sequenceNum: number, projectId?: string, isPublic = false) => {
2020
return {
2121
id: sequenceNum.toString(),
2222
name: `name ${sequenceNum}`,
@@ -42,7 +42,7 @@ const buildWorkflowDefinition = (sequenceNum: number, projectId?: string) => {
4242
schema: {},
4343
},
4444
projectId: projectId,
45-
isPublic: false,
45+
isPublic: isPublic,
4646
};
4747
};
4848

@@ -83,7 +83,7 @@ describe('WorkflowDefinitionService', () => {
8383

8484
beforeEach(async () => {
8585
await prismaService.workflowDefinition.create({
86-
data: buildWorkflowDefinition(1),
86+
data: buildWorkflowDefinition(Math.floor(Math.random() * 1000) + 1),
8787
});
8888

8989
const customer = await createCustomer(
@@ -242,4 +242,41 @@ describe('WorkflowDefinitionService', () => {
242242
expect(latestWorkflowVersion.id).toEqual(updatedWorkflowDefintiion.id);
243243
});
244244
});
245+
246+
describe('Public records (templates)', () => {
247+
it('should not allow editing of public records', async () => {
248+
// Arrange
249+
const publicWorkflowDefinition = await prismaService.workflowDefinition.create({
250+
data: buildWorkflowDefinition(11, undefined, true),
251+
});
252+
253+
const updateArgs = {
254+
definition: { some: 'new definition' },
255+
};
256+
257+
// Act & Assert
258+
await expect(
259+
workflowDefinitionService.updateById(publicWorkflowDefinition.id, updateArgs as any, [
260+
project.id,
261+
]),
262+
).rejects.toThrow('Cannot update public workflow definition templates');
263+
});
264+
265+
it('should allow reading of public records', async () => {
266+
// Arrange
267+
const publicWorkflowDefinition = await prismaService.workflowDefinition.create({
268+
data: buildWorkflowDefinition(23, undefined, true),
269+
});
270+
271+
// Act
272+
const result = await workflowDefinitionService.getLatestVersion(publicWorkflowDefinition.id, [
273+
project.id,
274+
]);
275+
276+
// Assert
277+
expect(result).toBeDefined();
278+
expect(result.id).toEqual(publicWorkflowDefinition.id);
279+
expect(result.isPublic).toBe(true);
280+
});
281+
});
245282
});

0 commit comments

Comments
 (0)