Skip to content

Instantly share code, notes, and snippets.

@tdtgit
Last active May 21, 2024 03:45
Show Gist options
  • Save tdtgit/ca2fa57c0f0aca35235e77b31b53d35d to your computer and use it in GitHub Desktop.
Save tdtgit/ca2fa57c0f0aca35235e77b31b53d35d to your computer and use it in GitHub Desktop.

Revisions

  1. tdtgit revised this gist May 21, 2024. 1 changed file with 18 additions and 4 deletions.
    22 changes: 18 additions & 4 deletions udontknowhowmuchuspent.js
    Original file line number Diff line number Diff line change
    @@ -33,9 +33,23 @@ async function fetchOrderDetails(orderID) {

    // Function to fetch order data from the API
    async function fetchOrderData() {
    const url = `https://www.uniqlo.com/vn/api/commerce/v3/vi/orders?searchPage=1&displayResults=5`;
    const orderData = await fetchData(url);
    return orderData ? orderData.orders : [];
    let orders = [];
    let searchPage = 1;
    let hasMoreData = true;

    while (hasMoreData) {
    const url = `https://www.uniqlo.com/vn/api/commerce/v3/vi/orders?searchPage=${searchPage}&displayResults=10`;
    const orderData = await fetchData(url);

    if (orderData && orderData.orders.length > 0) {
    orders = [...orders, ...orderData.orders];
    searchPage++;
    } else {
    hasMoreData = false;
    }
    }

    return orders;
    }

    // Function to delay execution
    @@ -67,4 +81,4 @@ async function processOrderData() {
    }

    // Call the function to start processing order data and calculating the total spent
    processOrderData();
    processOrderData();
  2. tdtgit revised this gist May 21, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion udontknowhowmuchuspent.js
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ async function fetchOrderDetails(orderID) {

    // Function to fetch order data from the API
    async function fetchOrderData() {
    const url = `https://www.uniqlo.com/vn/api/commerce/v3/vi/orders?searchPage=1&displayResults=50`;
    const url = `https://www.uniqlo.com/vn/api/commerce/v3/vi/orders?searchPage=1&displayResults=5`;
    const orderData = await fetchData(url);
    return orderData ? orderData.orders : [];
    }
  3. tdtgit revised this gist Aug 3, 2023. 1 changed file with 27 additions and 40 deletions.
    67 changes: 27 additions & 40 deletions udontknowhowmuchuspent.js
    Original file line number Diff line number Diff line change
    @@ -6,54 +6,36 @@ function formatCurrency(amount, currencyCode) {
    }).format(amount);
    }

    // Function to fetch order details using the order ID
    async function fetchOrderDetails(orderID) {
    const url = `https://www.uniqlo.com/vn/api/commerce/v3/vi/orders/${orderID}`;

    // Function to fetch data from a URL and handle errors
    async function fetchData(url) {
    try {
    const response = await fetch(url);

    if (response.ok) {
    const data = await response.json();

    if (data.status === 'ok' && data.result && data.result.orderDetail) {
    return data.result.orderDetail;
    } else {
    console.log(`Error parsing order details for order ID ${orderID}`);
    }
    } else {
    console.log(`Error fetching order details for order ID ${orderID}`);
    if (!response.ok) {
    throw new Error(`Error fetching data from ${url}`);
    }
    const data = await response.json();
    if (data.status !== 'ok') {
    throw new Error(`Error in data response from ${url}`);
    }
    return data.result;
    } catch (error) {
    console.error('An error occurred:', error);
    return null;
    }

    return null;
    }

    // Function to fetch order details using the order ID
    async function fetchOrderDetails(orderID) {
    const url = `https://www.uniqlo.com/vn/api/commerce/v3/vi/orders/${orderID}`;
    const orderDetail = await fetchData(url);
    return orderDetail ? orderDetail.orderDetail : null;
    }

    // Function to fetch order data from the API
    async function fetchOrderData() {
    const url = `https://www.uniqlo.com/vn/api/commerce/v3/vi/orders?searchPage=1&displayResults=50`;

    try {
    const response = await fetch(url);

    if (response.ok) {
    const data = await response.json();

    if (data.status === 'ok' && data.result && data.result.orders) {
    return data.result.orders;
    } else {
    console.log('Error parsing order data');
    }
    } else {
    console.log('Error fetching order data');
    }
    } catch (error) {
    console.error('An error occurred:', error);
    }

    return [];
    const orderData = await fetchData(url);
    return orderData ? orderData.orders : [];
    }

    // Function to delay execution
    @@ -69,14 +51,19 @@ async function processOrderData() {
    for (const order of orders) {
    const orderDetails = await fetchOrderDetails(order.no);
    if (orderDetails) {
    totalSpent += orderDetails.totalAmountTaxIncluded.value;
    const orderDate = new Date(orderDetails.createdDatetime * 1000).toLocaleString();
    const orderValue = orderDetails.totalAmountTaxIncluded.value;

    console.log(`Processing order: ${orderDate} - Value: ${formatCurrency(orderValue, 'VND')}`);

    totalSpent += orderValue;
    }

    await delay(500); // Add a delay of 1 second between requests
    await delay(500); // Add a delay of 0.5 seconds between requests
    }

    const formattedTotalSpent = formatCurrency(totalSpent, 'VND');
    console.log(`%cTotal Spent on Uniqlo.com: %c${formattedTotalSpent}`, 'font-weight: bold;font-size: 24px', 'font-weight: bold; color: green;font-size: 24px');
    console.log(`%cTotal Spent on Uniqlo.com: %c${formattedTotalSpent}`, 'font-weight: bold; font-size: 24px;', 'font-weight: bold; color: green; font-size: 24px;');
    }

    // Call the function to start processing order data and calculating the total spent
  4. tdtgit revised this gist Aug 2, 2023. No changes.
  5. tdtgit revised this gist Aug 2, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion udontknowhowmuchuspent.js
    Original file line number Diff line number Diff line change
    @@ -76,7 +76,7 @@ async function processOrderData() {
    }

    const formattedTotalSpent = formatCurrency(totalSpent, 'VND');
    console.log(`%cTotal Spent on Uniqlo.com: %c${formattedTotalSpent}`, 'font-weight: bold;', 'font-weight: bold; color: green;');
    console.log(`%cTotal Spent on Uniqlo.com: %c${formattedTotalSpent}`, 'font-weight: bold;font-size: 24px', 'font-weight: bold; color: green;font-size: 24px');
    }

    // Call the function to start processing order data and calculating the total spent
  6. tdtgit created this gist Aug 2, 2023.
    83 changes: 83 additions & 0 deletions udontknowhowmuchuspent.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    // Function to format a number as currency
    function formatCurrency(amount, currencyCode) {
    return new Intl.NumberFormat('vi-VN', {
    style: 'currency',
    currency: currencyCode,
    }).format(amount);
    }

    // Function to fetch order details using the order ID
    async function fetchOrderDetails(orderID) {
    const url = `https://www.uniqlo.com/vn/api/commerce/v3/vi/orders/${orderID}`;

    try {
    const response = await fetch(url);

    if (response.ok) {
    const data = await response.json();

    if (data.status === 'ok' && data.result && data.result.orderDetail) {
    return data.result.orderDetail;
    } else {
    console.log(`Error parsing order details for order ID ${orderID}`);
    }
    } else {
    console.log(`Error fetching order details for order ID ${orderID}`);
    }
    } catch (error) {
    console.error('An error occurred:', error);
    }

    return null;
    }

    // Function to fetch order data from the API
    async function fetchOrderData() {
    const url = `https://www.uniqlo.com/vn/api/commerce/v3/vi/orders?searchPage=1&displayResults=50`;

    try {
    const response = await fetch(url);

    if (response.ok) {
    const data = await response.json();

    if (data.status === 'ok' && data.result && data.result.orders) {
    return data.result.orders;
    } else {
    console.log('Error parsing order data');
    }
    } else {
    console.log('Error fetching order data');
    }
    } catch (error) {
    console.error('An error occurred:', error);
    }

    return [];
    }

    // Function to delay execution
    function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
    }

    // Function to loop through order data, fetch order details, and calculate the total spent
    async function processOrderData() {
    const orders = await fetchOrderData();
    let totalSpent = 0;

    for (const order of orders) {
    const orderDetails = await fetchOrderDetails(order.no);
    if (orderDetails) {
    totalSpent += orderDetails.totalAmountTaxIncluded.value;
    }

    await delay(500); // Add a delay of 1 second between requests
    }

    const formattedTotalSpent = formatCurrency(totalSpent, 'VND');
    console.log(`%cTotal Spent on Uniqlo.com: %c${formattedTotalSpent}`, 'font-weight: bold;', 'font-weight: bold; color: green;');
    }

    // Call the function to start processing order data and calculating the total spent
    processOrderData();