1
1
const _ = require ( 'lodash' ) ;
2
2
const mongoose = require ( 'mongoose' ) ;
3
3
4
+ const { getModel } = require ( '../model' ) ;
4
5
const { JOB , QUEUE } = require ( '../constants' ) ;
6
+ const createDocument = require ( '../index/fills/create-document' ) ;
5
7
const elasticsearch = require ( '../util/elasticsearch' ) ;
8
+ const getIndexName = require ( '../index/get-index-name' ) ;
6
9
7
10
const indexFillProtocolFee = async ( job , { logger } ) => {
8
11
const { fillId, protocolFee } = job . data ;
@@ -15,23 +18,27 @@ const indexFillProtocolFee = async (job, { logger }) => {
15
18
throw new Error ( `Invalid value: ${ protocolFee } ` ) ;
16
19
}
17
20
18
- const exists = await elasticsearch
19
- . getClient ( )
20
- . exists ( { id : fillId , index : 'fills' , _source : false } ) ;
21
- const indexed = exists . body ;
21
+ const fill = await getModel ( 'Fill' )
22
+ . findOne ( { _id : fillId } )
23
+ . populate ( [
24
+ { path : 'takerMetadata' , select : 'isContract' } ,
25
+ { path : 'transaction' , select : 'from' } ,
26
+ ] )
27
+ . lean ( ) ;
22
28
23
- if ( ! indexed ) {
24
- throw new Error ( `Could not index protocol fee of fill: ${ fillId } ` ) ;
29
+ if ( fill === null ) {
30
+ throw new Error ( `Could not find fill: ${ fillId } ` ) ;
25
31
}
26
32
27
33
await elasticsearch . getClient ( ) . update ( {
28
34
id : fillId ,
29
- index : 'fills' ,
35
+ index : getIndexName ( 'fills' ) ,
30
36
body : {
31
37
doc : {
32
38
protocolFeeUSD : protocolFee ,
33
39
updatedAt : new Date ( Date . now ( ) ) . toISOString ( ) ,
34
40
} ,
41
+ upsert : createDocument ( fill ) ,
35
42
} ,
36
43
} ) ;
37
44
0 commit comments