Skip to content

Commit

Permalink
Merge pull request #267 from ZIMkaRU/bugfix/disallow-user-removal-whe…
Browse files Browse the repository at this point in the history
…n-sync-going

Disallow user removal when sync going
  • Loading branch information
prdn authored Apr 4, 2023
2 parents 6a770ef + 89e7847 commit 8cd8c59
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
13 changes: 11 additions & 2 deletions workers/loc.api/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,18 @@ class UserUpdatingError extends AuthError {
}

class UserRemovingError extends AuthError {
constructor (message = 'ERR_USER_REMOVE_HAS_BEEN_FAILED') {
constructor (message = 'ERR_USER_REMOVAL_HAS_BEEN_FAILED') {
super(message)

this.statusMessage = 'User remove has been failed'
this.statusMessage = 'User removal has been failed'
}
}

class UserRemovingDuringSyncError extends UserRemovingError {
constructor (message = 'USER_REMOVAL_HAS_BEEN_DISALLOWED_DURING_SYNC') {
super(message)

this.statusMessage = 'User removal has been disallowed during sync'
}
}

Expand Down Expand Up @@ -250,6 +258,7 @@ module.exports = {
SubAccountUpdatingError,
UserUpdatingError,
UserRemovingError,
UserRemovingDuringSyncError,
UserWasPreviouslyStoredInDbError,
SubAccountLedgersBalancesRecalcError,
DatePropNameError,
Expand Down
8 changes: 8 additions & 0 deletions workers/loc.api/sync/authenticator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
const {
UserUpdatingError,
UserRemovingError,
UserRemovingDuringSyncError,
UserWasPreviouslyStoredInDbError,
AuthTokenGenerationError
} = require('../../errors')
Expand All @@ -24,6 +25,7 @@ const {
pickProps,
pickSessionProps
} = require('./helpers')
const Progress = require('../progress')

const { decorateInjectable } = require('../../di/utils')

Expand Down Expand Up @@ -926,6 +928,12 @@ class Authenticator {
}
}
)
const progress = await Progress
.getNonEstimatedProgress(this.dao, this.TABLES_NAMES)

if (progress < 100) {
throw new UserRemovingDuringSyncError()
}

const res = await this.dao.removeElemsFromDb(
this.TABLES_NAMES.USERS,
Expand Down
15 changes: 11 additions & 4 deletions workers/loc.api/sync/progress/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,22 @@ class Progress extends EventEmitter {
}

async getProgress () {
const progressObj = await this.dao
.getElemInCollBy(this.TABLES_NAMES.PROGRESS)
const progress = await this.constructor
.getNonEstimatedProgress(this.dao, this.TABLES_NAMES)
const estimatedSyncTime = this._estimateSyncTime({ progress })

return estimatedSyncTime
}

static async getNonEstimatedProgress (dao, TABLES_NAMES) {
const progressObj = await dao
.getElemInCollBy(TABLES_NAMES.PROGRESS)

const progress = typeof progressObj?.value === 'string'
? tryParseJSON(progressObj.value, true)
: 'SYNCHRONIZATION_HAS_NOT_STARTED_YET'
const estimatedSyncTime = this._estimateSyncTime({ progress })

return estimatedSyncTime
return progress
}

activateSyncTimeEstimate () {
Expand Down

0 comments on commit 8cd8c59

Please sign in to comment.