Add URL-based change review and selective change-set rollback
This commit is contained in:
@@ -49,11 +49,14 @@ export type SnapshotState = {
|
||||
};
|
||||
|
||||
export type ChangeItem = {
|
||||
id: string;
|
||||
entity: "calendar_event" | "contact_note" | "message" | "deal";
|
||||
entityId: string | null;
|
||||
action: "created" | "updated" | "deleted";
|
||||
title: string;
|
||||
before: string;
|
||||
after: string;
|
||||
undo: UndoOp[];
|
||||
};
|
||||
|
||||
type UndoOp =
|
||||
@@ -71,6 +74,7 @@ export type ChangeSet = {
|
||||
summary: string;
|
||||
items: ChangeItem[];
|
||||
undo: UndoOp[];
|
||||
rolledBackItemIds: string[];
|
||||
};
|
||||
|
||||
function fmt(val: string | null | undefined) {
|
||||
@@ -190,18 +194,24 @@ export async function captureSnapshot(prisma: PrismaClient, teamId: string): Pro
|
||||
export function buildChangeSet(before: SnapshotState, after: SnapshotState): ChangeSet | null {
|
||||
const items: ChangeItem[] = [];
|
||||
const undo: UndoOp[] = [];
|
||||
const pushItem = (item: Omit<ChangeItem, "id">) => {
|
||||
const next: ChangeItem = { ...item, id: randomUUID() };
|
||||
items.push(next);
|
||||
undo.push(...next.undo);
|
||||
};
|
||||
|
||||
for (const [id, row] of after.calendarById) {
|
||||
const prev = before.calendarById.get(id);
|
||||
if (!prev) {
|
||||
items.push({
|
||||
pushItem({
|
||||
entity: "calendar_event",
|
||||
entityId: row.id,
|
||||
action: "created",
|
||||
title: `Event created: ${row.title}`,
|
||||
before: "",
|
||||
after: toCalendarText(row),
|
||||
undo: [{ kind: "delete_calendar_event", id }],
|
||||
});
|
||||
undo.push({ kind: "delete_calendar_event", id });
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
@@ -214,95 +224,104 @@ export function buildChangeSet(before: SnapshotState, after: SnapshotState): Cha
|
||||
fmt(prev.archivedAt) !== fmt(row.archivedAt) ||
|
||||
prev.contactId !== row.contactId
|
||||
) {
|
||||
items.push({
|
||||
pushItem({
|
||||
entity: "calendar_event",
|
||||
entityId: row.id,
|
||||
action: "updated",
|
||||
title: `Event updated: ${row.title}`,
|
||||
before: toCalendarText(prev),
|
||||
after: toCalendarText(row),
|
||||
undo: [{ kind: "restore_calendar_event", data: prev }],
|
||||
});
|
||||
undo.push({ kind: "restore_calendar_event", data: prev });
|
||||
}
|
||||
}
|
||||
|
||||
for (const [id, row] of before.calendarById) {
|
||||
if (after.calendarById.has(id)) continue;
|
||||
items.push({
|
||||
pushItem({
|
||||
entity: "calendar_event",
|
||||
entityId: row.id,
|
||||
action: "deleted",
|
||||
title: `Event archived: ${row.title}`,
|
||||
before: toCalendarText(row),
|
||||
after: "",
|
||||
undo: [{ kind: "restore_calendar_event", data: row }],
|
||||
});
|
||||
undo.push({ kind: "restore_calendar_event", data: row });
|
||||
}
|
||||
|
||||
for (const [contactId, row] of after.noteByContactId) {
|
||||
const prev = before.noteByContactId.get(contactId);
|
||||
if (!prev) {
|
||||
items.push({
|
||||
pushItem({
|
||||
entity: "contact_note",
|
||||
entityId: contactId,
|
||||
action: "created",
|
||||
title: `Summary added: ${row.contactName}`,
|
||||
before: "",
|
||||
after: row.content,
|
||||
undo: [{ kind: "restore_contact_note", contactId, content: null }],
|
||||
});
|
||||
undo.push({ kind: "restore_contact_note", contactId, content: null });
|
||||
continue;
|
||||
}
|
||||
if (prev.content !== row.content) {
|
||||
items.push({
|
||||
pushItem({
|
||||
entity: "contact_note",
|
||||
entityId: contactId,
|
||||
action: "updated",
|
||||
title: `Summary updated: ${row.contactName}`,
|
||||
before: prev.content,
|
||||
after: row.content,
|
||||
undo: [{ kind: "restore_contact_note", contactId, content: prev.content }],
|
||||
});
|
||||
undo.push({ kind: "restore_contact_note", contactId, content: prev.content });
|
||||
}
|
||||
}
|
||||
|
||||
for (const [contactId, row] of before.noteByContactId) {
|
||||
if (after.noteByContactId.has(contactId)) continue;
|
||||
items.push({
|
||||
pushItem({
|
||||
entity: "contact_note",
|
||||
entityId: contactId,
|
||||
action: "deleted",
|
||||
title: `Summary cleared: ${row.contactName}`,
|
||||
before: row.content,
|
||||
after: "",
|
||||
undo: [{ kind: "restore_contact_note", contactId, content: row.content }],
|
||||
});
|
||||
undo.push({ kind: "restore_contact_note", contactId, content: row.content });
|
||||
}
|
||||
|
||||
for (const [id, row] of after.messageById) {
|
||||
if (before.messageById.has(id)) continue;
|
||||
items.push({
|
||||
pushItem({
|
||||
entity: "message",
|
||||
entityId: row.id,
|
||||
action: "created",
|
||||
title: `Message created: ${row.contactName}`,
|
||||
before: "",
|
||||
after: toMessageText(row),
|
||||
undo: [{ kind: "delete_contact_message", id }],
|
||||
});
|
||||
undo.push({ kind: "delete_contact_message", id });
|
||||
}
|
||||
|
||||
for (const [id, row] of after.dealById) {
|
||||
const prev = before.dealById.get(id);
|
||||
if (!prev) continue;
|
||||
if (prev.stage !== row.stage || fmt(prev.nextStep) !== fmt(row.nextStep) || fmt(prev.summary) !== fmt(row.summary)) {
|
||||
items.push({
|
||||
pushItem({
|
||||
entity: "deal",
|
||||
entityId: row.id,
|
||||
action: "updated",
|
||||
title: `Deal updated: ${row.title}`,
|
||||
before: toDealText(prev),
|
||||
after: toDealText(row),
|
||||
});
|
||||
undo.push({
|
||||
kind: "restore_deal",
|
||||
id,
|
||||
stage: prev.stage,
|
||||
nextStep: prev.nextStep,
|
||||
summary: prev.summary,
|
||||
undo: [
|
||||
{
|
||||
kind: "restore_deal",
|
||||
id,
|
||||
stage: prev.stage,
|
||||
nextStep: prev.nextStep,
|
||||
summary: prev.summary,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -320,11 +339,12 @@ export function buildChangeSet(before: SnapshotState, after: SnapshotState): Cha
|
||||
summary: `Created: ${created}, Updated: ${updated}, Archived: ${deleted}`,
|
||||
items,
|
||||
undo,
|
||||
rolledBackItemIds: [],
|
||||
};
|
||||
}
|
||||
|
||||
export async function rollbackChangeSet(prisma: PrismaClient, teamId: string, changeSet: ChangeSet) {
|
||||
const ops = [...changeSet.undo].reverse();
|
||||
async function applyUndoOps(prisma: PrismaClient, teamId: string, undoOps: UndoOp[]) {
|
||||
const ops = [...undoOps].reverse();
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
for (const op of ops) {
|
||||
@@ -424,3 +444,31 @@ export async function rollbackChangeSet(prisma: PrismaClient, teamId: string, ch
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function rollbackChangeSet(prisma: PrismaClient, teamId: string, changeSet: ChangeSet) {
|
||||
await applyUndoOps(prisma, teamId, changeSet.undo);
|
||||
}
|
||||
|
||||
export async function rollbackChangeSetItems(
|
||||
prisma: PrismaClient,
|
||||
teamId: string,
|
||||
changeSet: ChangeSet,
|
||||
itemIds: string[],
|
||||
) {
|
||||
const wanted = new Set(itemIds.filter(Boolean));
|
||||
if (!wanted.size) return;
|
||||
|
||||
const itemUndoOps = changeSet.items
|
||||
.filter((item) => wanted.has(item.id))
|
||||
.flatMap((item) => (Array.isArray(item.undo) ? item.undo : []));
|
||||
|
||||
if (itemUndoOps.length > 0) {
|
||||
await applyUndoOps(prisma, teamId, itemUndoOps);
|
||||
return;
|
||||
}
|
||||
|
||||
// Legacy fallback for old change sets without per-item undo.
|
||||
if (wanted.size >= changeSet.items.length && Array.isArray(changeSet.undo) && changeSet.undo.length > 0) {
|
||||
await applyUndoOps(prisma, teamId, changeSet.undo);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user