Created
August 30, 2023 19:40
-
-
Save dane-stevens/5cc711fba37e277435d1bee9b915a330 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
// Name: Format Addresses - Fulfillment Files | |
import "@johnlindquist/kit"; | |
import { parse } from "csv-parse"; | |
import { stringify } from "csv-stringify"; | |
import fs from "fs"; | |
import path from "path"; | |
import { transform } from "stream-transform"; | |
// const files = await drop(); | |
// const filePath = files[0].path; | |
const choice = await arg("What would you like to do?", [ | |
"Format Addresses", | |
"Update Database", | |
]); | |
const filePath = await selectFile(); | |
const readStream = fs.createReadStream(filePath); | |
const parser = parse({ | |
delimiter: ",", | |
}); | |
const replacementDb = await db({}); | |
if (choice === "Update Database") { | |
let isCorrectFile = false; | |
const transformer = transform(async (record) => { | |
await replacementDb.read(); | |
const [ | |
id, | |
orderNumber, | |
attention, | |
shippingBusinessName, | |
originalShippingAddress, | |
formattedShippingAddress, | |
formattedShippingAddressLine2, | |
...rest | |
] = record; | |
if ( | |
isCorrectFile && | |
originalShippingAddress.trim().length > 0 && | |
formattedShippingAddress.trim().length > 0 && | |
formattedShippingAddressLine2.trim().length > 0 | |
) { | |
replacementDb[originalShippingAddress] = [ | |
formattedShippingAddress, | |
formattedShippingAddressLine2, | |
]; | |
await replacementDb.write(); | |
} | |
if ( | |
id === "ID" && | |
orderNumber === "ORDER #" && | |
attention === "ATTENTION" && | |
shippingBusinessName === "SHIPPING BUSINESS NAME" && | |
originalShippingAddress === "ORIGINAL SHIPPING ADDRESS" && | |
formattedShippingAddress === "FORMATTED SHIPPING ADDRESS" && | |
formattedShippingAddressLine2 === "FORMATTED SHIPPING ADDRESS LINE 2" | |
) { | |
isCorrectFile = true; | |
} | |
return null; | |
}); | |
readStream.pipe(parser).pipe(transformer); | |
await div("Done"); | |
} | |
if (choice === "Format Addresses") { | |
const dir = path.dirname(filePath); | |
const filePathBase = path.basename(filePath); | |
const lastIndex = filePathBase.lastIndexOf("."); | |
const fileName = filePathBase.slice(0, lastIndex); | |
const fileExtension = filePathBase.slice(lastIndex + 1); | |
function replaceAddress(address, address2) { | |
const replacementValue = replacementDb.data[address]; | |
if (replacementValue) { | |
return [...replacementValue]; | |
} | |
return [address, address2]; | |
} | |
const stringifier = stringify({ | |
delimiter: ",", | |
}); | |
const transformer = transform((record) => { | |
const [ | |
id, | |
orderNumber, | |
attention, | |
shippingBusinessName, | |
shippingAddress, | |
shippingAddressLine2, | |
...rest | |
] = record; | |
if (id === "ID" && orderNumber === "ORDER #") { | |
return [ | |
id, | |
orderNumber, | |
attention, | |
shippingBusinessName, | |
"ORIGINAL SHIPPING ADDRESS", | |
"FORMATTED SHIPPING ADDRESS", | |
"FORMATTED SHIPPING ADDRESS LINE 2", | |
...rest, | |
]; | |
} | |
const [formattedShippingAddress, formattedShippingAddressLine2] = | |
replaceAddress(shippingAddress, shippingAddressLine2); | |
return [ | |
id, | |
orderNumber, | |
attention, | |
shippingBusinessName, | |
shippingAddress, | |
formattedShippingAddress, | |
formattedShippingAddressLine2, | |
...rest, | |
]; | |
}); | |
const writeStream = fs.createWriteStream( | |
path.join(dir, `${fileName}_formatted.${fileExtension}`) | |
); | |
readStream.pipe(parser).pipe(transformer).pipe(stringifier).pipe(writeStream); | |
await div(`Done`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment