Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions platforms/cerberus/src/controllers/WebhookController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,15 @@ export class WebhookController {

// Only process if there's actually a charter change, not just a message update
if (newCharter !== undefined && newCharter !== null && oldCharter !== newCharter) {
try {
await this.cerberusTriggerService.processCharterChange(
group.id,
group.name,
oldCharter,
newCharter
);
} catch (error) {
// Don't await - let it run asynchronously to avoid blocking webhook response
this.cerberusTriggerService.processCharterChange(
group.id,
group.name,
oldCharter,
newCharter
).catch((error) => {
console.error("Error in processCharterChange:", error);
}
});
}
} else {
// Check if group already exists by ename (only if ename is available)
Expand Down Expand Up @@ -223,16 +222,15 @@ export class WebhookController {

// Check if new group has a charter and send Cerberus welcome message
if (group.charter) {
try {
await this.cerberusTriggerService.processCharterChange(
group.id,
group.name,
undefined, // No old charter for new groups
group.charter
);
} catch (error) {
// Don't await - let it run asynchronously to avoid blocking webhook response
this.cerberusTriggerService.processCharterChange(
group.id,
group.name,
undefined, // No old charter for new groups
group.charter
).catch((error) => {
console.error("Error in processCharterChange for new group:", error);
}
});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Request, Response } from "express";
import { GroupService } from "../services/GroupService";
import { CharterSignatureService } from "../services/CharterSignatureService";
import { spinUpEVault } from "web3-adapter";
import { adapter } from "../web3adapter";
import dotenv from "dotenv";

dotenv.config();
Expand All @@ -27,6 +28,9 @@ export class GroupController {
participants: []
});

// Lock the group so it doesn't sync until charter+ename are added
adapter.addToLockedIds(group.id);

// Add participants including the creator
const allParticipants = [...new Set([userId, ...participants])];
for (const participantId of allParticipants) {
Expand Down Expand Up @@ -142,13 +146,41 @@ export class GroupController {
updateData.ename = evaultResult.w3id;
}

// Unlock the group so it CAN sync now that it has charter+ename
if (needsEVault) {
const lockIndex = adapter.lockedIds.indexOf(id);
if (lockIndex > -1) {
adapter.lockedIds.splice(lockIndex, 1);
}
}

// Now save with both charter and ename (if provisioned)
const updatedGroup = await this.groupService.updateGroup(id, updateData);

if (!updatedGroup) {
return res.status(404).json({ error: "Group not found" });
}

// HACK: Manually trigger sync after delay to ensure complete data is sent
if (needsEVault) {
setTimeout(async () => {
try {
// Re-fetch the complete group entity with all relations
const completeGroup = await this.groupService.getGroupById(id);
if (completeGroup && completeGroup.charter && completeGroup.ename) {
// Convert to plain object and trigger the adapter manually
const plainGroup = JSON.parse(JSON.stringify(completeGroup));
await adapter.handleChange({
data: plainGroup,
tableName: "groups"
});
}
} catch (error) {
console.error("Error in manual sync:", error);
}
}, 2000); // 2 second delay
}

res.json(updatedGroup);
} catch (error) {
console.error("Error updating charter:", error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,24 @@ export class GroupService {
}

async updateGroup(id: string, groupData: Partial<Group>): Promise<Group | null> {
// If updating the charter, we need to delete all existing signatures
// since the charter content has changed
if (groupData.charter !== undefined) {
// Get the current group to check if charter is being updated
const currentGroup = await this.getGroupById(id);
if (currentGroup && currentGroup.charter !== groupData.charter) {
// Charter content has changed, so delete all existing signatures
console.log(`Charter updated for group ${id}, deleting all existing signatures`);
await this.charterSignatureService.deleteAllSignaturesForGroup(id);
}
}

// Get the current group, merge the data, and save it to trigger ORM events
const currentGroup = await this.getGroupById(id);
if (!currentGroup) {
throw new Error("Group not found");
}

// Check if charter is being updated/added to delete signatures BEFORE saving
const charterChanged = groupData.charter !== undefined && currentGroup.charter !== groupData.charter;
if (charterChanged) {
await this.charterSignatureService.deleteAllSignaturesForGroup(id);
}

// Merge the new data with the existing group
Object.assign(currentGroup, groupData);

// Save the merged group to trigger ORM subscribers
// Save the merged group to trigger ORM subscribers - ONE save with all data
const updatedGroup = await this.groupRepository.save(currentGroup);

return updatedGroup;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ export class PostgresSubscriber implements EntitySubscriberInterface {
// Handle regular entity changes
const data = this.entityToPlain(entity);
if (!data.id) return;

// Skip groups without charter - they'll be manually synced when charter is added
if (tableName === "groups" && !data.charter) {
return;
}

try {
setTimeout(async () => {
Expand Down