@@ -169,26 +169,45 @@ const getPurgeFn = () => {
169169 return purgeFn ;
170170} ;
171171
172- const getPurgedGroup = ( group , alreadyPurged , toPurge ) => {
172+ const getPurgedGroup = ( group , currentlyPurged , toPurge , previouslyPurged ) => {
173173 const contactId = group . contact . _id ;
174174 const purgedContactDocId = serverSidePurgeUtils . getPurgedGroupId ( contactId ) ;
175175
176176 const doc = {
177177 _id : purgedContactDocId ,
178- _rev : alreadyPurged [ purgedContactDocId ] ,
178+ _rev : currentlyPurged ?. [ purgedContactDocId ] ,
179179 } ;
180180
181181 if ( ! group . ids . length ) {
182- return { _deleted : true , ...doc } ;
182+ return previouslyPurged ?. [ purgedContactDocId ] ? { _deleted : true , ...doc } : undefined ;
183+ }
184+
185+ const ids = Object . fromEntries ( group . ids . map ( id => [ id , ( toPurge [ id ] && currentlyPurged [ id ] ) || false ] ) ) ;
186+ if ( previouslyPurged ?. [ purgedContactDocId ] ) {
187+ const previousIds = Object . fromEntries ( group . ids . map ( id => [ id , previouslyPurged [ id ] || false ] ) ) ;
188+ if ( ! ! previouslyPurged [ contactId ] === ! ! toPurge [ contactId ] && areGroupIdsIdentical ( ids , previousIds ) ) {
189+ return ;
190+ }
183191 }
184192
185193 return {
186194 ...doc ,
187- purged_contact : toPurge [ contactId ] || false ,
188- ids : Object . fromEntries ( group . ids . map ( id => [ id , ( toPurge [ id ] && alreadyPurged [ id ] ) || false ] ) ) ,
195+ purged_contact : ! ! toPurge [ contactId ] ,
196+ ids : ids ,
189197 } ;
190198} ;
191199
200+ const areGroupIdsIdentical = ( ids1 , ids2 ) => {
201+ if ( ids1 . length !== ids2 . length ) {
202+ return false ;
203+ }
204+
205+ const sortedIds1 = Object . keys ( ids1 ) . sort ( ) ;
206+ const sortedIds2 = Object . keys ( ids2 ) . sort ( ) ;
207+
208+ return sortedIds1 . every ( ( key , idx ) => key === sortedIds2 [ idx ] && ids1 [ key ] === ids2 [ key ] ) ;
209+ } ;
210+
192211const getPurgeUpdates = ( groups , ids , alreadyPurged , toPurge ) => {
193212 const docs = [ ] ;
194213 ids = [ ...ids , ...Object . keys ( groups ) ] ;
@@ -210,17 +229,24 @@ const getPurgeUpdates = (groups, ids, alreadyPurged, toPurge) => {
210229
211230const updatePurgedDocs = async ( rolesHashes , groups , ids , alreadyPurged , toPurge ) => {
212231 for ( const hash of rolesHashes ) {
232+ const currentlyPurged = { ...alreadyPurged [ hash ] } ;
213233 const docs = getPurgeUpdates ( groups , ids , alreadyPurged [ hash ] , toPurge [ hash ] ) ;
214234
215235 if ( docs . length ) {
216236 const results = await bulkDocs ( getPurgeDb ( hash ) , docs ) ;
217237 for ( const result of results ) {
218- alreadyPurged [ hash ] [ serverSidePurgeUtils . extractId ( result . id ) ] = result . rev ;
238+ currentlyPurged [ serverSidePurgeUtils . extractId ( result . id ) ] = result . rev ;
219239 }
220240 }
221241
222- const groupDocs = Object . values ( groups ) . map ( group => getPurgedGroup ( group , alreadyPurged [ hash ] , toPurge [ hash ] ) ) ;
223- await bulkDocs ( getPurgeDb ( hash ) , groupDocs ) ;
242+ const groupDocs = Object
243+ . values ( groups )
244+ . map ( group => getPurgedGroup ( group , currentlyPurged , toPurge [ hash ] , alreadyPurged [ hash ] ) )
245+ . filter ( Boolean ) ;
246+
247+ if ( groupDocs . length ) {
248+ await bulkDocs ( getPurgeDb ( hash ) , groupDocs ) ;
249+ }
224250 }
225251} ;
226252
0 commit comments