Return a new token for existing account instead of duplicating accounts

This commit is contained in:
Ben Gotow 2016-07-11 16:01:18 -07:00
parent 5085730902
commit 3c63268eec
4 changed files with 49 additions and 24 deletions

View file

@ -43,24 +43,33 @@ const buildAccountWith = ({name, email, provider, settings, credentials}) => {
return DatabaseConnector.forShared().then((db) => {
const {AccountToken, Account} = db;
const account = Account.build({
name: name,
provider: provider,
emailAddress: email,
connectionSettings: settings,
syncPolicy: SyncPolicy.defaultPolicy(),
lastSyncCompletions: [],
})
account.setCredentials(credentials);
return Account.find({
where: {
emailAddress: email,
connectionSettings: JSON.stringify(settings),
},
}).then((existing) => {
const account = existing || Account.build({
name: name,
provider: provider,
emailAddress: email,
connectionSettings: settings,
syncPolicy: SyncPolicy.defaultPolicy(),
lastSyncCompletions: [],
})
return account.save().then((saved) =>
AccountToken.create({accountId: saved.id}).then((token) =>
DatabaseConnector.prepareAccountDatabase(saved.id).thenReturn({
account: saved,
token: token,
})
)
);
// always update with the latest credentials
account.setCredentials(credentials);
return account.save().then((saved) =>
AccountToken.create({accountId: saved.id}).then((token) =>
DatabaseConnector.prepareAccountDatabase(saved.id).thenReturn({
account: saved,
token: token,
})
)
);
});
});
}
@ -76,7 +85,6 @@ module.exports = (server) => {
validate: {
query: {
client_id: Joi.string().required(),
n1_id: Joi.string(),
},
payload: {
email: Joi.string().email().required(),
@ -98,7 +106,11 @@ module.exports = (server) => {
const {settings, email, provider, name} = request.payload;
if (provider === 'imap') {
connectionChecks.push(IMAPConnection.connect({db: dbStub, settings}))
connectionChecks.push(IMAPConnection.connect({
logger: request.logger,
settings: settings,
db: dbStub,
}));
}
Promise.all(connectionChecks).then(() => {
@ -187,9 +199,12 @@ module.exports = (server) => {
client_id: GMAIL_CLIENT_ID,
client_secret: GMAIL_CLIENT_SECRET,
}
Promise.all([
IMAPConnection.connect({db: {}, settings: Object.assign({}, settings, credentials)}),
IMAPConnection.connect({
logger: request.logger,
settings: Object.assign({}, settings, credentials),
db: {},
}),
])
.then(() =>
buildAccountWith({

View file

@ -156,8 +156,6 @@ class IMAPBox {
}
return this._imap.closeBoxAsync(expunge)
}
}
@ -178,6 +176,11 @@ class IMAPConnection extends EventEmitter {
constructor({db, settings, logger = console} = {}) {
super();
if (!(settings instanceof Object)) {
throw new Error("IMAPConnection: Must be instantiated with `settings`")
}
this._logger = logger;
this._db = db;
this._queue = [];

View file

@ -1,4 +1,6 @@
/* eslint react/react-in-jsx-scope: 0*/
/* eslint no-console: 0*/
const React = window.React;
const ReactDOM = window.ReactDOM;
const {

View file

@ -108,7 +108,12 @@ class SyncWorker {
return Promise.reject(new Error("ensureConnection: There are no IMAP connection credentials for this account."))
}
const conn = new IMAPConnection({db: this._db, settings: Object.assign({}, settings, credentials), logger: this._logger});
const conn = new IMAPConnection({
db: this._db,
settings: Object.assign({}, settings, credentials),
logger: this._logger,
});
conn.on('mail', () => {
this._onConnectionIdleUpdate();
})