From 288baa7e9445b910ed597d5c5e690af8cc7d9d52 Mon Sep 17 00:00:00 2001 From: Johannes Zellner Date: Thu, 15 Dec 2016 16:41:16 +0100 Subject: [PATCH] Rename mailbox when location changes Fixes #118 --- src/apps.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/apps.js b/src/apps.js index 5d4cca465..f252f58aa 100644 --- a/src/apps.js +++ b/src/apps.js @@ -629,16 +629,24 @@ function configure(appId, data, auditSource, callback) { debug('Will configure app with id:%s values:%j', appId, values); - appdb.setInstallationCommand(appId, appdb.ISTATE_PENDING_CONFIGURE, values, function (error) { - if (error && error.reason === DatabaseError.ALREADY_EXISTS) return callback(getDuplicateErrorDetails(location, portBindings, error)); + var oldName = (app.location ? app.location : app.manifest.title.toLowerCase().replace(/[^a-zA-Z0-9]/g, '')) + '.app'; + var newName = (location ? location : app.manifest.title.toLowerCase().replace(/[^a-zA-Z0-9]/g, '')) + '.app'; + mailboxdb.updateName(oldName, newName, function (error) { + if (error && error.reason === DatabaseError.ALREADY_EXISTS) return callback(new AppsError(AppsError.ALREADY_EXISTS, 'This mailbox is already taken')); if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new AppsError(AppsError.BAD_STATE)); if (error) return callback(new AppsError(AppsError.INTERNAL_ERROR, error)); - taskmanager.restartAppTask(appId); + appdb.setInstallationCommand(appId, appdb.ISTATE_PENDING_CONFIGURE, values, function (error) { + if (error && error.reason === DatabaseError.ALREADY_EXISTS) return callback(getDuplicateErrorDetails(location, portBindings, error)); + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new AppsError(AppsError.BAD_STATE)); + if (error) return callback(new AppsError(AppsError.INTERNAL_ERROR, error)); - eventlog.add(eventlog.ACTION_APP_CONFIGURE, auditSource, { appId: appId }); + taskmanager.restartAppTask(appId); - callback(null); + eventlog.add(eventlog.ACTION_APP_CONFIGURE, auditSource, { appId: appId }); + + callback(null); + }); }); }); }