(20241216) Payment auth bug fix.
This commit is contained in:
@@ -90,7 +90,7 @@ from bson import ObjectId
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class AuthTokenController(BaseModel):
|
||||
class CoreAuthTokenController(BaseModel):
|
||||
|
||||
# ┏┓┓ ┓┏
|
||||
# ┃ ┃┏┓┏┏ ┃┃┏┓┏┓┏
|
||||
|
||||
@@ -31,8 +31,6 @@
|
||||
|
||||
# To make sibling directories accessible for imports:
|
||||
import sys
|
||||
from pyexpat.errors import messages
|
||||
|
||||
sys.path.append(".")
|
||||
sys.path.append("..")
|
||||
|
||||
@@ -110,7 +108,7 @@ import asyncio
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class MessageController(BaseModel):
|
||||
class CoreMessageController(BaseModel):
|
||||
|
||||
# ┏┓┓ ┓┏
|
||||
# ┃ ┃┏┓┏┏ ┃┃┏┓┏┓┏
|
||||
|
||||
+70
-126
@@ -6,11 +6,11 @@
|
||||
|
||||
DATE:
|
||||
|
||||
Thursday, 12th Dec., 2024
|
||||
Monday, 16th Dec., 2024
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To handle all messages from one place.
|
||||
To handle all payments from one place.
|
||||
|
||||
REFERENCES:
|
||||
|
||||
@@ -31,8 +31,6 @@
|
||||
|
||||
# To make sibling directories accessible for imports:
|
||||
import sys
|
||||
from pyexpat.errors import messages
|
||||
|
||||
sys.path.append(".")
|
||||
sys.path.append("..")
|
||||
|
||||
@@ -50,7 +48,7 @@ from controllers.base import BaseModel
|
||||
|
||||
# Data models:
|
||||
from models.core.auth_token import CoreAuthTokenModel
|
||||
from models.core.message import CoreMessageModel
|
||||
from models.core.payment import CorePaymentModel, PaymentEvent
|
||||
from models.core.user import CoreUserInfoModel
|
||||
|
||||
# To work with MongoDB:
|
||||
@@ -110,56 +108,36 @@ import asyncio
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class MessageController(BaseModel):
|
||||
class CorePaymentController(BaseModel):
|
||||
|
||||
# ┏┓┓ ┓┏
|
||||
# ┃ ┃┏┓┏┏ ┃┃┏┓┏┓┏
|
||||
# ┗┛┗┗┻┛┛ ┗┛┗┻┛ ┛
|
||||
|
||||
# For MongoDB:
|
||||
MESSAGES_COLLECTION = "_messages"
|
||||
PAYMENTS_COLLECTION = "_payments"
|
||||
|
||||
# ┏┓┳┓┳┳┳┓ ┏┓
|
||||
# ┃ ┣┫┃┃┃┃ ━━ ┃ ┏┓┏┓┏┓╋┏┓
|
||||
# ┗┛┛┗┗┛┻┛ ┗┛┛ ┗ ┗┻┗┗
|
||||
|
||||
async def insert(
|
||||
async def init(
|
||||
self,
|
||||
mongo_conn: AsyncMongo,
|
||||
message: CoreMessageModel
|
||||
payment: CorePaymentModel
|
||||
) -> ObjectId:
|
||||
|
||||
"""
|
||||
Simply insert one message document into the database.
|
||||
Simply insert one payment document into the database.
|
||||
:param mongo_conn: The instance of the database connector to use for the operation.
|
||||
:param message: The message to save into the database.
|
||||
:param payment: The payment whose record needs to be saved in the database.
|
||||
:return: The object id of the inserted document.
|
||||
"""
|
||||
|
||||
# Simply insert the document:
|
||||
return await mongo_conn.insert_one(
|
||||
collection = self.MESSAGES_COLLECTION,
|
||||
document = message,
|
||||
raise_exception = True
|
||||
)
|
||||
|
||||
async def bulk_write(
|
||||
self,
|
||||
mongo_conn: AsyncMongo,
|
||||
mongo_operations: list
|
||||
) -> int:
|
||||
|
||||
"""
|
||||
Needed in cases like forcing re-sync of mails where you need to perform actions like bulk replacements of
|
||||
existing documents. Not recommended to use. Please use very carefully to ensure document integrity.
|
||||
:param mongo_conn: The instance of the database connector to use for the operation.
|
||||
:param mongo_operations: The list operations that are supported by MongoDB's Bulk Write system.
|
||||
:return: The no. of documents affected.
|
||||
"""
|
||||
|
||||
return await mongo_conn.bulk_write(
|
||||
collection = self.MESSAGES_COLLECTION,
|
||||
requests = mongo_operations,
|
||||
collection = self.PAYMENTS_COLLECTION,
|
||||
document = payment,
|
||||
raise_exception = True
|
||||
)
|
||||
|
||||
@@ -167,7 +145,7 @@ class MessageController(BaseModel):
|
||||
# ┃ ┣┫┃┃┃┃ ━━ ┣┫┏┓╋┏┓┓┏┓┓┏┏┓
|
||||
# ┗┛┛┗┗┛┻┛ ┛┗┗ ┗┛ ┗┗ ┗┛┗
|
||||
|
||||
async def count_messages(
|
||||
async def count_payments(
|
||||
self,
|
||||
mongo_conn: AsyncMongo,
|
||||
token_ids: List[ObjectId | str],
|
||||
@@ -175,11 +153,11 @@ class MessageController(BaseModel):
|
||||
) -> int:
|
||||
|
||||
"""
|
||||
Just counts the no. of messages that match a given set of conditions.
|
||||
Just counts the no. of payment records that match a given set of conditions.
|
||||
:param mongo_conn: The instance of the database connector to use for the operation.
|
||||
:param token_ids: The token ids of the accounts from which these messages must be fetched.
|
||||
:param token_ids: The token ids of the accounts from which these payment details must be fetched.
|
||||
:param additional_filter: Any addition filters to use.
|
||||
:return: The no. of messages that match the given conditions.
|
||||
:return: The no. of payment records that match the given conditions.
|
||||
"""
|
||||
|
||||
# Prepare the filter:
|
||||
@@ -192,7 +170,7 @@ class MessageController(BaseModel):
|
||||
|
||||
# Get the count of the documents that match the criteria:
|
||||
count = await mongo_conn.count(
|
||||
collection = self.MESSAGES_COLLECTION,
|
||||
collection = self.PAYMENTS_COLLECTION,
|
||||
filter = filter_json,
|
||||
raise_exception = True
|
||||
)
|
||||
@@ -200,23 +178,23 @@ class MessageController(BaseModel):
|
||||
# Done here:
|
||||
return count
|
||||
|
||||
async def get_previews(
|
||||
async def get_payment_previews(
|
||||
self,
|
||||
mongo_conn: AsyncMongo,
|
||||
token_ids: List[ObjectId | str],
|
||||
limit: int = 100,
|
||||
skip: int = 0,
|
||||
additional_filter: dict = None
|
||||
) -> List[CoreMessageModel] | None:
|
||||
) -> List[CorePaymentModel] | None:
|
||||
|
||||
"""
|
||||
Fetches many messages in one call, but leaves out the full payloads.
|
||||
Fetches many payment details in one call, but just their previews.
|
||||
:param mongo_conn: The instance of the database connector to use for the operation.
|
||||
:param token_ids: The token ids of the accounts from which these messages must be fetched.
|
||||
:param limit: The max. no. of messages to retrieve in this call.
|
||||
:param skip: The no. of initial messages to skip. Useful for pagination.
|
||||
:param limit: The max. no. of payment details to retrieve in this call.
|
||||
:param skip: The no. of initial payment details to skip. Useful for pagination.
|
||||
:param additional_filter: Any addition filters to use.
|
||||
:return: The list of messages (as the message model). This list can be empty.
|
||||
:return: The list of payments (as the payments model). This list can be empty.
|
||||
"""
|
||||
|
||||
# Prepare the filter:
|
||||
@@ -230,68 +208,9 @@ class MessageController(BaseModel):
|
||||
# We fetch the messages that are identified by a specific token id,
|
||||
# with the specified fetching limits, while enforcing the sorting condition:
|
||||
records = await mongo_conn.find_many(
|
||||
collection = self.MESSAGES_COLLECTION,
|
||||
filter = filter_json,
|
||||
limit = limit,
|
||||
skip = skip,
|
||||
sort = {"ts": -1},
|
||||
projection = {
|
||||
"_id": True,
|
||||
"ts": True,
|
||||
"syncTs": True,
|
||||
"tokenId": True,
|
||||
"serviceType": True,
|
||||
"client": True,
|
||||
"clientMessageId": True,
|
||||
"clientThreadId": True,
|
||||
"isSent": True,
|
||||
"isBroadcast": True,
|
||||
"sentSuccessfully": True,
|
||||
"sender": True,
|
||||
"chat": True,
|
||||
"snippet": True,
|
||||
"aiSnippet": True,
|
||||
"tags": True
|
||||
},
|
||||
raise_exception = True
|
||||
)
|
||||
|
||||
# Convert the fetched records to instances of the data model and return:
|
||||
for record in records: record["message"] = {}
|
||||
return [CoreMessageModel(**record) for record in records]
|
||||
|
||||
async def get_messages(
|
||||
self,
|
||||
mongo_conn: AsyncMongo,
|
||||
token_ids: List[ObjectId | str],
|
||||
limit: int = 100,
|
||||
skip: int = 0,
|
||||
additional_filter: dict = None
|
||||
) -> List[CoreMessageModel] | None:
|
||||
|
||||
"""
|
||||
Fetches many full messages in one call.
|
||||
:param mongo_conn: The instance of the database connector to use for the operation.
|
||||
:param token_ids: The token ids of the accounts from which these messages must be fetched.
|
||||
:param limit: The max. no. of messages to retrieve in this call.
|
||||
:param skip: The no. of initial messages to skip. Useful for pagination.
|
||||
:param additional_filter: Any addition filters to use.
|
||||
:return: The list of messages (as the message model). This list can be empty.
|
||||
"""
|
||||
|
||||
# Prepare the filter:
|
||||
if not isinstance(token_ids, list): token_ids = [token_ids]
|
||||
token_ids = [ObjectId(t) for t in token_ids]
|
||||
filter_json = {"tokenId": {"$in": token_ids}}
|
||||
if additional_filter:
|
||||
for k, v in additional_filter.items():
|
||||
filter_json[k] = v
|
||||
|
||||
# We fetch the messages that are identified by a specific token id,
|
||||
# with the specified fetching limits, while enforcing the sorting condition:
|
||||
records = await mongo_conn.find_many(
|
||||
collection = self.MESSAGES_COLLECTION,
|
||||
collection = self.PAYMENTS_COLLECTION,
|
||||
filter = filter_json,
|
||||
projection = {"events": False},
|
||||
limit = limit,
|
||||
skip = skip,
|
||||
sort = {"ts": -1},
|
||||
@@ -299,28 +218,29 @@ class MessageController(BaseModel):
|
||||
)
|
||||
|
||||
# Convert the fetched records to instances of the data model and return:
|
||||
return [CoreMessageModel(**record) for record in records]
|
||||
for record in records: record["events"] = []
|
||||
return [CorePaymentModel(**record) for record in records]
|
||||
|
||||
async def get_message(
|
||||
async def get_payment(
|
||||
self,
|
||||
mongo_conn: AsyncMongo,
|
||||
token_id: ObjectId | str,
|
||||
message_id: ObjectId | str,
|
||||
) -> CoreMessageModel | None:
|
||||
payment_id: ObjectId | str,
|
||||
) -> CorePaymentModel | None:
|
||||
|
||||
"""
|
||||
Gets one message if you know its message id.
|
||||
Gets one payment detail if you know its payment id.
|
||||
:param mongo_conn: The instance of the database connector to use for the operation.
|
||||
:param token_id: The id of the auth-token associated with the message. Needed for security.
|
||||
:param message_id: The id of the message that needs to be read.
|
||||
:return: The contents of that one message in a structured format.
|
||||
:param token_id: The id of the auth-token associated with the payment. Needed for security.
|
||||
:param payment_id: The id of the payment detail that needs to be read.
|
||||
:return: The contents of that one payment detail in a structured format.
|
||||
"""
|
||||
|
||||
# We fetch the whole payload of that one message:
|
||||
record = await mongo_conn.find_one(
|
||||
collection = self.MESSAGES_COLLECTION,
|
||||
collection = self.PAYMENTS_COLLECTION,
|
||||
filter = {
|
||||
"_id": ObjectId(message_id),
|
||||
"_id": ObjectId(payment_id),
|
||||
"tokenId": ObjectId(token_id)
|
||||
},
|
||||
raise_exception = True
|
||||
@@ -331,40 +251,64 @@ class MessageController(BaseModel):
|
||||
|
||||
# If a record was found,
|
||||
# we return it as our data model:
|
||||
return CoreMessageModel(**record)
|
||||
return CorePaymentModel(**record)
|
||||
|
||||
# ┏┓┳┓┳┳┳┓ ┳┳ ┓
|
||||
# ┃ ┣┫┃┃┃┃ ━━ ┃┃┏┓┏┫┏┓╋┏┓
|
||||
# ┗┛┛┗┗┛┻┛ ┗┛┣┛┗┻┗┻┗┗
|
||||
# ┛
|
||||
|
||||
# We don't support updating messages themselves,
|
||||
# but we will allow updating fields like tags, marking as read or unread, etc.
|
||||
# We don't support updating payments themselves,
|
||||
# but we will allow updating fields like tags, adding events, etc.
|
||||
|
||||
async def add_event(
|
||||
self,
|
||||
mongo_conn: AsyncMongo,
|
||||
payment_id: ObjectId | str,
|
||||
event: PaymentEvent
|
||||
) -> bool:
|
||||
|
||||
"""
|
||||
Add an event to an existing record of a payment detail.
|
||||
:param mongo_conn: The instance of the database connector to use for the operation.
|
||||
:param payment_id: The id of the payment detail that needs to be read.
|
||||
:param event: The event that occurred. This will typically be generated by the third-party client.
|
||||
:return: True if successfully noted, else False.
|
||||
"""
|
||||
|
||||
# Try to update the existing record:
|
||||
return await mongo_conn.update_one(
|
||||
collection = self.PAYMENTS_COLLECTION,
|
||||
filter = {"_id": ObjectId(payment_id)},
|
||||
update = {"$push": {"events": event}},
|
||||
upsert = False,
|
||||
raise_exception = True
|
||||
)
|
||||
|
||||
async def update_tags(
|
||||
self,
|
||||
mongo_conn: AsyncMongo,
|
||||
token_id: ObjectId | str,
|
||||
message_id: ObjectId | str,
|
||||
payment_id: ObjectId | str,
|
||||
unset_tags: List[str] = None,
|
||||
set_tags: List[str] = None
|
||||
) -> bool:
|
||||
|
||||
"""
|
||||
Updates the tags on one message. The tags to remove are processed first, the ones to add are processed later.
|
||||
Updates the tags on one payment. The tags to remove are processed first, the ones to add are processed later.
|
||||
:param mongo_conn: The instance of the database connector to use for the operation.
|
||||
:param token_id: The id of the auth-token associated with the message. Needed for security.
|
||||
:param message_id: The id of the message that needs to be read.
|
||||
:param unset_tags: The tags to remove from the message.
|
||||
:param set_tags: The tags to add to the message.
|
||||
:param token_id: The id of the auth-token associated with the payment. Needed for security.
|
||||
:param payment_id: The id of the payment detail that needs to be read.
|
||||
:param unset_tags: The tags to remove from the payment record.
|
||||
:param set_tags: The tags to add to the payment record.
|
||||
:return: True if the update was successful, else False.
|
||||
"""
|
||||
|
||||
# Update the tags:
|
||||
return await mongo_conn.update_one(
|
||||
collection = self.MESSAGES_COLLECTION,
|
||||
collection = self.PAYMENTS_COLLECTION,
|
||||
filter = {
|
||||
"_id": ObjectId(message_id),
|
||||
"_id": ObjectId(payment_id),
|
||||
"tokenId": ObjectId(token_id)
|
||||
},
|
||||
update = [{
|
||||
|
||||
Reference in New Issue
Block a user