Overview of the Stateset One Platform Reporting
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);
import base64
import io
import os
from google.cloud import storage
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (
Mail, Attachment, FileContent, FileName,
FileType, To, Disposition)
sg = SendGridAPIClient(process.env.SENDGRID_API)
def send_mail(data, context):
message = Mail(
from_email='',
to_emails= [To('email@customer.com'), To('partner@customer.com'), To('manager@customer.com')],
subject='Returns Report',
html_content='This is the returns report autogenerated via Stateset.'
)
storage_client = storage.Client(process.env.STORAGE_CLIENT)
bucket = storage_client.get_bucket(process.env.STORAGE_BUCKET)
blob = bucket.blob(data['name'])
data = blob.download_as_bytes()
bytes = io.BytesIO(data)
bytes.seek(0)
data = bytes.read()
encoded = base64.b64encode(data)
csv_file = str(encoded,'utf-8')
attachment = Attachment()
attachment.file_content = FileContent(csv_file)
attachment.file_type = FileType('text/csv')
attachment.file_name = FileName('returns-export.csv')
attachment.disposition = Disposition('attachment')
message.attachment = attachment
response = sg.send(message)
print(response.status_code, response.body, response.headers)