Merge pull request #264 from mvanhorn/fix/232-deduplicate-agent-names
fix(server): auto-deduplicate agent names on creation
This commit is contained in:
@@ -341,13 +341,17 @@ export function agentService(db: Db) {
|
|||||||
await ensureManager(companyId, data.reportsTo);
|
await ensureManager(companyId, data.reportsTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
await assertCompanyShortnameAvailable(companyId, data.name);
|
const existingAgents = await db
|
||||||
|
.select({ id: agents.id, name: agents.name, status: agents.status })
|
||||||
|
.from(agents)
|
||||||
|
.where(eq(agents.companyId, companyId));
|
||||||
|
const uniqueName = deduplicateAgentName(data.name, existingAgents);
|
||||||
|
|
||||||
const role = data.role ?? "general";
|
const role = data.role ?? "general";
|
||||||
const normalizedPermissions = normalizeAgentPermissions(data.permissions, role);
|
const normalizedPermissions = normalizeAgentPermissions(data.permissions, role);
|
||||||
const created = await db
|
const created = await db
|
||||||
.insert(agents)
|
.insert(agents)
|
||||||
.values({ ...data, companyId, role, permissions: normalizedPermissions })
|
.values({ ...data, name: uniqueName, companyId, role, permissions: normalizedPermissions })
|
||||||
.returning()
|
.returning()
|
||||||
.then((rows) => rows[0]);
|
.then((rows) => rows[0]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user