allow renaming domains

This commit is contained in:
Andris Reinman 2018-02-19 16:12:43 +02:00
parent f9566ce2a7
commit 3bc95d645b
5 changed files with 164 additions and 4 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1 +1 @@
define({ "name": "wildduck", "version": "1.0.0", "description": "WildDuck API docs", "title": "WildDuck API", "url": "https://api.wildduck.email", "sampleUrl": false, "defaultVersion": "0.0.0", "apidoc": "0.3.0", "generator": { "name": "apidoc", "time": "2018-01-30T14:14:01.100Z", "url": "http://apidocjs.com", "version": "0.17.6" } });
define({ "name": "wildduck", "version": "1.0.0", "description": "WildDuck API docs", "title": "WildDuck API", "url": "https://api.wildduck.email", "sampleUrl": false, "defaultVersion": "0.0.0", "apidoc": "0.3.0", "generator": { "name": "apidoc", "time": "2018-02-19T14:11:09.580Z", "url": "http://apidocjs.com", "version": "0.17.6" } });

View file

@ -1 +1 @@
{ "name": "wildduck", "version": "1.0.0", "description": "WildDuck API docs", "title": "WildDuck API", "url": "https://api.wildduck.email", "sampleUrl": false, "defaultVersion": "0.0.0", "apidoc": "0.3.0", "generator": { "name": "apidoc", "time": "2018-01-30T14:14:01.100Z", "url": "http://apidocjs.com", "version": "0.17.6" } }
{ "name": "wildduck", "version": "1.0.0", "description": "WildDuck API docs", "title": "WildDuck API", "url": "https://api.wildduck.email", "sampleUrl": false, "defaultVersion": "0.0.0", "apidoc": "0.3.0", "generator": { "name": "apidoc", "time": "2018-02-19T14:11:09.580Z", "url": "http://apidocjs.com", "version": "0.17.6" } }

View file

@ -2303,4 +2303,164 @@ module.exports = (db, server) => {
});
});
});
/**
* @api {put} /addresses/renameDomain Rename domain in addresses
* @apiName PutRenameDomain
* @apiGroup Addresses
* @apiHeader {String} X-Access-Token Optional access token if authentication is enabled
* @apiHeaderExample {json} Header-Example:
* {
* "X-Access-Token": "59fc66a03e54454869460e45"
* }
*
* @apiParam {String} oldDomain Old Domain Name
* @apiParam {String} newDomain New Domain Name
*
* @apiSuccess {Boolean} success Indicates successful response
*
* @apiError error Description of the error
*
* @apiExample {curl} Example usage:
* curl -i -XPUT http://localhost:8080/addresses/renameDomain \
* -H 'Content-type: application/json' \
* -d '{
* "oldDomain": "example.com",
* "newDomain": "blurdybloop.com"
* }'
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "success": true
* }
*
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 200 OK
* {
* "error": "Failed to rename domain"
* }
*/
server.put('/addresses/renameDomain', (req, res, next) => {
res.charSet('utf-8');
const schema = Joi.object().keys({
oldDomain: Joi.string().required(),
newDomain: Joi.string().required()
});
const result = Joi.validate(req.params, schema, {
abortEarly: false,
convert: true
});
if (result.error) {
res.json({
error: result.error.message,
code: 'InputValidationError'
});
return next();
}
let oldDomain = tools.normalizeDomain(result.value.oldDomain);
let newDomain = tools.normalizeDomain(result.value.newDomain);
let updateAddresses = [];
let updateUsers = [];
let renameDomain = callback => {
let cursor = db.users.collection('addresses').find({
addrview: {
$regex: '@' + oldDomain.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') + '$'
}
});
let processNext = () => {
cursor.next((err, addressData) => {
if (err) {
return callback(err);
}
if (!addressData) {
return cursor.close(() => {
if (!updateAddresses.length) {
return callback(null, false);
}
db.users.collection('addresses').bulkWrite(
updateAddresses,
{
ordered: false,
w: 1
},
err => {
if (err) {
return callback(err);
}
db.users.collection('users').bulkWrite(
updateUsers,
{
ordered: false,
w: 1
},
err => {
if (err) {
return callback(err);
}
return callback(null, true);
}
);
}
);
});
}
updateAddresses.push({
updateOne: {
filter: {
_id: addressData._id
},
update: {
$set: {
address: addressData.address.replace(/@.+$/, () => '@' + newDomain),
addrview: addressData.addrview.replace(/@.+$/, () => '@' + newDomain)
}
}
}
});
updateUsers.push({
updateOne: {
filter: {
_id: addressData.user,
address: addressData.address
},
update: {
$set: {
address: addressData.address.replace(/@.+$/, () => '@' + newDomain)
}
}
}
});
return setImmediate(processNext);
});
};
processNext();
};
renameDomain(err => {
if (err) {
res.json({
error: 'MongoDB Error: ' + err.message,
code: 'InternalDatabaseError'
});
return next();
}
res.json({
success: true
});
});
});
};