diff --git a/src/mailboxdb.js b/src/mailboxdb.js index 359642092..ba122226d 100644 --- a/src/mailboxdb.js +++ b/src/mailboxdb.js @@ -38,7 +38,7 @@ function add(name, ownerId, ownerType, callback) { assert.strictEqual(typeof ownerType, 'string'); assert.strictEqual(typeof callback, 'function'); - database.query('INSERT INTO mailboxes (name, ownerId, ownerType) VALUES (?, ?, ?)', [ name, ownerId, ownerType, name ], function (error) { + database.query('INSERT INTO mailboxes (name, ownerId, ownerType) VALUES (?, ?, ?)', [ name, ownerId, ownerType ], function (error) { if (error && error.code === 'ER_DUP_ENTRY') return callback(new DatabaseError(DatabaseError.ALREADY_EXISTS)); if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error)); diff --git a/src/test/user-test.js b/src/test/user-test.js index 450dd14ad..ee6a038b5 100644 --- a/src/test/user-test.js +++ b/src/test/user-test.js @@ -187,6 +187,14 @@ describe('User', function () { }); }); + it('did create mailbox', function (done) { + mailboxdb.getMailbox(USERNAME.toLowerCase(), function (error, mailbox) { + expect(error).to.be(null); + expect(mailbox.ownerType).to.be(mailboxdb.TYPE_USER); + done(); + }); + }); + it('fails because of invalid BAD_FIELD', function (done) { expect(function () { user.create(EMAIL, {}, function () {}); diff --git a/src/user.js b/src/user.js index fe6e472ef..dfa29c5e3 100644 --- a/src/user.js +++ b/src/user.js @@ -372,19 +372,24 @@ function updateUser(userId, data, auditSource, callback) { if (error) return callback(error); } - // TODO: maybe delete the old username mailbox - asyncIf(!!data.username, mailboxdb.add.bind(null, data.username, userId /* owner */, mailboxdb.TYPE_USER), function (error) { - if (error && error.reason === DatabaseError.ALREADY_EXISTS) return callback(new UserError(UserError.ALREADY_EXISTS, error.message)); + userdb.get(userId, function (error, user) { + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UserError(UserError.NOT_FOUND)); if (error) return callback(new UserError(UserError.INTERNAL_ERROR, error)); - userdb.update(userId, data, function (error) { + asyncIf(data.username && user.username !== data.username, mailboxdb.add.bind(null, data.username, userId /* owner */, mailboxdb.TYPE_USER), function (error) { if (error && error.reason === DatabaseError.ALREADY_EXISTS) return callback(new UserError(UserError.ALREADY_EXISTS, error.message)); - if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UserError(UserError.NOT_FOUND, error)); if (error) return callback(new UserError(UserError.INTERNAL_ERROR, error)); - eventlog.add(eventlog.ACTION_USER_UPDATE, auditSource, { userId: userId }); + userdb.update(userId, data, function (error) { + if (error && error.reason === DatabaseError.ALREADY_EXISTS) return callback(new UserError(UserError.ALREADY_EXISTS, error.message)); + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UserError(UserError.NOT_FOUND, error)); + if (error) return callback(new UserError(UserError.INTERNAL_ERROR, error)); - callback(null); + eventlog.add(eventlog.ACTION_USER_UPDATE, auditSource, { userId: userId }); + + // delete old mailbox + asyncIf(user.username && user.username !== data.username, mailboxdb.del.bind(null, user.username), callback); + }); }); }); }