@@ -125,6 +125,34 @@ class BaseUserRepository extends BaseRepository {
125125 return user || null
126126 }
127127
128+ /**
129+ * Delete a user by UUID from both BaseUser and RegistryUser collections,
130+ * and remove the user reference from any organizations.
131+ *
132+ * @param {string } uuid - The UUID of the user to delete.
133+ * @param {object } options - Mongoose options for the delete operations.
134+ * @returns {Promise<number> } Number of deleted documents (should be 1 if successful).
135+ */
136+ async deleteUserByUUID ( uuid , options = { } ) {
137+ // Delete from BaseUser collection
138+ const deleteResult = await BaseUser . deleteOne ( { UUID : uuid } , options )
139+
140+ // Delete from RegistryUser collection
141+ await RegistryUser . deleteOne ( { UUID : uuid } , options )
142+
143+ // Remove user from any organization’s users and admins arrays
144+ const orgs = await BaseOrgModel . find ( { $or : [ { users : uuid } , { admins : uuid } ] } )
145+ for ( const org of orgs ) {
146+ org . users = org . users . filter ( u => u !== uuid )
147+ if ( Array . isArray ( org . admins ) ) {
148+ org . admins = org . admins . filter ( a => a !== uuid )
149+ }
150+ await org . save ( { options } )
151+ }
152+
153+ return deleteResult . deletedCount
154+ }
155+
128156 async getUserUUID ( username , orgShortname , options = { } , isLegacyObject = false ) {
129157 const user = await this . findOneByUsernameAndOrgShortname ( username , orgShortname , options , isLegacyObject )
130158 if ( user ) {
0 commit comments