Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow user removal when sync going #267

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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