@@ -172,27 +172,60 @@ export async function convertSlurMetadataFromDBtoJSON() {
172
172
}
173
173
}
174
174
175
+ export async function deleteSlurMetadataEntries ( entriesToDelete ) {
176
+ if ( ! Array . isArray ( entriesToDelete ) || entriesToDelete . length === 0 ) {
177
+ console . warn ( "No valid entries provided for deletion." ) ;
178
+ return ;
179
+ }
180
+
181
+ try {
182
+ for ( const entry of entriesToDelete ) {
183
+ await db . words_metadata
184
+ . filter ( dbEntry =>
185
+ dbEntry . label === entry . label &&
186
+ dbEntry . level_of_severity === entry . level_of_severity &&
187
+ dbEntry . meaning === entry . meaning &&
188
+ JSON . stringify ( dbEntry . categories ) === JSON . stringify ( entry . categories ) &&
189
+ dbEntry . language === entry . language &&
190
+ dbEntry . timestamp === entry . timestamp
191
+ )
192
+ . delete ( ) ;
193
+ }
194
+ console . log ( `${ entriesToDelete . length } metadata entries deleted.` ) ;
195
+ } catch ( error ) {
196
+ console . error ( 'Error deleting slur metadata:' , error ) ;
197
+ }
198
+ }
199
+
175
200
export async function fetchPublicSlursMetadata ( ) {
176
201
try {
177
202
// Fetch slurs from the backend
178
203
const publicSlursMetadata = await getPublicSlursMetadata ( ) ;
179
204
// Fetch existing metadata from the indexed database
180
205
const existingMetadata = await getAllSlurMetadata ( ) ;
181
- // Convert existing metadata objects to JSON strings for comparison
206
+ // Convert existing metadata to a Set of JSON strings for exact comparison
207
+ const publicMetadataSet = new Set ( publicSlursMetadata . map ( meta => JSON . stringify ( meta ) ) ) ;
182
208
const existingMetadataSet = new Set ( existingMetadata . map ( meta => JSON . stringify ( meta ) ) ) ;
183
- // Filter out metadata entries that already exist
209
+ // Identify metadata that needs to be added (exists in fetched data but not in DB)
184
210
const newMetadata = publicSlursMetadata . filter ( meta =>
185
211
! existingMetadataSet . has ( JSON . stringify ( meta ) )
186
212
) ;
187
- if ( newMetadata . length === 0 ) {
188
- console . log ( "All public slurs metadata already exist in the database. Skipping addition." ) ;
189
- return ;
213
+ // Identify metadata that needs to be removed (exists in DB but not in fetched data)
214
+ const outdatedMetadata = existingMetadata . filter ( meta =>
215
+ ! publicMetadataSet . has ( JSON . stringify ( meta ) )
216
+ ) ;
217
+ // Add new metadata
218
+ if ( newMetadata . length > 0 ) {
219
+ await bulkAddSlurMetadata ( newMetadata ) ;
220
+ console . log ( `${ newMetadata . length } new slur metadata entries added.` ) ;
221
+ }
222
+ // Delete outdated metadata
223
+ if ( outdatedMetadata . length > 0 ) {
224
+ await deleteSlurMetadataEntries ( outdatedMetadata ) ;
225
+ console . log ( `${ outdatedMetadata . length } outdated slur metadata entries removed.` ) ;
190
226
}
191
-
192
- await bulkAddSlurMetadata ( newMetadata ) ;
193
- console . log ( `${ newMetadata . length } new slur metadata entries added.` ) ;
194
227
} catch ( error ) {
195
- console . error ( 'Error fetching and adding public slurs metadata:' , error ) ;
228
+ console . error ( 'Error fetching and updating public slurs metadata:' , error ) ;
196
229
}
197
230
}
198
231
0 commit comments