refs #188 Rescue order when account order is unexpected value

This commit is contained in:
AkiraFukushima 2018-04-05 22:58:02 +09:00
parent b0d39770b3
commit 74c712ac38
2 changed files with 19 additions and 0 deletions

View File

@ -132,6 +132,24 @@ export default class Account {
const updated = await this.updateAccount(ac._id, Object.assign(ac, { order: (ac.order + 1) }))
return updated
}
/*
* cleanup
* Check order of all accounts, and fix if order is negative value or over the length.
*/
async cleanup () {
const accounts = await this.listAccounts()
if (accounts.length < 1) {
return accounts.length
}
if (accounts[0].order < 1 || accounts[accounts.length - 1].order > accounts.length) {
await Promise.all(accounts.map(async (element, index) => {
const update = await this.updateAccount(element._id, Object.assign(element, { order: index + 1 }))
return update
}))
}
return null
}
}
class EmptyRecordError {

View File

@ -44,6 +44,7 @@ let db = new Datastore({
async function listAccounts () {
try {
const account = new Account(db)
await account.cleanup()
const accounts = await account.listAccounts()
return accounts
} catch (err) {