11const _ = require ( 'lodash' ) ;
22const mongoose = require ( 'mongoose' ) ;
33
4+ const { getModel } = require ( '../model' ) ;
45const { JOB , QUEUE } = require ( '../constants' ) ;
6+ const createDocument = require ( '../index/fills/create-document' ) ;
57const elasticsearch = require ( '../util/elasticsearch' ) ;
8+ const getIndexName = require ( '../index/get-index-name' ) ;
69
710const indexFillProtocolFee = async ( job , { logger } ) => {
811 const { fillId, protocolFee } = job . data ;
@@ -15,23 +18,27 @@ const indexFillProtocolFee = async (job, { logger }) => {
1518 throw new Error ( `Invalid value: ${ protocolFee } ` ) ;
1619 }
1720
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 ( ) ;
2228
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 } ` ) ;
2531 }
2632
2733 await elasticsearch . getClient ( ) . update ( {
2834 id : fillId ,
29- index : 'fills' ,
35+ index : getIndexName ( 'fills' ) ,
3036 body : {
3137 doc : {
3238 protocolFeeUSD : protocolFee ,
3339 updatedAt : new Date ( Date . now ( ) ) . toISOString ( ) ,
3440 } ,
41+ upsert : createDocument ( fill ) ,
3542 } ,
3643 } ) ;
3744
0 commit comments