refs #188 Rescue order when account order is unexpected value
This commit is contained in:
parent
b0d39770b3
commit
74c712ac38
|
@ -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 {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue