Skip to content

Commit 9221926

Browse files
authored
fix: issue with cerberus only triggering after an edit (#433)
* fix: issue with cerberus only triggering after an edit * chore: remove logs * chore: increaase timeout to 5s
1 parent 490ae34 commit 9221926

File tree

4 files changed

+61
-31
lines changed

4 files changed

+61
-31
lines changed

platforms/cerberus/src/controllers/WebhookController.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,15 @@ export class WebhookController {
175175

176176
// Only process if there's actually a charter change, not just a message update
177177
if (newCharter !== undefined && newCharter !== null && oldCharter !== newCharter) {
178-
try {
179-
await this.cerberusTriggerService.processCharterChange(
180-
group.id,
181-
group.name,
182-
oldCharter,
183-
newCharter
184-
);
185-
} catch (error) {
178+
// Don't await - let it run asynchronously to avoid blocking webhook response
179+
this.cerberusTriggerService.processCharterChange(
180+
group.id,
181+
group.name,
182+
oldCharter,
183+
newCharter
184+
).catch((error) => {
186185
console.error("Error in processCharterChange:", error);
187-
}
186+
});
188187
}
189188
} else {
190189
// Check if group already exists by ename (only if ename is available)
@@ -223,16 +222,15 @@ export class WebhookController {
223222

224223
// Check if new group has a charter and send Cerberus welcome message
225224
if (group.charter) {
226-
try {
227-
await this.cerberusTriggerService.processCharterChange(
228-
group.id,
229-
group.name,
230-
undefined, // No old charter for new groups
231-
group.charter
232-
);
233-
} catch (error) {
225+
// Don't await - let it run asynchronously to avoid blocking webhook response
226+
this.cerberusTriggerService.processCharterChange(
227+
group.id,
228+
group.name,
229+
undefined, // No old charter for new groups
230+
group.charter
231+
).catch((error) => {
234232
console.error("Error in processCharterChange for new group:", error);
235-
}
233+
});
236234
}
237235
}
238236
}

platforms/group-charter-manager-api/src/controllers/GroupController.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Request, Response } from "express";
22
import { GroupService } from "../services/GroupService";
33
import { CharterSignatureService } from "../services/CharterSignatureService";
44
import { spinUpEVault } from "web3-adapter";
5+
import { adapter } from "../web3adapter";
56
import dotenv from "dotenv";
67

78
dotenv.config();
@@ -27,6 +28,9 @@ export class GroupController {
2728
participants: []
2829
});
2930

31+
// Lock the group so it doesn't sync until charter+ename are added
32+
adapter.addToLockedIds(group.id);
33+
3034
// Add participants including the creator
3135
const allParticipants = [...new Set([userId, ...participants])];
3236
for (const participantId of allParticipants) {
@@ -142,13 +146,41 @@ export class GroupController {
142146
updateData.ename = evaultResult.w3id;
143147
}
144148

149+
// Unlock the group so it CAN sync now that it has charter+ename
150+
if (needsEVault) {
151+
const lockIndex = adapter.lockedIds.indexOf(id);
152+
if (lockIndex > -1) {
153+
adapter.lockedIds.splice(lockIndex, 1);
154+
}
155+
}
156+
145157
// Now save with both charter and ename (if provisioned)
146158
const updatedGroup = await this.groupService.updateGroup(id, updateData);
147159

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

164+
// HACK: Manually trigger sync after delay to ensure complete data is sent
165+
if (needsEVault) {
166+
setTimeout(async () => {
167+
try {
168+
// Re-fetch the complete group entity with all relations
169+
const completeGroup = await this.groupService.getGroupById(id);
170+
if (completeGroup && completeGroup.charter && completeGroup.ename) {
171+
// Convert to plain object and trigger the adapter manually
172+
const plainGroup = JSON.parse(JSON.stringify(completeGroup));
173+
await adapter.handleChange({
174+
data: plainGroup,
175+
tableName: "groups"
176+
});
177+
}
178+
} catch (error) {
179+
console.error("Error in manual sync:", error);
180+
}
181+
}, 5000); // 5 second delay
182+
}
183+
152184
res.json(updatedGroup);
153185
} catch (error) {
154186
console.error("Error updating charter:", error);

platforms/group-charter-manager-api/src/services/GroupService.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,24 @@ export class GroupService {
2828
}
2929

3030
async updateGroup(id: string, groupData: Partial<Group>): Promise<Group | null> {
31-
// If updating the charter, we need to delete all existing signatures
32-
// since the charter content has changed
33-
if (groupData.charter !== undefined) {
34-
// Get the current group to check if charter is being updated
35-
const currentGroup = await this.getGroupById(id);
36-
if (currentGroup && currentGroup.charter !== groupData.charter) {
37-
// Charter content has changed, so delete all existing signatures
38-
console.log(`Charter updated for group ${id}, deleting all existing signatures`);
39-
await this.charterSignatureService.deleteAllSignaturesForGroup(id);
40-
}
41-
}
42-
4331
// Get the current group, merge the data, and save it to trigger ORM events
4432
const currentGroup = await this.getGroupById(id);
4533
if (!currentGroup) {
4634
throw new Error("Group not found");
4735
}
4836

37+
// Check if charter is being updated/added to delete signatures BEFORE saving
38+
const charterChanged = groupData.charter !== undefined && currentGroup.charter !== groupData.charter;
39+
if (charterChanged) {
40+
await this.charterSignatureService.deleteAllSignaturesForGroup(id);
41+
}
42+
4943
// Merge the new data with the existing group
5044
Object.assign(currentGroup, groupData);
5145

52-
// Save the merged group to trigger ORM subscribers
46+
// Save the merged group to trigger ORM subscribers - ONE save with all data
5347
const updatedGroup = await this.groupRepository.save(currentGroup);
48+
5449
return updatedGroup;
5550
}
5651

platforms/group-charter-manager-api/src/web3adapter/watchers/subscriber.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ export class PostgresSubscriber implements EntitySubscriberInterface {
200200
// Handle regular entity changes
201201
const data = this.entityToPlain(entity);
202202
if (!data.id) return;
203+
204+
// Skip groups without charter - they'll be manually synced when charter is added
205+
if (tableName === "groups" && !data.charter) {
206+
return;
207+
}
203208

204209
try {
205210
setTimeout(async () => {

0 commit comments

Comments
 (0)