@@ -60,8 +60,6 @@ export class PostgresSubscriber implements EntitySubscriberInterface {
60
60
enrichedEntity . author = author ;
61
61
}
62
62
63
- console . log ( "ENRICHING," , tableName )
64
-
65
63
// Special handling for charter signatures: always load the user and substitute at userId
66
64
if ( tableName === "charter_signature" && entity . userId ) {
67
65
const user = await AppDataSource . getRepository (
@@ -83,7 +81,6 @@ export class PostgresSubscriber implements EntitySubscriberInterface {
83
81
* Called after entity insertion.
84
82
*/
85
83
async afterInsert ( event : InsertEvent < any > ) {
86
- console . log ( "afterInsert?" )
87
84
let entity = event . entity ;
88
85
if ( entity ) {
89
86
entity = ( await this . enrichEntity (
@@ -105,145 +102,71 @@ export class PostgresSubscriber implements EntitySubscriberInterface {
105
102
* Called before entity update.
106
103
*/
107
104
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
+ }
109
113
}
110
114
111
115
/**
112
116
* Called after entity update.
113
117
*/
114
118
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
-
123
119
// For updates, we need to reload the full entity since event.entity only contains changed fields
124
120
let entity = event . entity ;
125
121
126
122
// Try different ways to get the entity ID
127
123
let entityId = event . entity ?. id || event . databaseEntity ?. id ;
128
124
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 ;
136
130
}
137
131
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
161
134
}
162
135
163
- console . log ( "🔍 Final entityId:" , entityId ) ;
164
-
165
136
if ( entityId ) {
166
137
// Reload the full entity from the database
167
138
const repository = AppDataSource . getRepository ( event . metadata . target ) ;
168
139
const entityName = typeof event . metadata . target === 'function'
169
140
? event . metadata . target . name
170
141
: event . metadata . target ;
171
142
172
- console . log ( "🔍 Reloading entity:" , { entityId, entityName } ) ;
173
-
174
143
const fullEntity = await repository . findOne ( {
175
144
where : { id : entityId } ,
176
145
relations : this . getRelationsForEntity ( entityName )
177
146
} ) ;
178
147
179
148
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
-
192
149
// Check eVault creation BEFORE enriching the entity
193
150
if ( entityName === "Group" && fullEntity . charter && fullEntity . charter . trim ( ) !== "" ) {
194
- console . log ( "✅ Group entity with charter detected" ) ;
195
-
196
151
// Check if this group doesn't have an ename yet (meaning eVault wasn't created)
197
152
if ( ! fullEntity . ename ) {
198
- console . log ( "🎯 eVault creation conditions met! Group:" , fullEntity . id , "needs eVault" ) ;
199
-
200
153
// Fire and forget eVault creation
201
154
this . spinUpGroupEVault ( fullEntity ) . catch ( error => {
202
155
console . error ( "Failed to create eVault for group:" , fullEntity . id , error ) ;
203
156
} ) ;
204
- } else {
205
- console . log ( "⚠️ Group already has ename, skipping eVault creation:" , fullEntity . ename ) ;
206
157
}
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
- } ) ;
214
158
}
215
159
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
-
224
160
entity = ( await this . enrichEntity (
225
161
fullEntity ,
226
162
event . metadata . tableName ,
227
163
event . metadata . target
228
164
) ) 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
- } ) ;
237
165
} else {
238
166
console . log ( "❌ Could not load full entity for ID:" , entityId ) ;
239
167
}
240
168
} else {
241
169
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
- } ) ;
247
170
}
248
171
249
172
this . handleChange (
@@ -292,9 +215,7 @@ export class PostgresSubscriber implements EntitySubscriberInterface {
292
215
293
216
// Handle regular entity changes
294
217
const data = this . entityToPlain ( entity ) ;
295
- console . log ( data , entity )
296
218
if ( ! data . id ) return ;
297
- console . log ( "hmm?" )
298
219
299
220
try {
300
221
setTimeout ( async ( ) => {
@@ -442,9 +363,13 @@ console.log("hmm?")
442
363
443
364
console . log ( "eVault created successfully:" , evaultResult ) ;
444
365
445
- // Update the group with the ename (w3id)
366
+ // Update the group with the ename (w3id) - use save() to trigger ORM events
446
367
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
+ }
448
373
449
374
console . log ( "Group updated with ename:" , evaultResult . w3id ) ;
450
375
0 commit comments