(20241207) Started making data models for how things will be stored in the database (mongo).

This commit is contained in:
2024-12-07 17:53:28 +05:30
parent 9d5fb6805b
commit 4583a5e63c
27 changed files with 251 additions and 144 deletions
+63 -49
View File
@@ -10,7 +10,7 @@
OBJECTIVE:
To define how messages will be stored in the database.
To define how auth tokens will be stored in the database.
REFERENCES:
@@ -77,7 +77,7 @@ import datetime
# *****************************************************************************************************************
class CoreMessageModel(BaseModel):
class CoreAuthTokenModel(BaseModel):
version: str = Field(
description = "a hint about the version no. of this message",
@@ -86,52 +86,59 @@ class CoreMessageModel(BaseModel):
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",
"telegram",
"whatsapp",
"nimbusSmsIndia",
"savvyBulkSmsKenya"
"gmail", "outlook", # ...................... Mail Clients
"telegram", "whatsapp", # .................. Chat Clients
"nimbusSmsIndia", "savvyBulkSmsKenya", # ... SMS Clients
"razorpay", "safaricomMPesaExpress" # ...... Payment Gateways
] = Field(
description = "the third-part client that was used",
frozen = True
)
clientMessageId: str | int = Field(
description = "how the client identifies this message",
authType: Literal["oauth", "auth"] = Field(
description = "the type of authentication procedure used",
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
firstRequestTs: AwareDatetime = Field(
description = "the time (utc) at which authorization was first requested",
frozen = True
)
payload: dict = Field(
description = "the actual contents of the message",
lastRequestTs: AwareDatetime = Field(
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
)
@@ -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):
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__":
from utils_v2.string import json
message = CoreMessageModel(
ts = date_time.get_current_utc_date_time(as_string = False),
tokenId = "67519cf3a7804fcbc6f12452",
auth_token = CoreAuthTokenModel(
serviceType = "email",
client = "gmail",
clientMessageId = 123,
clientThreadId = 456,
payload = {
"from": "bhopli@gmil.com",
"to": "hello@thecaoffice.com",
"message": "Hello, World!"
authType = "oauth",
firstRequestTs = date_time.get_current_utc_date_time(as_string = False),
lastRequestTs = date_time.get_current_utc_date_time(as_string = False),
firstRefreshTs = date_time.get_current_utc_date_time(as_string = False),
lastRefreshTs = date_time.get_current_utc_date_time(as_string = False),
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))
+85 -47
View File
@@ -6,11 +6,11 @@
DATE:
Friday, 6th Dec., 2024.
Saturday, 7th Dec., 2024.
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:
@@ -36,13 +36,16 @@ sys.path.append(".")
sys.path.append("..")
# 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
# My utils:
from utils_v2.string import regex
from utils_v2.date_time import date_time
# To work with MongoDB:
from bson.objectid import ObjectId
# To work with date and time:
import datetime
@@ -54,8 +57,7 @@ import datetime
# *****************************************************************************************************************
# RegEx Patterns:
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}$"
# --- Nothing Yet
# *****************************************************************************************************************
@@ -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(
description = "the token granted by BotFather",
version: str = Field(
description = "a hint about the version no. of this message",
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
)
@@ -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:
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)
# ---------------------------------------------------------------------------------------------------------------------
class ChatAuthRequestData(BaseModel):
chatClient: Literal["telegram", "whatsapp"] = Field(alias = "client")
auth: Union[TelegramAuth]
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "forbid"
@field_validator("tokenId", mode = "before")
def parse_oid(cls, value):
try: value = ObjectId(value)
except: pass
return value
# *****************************************************************************************************************
@@ -141,5 +164,20 @@ class ChatAuthRequestData(BaseModel):
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))
+90 -35
View File
@@ -10,7 +10,7 @@
OBJECTIVE:
To define how messages will be stored in the database.
To define how payment transactions will be stored in the database.
REFERENCES:
@@ -49,6 +49,9 @@ from bson.objectid import ObjectId
# To work with date and time:
import datetime
# To work with currencies:
import pycountry
# *****************************************************************************************************************
# ***** ****
@@ -77,7 +80,7 @@ import datetime
# *****************************************************************************************************************
class CoreMessageModel(BaseModel):
class CorePaymentModel(BaseModel):
version: str = Field(
description = "a hint about the version no. of this message",
@@ -86,48 +89,79 @@ class CoreMessageModel(BaseModel):
default = "1.0.0"
)
ts: AwareDatetime = Field(
description = "the time (utc) at which this message was sent by the sender",
frozen = True
paymentStatus: Literal["requested", "paid", "rejected"] = Field(
description = "the status of the payment request to see what stage of the process we are in",
frozen = False,
default = "requested"
)
readTs: AwareDatetime = Field(
description = "the time (utc) at which this message was read and stored by your server",
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",
requestTs: AwareDatetime = Field(
description = "the time (utc) at which the payment request was initiated",
frozen = True,
examples = ["gmail", "outlook", "telegram", "whatsapp"]
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
)
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,
responseTs: AwareDatetime | None = Field(
description = "the time (utc) at which the payer responded to the payment request",
frozen = False,
default = None
)
payload: dict = Field(
description = "the actual contents of the message",
tokenId: ObjectId = Field(
description = "the id of the auth token that is associated with this payment",
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:
extra = "allow"
arbitrary_types_allowed = True
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator("ts", "readTs", mode = "before")
@field_validator("requestTs", "responseTs", mode = "before")
def parse_date_time(cls, value):
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
@@ -150,6 +185,12 @@ class CoreMessageModel(BaseModel):
except: pass
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__":
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))