Skip to content

Commit

Permalink
✨ Get pdfs
Browse files Browse the repository at this point in the history
  • Loading branch information
xeloader committed Nov 13, 2020
1 parent 1c8ffbe commit df20b29
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Antar att fler har samma problem så here you go!
## Support
- :white_check_mark: Export av kunder
- :white_check_mark: Export av fakturainformation
- :x: Export av PDF
- :white_check_mark: Export av PDFer


## Instruktioner
Expand All @@ -34,7 +34,7 @@ Se till att senaste LTS-version av Node är installerat.
API_PASSWORD=0000111122222333334444455556666
```
6. Kör programmet med `npm run export`
7. CSV-filen ligger i `export/`
7. Din data ligger i `export/`
---
Expand Down
50 changes: 42 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,27 @@ const getAllCustomers = () =>
const getAllBills = () =>
getUntilEmpty(getBills)

const saveToFile = (content, fileName) => {
const getPDF = async (invoiceId) => {
const api = getAPI()
const [err, res] = await to(
api.get(`billogram/${invoiceId}.pdf`)
)
if (err) return console.log('ERR', err.message, err.response.data)
return {
id: invoiceId,
data: res.data.data.content
}
}

const getPDFsFromBills = async (invoices) => {
const promises = invoices.map((invoice) => getPDF(invoice.id))
const pdfs = await Promise.all(promises)
return pdfs
}

const saveToFile = (content, fileName, enc = 'utf8') => {
const filePath = `./export/${fileName}`
fs.writeFileSync(filePath, content, 'utf8')
fs.writeFileSync(filePath, content, enc)
}

const arrToCSV = (items = []) => {
Expand All @@ -112,19 +130,35 @@ const getEverything = async () => {
getAllBills(),
getAllCustomers()
])
const pdfs = await getPDFsFromBills(bills)
return {
bills,
customers
customers,
pdfs
}
}

const cuteMkdir = (path) => {
try {
fs.mkdirSync(path)
} catch (err) {
console.log('err creating dir', err.message)
}
}

getEverything()
.then((everything) => {
const timestamp = new Date().toISOString()
const getFileName = (category) => `${timestamp}-${category}.csv`
const keys = Object.keys(everything)
const flatData = keys.map((key) => everything[key].map(flatten))
console.log('INFO, creating export dir')
cuteMkdir(`export/${timestamp}`)
console.log('INFO, saving csv files..')
const getFileName = (category) => `${timestamp}/${category}`
const { pdfs, ...rest } = everything
const keys = Object.keys(rest)
const flatData = keys.map((key) => rest[key].map(flatten))
const csvs = keys.map((_, i) => arrToCSV(flatData[i]))
keys.forEach((key, i) => saveToFile(csvs[i], getFileName(key)))
console.log('SUCCESS, export is available in the export/ folder.')
keys.forEach((key, i) => saveToFile(csvs[i], getFileName(key + '.csv')))
console.log('INFO, saving pdf files..')
pdfs.forEach((pdf) => saveToFile(pdf.data, getFileName(pdf.id + '.pdf'), 'base64'))
console.log(`SUCCESS, export is available at export/${timestamp}/ folder.`)
})

0 comments on commit df20b29

Please sign in to comment.