Created
September 16, 2020 04:12
-
-
Save vbguard/d78c0054329a42e7e21e58e602876a8f to your computer and use it in GitHub Desktop.
This file contains 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
const allVisitsStream = visitsModel | |
.find() | |
.populate('contact') | |
.populate('helper') | |
.populate('helpRequest') | |
.cursor() | |
; | |
const transformer = (doc) => { | |
return { | |
'VISIT STATUS': doc.status, | |
'HELP REQUEST NUMBER': doc.helpRequest?.helpNumber, | |
'CUSTOMER': `${doc.contact?.firstName} ${doc.contact?.lastName}`, | |
'HELPER': `${doc.helper?.firstName} ${doc.helper?.lastName}`, | |
'DATE': moment.utc(doc.date).format('DD-MM-YYYY'), | |
'DAY OF WEEK': moment.utc(doc.date).format('dddd'), | |
'START TIME': moment.utc(doc.plannedStartTime).format('HH:mm'), | |
'END TIME': moment.utc(doc.plannedEndTime).format('HH:mm'), | |
'DURATION': doc.duration, | |
'INVOICE RATE': doc.invoiceRate, | |
'INVOICE TOTAL': doc.totalCost, | |
'HELPER NET': doc.helperNetPayment, | |
'GLS VAT': doc.glsVAT, | |
'HELPER VAT': doc.helperVAT, | |
'PAYMENT METHOD': doc.contact.paymentMethod | |
}; | |
}; | |
const csvStream = fastCsv.format({ headers: true }).transform(transformer); | |
const readStream = allVisitsStream.pipe(csvStream); | |
return reply.type('text/csv').send(readStream); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment