(20241207) Started making data models for how things will be stored in the database (mongo).
This commit is contained in:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user