Skip to content

Commit b7bf95b

Browse files
authored
chore: fix charter subsciber (#339)
1 parent d009738 commit b7bf95b

File tree

2 files changed

+33
-98
lines changed

2 files changed

+33
-98
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,18 @@ export class GroupService {
4040
}
4141
}
4242

43-
await this.groupRepository.update(id, groupData);
44-
return await this.getGroupById(id);
43+
// Get the current group, merge the data, and save it to trigger ORM events
44+
const currentGroup = await this.getGroupById(id);
45+
if (!currentGroup) {
46+
throw new Error("Group not found");
47+
}
48+
49+
// Merge the new data with the existing group
50+
Object.assign(currentGroup, groupData);
51+
52+
// Save the merged group to trigger ORM subscribers
53+
const updatedGroup = await this.groupRepository.save(currentGroup);
54+
return updatedGroup;
4555
}
4656

4757
async saveGroup(group: Group): Promise<Group> {

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

Lines changed: 21 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ export class PostgresSubscriber implements EntitySubscriberInterface {
6060
enrichedEntity.author = author;
6161
}
6262

63-
console.log("ENRICHING,", tableName)
64-
6563
// Special handling for charter signatures: always load the user and substitute at userId
6664
if (tableName === "charter_signature" && entity.userId) {
6765
const user = await AppDataSource.getRepository(
@@ -83,7 +81,6 @@ export class PostgresSubscriber implements EntitySubscriberInterface {
8381
* Called after entity insertion.
8482
*/
8583
async afterInsert(event: InsertEvent<any>) {
86-
console.log("afterInsert?")
8784
let entity = event.entity;
8885
if (entity) {
8986
entity = (await this.enrichEntity(
@@ -105,145 +102,71 @@ export class PostgresSubscriber implements EntitySubscriberInterface {
105102
* Called before entity update.
106103
*/
107104
beforeUpdate(event: UpdateEvent<any>) {
108-
// Handle any pre-update processing if needed
105+
// Capture the entity ID before the update happens
106+
const entityId = event.entity?.id || event.databaseEntity?.id;
107+
if (entityId) {
108+
// Store the ID temporarily for afterUpdate to use
109+
(this as any).lastUpdateEntityId = entityId;
110+
} else {
111+
console.log("⚠️ beforeUpdate: Could not capture entity ID");
112+
}
109113
}
110114

111115
/**
112116
* Called after entity update.
113117
*/
114118
async afterUpdate(event: UpdateEvent<any>) {
115-
console.log("🔍 afterUpdate triggered:", {
116-
hasEntity: !!event.entity,
117-
entityId: event.entity?.id,
118-
databaseEntity: event.databaseEntity?.id,
119-
tableName: event.metadata.tableName,
120-
target: event.metadata.target
121-
});
122-
123119
// For updates, we need to reload the full entity since event.entity only contains changed fields
124120
let entity = event.entity;
125121

126122
// Try different ways to get the entity ID
127123
let entityId = event.entity?.id || event.databaseEntity?.id;
128124

129-
if (!entityId && event.entity) {
130-
// If we have the entity but no ID, try to extract it from the entity object
131-
const entityKeys = Object.keys(event.entity);
132-
console.log("🔍 Entity keys:", entityKeys);
133-
134-
// Look for common ID field names
135-
entityId = event.entity.id || event.entity.Id || event.entity.ID || event.entity._id;
125+
// Use the entity ID captured in beforeUpdate
126+
if (!entityId && (this as any).lastUpdateEntityId) {
127+
entityId = (this as any).lastUpdateEntityId;
128+
// Clear it after use
129+
delete (this as any).lastUpdateEntityId;
136130
}
137131

138-
// If still no ID, try to find the entity by matching the changed data
139-
if (!entityId && event.entity) {
140-
try {
141-
console.log("🔍 Trying to find entity by matching changed data...");
142-
const repository = AppDataSource.getRepository(event.metadata.target);
143-
const changedData = event.entity;
144-
145-
// For Group entities, try to find by charter content
146-
if (changedData.charter) {
147-
console.log("🔍 Looking for group with charter content...");
148-
const matchingEntity = await repository.findOne({
149-
where: { charter: changedData.charter },
150-
select: ['id']
151-
});
152-
153-
if (matchingEntity) {
154-
entityId = matchingEntity.id;
155-
console.log("🔍 Found entity by charter match:", entityId);
156-
}
157-
}
158-
} catch (error) {
159-
console.log("❌ Error finding entity by changed data:", error);
160-
}
132+
if (!entityId) {
133+
return; // Skip processing this event
161134
}
162135

163-
console.log("🔍 Final entityId:", entityId);
164-
165136
if (entityId) {
166137
// Reload the full entity from the database
167138
const repository = AppDataSource.getRepository(event.metadata.target);
168139
const entityName = typeof event.metadata.target === 'function'
169140
? event.metadata.target.name
170141
: event.metadata.target;
171142

172-
console.log("🔍 Reloading entity:", { entityId, entityName });
173-
174143
const fullEntity = await repository.findOne({
175144
where: { id: entityId },
176145
relations: this.getRelationsForEntity(entityName)
177146
});
178147

179148
if (fullEntity) {
180-
console.log("✅ Full entity loaded:", { id: fullEntity.id, tableName: event.metadata.tableName });
181-
182-
// Debug: Log the exact entity we're working with
183-
console.log("🔍 Full entity details:", {
184-
id: fullEntity.id,
185-
name: fullEntity.name,
186-
charter: fullEntity.charter,
187-
ename: fullEntity.ename,
188-
entityName: entityName,
189-
isGroup: entityName === "Group"
190-
});
191-
192149
// Check eVault creation BEFORE enriching the entity
193150
if (entityName === "Group" && fullEntity.charter && fullEntity.charter.trim() !== "") {
194-
console.log("✅ Group entity with charter detected");
195-
196151
// Check if this group doesn't have an ename yet (meaning eVault wasn't created)
197152
if (!fullEntity.ename) {
198-
console.log("🎯 eVault creation conditions met! Group:", fullEntity.id, "needs eVault");
199-
200153
// Fire and forget eVault creation
201154
this.spinUpGroupEVault(fullEntity).catch(error => {
202155
console.error("Failed to create eVault for group:", fullEntity.id, error);
203156
});
204-
} else {
205-
console.log("⚠️ Group already has ename, skipping eVault creation:", fullEntity.ename);
206157
}
207-
} else {
208-
console.log("❌ eVault conditions not met:", {
209-
isGroup: entityName === "Group",
210-
hasCharter: !!fullEntity.charter,
211-
charterNotEmpty: fullEntity.charter ? fullEntity.charter.trim() !== "" : false,
212-
hasEname: !!fullEntity.ename
213-
});
214158
}
215159

216-
// Debug: Log the entity BEFORE enrichment
217-
console.log("🔍 Entity BEFORE enrichment:", {
218-
id: fullEntity.id,
219-
charter: fullEntity.charter,
220-
ename: fullEntity.ename,
221-
tableName: event.metadata.tableName
222-
});
223-
224160
entity = (await this.enrichEntity(
225161
fullEntity,
226162
event.metadata.tableName,
227163
event.metadata.target
228164
)) as ObjectLiteral;
229-
230-
// Debug: Log the entity AFTER enrichment
231-
console.log("🔍 Entity AFTER enrichment:", {
232-
id: entity.id,
233-
charter: entity.charter,
234-
ename: entity.ename,
235-
tableName: event.metadata.tableName
236-
});
237165
} else {
238166
console.log("❌ Could not load full entity for ID:", entityId);
239167
}
240168
} else {
241169
console.log("❌ No entity ID found in update event");
242-
console.log("🔍 Event details:", {
243-
entity: event.entity,
244-
databaseEntity: event.databaseEntity,
245-
metadata: event.metadata
246-
});
247170
}
248171

249172
this.handleChange(
@@ -292,9 +215,7 @@ export class PostgresSubscriber implements EntitySubscriberInterface {
292215

293216
// Handle regular entity changes
294217
const data = this.entityToPlain(entity);
295-
console.log(data, entity)
296218
if (!data.id) return;
297-
console.log("hmm?")
298219

299220
try {
300221
setTimeout(async () => {
@@ -442,9 +363,13 @@ console.log("hmm?")
442363

443364
console.log("eVault created successfully:", evaultResult);
444365

445-
// Update the group with the ename (w3id)
366+
// Update the group with the ename (w3id) - use save() to trigger ORM events
446367
const groupRepository = AppDataSource.getRepository("Group");
447-
await groupRepository.update(group.id, { ename: evaultResult.w3id });
368+
const groupToUpdate = await groupRepository.findOne({ where: { id: group.id } });
369+
if (groupToUpdate) {
370+
groupToUpdate.ename = evaultResult.w3id;
371+
await groupRepository.save(groupToUpdate);
372+
}
448373

449374
console.log("Group updated with ename:", evaultResult.w3id);
450375

0 commit comments

Comments
 (0)