Skip to content

Commit dccf0b1

Browse files
authored
Merge pull request #448 from bitfinexcom/staging
Release version to master
2 parents be56393 + a8bd739 commit dccf0b1

File tree

7 files changed

+81
-52
lines changed

7 files changed

+81
-52
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bfx-reports-framework",
3-
"version": "4.21.4",
3+
"version": "4.21.5",
44
"description": "Bitfinex reports framework",
55
"main": "worker.js",
66
"license": "Apache-2.0",

workers/loc.api/sync/dao/helpers/get-where-query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const _getIsNullOperator = (
124124
return false
125125
}
126126
const _alias = alias && typeof alias === 'string'
127-
? `${alias}_`
127+
? `${alias}.`
128128
: ''
129129
const operator = fieldName === FILTER_CONDITIONS.IS_NOT_NULL
130130
? SQL_OPERATORS.IS_NOT_NULL

workers/loc.api/sync/sync.queue/index.js

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const depsTypes = (TYPES) => [
2929
TYPES.DataInserterFactory,
3030
TYPES.Progress,
3131
TYPES.SyncSchema,
32-
TYPES.SyncInterrupter
32+
TYPES.SyncInterrupter,
33+
TYPES.Authenticator
3334
]
3435
class SyncQueue extends EventEmitter {
3536
constructor (
@@ -39,7 +40,8 @@ class SyncQueue extends EventEmitter {
3940
dataInserterFactory,
4041
progress,
4142
syncSchema,
42-
syncInterrupter
43+
syncInterrupter,
44+
authenticator
4345
) {
4446
super()
4547

@@ -50,6 +52,7 @@ class SyncQueue extends EventEmitter {
5052
this.progress = progress
5153
this.syncSchema = syncSchema
5254
this.syncInterrupter = syncInterrupter
55+
this.authenticator = authenticator
5356
this.name = this.TABLES_NAMES.SYNC_QUEUE
5457

5558
this.methodCollMap = this._filterMethodCollMap(
@@ -85,8 +88,14 @@ class SyncQueue extends EventEmitter {
8588
} = params ?? {}
8689

8790
if (
88-
!Number.isInteger(ownerUserId) &&
89-
!isOwnerScheduler
91+
(
92+
!Number.isInteger(ownerUserId) &&
93+
!isOwnerScheduler
94+
) ||
95+
(
96+
Number.isInteger(ownerUserId) &&
97+
isOwnerScheduler
98+
)
9099
) {
91100
throw new SyncQueueOwnerSettingError()
92101
}
@@ -96,17 +105,36 @@ class SyncQueue extends EventEmitter {
96105
: [syncColls]
97106
checkCollPermission(_syncColls, this.ALLOWED_COLLS)
98107

99-
const ownerUserIdFilter = Number.isInteger(ownerUserId)
100-
? { ownerUserId }
101-
: {}
102-
const isOwnerSchedulerFilter = isOwnerScheduler
103-
? { isOwnerScheduler: 1 }
104-
: {}
108+
const signedInUserIds = this.#getSignedInUserIds()
109+
110+
if (
111+
isOwnerScheduler &&
112+
signedInUserIds.length === 0
113+
) {
114+
return
115+
}
116+
117+
const subQueryFilter = Number.isInteger(ownerUserId)
118+
? {
119+
$or: {
120+
$eq: {
121+
isOwnerScheduler: 1,
122+
ownerUserId
123+
}
124+
}
125+
}
126+
: {
127+
$or: {
128+
$eq: { isOwnerScheduler: 1 },
129+
$in: { ownerUserId: signedInUserIds }
130+
}
131+
}
105132

106133
const allSyncs = await this._getAll({
107-
state: [NEW_JOB_STATE, ERROR_JOB_STATE],
108-
...ownerUserIdFilter,
109-
...isOwnerSchedulerFilter
134+
filter: {
135+
$in: { state: [NEW_JOB_STATE, ERROR_JOB_STATE, LOCKED_JOB_STATE] }
136+
},
137+
subQuery: { filter: subQueryFilter }
110138
})
111139
const hasALLInDB = allSyncs.some(item => {
112140
return item.collName === this.ALLOWED_COLLS.ALL
@@ -272,13 +300,16 @@ class SyncQueue extends EventEmitter {
272300
: {}
273301

274302
const futureSyncs = await this._getAll({
275-
state: [
276-
NEW_JOB_STATE,
277-
LOCKED_JOB_STATE,
278-
ERROR_JOB_STATE
279-
],
280-
...ownerUserIdFilter
281-
}, { limit: 10 })
303+
filter: {
304+
state: [
305+
NEW_JOB_STATE,
306+
LOCKED_JOB_STATE,
307+
ERROR_JOB_STATE
308+
],
309+
...ownerUserIdFilter
310+
},
311+
limit: 10
312+
})
282313

283314
const allSyncs = (
284315
!Array.isArray(futureSyncs) ||
@@ -335,17 +366,20 @@ class SyncQueue extends EventEmitter {
335366
return (1 / multipliersSum) * currMultipliers
336367
}
337368

338-
_getAll (filter, opts) {
369+
_getAll (params) {
339370
const {
371+
filter,
372+
subQuery,
340373
sort = this._sort,
341-
limit = null
342-
} = opts ?? {}
374+
limit
375+
} = params ?? {}
343376

344377
return this.dao.getElemsInCollBy(
345378
this.name,
346379
{
347-
sort,
348380
filter,
381+
subQuery,
382+
sort,
349383
limit
350384
}
351385
)
@@ -441,6 +475,13 @@ class SyncQueue extends EventEmitter {
441475

442476
this.emit('progress', progress)
443477
}
478+
479+
#getSignedInUserIds () {
480+
const sessions = this.authenticator.getUserSessions()
481+
482+
return [...sessions]
483+
.map(([token, session]) => session._id)
484+
}
444485
}
445486

446487
decorateInjectable(SyncQueue, depsTypes)

workers/loc.api/sync/transaction.tax.report/helpers/find-public-trade.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ module.exports = (pubTrades, mts) => {
1111
if (pubTrades[middleIndex]?.mts === mts) {
1212
return pubTrades[middleIndex]
1313
}
14-
if (mts < pubTrades[middleIndex]?.mts) {
14+
if (mts > pubTrades[middleIndex]?.mts) {
1515
endIndex = middleIndex - 1
1616
}
17-
if (mts > pubTrades[middleIndex]?.mts) {
17+
if (mts < pubTrades[middleIndex]?.mts) {
1818
startIndex = middleIndex + 1
1919
}
2020
}

workers/loc.api/sync/transaction.tax.report/index.js

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const INTERRUPTER_NAMES = require(
77
)
88

99
const { pushLargeArr } = require('../../helpers/utils')
10-
const { getBackIterable } = require('../helpers')
1110
const { PubTradeFindForTrxTaxError } = require('../../errors')
1211

1312
const {
@@ -21,8 +20,6 @@ const {
2120
getCcyPairForConversion
2221
} = require('./helpers')
2322

24-
const isTestEnv = process.env.NODE_ENV === 'test'
25-
2623
const { decorateInjectable } = require('../../di/utils')
2724

2825
const depsTypes = (TYPES) => [
@@ -280,13 +277,11 @@ class TransactionTaxReport {
280277
return
281278
}
282279

283-
const trxPriceCalculatorIterator = getBackIterable(trxPriceCalculators)
284-
285280
let pubTrades = []
286-
let pubTradeStart = pubTrades[0]?.mts
287-
let pubTradeEnd = pubTrades[pubTrades.length - 1]?.mts
281+
let pubTradeStart = pubTrades[pubTrades.length - 1]?.mts
282+
let pubTradeEnd = pubTrades[0]?.mts
288283

289-
for (const trxPriceCalculator of trxPriceCalculatorIterator) {
284+
for (const trxPriceCalculator of trxPriceCalculators) {
290285
count += 1
291286

292287
if (interrupter.hasInterrupted()) {
@@ -300,12 +295,12 @@ class TransactionTaxReport {
300295
pubTradeStart > trx.mtsCreate ||
301296
pubTradeEnd < trx.mtsCreate
302297
) {
303-
const start = trx.mtsCreate - 1
298+
const end = trx.mtsCreate
304299

305300
pubTrades = await this.#getPublicTrades(
306301
{
307302
symbol: getCcyPairForConversion(symbol, trxPriceCalculator),
308-
start
303+
end
309304
},
310305
opts
311306
)
@@ -342,7 +337,7 @@ class TransactionTaxReport {
342337
const res = await this.#getPublicTrades(
343338
{
344339
symbol: getCcyPairForConversion(symbol, trxPriceCalculator),
345-
start
340+
end
346341
},
347342
opts
348343
)
@@ -366,8 +361,8 @@ class TransactionTaxReport {
366361
}
367362
}
368363

369-
pubTradeStart = start ?? pubTrades[0]?.mts
370-
pubTradeEnd = pubTrades[pubTrades.length - 1]?.mts
364+
pubTradeStart = pubTrades[pubTrades.length - 1]?.mts
365+
pubTradeEnd = end ?? pubTrades[0]?.mts
371366
}
372367

373368
if (
@@ -455,7 +450,7 @@ class TransactionTaxReport {
455450
symbol,
456451
start = 0,
457452
end = Date.now(),
458-
sort = 1,
453+
sort = -1,
459454
limit = 10000
460455
} = params ?? {}
461456
const { interrupter } = opts ?? {}
@@ -505,13 +500,6 @@ class TransactionTaxReport {
505500
onceInterruptPromise
506501
])
507502

508-
if (isTestEnv) {
509-
/*
510-
* Need to reverse pub-trades array for test env
511-
* as mocked test server return data in desc order
512-
*/
513-
return getResponse(res.reverse())
514-
}
515503
if (Array.isArray(res)) {
516504
return getResponse(res)
517505
}

0 commit comments

Comments
 (0)