feat: enable assignee selection when creating tickets

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
rever-tecnologia 2025-10-06 11:36:19 -03:00
parent fe7025d433
commit be27dcfd15
4 changed files with 182 additions and 16 deletions

View file

@ -1,5 +1,8 @@
import { mutation, query } from "./_generated/server";
import { v } from "convex/values";
import { ConvexError, v } from "convex/values";
import { requireAdmin } from "./rbac";
const STAFF_ROLES = new Set(["ADMIN", "MANAGER", "AGENT", "COLLABORATOR"]);
export const ensureUser = mutation({
args: {
@ -69,11 +72,41 @@ export const ensureUser = mutation({
export const listAgents = query({
args: { tenantId: v.string() },
handler: async (ctx, { tenantId }) => {
const agents = await ctx.db
const users = await ctx.db
.query("users")
.withIndex("by_tenant_role", (q) => q.eq("tenantId", tenantId).eq("role", "AGENT"))
.withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
.collect();
return agents;
return users
.filter((user) => {
const normalizedRole = (user.role ?? "AGENT").toUpperCase();
return STAFF_ROLES.has(normalizedRole);
})
.sort((a, b) => a.name.localeCompare(b.name, "pt-BR"));
},
});
export const deleteUser = mutation({
args: { userId: v.id("users"), actorId: v.id("users") },
handler: async (ctx, { userId, actorId }) => {
const user = await ctx.db.get(userId);
if (!user) {
return { status: "not_found" };
}
await requireAdmin(ctx, actorId, user.tenantId);
const assignedTickets = await ctx.db
.query("tickets")
.withIndex("by_tenant_assignee", (q) => q.eq("tenantId", user.tenantId).eq("assigneeId", userId))
.take(1);
if (assignedTickets.length > 0) {
throw new ConvexError("Usuário ainda está atribuído a tickets");
}
await ctx.db.delete(userId);
return { status: "deleted" };
},
});