File size: 7,170 Bytes
f25b29f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import secrets
import string
from bson import ObjectId
from pydantic import BaseModel, Field, ValidationError
from typing import List
import datetime
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from base64 import b64encode, b64decode
import base64
from pymongo.errors import DuplicateKeyError
from pymongo.errors import PyMongoError
import json
from config import settings


# Define a key. Note: it must be of length 16, 24, or 32.
secure_key = settings.secure_key


def encrypt(plain_text: str, key: bytes) -> str:
    cipher = AES.new(key, AES.MODE_CBC)
    iv = cipher.iv
    encrypted_text = cipher.encrypt(pad(plain_text.encode(), AES.block_size))
    return b64encode(iv + encrypted_text).decode()


def decrypt(encrypted_text: str, key: bytes) -> str:
    decrypted_text = b64decode(encrypted_text)
    iv = decrypted_text[:16]
    cipher = AES.new(key, AES.MODE_CBC, iv=iv)
    decrypted_text = unpad(cipher.decrypt(decrypted_text[16:]), AES.block_size)
    return decrypted_text.decode()


class PyObjectId(ObjectId):
    @classmethod
    def __get_validators__(cls):
        yield cls.validate

    @classmethod
    def validate(cls, v):
        if not ObjectId.is_valid(v):
            raise ValueError("Invalid objectid")
        return ObjectId(v)

    @classmethod
    def __modify_schema__(cls, field_schema):
        field_schema.update(type="string")


class ReceiptModel(BaseModel):
    id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
    receipt_key: str = Field(..., description="The unique key for the receipt.")
    content: List[List[str]] = Field(..., description="An array of single-element arrays, each containing receipt entry.")

    class Config:
        allow_population_by_field_name = True
        arbitrary_types_allowed = True
        json_encoders = {ObjectId: str}
        schema_extra = {
            'example': {
                'receipt_key': 'RzSZ0BTnuG',
                'content': [['YOUR GUEST NUMBER IS'], ['43'], ['IN-N-OUT BURGER LINQ']]
            },
            'title': 'ReceiptModel',
            'description': 'A model representing a receipt with a key and its contents.',
        }


class ReceiptDBModel(BaseModel):
    id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
    user: str = Field(..., description="The user who uploaded the receipt.")
    receipt_key: str = Field(..., description="The unique key for the receipt.")
    content: str = Field(..., description="A string representing DB receipt data.")

    class Config:
        allow_population_by_field_name = True
        arbitrary_types_allowed = True
        json_encoders = {ObjectId: str}
        schema_extra = {
            'example': {
                'user': 'user1',
                'receipt_key': 'RzSZ0BTnuG',
                'content': '{"store": "CVS Pharmacy", "location": "3300 S LAS VEGAS BLVD, LAS VEGAS, NV, 89109"}'
            },
            'title': 'ReceiptProcessedModel',
            'description': 'A model representing a receipt DB contents.',
        }


def merge_data(values):
    data = []
    for idx in range(len(values)):
        data.append([values[idx][1][0]])
        # print(data[idx])

    return data


async def store_data(data, db):
    print("Storing data...")

    key = generate_key()

    try:
        receipt = ReceiptModel(receipt_key=key, content=data)
    except ValidationError as e:
        print(f"An error occurred: {e}")
    else:
        # Convert the Pydantic model instance into a dictionary
        receipt_dict = receipt.dict()

        receipt_dict["content"] = encrypt(str(receipt_dict["content"]), base64.b64decode(secure_key))
        receipt_dict["created_at"] = datetime.datetime.utcnow()

        # Insert the dictionary into MongoDB
        try:
            result = await db["uploads"].insert_one(receipt_dict)
        except DuplicateKeyError:
            raise

        print(f"Inserted document with id: {result.inserted_id}")

        return key

    return None


async def get_receipt_data(key, db):
    print(f"Getting receipt data for key: {key}")

    receipt = await db["uploads"].find_one({"receipt_key": key})
    if receipt is not None:
        await db["uploads"].delete_one({"receipt_key": key})

        receipt['content'] = decrypt(receipt['content'], base64.b64decode(secure_key))

        return receipt['content']

    return None


async def store_receipt_db_data(chatgpt_user, receipt_id, receipt_content, db):
    print("Storing receipt data...")

    try:
        receipt = ReceiptDBModel(user=chatgpt_user, receipt_key=receipt_id, content=receipt_content)
    except ValidationError as e:
        print(f"An error occurred: {e}")
    else:
        # Convert the Pydantic model instance into a dictionary
        receipt_dict = receipt.dict()

        receipt_dict["content"] = encrypt(str(receipt_dict["content"]), base64.b64decode(secure_key))

        # Insert the dictionary into MongoDB
        try:
            query = {"user": chatgpt_user, "receipt_key": receipt_id}
            new_data = {"$set": {"content": receipt_dict["content"]}}

            result = await db["receipts"].update_one(query, new_data, upsert=True)
        except PyMongoError:
            raise

        print(f"Inserted document with id: {result}")

        return result

    return None


async def get_receipt_db_data(chatgpt_user, receipt_id, db):
    print(f"Getting receipt data for key: {receipt_id}")

    receipt = await db["receipts"].find_one({"user": chatgpt_user, "receipt_key": receipt_id})
    if receipt is not None:
        receipt['content'] = decrypt(receipt['content'], base64.b64decode(secure_key))
        return receipt['content']

    return None


async def get_user_receipt_db_ids(chatgpt_user, db):
    print(f"Getting user receipts ids for user: {chatgpt_user}")

    receipts_processed = await db["receipts"].find({"user": chatgpt_user}).to_list(length=100)

    receipts = []
    if receipts_processed is not None:
        for receipt in receipts_processed:
            receipts.append(receipt['receipt_key'])

    return receipts


async def delete_receipt_db_data(chatgpt_user, receipt_id, db):
    print(f"Deleting receipt data for key: {receipt_id}")

    result = await db["receipts"].delete_one({"user": chatgpt_user, "receipt_key": receipt_id})

    if result.deleted_count == 0:
        print(f"Receipt with id: {receipt_id} not found")
    else:
        print(f"Deleted document with id: {result}")

    return result


async def get_user_receipt_content_db(chatgpt_user, db):
    print(f"Getting user receipts fields for user: {chatgpt_user}")

    receipts_processed = await db["receipts"].find({"user": chatgpt_user}).to_list(length=100)

    receipts = []
    if receipts_processed is not None:
        for receipt in receipts_processed:
            receipt['content'] = decrypt(receipt['content'], base64.b64decode(secure_key))
            receipts.append(json.loads(receipt['content']))

    return receipts


def generate_key(length=10):
    alphabet = string.ascii_letters + string.digits
    key = ''.join(secrets.choice(alphabet) for i in range(length))
    return key