(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
+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))