const returnFields = [
"id",
"status",
"order_id",
"rma",
"tracking_number",
"description",
"customerEmail",
"zendesk_number",
"action_needed",
"reason_category",
"issue",
"order_date",
"shipped_date",
"requested_date",
"serial_number",
"scanned_serial_number",
"condition",
"amount",
"customer_id",
"tax_refunded",
"total_refunded",
"created_date",
"flat_rate_shipping",
"warehouse_received_date",
"warehouse_condition_date",
"country"
];
const opts = { returnFields };
// Current Time
var time = Math.floor(Date.now() / 1000);
// The ID of your GCS bucket
const bucketName = 'returns';
// The path to your file to upload
const filePath = `../stateset-app/static/data/returns/returns-data-${time}.csv`;
// The new ID for your GCS file
const destFileName = `returns-data-${time}.csv`;
// Upload to Google Cloud Function
async function uploadFile() {
// Uploads a local file to the bucket
await storage.bucket(bucketName).upload(filePath, {
destination: destFileName,
});
return res.status(200).json({ status: `${filename}-${time} uploaded to ${bucketName}.` })
}
// Get the Return Data
const return_records = await graphQLClient.request(GET_MY_RETURNS);
// JSON 2 CSV Parser
const json2csvParser = new Parser({ returnFields, quote: '', delimiter: ',' });
// Stateset CSV
const statesetCSV = json2csvParser.parse(return_records.returns);
// Parse the Response convert to CSV
let statesetWriter = createWriteStream(`../stateset-app/static/data/returns/returns-data-${time}.csv`)
statesetWriter.write(statesetCSV);
// Upload File
uploadFile().catch(console.error);