(20241216) Payment auth bug fix.
This commit is contained in:
@@ -54,7 +54,7 @@ from models.api.mail.sync import MailSyncOneResult, MailSyncManyResults
|
||||
|
||||
# Mail Clients:
|
||||
from utils_v2.goog.gmail.gmail_client import AsyncGMailClient
|
||||
from utils_v2.goog.models.data.auth_tokens import GoogleAuthTokens
|
||||
from utils_v2.goog.models.auth_tokens import GoogleAuthTokens
|
||||
|
||||
# To work with MongoDB:
|
||||
from bson import ObjectId
|
||||
|
||||
+152
-227
@@ -21,7 +21,8 @@
|
||||
N/A
|
||||
|
||||
"""
|
||||
import asyncio
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** IMPORT ***
|
||||
@@ -43,35 +44,31 @@ from utils_v2.date_time import date_time
|
||||
from utils_v2.database.async_mysql_v2 import AsyncMySQL
|
||||
from utils_v2.database.async_mongo_v2 import AsyncMongo, AsyncMongoStorage
|
||||
|
||||
# Base model:
|
||||
from controllers.base import BaseModel
|
||||
|
||||
# Data models:
|
||||
from models.core.user import CoreUserInfoModel
|
||||
from models.core.auth_token import CoreAuthTokenModel
|
||||
from models.core.message import CoreMessageModel
|
||||
from models.api.sms.send import (
|
||||
SMSSendRequestData,
|
||||
NimbusSMSIndiaMessage,
|
||||
SavvyBulkSMSKenyaMessage,
|
||||
SMSSendManyResults
|
||||
from models.core.payment import CorePaymentModel, PaymentEvent
|
||||
from models.api.finstitutions.payments.request import (
|
||||
PaymentRequestOneResult
|
||||
)
|
||||
|
||||
# SMS Clients:
|
||||
from utils_v2.sms.models.behaviour.nimbus.async_nimbus import AsyncNimbusSMS
|
||||
from utils_v2.sms.models.behaviour.savvy_bulk_sms.async_savvy_bulk_sms import AsyncSavvyBulkSMS
|
||||
from utils_v2.sms.models.data.sms_message import SentSMSMessageModel
|
||||
# Payment Clients:
|
||||
from utils_v2.payments.safaricom.models.auth import MPesaExpressAuthorization
|
||||
from utils_v2.payments.safaricom.controllers.m_pesa_express import SafaricomMPesaExpress
|
||||
|
||||
# To work with MongoDB:
|
||||
from bson import ObjectId
|
||||
from pymongo import InsertOne
|
||||
|
||||
# To work with datatypes:
|
||||
from typing import Literal, List, Dict, Any
|
||||
from typing import Literal, List, Dict, Any, Union
|
||||
|
||||
# To make API calls:
|
||||
import httpx
|
||||
|
||||
# For asynchronous activities:
|
||||
import asyncio
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
@@ -110,7 +107,7 @@ import httpx
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class SMSController:
|
||||
class PaymentController:
|
||||
|
||||
# ┏┓┓ ┓┏
|
||||
# ┃ ┃┏┓┏┏ ┃┃┏┓┏┓┏
|
||||
@@ -174,239 +171,167 @@ class SMSController:
|
||||
token_key = token_key
|
||||
)
|
||||
|
||||
# ┏┓ ┓
|
||||
# ┗┓┏┓┏┓┏┫
|
||||
# ┗┛┗ ┛┗┗┻
|
||||
# ┳┓ ┏┓
|
||||
# ┣┫┏┓┏┓┓┏┏┓┏╋ ┃┃┏┓┓┏┏┳┓┏┓┏┓╋┏
|
||||
# ┛┗┗ ┗┫┗┻┗ ┛┗ ┣┛┗┻┗┫┛┗┗┗ ┛┗┗┛
|
||||
# ┗ ┛
|
||||
|
||||
@staticmethod
|
||||
async def __send_from_nimbus_sms_india(
|
||||
http_client: httpx.AsyncClient,
|
||||
auth_token: CoreAuthTokenModel,
|
||||
messages: List[NimbusSMSIndiaMessage],
|
||||
) -> SMSSendManyResults:
|
||||
# @staticmethod
|
||||
# async def __request_from_safaricom_m_pesa_express(
|
||||
# http_client: httpx.AsyncClient,
|
||||
# auth_token: CoreAuthTokenModel,
|
||||
# payment: Union[SafaricomMPesaExpressRequest],
|
||||
# callback_url: str
|
||||
# ) -> PaymentRequestOneResult:
|
||||
#
|
||||
# # Start by assuming failure:
|
||||
# request_result = PaymentRequestOneResult()
|
||||
#
|
||||
# # Create the client:
|
||||
# client = SafaricomMPesaExpress(
|
||||
# auth = MPesaExpressAuthorization(
|
||||
# consumerKey = auth_token.auth["consumerKey"],
|
||||
# consumerSecret = auth_token.auth["consumerSecret"],
|
||||
# businessShortCode = auth_token.auth["businessShortCode"],
|
||||
# appPasskey = auth_token.auth["appPasskey"]
|
||||
# ),
|
||||
# http_client = http_client
|
||||
# )
|
||||
#
|
||||
# # Make the payment request:
|
||||
# client_response = await client.request_payment(
|
||||
# amount = payment.amount,
|
||||
# party_a = payment.partyA,
|
||||
# type = payment.transactionType,
|
||||
# reference = payment.accountReference,
|
||||
# description = payment.transactionDescription,
|
||||
# callback_url = callback_url,
|
||||
# payer_no = payment.phoneNo,
|
||||
# party_b = payment.partyB
|
||||
# )
|
||||
#
|
||||
# # Construct the payment details:
|
||||
# payment_details = CorePaymentModel(
|
||||
# user = None,
|
||||
# lastEventTs = date_time.get_current_utc_date_time(as_string = False),
|
||||
# lastPaymentStatus = "initiated" if client_response.success else "initFailed",
|
||||
# tokenId = auth_token.authTokenId,
|
||||
# amount = payment.amount,
|
||||
# curencyCode = "KES",
|
||||
# # metadata = payment.
|
||||
# )
|
||||
#
|
||||
# # Done here:
|
||||
# request_result.success = client_response.success
|
||||
# request_result.message = client_response.message
|
||||
# request_result.message = client_response.message
|
||||
# return request_result
|
||||
#
|
||||
# async def request_payment(
|
||||
# self,
|
||||
# mongo_conn: AsyncMongo,
|
||||
# http_client: httpx.AsyncClient,
|
||||
# auth_token: CoreAuthTokenModel,
|
||||
# payment: Union[SafaricomMPesaExpressRequest],
|
||||
# callback_url: str
|
||||
# ) -> PaymentRequestOneResult:
|
||||
#
|
||||
# # Start by assuming failure:
|
||||
# request_result = PaymentRequestOneResult()
|
||||
#
|
||||
# # Now we route the message to the appropriate client:
|
||||
# match auth_token.client:
|
||||
# case "safaricomMPesaExpress":
|
||||
# request_result = await self.__request_from_safaricom_m_pesa_express(
|
||||
# http_client = http_client,
|
||||
# auth_token = auth_token,
|
||||
# payment = payment,
|
||||
# callback_url = callback_url
|
||||
# )
|
||||
# case _:
|
||||
# request_result.message = f"invalid client {auth_token.client}"
|
||||
#
|
||||
# # Done here:
|
||||
# return request_result
|
||||
|
||||
# Start with a blank variable:
|
||||
send_results = SMSSendManyResults()
|
||||
# ┓ • ┓ ┏┓ ┏┓
|
||||
# ┃ ┓┏╋ ┏┓┏┓┏┫ ┃┓┏┓╋ ┃┃┏┓┓┏┏┳┓┏┓┏┓╋┏
|
||||
# ┗┛┗┛┗ ┗┻┛┗┗┻ ┗┛┗ ┗ ┣┛┗┻┗┫┛┗┗┗ ┛┗┗┛
|
||||
# ┛
|
||||
|
||||
# Initialize the third-party client:
|
||||
client = AsyncNimbusSMS(
|
||||
entity_id = auth_token.auth["entityId"],
|
||||
sender_id = auth_token.auth["senderId"],
|
||||
user_id = auth_token.auth["userId"],
|
||||
api_key = auth_token.auth["apiKey"],
|
||||
http_client = http_client
|
||||
)
|
||||
|
||||
# Iterate over all the messages you need to send:
|
||||
for message in messages:
|
||||
|
||||
# Send the SMS and return the response:
|
||||
client_response = await client.send_sms(
|
||||
recipient_number = message.recipientNo,
|
||||
message = message.text,
|
||||
template_id = message.templateId
|
||||
)
|
||||
|
||||
# Note down the results:
|
||||
send_results.totalCount += 1
|
||||
if client_response.success: send_results.successCount += 1
|
||||
else: send_results.failureCount += 1
|
||||
send_results.smsMessages.append(CoreMessageModel(
|
||||
ts = client_response.ts,
|
||||
syncTs = date_time.get_current_utc_date_time(as_string = False),
|
||||
tokenId = auth_token.authTokenId,
|
||||
serviceType = auth_token.serviceType,
|
||||
client = auth_token.client,
|
||||
clientMessageId = client_response.messageId,
|
||||
clientThreadId = message.recipientNo,
|
||||
isSent = True,
|
||||
isBroadcast = False,
|
||||
sentSuccessfully = client_response.success,
|
||||
sender = None,
|
||||
recipient = message.recipientNo,
|
||||
chat = None,
|
||||
message = client_response.model_dump(),
|
||||
snippet = message.text,
|
||||
aiSnippet = None,
|
||||
tags = ["sms", "nimbusSmsIndia"]
|
||||
))
|
||||
|
||||
# Done here:
|
||||
return send_results
|
||||
|
||||
@staticmethod
|
||||
async def __send_from_savvy_bulk_sms_kenya(
|
||||
http_client: httpx.AsyncClient,
|
||||
auth_token: CoreAuthTokenModel,
|
||||
messages: List[SavvyBulkSMSKenyaMessage],
|
||||
) -> SMSSendManyResults:
|
||||
|
||||
# Start with a blank variable:
|
||||
send_results = SMSSendManyResults()
|
||||
|
||||
# Initialize the third-party client:
|
||||
client = AsyncSavvyBulkSMS(
|
||||
partner_id = auth_token.auth["partnerId"],
|
||||
short_code = auth_token.auth["shortCode"],
|
||||
api_key = auth_token.auth["apiKey"],
|
||||
http_client = http_client
|
||||
)
|
||||
|
||||
# Iterate over all the messages you need to send:
|
||||
for message in messages:
|
||||
|
||||
# Send the SMS and return the response:
|
||||
client_response = await client.send_sms(
|
||||
recipient_number = message.recipientNo,
|
||||
message = message.text
|
||||
)
|
||||
|
||||
# Note down the results:
|
||||
send_results.totalCount += 1
|
||||
if client_response.success: send_results.successCount += 1
|
||||
else: send_results.failureCount += 1
|
||||
send_results.smsMessages.append(CoreMessageModel(
|
||||
ts = client_response.ts,
|
||||
syncTs = date_time.get_current_utc_date_time(as_string = False),
|
||||
tokenId = auth_token.authTokenId,
|
||||
serviceType = auth_token.serviceType,
|
||||
client = auth_token.client,
|
||||
clientMessageId = client_response.messageId,
|
||||
clientThreadId = message.recipientNo,
|
||||
isSent = True,
|
||||
isBroadcast = False,
|
||||
sentSuccessfully = client_response.success,
|
||||
sender = None,
|
||||
recipient = message.recipientNo,
|
||||
chat = None,
|
||||
message = client_response.model_dump(),
|
||||
snippet = message.text,
|
||||
aiSnippet = None,
|
||||
tags = ["sms", "savvyBulkSmsKenya"]
|
||||
))
|
||||
|
||||
# Done here:
|
||||
return send_results
|
||||
|
||||
async def send(
|
||||
self,
|
||||
mongo_conn: AsyncMongo,
|
||||
http_client: httpx.AsyncClient,
|
||||
# token_id: ObjectId | str,
|
||||
auth_token: CoreAuthTokenModel,
|
||||
messages: List[NimbusSMSIndiaMessage | SavvyBulkSMSKenyaMessage]
|
||||
) -> SMSSendManyResults:
|
||||
|
||||
# Start by assuming failure:
|
||||
send_results = SMSSendManyResults()
|
||||
|
||||
# # We first load the authorization tokens:
|
||||
# auth_token = await self.get_token(
|
||||
# mongo_conn = mongo_conn,
|
||||
# token_id = token_id,
|
||||
# )
|
||||
#
|
||||
# # If we failed to load the authorization tokens:
|
||||
# if not auth_token:
|
||||
# send_results.message = f"no such token id '{token_id}'"
|
||||
# return send_results
|
||||
|
||||
# Now we route the message to the appropriate client:
|
||||
match auth_token.client:
|
||||
case "nimbusSmsIndia":
|
||||
send_results = await self.__send_from_nimbus_sms_india(
|
||||
http_client = http_client,
|
||||
auth_token = auth_token,
|
||||
messages = messages
|
||||
)
|
||||
case "savvyBulkSmsKenya":
|
||||
send_results = await self.__send_from_savvy_bulk_sms_kenya(
|
||||
http_client = http_client,
|
||||
auth_token = auth_token,
|
||||
messages = messages
|
||||
)
|
||||
case _:
|
||||
send_results.message = f"invalid client {auth_token.client}"
|
||||
|
||||
# Save the results to MongoDB:
|
||||
tasks = []
|
||||
for sms in send_results.smsMessages:
|
||||
message_json = sms.model_dump()
|
||||
message_json.pop("_id", None)
|
||||
tasks.append(current_app.core_message_controller.insert(
|
||||
mongo_conn = mongo_conn,
|
||||
message = message_json
|
||||
))
|
||||
results = await asyncio.gather(*tasks)
|
||||
|
||||
# Done here:
|
||||
send_results.message = f"{send_results.successCount}/{send_results.totalCount} message(s) sent"
|
||||
return send_results
|
||||
|
||||
# ┓ • ┏┓ ┏┓ ┳┳┓
|
||||
# ┃ ┓┏╋ ┣╋ ┃┓┏┓╋ ┃┃┃┏┓┏┏┏┓┏┓┏┓┏
|
||||
# ┗┛┗┛┗ ┗┻ ┗┛┗ ┗ ┛ ┗┗ ┛┛┗┻┗┫┗ ┛
|
||||
# ┛
|
||||
|
||||
# These are simply for retrieving sms messages.
|
||||
# These are simply for retrieving payment records.
|
||||
# You need to already have them saved to the database.
|
||||
|
||||
# @staticmethod
|
||||
# async def list_messages(
|
||||
# mongo_conn: AsyncMongo,
|
||||
# token_ids: List[ObjectId | str],
|
||||
# limit: int = 100,
|
||||
# skip: int = 0,
|
||||
# additional_filter: dict = None
|
||||
# ) -> List[CoreMessageModel] | None:
|
||||
#
|
||||
# # regardless of what additional filter is provided from outside,
|
||||
# # we add a mail-selecting filter here:
|
||||
# if additional_filter is None: additional_filter = {}
|
||||
# additional_filter["serviceType"] = "sms"
|
||||
#
|
||||
# # Simply call the core model:
|
||||
# return await current_app.core_message_controller.get_message(
|
||||
# mongo_conn = mongo_conn,
|
||||
# token_ids = token_ids,
|
||||
# limit = limit,
|
||||
# skip = skip,
|
||||
# additional_filter = additional_filter
|
||||
# )
|
||||
#
|
||||
# @staticmethod
|
||||
# async def get_one_mail(
|
||||
# mongo_conn: AsyncMongo,
|
||||
# token_id: ObjectId | str,
|
||||
# message_id: ObjectId | str
|
||||
# ) -> CoreMessageModel | None:
|
||||
#
|
||||
# # Simply call the core model:
|
||||
# return await current_app.core_message_controller.get_message(
|
||||
# mongo_conn = mongo_conn,
|
||||
# token_id = token_id,
|
||||
# message_id = message_id
|
||||
# )
|
||||
@staticmethod
|
||||
async def list_payments(
|
||||
mongo_conn: AsyncMongo,
|
||||
token_ids: List[ObjectId | str],
|
||||
limit: int = 100,
|
||||
skip: int = 0,
|
||||
additional_filter: dict = None
|
||||
) -> List[CorePaymentModel] | None:
|
||||
|
||||
# Regardless of what additional filter is provided from outside,
|
||||
# we add a payment-selecting filter here:
|
||||
if additional_filter is None: additional_filter = {}
|
||||
additional_filter["serviceType"] = "paymentGateway"
|
||||
|
||||
# Simply call the core model:
|
||||
return await current_app.core_payment_controller.get_payment_previews(
|
||||
mongo_conn = mongo_conn,
|
||||
token_ids = token_ids,
|
||||
limit = limit,
|
||||
skip = skip,
|
||||
additional_filter = additional_filter
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def get_payment(
|
||||
mongo_conn: AsyncMongo,
|
||||
token_id: ObjectId | str,
|
||||
payment_id: ObjectId | str
|
||||
) -> CorePaymentModel | None:
|
||||
|
||||
# Simply call the core model:
|
||||
return await current_app.core_payment_controller.get_payment(
|
||||
mongo_conn = mongo_conn,
|
||||
token_id = token_id,
|
||||
payment_id = payment_id
|
||||
)
|
||||
|
||||
# ┳┳ ┓
|
||||
# ┃┃┏┓┏┫┏┓╋┏┓
|
||||
# ┗┛┣┛┗┻┗┻┗┗
|
||||
# ┛
|
||||
|
||||
@staticmethod
|
||||
async def add_event(
|
||||
mongo_conn: AsyncMongo,
|
||||
payment_id: ObjectId | str,
|
||||
event: PaymentEvent
|
||||
) -> bool:
|
||||
|
||||
# Simply call the core model:
|
||||
return await current_app.core_payment_controller.add_event(
|
||||
mongo_conn = mongo_conn,
|
||||
payment_id = payment_id,
|
||||
event = event
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def update_tags(
|
||||
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:
|
||||
|
||||
# Simply call the core model:
|
||||
return await current_app.core_message_controller.update_tags(
|
||||
return await current_app.core_payment_controller.update_tags(
|
||||
mongo_conn = mongo_conn,
|
||||
token_id = token_id,
|
||||
message_id = message_id,
|
||||
payment_id = payment_id,
|
||||
unset_tags = unset_tags,
|
||||
set_tags = set_tags
|
||||
)
|
||||
|
||||
+9
-17
@@ -21,7 +21,8 @@
|
||||
N/A
|
||||
|
||||
"""
|
||||
import asyncio
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** IMPORT ***
|
||||
@@ -58,9 +59,9 @@ from models.api.sms.send import (
|
||||
)
|
||||
|
||||
# SMS Clients:
|
||||
from utils_v2.sms.models.behaviour.nimbus.async_nimbus import AsyncNimbusSMS
|
||||
from utils_v2.sms.models.behaviour.savvy_bulk_sms.async_savvy_bulk_sms import AsyncSavvyBulkSMS
|
||||
from utils_v2.sms.models.data.sms_message import SentSMSMessageModel
|
||||
from utils_v2.sms.india.nimbus.controllers.async_nimbus import AsyncNimbusSMS
|
||||
from utils_v2.sms.kenya.savvy_bulk_sms.controllers.async_savvy_bulk_sms import AsyncSavvyBulkSMS
|
||||
from utils_v2.sms.models.sms_message import SentSMSMessageModel
|
||||
|
||||
# To work with MongoDB:
|
||||
from bson import ObjectId
|
||||
@@ -72,6 +73,9 @@ from typing import Literal, List, Dict, Any
|
||||
# To make API calls:
|
||||
import httpx
|
||||
|
||||
# For asynchronous activities:
|
||||
import asyncio
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
@@ -130,7 +134,7 @@ class SMSController:
|
||||
# ┛┗┗┻┗┛┗
|
||||
|
||||
@staticmethod
|
||||
async def set_token(
|
||||
async def set_token_direct(
|
||||
db_conn: AsyncMySQL,
|
||||
mongo_conn: AsyncMongo,
|
||||
auth_token: CoreAuthTokenModel,
|
||||
@@ -292,7 +296,6 @@ class SMSController:
|
||||
self,
|
||||
mongo_conn: AsyncMongo,
|
||||
http_client: httpx.AsyncClient,
|
||||
# token_id: ObjectId | str,
|
||||
auth_token: CoreAuthTokenModel,
|
||||
messages: List[NimbusSMSIndiaMessage | SavvyBulkSMSKenyaMessage]
|
||||
) -> SMSSendManyResults:
|
||||
@@ -300,17 +303,6 @@ class SMSController:
|
||||
# Start by assuming failure:
|
||||
send_results = SMSSendManyResults()
|
||||
|
||||
# # We first load the authorization tokens:
|
||||
# auth_token = await self.get_token(
|
||||
# mongo_conn = mongo_conn,
|
||||
# token_id = token_id,
|
||||
# )
|
||||
#
|
||||
# # If we failed to load the authorization tokens:
|
||||
# if not auth_token:
|
||||
# send_results.message = f"no such token id '{token_id}'"
|
||||
# return send_results
|
||||
|
||||
# Now we route the message to the appropriate client:
|
||||
match auth_token.client:
|
||||
case "nimbusSmsIndia":
|
||||
|
||||
@@ -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