(20241207) Started making data models for how things will be stored in the database (mongo).
This commit is contained in:
@@ -63,7 +63,7 @@ from utils_v2.api.async_quart import (
|
|||||||
from shared import constants
|
from shared import constants
|
||||||
|
|
||||||
# Data Models:
|
# Data Models:
|
||||||
from models.data.ai.llm import LLMRequestHeaders, LLMInput
|
from models.data.api.ai.llm import LLMRequestHeaders, LLMInput
|
||||||
|
|
||||||
# For asynchronous activities:
|
# For asynchronous activities:
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ from utils_v2.api.async_quart import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Data Models:
|
# Data Models:
|
||||||
from models.data.chat.auth import ChatAuthRequestHeaders, ChatAuthRequestData
|
from models.data.api.chat.auth import ChatAuthRequestHeaders, ChatAuthRequestData
|
||||||
|
|
||||||
# Common:
|
# Common:
|
||||||
from shared import constants
|
from shared import constants
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ from utils_v2.api.async_quart import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Data Models:
|
# Data Models:
|
||||||
from models.data.chat.auth import ChatAuthRequestHeaders, ChatAuthRequestData
|
from models.data.api.chat.auth import ChatAuthRequestHeaders, ChatAuthRequestData
|
||||||
|
|
||||||
# Common:
|
# Common:
|
||||||
from shared import constants
|
from shared import constants
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ from utils_v2.goog.models.data.auth_tokens import GoogleAuthTokens
|
|||||||
from shared import constants
|
from shared import constants
|
||||||
|
|
||||||
# Data Models:
|
# Data Models:
|
||||||
from models.data.mail.list import MailListRequestHeaders, MailListByAccountIdRequestData
|
from models.data.api.mail.list import MailListRequestHeaders, MailListByAccountIdRequestData
|
||||||
|
|
||||||
# To work with datatypes:
|
# To work with datatypes:
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ from utils_v2.goog.gmail.gmail_client import SCOPES_GMAIL_MAIL_MANAGEMENT
|
|||||||
from shared import constants
|
from shared import constants
|
||||||
|
|
||||||
# Data Models:
|
# Data Models:
|
||||||
from models.data.mail.oauth import (
|
from models.data.api.mail.oauth import (
|
||||||
OAuthMailAuthorizationRequestHeaders,
|
OAuthMailAuthorizationRequestHeaders,
|
||||||
OAuthMailAuthorizationRequestData
|
OAuthMailAuthorizationRequestData
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ from utils_v2.goog.models.data.auth_tokens import GoogleAuthTokens
|
|||||||
from shared import constants
|
from shared import constants
|
||||||
|
|
||||||
# Data Models:
|
# Data Models:
|
||||||
from models.data.mail.retrieve import MailGetRequestHeaders, MailGetRequestData
|
from models.data.api.mail.retrieve import MailGetRequestHeaders, MailGetRequestData
|
||||||
|
|
||||||
# To work with datatypes:
|
# To work with datatypes:
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
|||||||
@@ -70,8 +70,8 @@ from utils_v2.goog.models.data.auth_tokens import GoogleAuthTokens
|
|||||||
from shared import constants
|
from shared import constants
|
||||||
|
|
||||||
# Data Models:
|
# Data Models:
|
||||||
from models.data.mail.sync import MailSyncRequestHeaders, MailSyncRequestData
|
from models.data.api.mail.sync import MailSyncRequestHeaders, MailSyncRequestData
|
||||||
from models.data.mail.sync import MailSyncOneResult, MailSyncManyResults
|
from models.data.api.mail.sync import MailSyncOneResult, MailSyncManyResults
|
||||||
|
|
||||||
# To work with datatypes:
|
# To work with datatypes:
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ from utils_v2.sms.savvy_bulk_sms.async_savvy_bulk_sms import AsyncSavvyBulkSMS
|
|||||||
from shared import constants
|
from shared import constants
|
||||||
|
|
||||||
# Data Models:
|
# Data Models:
|
||||||
from models.data.sms.auth import SMSAuthRequestHeaders, SMSAuthRequestData
|
from models.data.api.sms.auth import SMSAuthRequestHeaders, SMSAuthRequestData
|
||||||
|
|
||||||
# For asynchronous activities:
|
# For asynchronous activities:
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ from utils_v2.api.async_quart import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Data models:
|
# Data models:
|
||||||
from models.data.tech.alerts import (
|
from models.data.api.tech.alerts import (
|
||||||
ChatAlertRequestHeaders,
|
ChatAlertRequestHeaders,
|
||||||
ChatAlertRequestData
|
ChatAlertRequestData
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ from utils_v2.database.async_mongo_v2 import AsyncMongo
|
|||||||
from models.behaviour.base import BaseModel
|
from models.behaviour.base import BaseModel
|
||||||
|
|
||||||
# Data Models:
|
# Data Models:
|
||||||
from models.data.ai.llm import LLMInput, LLMOutput, LLMUsageTokens
|
from models.data.api.ai.llm import LLMInput, LLMOutput, LLMUsageTokens
|
||||||
|
|
||||||
# To work with LLMs:
|
# To work with LLMs:
|
||||||
from langchain_openai import ChatOpenAI
|
from langchain_openai import ChatOpenAI
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ from utils_v2.goog.models.data.auth_tokens import GoogleAuthTokens
|
|||||||
from models.behaviour.base import BaseModel
|
from models.behaviour.base import BaseModel
|
||||||
|
|
||||||
# Data models:
|
# Data models:
|
||||||
from models.data.mail.sync import MailSyncOneResult, MailSyncManyResults
|
from models.data.api.mail.sync import MailSyncOneResult, MailSyncManyResults
|
||||||
|
|
||||||
# To work with MongoDB:
|
# To work with MongoDB:
|
||||||
from bson import ObjectId
|
from bson import ObjectId
|
||||||
@@ -59,7 +59,7 @@ from pymongo import InsertOne, UpdateOne, ReplaceOne
|
|||||||
|
|
||||||
# To work with LLMs:
|
# To work with LLMs:
|
||||||
from models.behaviour.ai.llm.open_ai import LLMOpenAI
|
from models.behaviour.ai.llm.open_ai import LLMOpenAI
|
||||||
from models.data.ai.llm import LLMInput
|
from models.data.api.ai.llm import LLMInput
|
||||||
|
|
||||||
# To work with datatypes:
|
# To work with datatypes:
|
||||||
from typing import Literal, List, Dict, Any
|
from typing import Literal, List, Dict, Any
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
OBJECTIVE:
|
OBJECTIVE:
|
||||||
|
|
||||||
To define how messages will be stored in the database.
|
To define how auth tokens will be stored in the database.
|
||||||
|
|
||||||
REFERENCES:
|
REFERENCES:
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ import datetime
|
|||||||
# *****************************************************************************************************************
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
class CoreMessageModel(BaseModel):
|
class CoreAuthTokenModel(BaseModel):
|
||||||
|
|
||||||
version: str = Field(
|
version: str = Field(
|
||||||
description = "a hint about the version no. of this message",
|
description = "a hint about the version no. of this message",
|
||||||
@@ -86,52 +86,59 @@ class CoreMessageModel(BaseModel):
|
|||||||
default = "1.0.0"
|
default = "1.0.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
ts: AwareDatetime = Field(
|
|
||||||
description = "the time (utc) at which this message was sent by the sender",
|
|
||||||
frozen = True
|
|
||||||
)
|
|
||||||
|
|
||||||
readTs: AwareDatetime = Field(
|
|
||||||
description = "the time (utc) at which this message was read and stored by your server",
|
|
||||||
frozen = True,
|
|
||||||
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
|
|
||||||
)
|
|
||||||
|
|
||||||
tokenId: ObjectId = Field(
|
|
||||||
description = "the id of the auth token that is associated with this message",
|
|
||||||
frozen = True
|
|
||||||
)
|
|
||||||
|
|
||||||
serviceType: Literal["email", "sms", "chat"] = Field(
|
serviceType: Literal["email", "sms", "chat"] = Field(
|
||||||
description = "the kind of service this message was sent/received from",
|
description = "the kind of service this message was sent/received from",
|
||||||
frozen = True
|
frozen = True
|
||||||
)
|
)
|
||||||
|
|
||||||
client: Literal[
|
client: Literal[
|
||||||
"gmail",
|
"gmail", "outlook", # ...................... Mail Clients
|
||||||
"outlook",
|
"telegram", "whatsapp", # .................. Chat Clients
|
||||||
"telegram",
|
"nimbusSmsIndia", "savvyBulkSmsKenya", # ... SMS Clients
|
||||||
"whatsapp",
|
"razorpay", "safaricomMPesaExpress" # ...... Payment Gateways
|
||||||
"nimbusSmsIndia",
|
|
||||||
"savvyBulkSmsKenya"
|
|
||||||
] = Field(
|
] = Field(
|
||||||
description = "the third-part client that was used",
|
description = "the third-part client that was used",
|
||||||
frozen = True
|
frozen = True
|
||||||
)
|
)
|
||||||
|
|
||||||
clientMessageId: str | int = Field(
|
authType: Literal["oauth", "auth"] = Field(
|
||||||
description = "how the client identifies this message",
|
description = "the type of authentication procedure used",
|
||||||
frozen = True
|
frozen = True
|
||||||
)
|
)
|
||||||
|
|
||||||
clientThreadId: str | int | None = Field(
|
firstRequestTs: AwareDatetime = Field(
|
||||||
description = "how the client identifies the chat/thread in which this message was sent/received",
|
description = "the time (utc) at which authorization was first requested",
|
||||||
frozen = True,
|
frozen = True
|
||||||
default = None
|
|
||||||
)
|
)
|
||||||
|
|
||||||
payload: dict = Field(
|
lastRequestTs: AwareDatetime = Field(
|
||||||
description = "the actual contents of the message",
|
description = "the time (utc) at which authorization was last requested",
|
||||||
|
frozen = False
|
||||||
|
)
|
||||||
|
|
||||||
|
firstRefreshTs: AwareDatetime = Field(
|
||||||
|
description = "the time (utc) at which the tokens were first refreshed",
|
||||||
|
frozen = False
|
||||||
|
)
|
||||||
|
|
||||||
|
lastRefreshTs: AwareDatetime = Field(
|
||||||
|
description = "the time (utc) at which the tokens were last refreshed",
|
||||||
|
frozen = False
|
||||||
|
)
|
||||||
|
|
||||||
|
token: dict | None = Field(
|
||||||
|
description = "the actual auth tokens of that client; will differ for each client",
|
||||||
|
frozen = True,
|
||||||
|
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
|
||||||
|
)
|
||||||
|
|
||||||
|
user: dict = Field(
|
||||||
|
description = "how you identify your user",
|
||||||
|
frozen = True
|
||||||
|
)
|
||||||
|
|
||||||
|
clientUserId: dict = Field(
|
||||||
|
description = "how third-party client identifies the same user",
|
||||||
frozen = True
|
frozen = True
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -148,16 +155,14 @@ class CoreMessageModel(BaseModel):
|
|||||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||||
|
|
||||||
@field_validator("ts", "readTs", mode = "before")
|
@field_validator(
|
||||||
|
"firstRequestTs",
|
||||||
|
"lastRequestTs", "firstRefreshTs", "lastRefreshTs",
|
||||||
|
mode = "before"
|
||||||
|
)
|
||||||
def parse_date_time(cls, value):
|
def parse_date_time(cls, value):
|
||||||
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
|
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
|
||||||
|
|
||||||
@field_validator("tokenId", mode = "before")
|
|
||||||
def parse_oid(cls, value):
|
|
||||||
try: value = ObjectId(value)
|
|
||||||
except: pass
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
# *****************************************************************************************************************
|
# *****************************************************************************************************************
|
||||||
# ***** ****
|
# ***** ****
|
||||||
@@ -169,18 +174,27 @@ class CoreMessageModel(BaseModel):
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from utils_v2.string import json
|
from utils_v2.string import json
|
||||||
|
|
||||||
message = CoreMessageModel(
|
auth_token = CoreAuthTokenModel(
|
||||||
ts = date_time.get_current_utc_date_time(as_string = False),
|
|
||||||
tokenId = "67519cf3a7804fcbc6f12452",
|
|
||||||
serviceType = "email",
|
serviceType = "email",
|
||||||
client = "gmail",
|
client = "gmail",
|
||||||
clientMessageId = 123,
|
authType = "oauth",
|
||||||
clientThreadId = 456,
|
firstRequestTs = date_time.get_current_utc_date_time(as_string = False),
|
||||||
payload = {
|
lastRequestTs = date_time.get_current_utc_date_time(as_string = False),
|
||||||
"from": "bhopli@gmil.com",
|
firstRefreshTs = date_time.get_current_utc_date_time(as_string = False),
|
||||||
"to": "hello@thecaoffice.com",
|
lastRefreshTs = date_time.get_current_utc_date_time(as_string = False),
|
||||||
"message": "Hello, World!"
|
token = {
|
||||||
|
"username": "testing123",
|
||||||
|
"password": "abcdefgh"
|
||||||
|
},
|
||||||
|
user = {
|
||||||
|
"userId": 0,
|
||||||
|
"entityId": 1,
|
||||||
|
"billingAccountId": 2,
|
||||||
|
"fullName": "Bhopli"
|
||||||
|
},
|
||||||
|
clientUserId = {
|
||||||
|
"email": "bhopli@gmail.com"
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
print("MESSAGE MODEL:", json.to_string(message.model_dump(), default = str))
|
print("AUTH-TOKEN MODEL:", json.to_string(auth_token.model_dump(), default = str))
|
||||||
|
|||||||
@@ -6,11 +6,11 @@
|
|||||||
|
|
||||||
DATE:
|
DATE:
|
||||||
|
|
||||||
Friday, 6th Dec., 2024.
|
Saturday, 7th Dec., 2024.
|
||||||
|
|
||||||
OBJECTIVE:
|
OBJECTIVE:
|
||||||
|
|
||||||
To provide a structure to receive auth details of various chat apps (like Telegram and WhatsApp).
|
To define how messages will be stored in the database.
|
||||||
|
|
||||||
REFERENCES:
|
REFERENCES:
|
||||||
|
|
||||||
@@ -36,13 +36,16 @@ sys.path.append(".")
|
|||||||
sys.path.append("..")
|
sys.path.append("..")
|
||||||
|
|
||||||
# For making data behaviour_models:
|
# For making data behaviour_models:
|
||||||
from pydantic import BaseModel, Field, field_validator, PastDatetime
|
from pydantic import BaseModel, Field, field_validator, PastDatetime, AwareDatetime
|
||||||
from typing import Optional, Literal, Union
|
from typing import Optional, Literal, Union
|
||||||
|
|
||||||
# My utils:
|
# My utils:
|
||||||
from utils_v2.string import regex
|
from utils_v2.string import regex
|
||||||
from utils_v2.date_time import date_time
|
from utils_v2.date_time import date_time
|
||||||
|
|
||||||
|
# To work with MongoDB:
|
||||||
|
from bson.objectid import ObjectId
|
||||||
|
|
||||||
# To work with date and time:
|
# To work with date and time:
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
@@ -54,8 +57,7 @@ import datetime
|
|||||||
# *****************************************************************************************************************
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
# RegEx Patterns:
|
# --- Nothing Yet
|
||||||
REGEX_SESSION_TOKEN = r"^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$"
|
|
||||||
|
|
||||||
|
|
||||||
# *****************************************************************************************************************
|
# *****************************************************************************************************************
|
||||||
@@ -75,11 +77,58 @@ REGEX_SESSION_TOKEN = r"^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]
|
|||||||
# *****************************************************************************************************************
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
class TelegramAuth(BaseModel):
|
class CoreMessageModel(BaseModel):
|
||||||
|
|
||||||
botToken: str = Field(
|
version: str = Field(
|
||||||
description = "the token granted by BotFather",
|
description = "a hint about the version no. of this message",
|
||||||
min_length = 1,
|
min_length = 1,
|
||||||
|
frozen = True,
|
||||||
|
default = "1.0.0"
|
||||||
|
)
|
||||||
|
|
||||||
|
ts: AwareDatetime = Field(
|
||||||
|
description = "the time (utc) at which this message was sent by the sender",
|
||||||
|
frozen = True
|
||||||
|
)
|
||||||
|
|
||||||
|
readTs: AwareDatetime = Field(
|
||||||
|
description = "the time (utc) at which this message was read and stored by your server",
|
||||||
|
frozen = True,
|
||||||
|
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
|
||||||
|
)
|
||||||
|
|
||||||
|
tokenId: ObjectId = Field(
|
||||||
|
description = "the id of the auth token that is associated with this message",
|
||||||
|
frozen = True
|
||||||
|
)
|
||||||
|
|
||||||
|
serviceType: Literal["email", "sms", "chat"] = Field(
|
||||||
|
description = "the kind of service this message was sent/received from",
|
||||||
|
frozen = True
|
||||||
|
)
|
||||||
|
|
||||||
|
client: Literal[
|
||||||
|
"gmail", "outlook", # ...................... Mail Clients
|
||||||
|
"telegram", "whatsapp", # .................. Chat Clients
|
||||||
|
"nimbusSmsIndia", "savvyBulkSmsKenya", # ... SMS Clients
|
||||||
|
] = Field(
|
||||||
|
description = "the third-part client that was used",
|
||||||
|
frozen = True
|
||||||
|
)
|
||||||
|
|
||||||
|
clientMessageId: str | int = Field(
|
||||||
|
description = "how the client identifies this message",
|
||||||
|
frozen = True
|
||||||
|
)
|
||||||
|
|
||||||
|
clientThreadId: str | int | None = Field(
|
||||||
|
description = "how the client identifies the chat/thread in which this message was sent/received",
|
||||||
|
frozen = True,
|
||||||
|
default = None
|
||||||
|
)
|
||||||
|
|
||||||
|
payload: dict = Field(
|
||||||
|
description = "the actual contents of the message; will differ for each client",
|
||||||
frozen = True
|
frozen = True
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -88,49 +137,23 @@ class TelegramAuth(BaseModel):
|
|||||||
# ┗┛┗┛┛┗┛┗┗┫
|
# ┗┛┗┛┛┗┛┗┗┫
|
||||||
# ┛
|
# ┛
|
||||||
|
|
||||||
class Config:
|
|
||||||
extra = "forbid"
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
class ChatAuthRequestHeaders(BaseModel):
|
|
||||||
|
|
||||||
sessionToken: str = Field(
|
|
||||||
description = "the session token of the user who is requesting the service",
|
|
||||||
pattern = REGEX_SESSION_TOKEN,
|
|
||||||
frozen = True,
|
|
||||||
alias = "X-Session-Token"
|
|
||||||
)
|
|
||||||
|
|
||||||
# ┏┓ ┏•
|
|
||||||
# ┃ ┏┓┏┓╋┓┏┓
|
|
||||||
# ┗┛┗┛┛┗┛┗┗┫
|
|
||||||
# ┛
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
extra = "allow"
|
extra = "allow"
|
||||||
|
arbitrary_types_allowed = True
|
||||||
|
|
||||||
def model_dump(self, *args, **kwargs):
|
# ┓┏ ┓• ┓ •
|
||||||
return super().model_dump(*args, by_alias = True, **kwargs)
|
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||||
|
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||||
|
|
||||||
|
@field_validator("ts", "readTs", mode = "before")
|
||||||
|
def parse_date_time(cls, value):
|
||||||
|
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------------------------------------------------
|
@field_validator("tokenId", mode = "before")
|
||||||
|
def parse_oid(cls, value):
|
||||||
|
try: value = ObjectId(value)
|
||||||
class ChatAuthRequestData(BaseModel):
|
except: pass
|
||||||
|
return value
|
||||||
chatClient: Literal["telegram", "whatsapp"] = Field(alias = "client")
|
|
||||||
auth: Union[TelegramAuth]
|
|
||||||
|
|
||||||
# ┏┓ ┏•
|
|
||||||
# ┃ ┏┓┏┓╋┓┏┓
|
|
||||||
# ┗┛┗┛┛┗┛┗┗┫
|
|
||||||
# ┛
|
|
||||||
|
|
||||||
class Config:
|
|
||||||
extra = "forbid"
|
|
||||||
|
|
||||||
|
|
||||||
# *****************************************************************************************************************
|
# *****************************************************************************************************************
|
||||||
@@ -141,5 +164,20 @@ class ChatAuthRequestData(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
from utils_v2.string import json
|
||||||
|
|
||||||
pass
|
message = CoreMessageModel(
|
||||||
|
ts = date_time.get_current_utc_date_time(as_string = False),
|
||||||
|
tokenId = "67519cf3a7804fcbc6f12452",
|
||||||
|
serviceType = "email",
|
||||||
|
client = "gmail",
|
||||||
|
clientMessageId = 123,
|
||||||
|
clientThreadId = 456,
|
||||||
|
payload = {
|
||||||
|
"from": "bhopli@gmil.com",
|
||||||
|
"to": "hello@thecaoffice.com",
|
||||||
|
"message": "Hello, World!"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
print("MESSAGE MODEL:", json.to_string(message.model_dump(), default = str))
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
OBJECTIVE:
|
OBJECTIVE:
|
||||||
|
|
||||||
To define how messages will be stored in the database.
|
To define how payment transactions will be stored in the database.
|
||||||
|
|
||||||
REFERENCES:
|
REFERENCES:
|
||||||
|
|
||||||
@@ -49,6 +49,9 @@ from bson.objectid import ObjectId
|
|||||||
# To work with date and time:
|
# To work with date and time:
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
# To work with currencies:
|
||||||
|
import pycountry
|
||||||
|
|
||||||
|
|
||||||
# *****************************************************************************************************************
|
# *****************************************************************************************************************
|
||||||
# ***** ****
|
# ***** ****
|
||||||
@@ -77,7 +80,7 @@ import datetime
|
|||||||
# *****************************************************************************************************************
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
class CoreMessageModel(BaseModel):
|
class CorePaymentModel(BaseModel):
|
||||||
|
|
||||||
version: str = Field(
|
version: str = Field(
|
||||||
description = "a hint about the version no. of this message",
|
description = "a hint about the version no. of this message",
|
||||||
@@ -86,48 +89,79 @@ class CoreMessageModel(BaseModel):
|
|||||||
default = "1.0.0"
|
default = "1.0.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
ts: AwareDatetime = Field(
|
paymentStatus: Literal["requested", "paid", "rejected"] = Field(
|
||||||
description = "the time (utc) at which this message was sent by the sender",
|
description = "the status of the payment request to see what stage of the process we are in",
|
||||||
frozen = True
|
frozen = False,
|
||||||
|
default = "requested"
|
||||||
)
|
)
|
||||||
|
|
||||||
readTs: AwareDatetime = Field(
|
requestTs: AwareDatetime = Field(
|
||||||
description = "the time (utc) at which this message was read and stored by your server",
|
description = "the time (utc) at which the payment request was initiated",
|
||||||
frozen = True
|
|
||||||
)
|
|
||||||
|
|
||||||
tokenId: ObjectId = Field(
|
|
||||||
description = "the id of the auth token that is associated with this message",
|
|
||||||
frozen = True
|
|
||||||
)
|
|
||||||
|
|
||||||
serviceType: Literal["email", "sms", "chat"] = Field(
|
|
||||||
description = "the kind of service this message was sent/received from",
|
|
||||||
frozen = True
|
|
||||||
)
|
|
||||||
|
|
||||||
client: str = Field(
|
|
||||||
description = "the third-part client that was used",
|
|
||||||
frozen = True,
|
frozen = True,
|
||||||
examples = ["gmail", "outlook", "telegram", "whatsapp"]
|
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
|
||||||
)
|
)
|
||||||
|
|
||||||
clientMessageId: str | int = Field(
|
responseTs: AwareDatetime | None = Field(
|
||||||
description = "how the client identifies this message",
|
description = "the time (utc) at which the payer responded to the payment request",
|
||||||
frozen = True
|
frozen = False,
|
||||||
)
|
|
||||||
|
|
||||||
clientThreadId: str | int | None = Field(
|
|
||||||
description = "how the client identifies the chat/thread in which this message was sent/received",
|
|
||||||
frozen = True,
|
|
||||||
default = None
|
default = None
|
||||||
)
|
)
|
||||||
|
|
||||||
payload: dict = Field(
|
tokenId: ObjectId = Field(
|
||||||
description = "the actual contents of the message",
|
description = "the id of the auth token that is associated with this payment",
|
||||||
frozen = True
|
frozen = True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
amount: float | int = Field(
|
||||||
|
description = "the amount of money being requested"
|
||||||
|
)
|
||||||
|
|
||||||
|
currencyCode: str = Field(
|
||||||
|
description = "the three-letter ISO 4217 code to identify the currency",
|
||||||
|
frozen = True,
|
||||||
|
examples = ["INR", "USD", "KES"]
|
||||||
|
)
|
||||||
|
|
||||||
|
metadata: dict = Field(
|
||||||
|
description = "any arbitrary amount of data to identify the user and payment details",
|
||||||
|
frozen = True
|
||||||
|
)
|
||||||
|
|
||||||
|
client: Literal["razorpay", "safaricomMPesaExpress"] = Field(
|
||||||
|
description = "the third-part client that was used",
|
||||||
|
frozen = True
|
||||||
|
)
|
||||||
|
|
||||||
|
clientPaymentReferenceId: int | str = Field(
|
||||||
|
description = "the reference id given by the third-party client",
|
||||||
|
frozen = False,
|
||||||
|
default = None
|
||||||
|
)
|
||||||
|
|
||||||
|
clientPaymentRequestHttpCode: int | str = Field(
|
||||||
|
description = "the http code the third-party client returned when you requested the payment",
|
||||||
|
frozen = False,
|
||||||
|
default = None
|
||||||
|
)
|
||||||
|
|
||||||
|
clientPaymentRequestJSON: str | int = Field(
|
||||||
|
description = "how the third-party client responded when you requested the payment",
|
||||||
|
frozen = False,
|
||||||
|
default = None
|
||||||
|
)
|
||||||
|
|
||||||
|
clientPaymentResponseHttpCode: int | str = Field(
|
||||||
|
description = "the http code the third-party client returned when your user responded to the payment request",
|
||||||
|
frozen = False,
|
||||||
|
default = None
|
||||||
|
)
|
||||||
|
|
||||||
|
clientPaymentResponseJSON: str | int | None = Field(
|
||||||
|
description = "how the third-party client responded when your user responded to the payment request",
|
||||||
|
frozen = False,
|
||||||
|
default = None
|
||||||
|
)
|
||||||
|
|
||||||
# ┏┓ ┏•
|
# ┏┓ ┏•
|
||||||
# ┃ ┏┓┏┓╋┓┏┓
|
# ┃ ┏┓┏┓╋┓┏┓
|
||||||
# ┗┛┗┛┛┗┛┗┗┫
|
# ┗┛┗┛┛┗┛┗┗┫
|
||||||
@@ -135,12 +169,13 @@ class CoreMessageModel(BaseModel):
|
|||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
extra = "allow"
|
extra = "allow"
|
||||||
|
arbitrary_types_allowed = True
|
||||||
|
|
||||||
# ┓┏ ┓• ┓ •
|
# ┓┏ ┓• ┓ •
|
||||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||||
|
|
||||||
@field_validator("ts", "readTs", mode = "before")
|
@field_validator("requestTs", "responseTs", mode = "before")
|
||||||
def parse_date_time(cls, value):
|
def parse_date_time(cls, value):
|
||||||
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
|
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
|
||||||
|
|
||||||
@@ -150,6 +185,12 @@ class CoreMessageModel(BaseModel):
|
|||||||
except: pass
|
except: pass
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
@field_validator("currencyCode", mode = "before")
|
||||||
|
def validate_currency(cls, value):
|
||||||
|
currency = pycountry.currencies.get(alpha_3 = value)
|
||||||
|
if currency is None: raise ValueError("invalid currency code, please use iso 4217 standard")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
# *****************************************************************************************************************
|
# *****************************************************************************************************************
|
||||||
# ***** ****
|
# ***** ****
|
||||||
@@ -160,4 +201,18 @@ class CoreMessageModel(BaseModel):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
pass
|
from utils_v2.string import json
|
||||||
|
|
||||||
|
payment = CorePaymentModel(
|
||||||
|
paymentStatus = "requested",
|
||||||
|
tokenId = "67519cf3a7804fcbc6f12452",
|
||||||
|
amount = 1.00,
|
||||||
|
currencyCode = "INR",
|
||||||
|
metadata = {
|
||||||
|
"userId": 1,
|
||||||
|
"name": "My Test"
|
||||||
|
},
|
||||||
|
client = "safaricomMPesaExpress"
|
||||||
|
)
|
||||||
|
|
||||||
|
print("PAYMENT TXN. MODEL:", json.to_string(payment.model_dump(), default = str))
|
||||||
|
|||||||
Reference in New Issue
Block a user