Change DKIM and Alias domains on domainRename

This commit is contained in:
Andris Reinman 2018-09-21 10:47:15 +03:00
parent 8fc272b11e
commit fe4c09d0e0

View file

@ -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);
})
);
};