const {promises: fs} = require('fs') const easypost = new (require('@easypost/api'))(process.env.EASYPOST_API_KEY); module.exports = async function doImport(oldDB, path) { const items = await oldDB.sock.find().lean().exec() const users = await oldDB.user.find({email: {$exists: true}}).populate('purchases').lean().exec() const carts = users.map(u => u.purchases).flat() console.log(`Loaded ${carts.length} purchases from ${users.length} users`) let count = 1; for(const user of users){ delete user._id delete user.__v, delete user.password, delete user.emailPin delete user.cart process.stdout.clearLine(); process.stdout.cursorTo(0); process.stdout.write(`Retrieving details for ${user.email} (${count++}/${users.length}) `) for(const cart of user.purchases){ delete cart._id delete cart.__v cart.items = cart.items.map(id => items.find(item => item._id.toString() === id.toString()).urlslug) shipmentPromise = easypost.Shipment.retrieve(cart.shipment) addressPromise = easypost.Address.retrieve(cart.address) cart.shipment = await shipmentPromise cart.address = await addressPromise } } process.stdout.clearLine(); process.stdout.cursorTo(0); process.stdout.write(`All address + shipment info retrieved, writing data file`) fs.writeFile(path, JSON.stringify(users, null, 2)) }