From 43dac9a2ee9dccef052d608da56a2e2d909fd958 Mon Sep 17 00:00:00 2001 From: David McFadzean Date: Wed, 20 Mar 2024 12:25:25 -0400 Subject: [PATCH] Refactored verifyDb --- gatekeeper.js | 16 ++++++++++++++++ server.js | 14 ++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/gatekeeper.js b/gatekeeper.js index 2b6912b3..bfcd8217 100644 --- a/gatekeeper.js +++ b/gatekeeper.js @@ -34,6 +34,7 @@ export function writeDb(db) { export async function verifyDb() { const db = loadDb(); + const backup = JSON.parse(JSON.stringify(db)); if (!db.anchors) { return 0; @@ -52,9 +53,24 @@ export async function verifyDb() { catch (error) { console.log(`${n} ${did} ${error}`); invalid += 1; + + if (db.anchors[did]) { + const anchor = db.anchors[did]; + const registry = anchor.mdip.registry; + delete db[registry][did]; + delete db.anchors[did]; + } } } + if (invalid > 0) { + const today = new Date(); + const dateString = today.toISOString().split('T')[0]; + const backupName = `${dataFolder}/mdip.backup.${dateString}.json`; + fs.writeFileSync(backupName, JSON.stringify(backup, null, 4)); + writeDb(db); + } + return invalid; } diff --git a/server.js b/server.js index d41f7c9a..b24d1e86 100644 --- a/server.js +++ b/server.js @@ -127,15 +127,13 @@ const port = 3000; app.use('/api/v1', v1router); gatekeeper.verifyDb().then((invalid) => { - if (invalid === 0) { - app.listen(port, () => { - console.log(`Server is running on port ${port}`); - }); - } - else { - console.log(`${invalid} invalid DIDs in MDIP db`); - process.exit(); + if (invalid > 0) { + console.log(`${invalid} invalid DIDs removed from MDIP db`); } + + app.listen(port, () => { + console.log(`Server is running on port ${port}`); + }); }); process.on('uncaughtException', (error) => {