Skip to content

Commit 20d5918

Browse files
committed
Prevent duplicate usage of tool
1 parent f7fb513 commit 20d5918

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

dbschema/tool.gel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Tool {
1919
overloaded required container: default::Resource {
2020
on target delete delete source;
2121
}
22+
constraint exclusive on ((.tool, .container));
2223

2324
startDate: cal::local_date;
2425
}

src/components/tools/tool-usage/tool-usage.gel.repository.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,19 @@ export class ToolUsageRepository
5252
}));
5353
},
5454
);
55+
56+
async usageFor(container: ID<'Resource'>, tool: ID<'Tool'>) {
57+
return await this.db.run(this.usageForQuery, { container, tool });
58+
}
59+
private readonly usageForQuery = e.params(
60+
{ container: e.uuid, tool: e.uuid },
61+
($) =>
62+
e.select(e.Tool.Usage, (usage) => ({
63+
...this.hydrate(usage),
64+
filter_single: {
65+
tool: e.cast(e.Tool, $.tool),
66+
container: e.cast(e.Resource, $.container),
67+
},
68+
})),
69+
);
5570
}

src/components/tools/tool-usage/tool-usage.neo4j.repository.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ export class ToolUsageRepository extends DtoRepository(ToolUsage) {
9292
return result;
9393
}
9494

95+
async usageFor(container: ID<'Resource'>, tool: ID<'Tool'>) {
96+
const res = await this.db
97+
.query()
98+
.match([
99+
node('container', 'BaseNode', { id: container }),
100+
relation('out', '', 'uses', ACTIVE),
101+
node('node', 'ToolUsage'),
102+
relation('out', '', 'tool', ACTIVE),
103+
node('tool', 'Tool', { id: tool }),
104+
])
105+
.apply(this.hydrate())
106+
.first();
107+
return res?.dto ?? null;
108+
}
109+
95110
async create(input: CreateToolUsage) {
96111
const initialProps = {
97112
startDate: input.startDate,

src/components/tools/tool-usage/tool-usage.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Injectable } from '@nestjs/common';
22
import { mapKeys } from '@seedcompany/common';
33
import {
4+
DuplicateException,
45
type ID,
56
InputException,
67
isIdLike,
@@ -112,6 +113,13 @@ export class ToolUsageService {
112113
) {
113114
throw new InputException('No recursion');
114115
}
116+
const prev = await this.repo.usageFor(input.container, input.tool);
117+
if (prev) {
118+
throw new DuplicateException(
119+
'tool',
120+
'This resource already uses this tool',
121+
);
122+
}
115123

116124
const dto = await this.repo.create(input);
117125
this.privileges

0 commit comments

Comments
 (0)