File size: 2,973 Bytes
360b354
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import requests
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad, pad
from datetime import datetime, timedelta
import json
from ..utils import dev_url

JWT_SECRET_KEY = "super-secret-key"

def encrypt(raw):
    try:
        raw = str(raw)
        raw = pad(raw.encode(), 32)
        cipher = AES.new(JWT_SECRET_KEY.encode("utf-8"), AES.MODE_ECB)
        return base64.b64encode(cipher.encrypt(raw))
    except Exception as e:
        print(e)
        return None

def get_date_range(period):
    today = datetime.now()
    if period == "week":
        start_date = today - timedelta(days=7)
    elif period == "month":
        start_date = today - timedelta(days=30)
    elif period == "six months":
        start_date = today - timedelta(days=180)
    elif period == "whole":
        start_date = datetime(1970, 1, 1)
    elif period == "yesterday":
        start_date = today - timedelta(days=1)
        end_date = start_date
    elif period == "today":
        start_date = today
        end_date = start_date
    else:
        raise ValueError("Invalid period type. Choose from 'week', 'month', 'six months', 'whole', 'yesterday', or 'today'.")
    
    end_date = today if period not in ["yesterday", "today"] else end_date
    return start_date, end_date

def fetch_response(store_id, brand_id, period=None, start_date=None, end_date=None, hours=None, minutes=None, seconds=None):
    if period:
        start_date, end_date = get_date_range(period)
    elif not start_date or not end_date:
        raise ValueError("Start date and end date must be provided if period is not specified.")
    
    if start_date == end_date:
        if hours is not None and minutes is not None and seconds is not None:
            start_date = start_date.replace(hour=hours, minute=minutes, second=seconds)
            end_date = end_date.replace(hour=hours, minute=minutes, second=seconds)
        else:
            start_date = start_date.replace(hour=0, minute=0, second=0)
            end_date = end_date.replace(hour=23, minute=59, second=59)
    
    start_date_str = start_date.strftime("%Y-%m-%d %H:%M:%S")
    end_date_str = end_date.strftime("%Y-%m-%d %H:%M:%S")

    encrypted = encrypt(brand_id)
    if encrypted is None:
        print("Error in encryption")
        return
    
    url = f"https://dev.sahlhub.com/api/v1/order/archiveOrder?storeId={store_id}&startDate={start_date_str}&enddate={end_date_str}"

    auth_token = "SAHL_COOKIE_KEY"
    headers = {
        "Content-Type": "application/json",
        "userToken": auth_token,
        "brandid": encrypted.decode("utf-8", "ignore"),
        "origin": "https://pos-dev.sahlhub.com",
    }

    response = requests.request("GET", url, headers=headers)
    
    if response.status_code == 200:
        data = response.json()
        return data
    else:
        print(f"Failed to fetch data. Status code: {response.status_code}")
        print(response.text)
        return None