This commit is contained in:
Bryan1029384756
2026-04-18 15:41:51 -05:00
parent 938df217f4
commit 593eaba82e
47 changed files with 3539 additions and 212 deletions

View File

@@ -3,6 +3,7 @@ import { v } from "convex/values";
import { GenericMutationCtx } from "convex/server";
import { DataModel, Id } from "./_generated/dataModel";
import { internal } from "./_generated/api";
import { AUDIT_ACTIONS, logAudit } from "./audit";
type TableWithChannelIndex =
| "channelKeys"
@@ -74,6 +75,7 @@ export const create = mutation({
categoryId: v.optional(v.id("categories")),
topic: v.optional(v.string()),
position: v.optional(v.number()),
actorId: v.optional(v.id("userProfiles")),
},
returns: v.object({ id: v.id("channels") }),
handler: async (ctx, args) => {
@@ -112,6 +114,17 @@ export const create = mutation({
position,
});
if (args.actorId) {
await logAudit(ctx, {
actorId: args.actorId,
action: AUDIT_ACTIONS.CHANNEL_CREATE,
targetType: "channel",
targetId: id,
targetName: args.name,
metadata: { type: args.type || "text" },
});
}
return { id };
},
});
@@ -136,6 +149,7 @@ export const rename = mutation({
args: {
id: v.id("channels"),
name: v.string(),
actorId: v.optional(v.id("userProfiles")),
},
returns: v.object({
_id: v.id("channels"),
@@ -156,7 +170,20 @@ export const rename = mutation({
throw new Error("Channel not found");
}
const oldName = channel.name;
await ctx.db.patch(args.id, { name: args.name });
if (args.actorId && oldName !== args.name) {
await logAudit(ctx, {
actorId: args.actorId,
action: AUDIT_ACTIONS.CHANNEL_RENAME,
targetType: "channel",
targetId: args.id,
targetName: args.name,
metadata: { from: oldName, to: args.name },
});
}
return { ...channel, name: args.name };
},
});
@@ -205,7 +232,10 @@ export const reorderChannels = mutation({
// Delete channel + cascade messages and keys
export const remove = mutation({
args: { id: v.id("channels") },
args: {
id: v.id("channels"),
actorId: v.optional(v.id("userProfiles")),
},
returns: v.object({ success: v.boolean() }),
handler: async (ctx, args) => {
const channel = await ctx.db.get(args.id);
@@ -213,6 +243,9 @@ export const remove = mutation({
throw new Error("Channel not found");
}
const deletedName = channel.name;
const deletedType = channel.type;
// Delete reactions for all messages in this channel
const messages = await ctx.db
.query("messages")
@@ -240,6 +273,17 @@ export const remove = mutation({
await ctx.db.delete(args.id);
if (args.actorId) {
await logAudit(ctx, {
actorId: args.actorId,
action: AUDIT_ACTIONS.CHANNEL_DELETE,
targetType: "channel",
targetId: args.id,
targetName: deletedName,
metadata: { type: deletedType },
});
}
return { success: true };
},
});