Web Apps

Stock Sync App

6min

Introduction

This script is designed for mainlyextracting stock data from a Cybertill, performing data transformation, and generating CSV files.

Functionality

getAllData(location)

This function is responsible for extracting data from a database for a specific location and writing it to a CSV file. It follows these steps:

  1. Fetch data from the database for the specified location.
  2. Filter the data to extract relevant information, such as brands and seasons.
  3. Fetch items using the getItems function with filtering criteria.
  4. Append the extracted data to the allDatadetails array.
  5. Repeat the data extraction process until all data is fetched.
  6. Write the combined data to a CSV file with the location's name.

getItems(limit, offset, location_id, brandSQL, seasonSQL)

This function constructs an SQL query based on the provided parameters and fetches data from the database. The steps include:

  1. Construct a SQL query with parameters like limit, offset, location_id, brandSQL, and seasonSQL.
  2. Fetch data from the database using the constructed query.
  3. Return the fetched data.

getlocations()

This function retrieves a list of locations from the database. It constructs and executes an SQL query to obtain this information.

generateDefaultLocationFile(locationNames)

This function generates a summary CSV file that aggregates data from multiple location-specific CSV files. The steps include:

  1. Iterate over the provided locationNames.
  2. Read each location-specific CSV file and collect data.
  3. Group the collected data by the "ref" field.
  4. Calculate the sum of the "stock" values for each group.
  5. Generate a CSV file with the aggregated data and write it to "./csv/default.csv".

main()

The main function is the entry point of the script. It coordinates the execution of various tasks, including:

  1. Fetching the list of locations.
  2. Iterating over each location to extract data.
  3. Generating the default location CSV file.
  4. Uploading data for all locations.
  5. Writing the timestamp of the last run to a JSON file.