From fe4c09d0e066594d2fed98771b79301584cbe03d Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Fri, 21 Sep 2018 10:47:15 +0300 Subject: [PATCH] Change DKIM and Alias domains on domainRename --- lib/api/addresses.js | 53 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/lib/api/addresses.js b/lib/api/addresses.js index 14c58262..ea1cd05d 100644 --- a/lib/api/addresses.js +++ b/lib/api/addresses.js @@ -7,6 +7,7 @@ const ObjectID = require('mongodb').ObjectID; const tools = require('../tools'); const consts = require('../consts'); const roles = require('../roles'); +const log = require('npmlog'); module.exports = (db, server) => { /** @@ -2496,6 +2497,14 @@ module.exports = (db, server) => { } }); + let response = { + success: true, + modifiedAddresses: 0, + modifiedUsers: 0, + modifiedDkim: 0, + modifiedAliases: 0 + }; + let addressData; try { while ((addressData = await cursor.next())) { @@ -2539,10 +2548,11 @@ module.exports = (db, server) => { if (updateAddresses.length) { try { - await db.users.collection('addresses').bulkWrite(updateAddresses, { + let r = await db.users.collection('addresses').bulkWrite(updateAddresses, { ordered: false, w: 1 }); + response.modifiedAddresses = r.modifiedCount; } catch (err) { res.json({ error: 'MongoDB Error: ' + err.message, @@ -2552,10 +2562,11 @@ module.exports = (db, server) => { } try { - await db.users.collection('users').bulkWrite(updateUsers, { + let r = await db.users.collection('users').bulkWrite(updateUsers, { ordered: false, w: 1 }); + response.modifiedUsers = r.modifiedCount; } catch (err) { res.json({ error: 'MongoDB Error: ' + err.message, @@ -2565,9 +2576,41 @@ module.exports = (db, server) => { } } - res.json({ - success: true - }); + // UPDATE DKIM + try { + let r = await db.database.collection('dkim').updateMany( + { + domain: oldDomain + }, + { + $set: { + domain: newDomain + } + } + ); + response.modifiedDkim = r.modifiedCount; + } catch (err) { + log.error('RenameDomain', 'DKIMERR old=%s new=%s error=%s', oldDomain, newDomain, err.message); + } + + // UPDATE ALIASES + try { + let r = await db.users.collection('domainaliases').updateMany( + { + domain: oldDomain + }, + { + $set: { + domain: newDomain + } + } + ); + response.modifiedAliases = r.modifiedCount; + } catch (err) { + log.error('RenameDomain', 'ALIASERR old=%s new=%s error=%s', oldDomain, newDomain, err.message); + } + + res.json(response); }) ); };