Created
January 29, 2024 09:27
-
-
Save akash-gajjar/f7092924b0a5f2169a737db6e5fda4c5 to your computer and use it in GitHub Desktop.
Invoice Timline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
'$match': { | |
'driver_id': new ObjectId('657953b26a91175eb8b6bc93'), | |
'load_id': { | |
'$exists': true, | |
'$nin': [ | |
'', null | |
] | |
} | |
} | |
}, { | |
'$sort': { | |
'start_time': 1 | |
} | |
}, { | |
'$facet': { | |
'trucks': [ | |
{ | |
'$group': { | |
'_id': null, | |
'list': { | |
'$addToSet': '$vehicle_name' | |
} | |
} | |
}, { | |
'$unwind': { | |
'path': '$list' | |
} | |
}, { | |
'$lookup': { | |
'from': 'invoices', | |
'localField': 'list', | |
'foreignField': 'truck', | |
'pipeline': [ | |
{ | |
'$lookup': { | |
'from': 'loaddetails', | |
'localField': 'load_number', | |
'foreignField': 'load_id', | |
'pipeline': [ | |
{ | |
'$group': { | |
'_id': '$type', | |
'min': { | |
'$min': '$available_from' | |
}, | |
'max': { | |
'$max': '$available_from' | |
}, | |
'list': { | |
'$push': '$$ROOT' | |
} | |
} | |
}, { | |
'$project': { | |
'_id': 0, | |
'k': '$_id', | |
'v': { | |
'min': '$min', | |
'max': '$max', | |
'list': '$list' | |
} | |
} | |
} | |
], | |
'as': 'loaddetails' | |
} | |
}, { | |
'$project': { | |
'status': 1, | |
'load_number': 1, | |
'truck': 1, | |
'loaddetails': { | |
'$arrayToObject': '$loaddetails' | |
} | |
} | |
}, { | |
'$match': { | |
'loaddetails.Shipper.min': { | |
'$exists': true | |
} | |
} | |
}, { | |
'$lookup': { | |
'from': 'hoslogs', | |
'localField': 'truck', | |
'foreignField': 'vehicle_name', | |
'let': { | |
'load_id': '$load_number' | |
}, | |
'pipeline': [ | |
{ | |
'$match': { | |
'$expr': { | |
'$eq': [ | |
'$load_id', '$$load_id' | |
] | |
}, | |
'status': { | |
'$in': [ | |
'Load Securement Hours', 'Unloading Hours' | |
] | |
} | |
} | |
}, { | |
'$group': { | |
'_id': { | |
'$cond': { | |
'if': { | |
'$eq': [ | |
'$status', 'Load Securement Hours' | |
] | |
}, | |
'then': 'loading', | |
'else': 'unloading' | |
} | |
}, | |
'logs': { | |
'$push': '$$ROOT' | |
} | |
} | |
}, { | |
'$sort': { | |
'_id': 1 | |
} | |
}, { | |
'$project': { | |
'_id': 0, | |
'k': '$_id', | |
'v': '$logs' | |
} | |
} | |
], | |
'as': 'logs' | |
} | |
}, { | |
'$addFields': { | |
'logs': { | |
'$arrayToObject': '$logs' | |
} | |
} | |
} | |
], | |
'as': 'invoices' | |
} | |
} | |
], | |
'loadedLogs': [ | |
{ | |
'$match': { | |
'status': { | |
'$in': [ | |
'Load Securement Hours', 'Unloading Hours' | |
] | |
} | |
} | |
}, { | |
'$group': { | |
'_id': '$load_id', | |
'logs': { | |
'$push': '$$ROOT' | |
} | |
} | |
}, { | |
'$sort': { | |
'_id': 1 | |
} | |
} | |
] | |
} | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment