Mbonea commited on
Commit
d5bd833
·
1 Parent(s): 42f4a62

updated the db

Browse files
App/Messages/Model.py CHANGED
@@ -19,6 +19,7 @@ class Message(Model):
19
  sim_number = fields.IntField(null=True)
20
  parsed_data = fields.JSONField(null=True) # New field for parsed data
21
  created_time = fields.DatetimeField(auto_now_add=True)
 
22
 
23
  class Meta:
24
  table = "messages"
 
19
  sim_number = fields.IntField(null=True)
20
  parsed_data = fields.JSONField(null=True) # New field for parsed data
21
  created_time = fields.DatetimeField(auto_now_add=True)
22
+
23
 
24
  class Meta:
25
  table = "messages"
App/Subscriptions/Schema.py CHANGED
@@ -14,6 +14,20 @@ class CreateSubscriptionRequest(BaseModel):
14
  plan_id: str = Field(..., description="ID of the plan to base the subscription on")
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  class UpdateUsageRequest(BaseModel):
18
  download_mb: float = Field(0.0, ge=0, description="Download usage in megabytes")
19
  upload_mb: float = Field(0.0, ge=0, description="Upload usage in megabytes")
 
14
  plan_id: str = Field(..., description="ID of the plan to base the subscription on")
15
 
16
 
17
+ class CreateCustomSubscriptionRequest(BaseModel):
18
+ user_id: Optional[str] = Field(
19
+ None, description="ID of the user to create subscription for"
20
+ )
21
+ phone_number: Optional[str] = Field(
22
+ None, description="The Phone Number of the user to create subscription for"
23
+ )
24
+ plan_id: str = Field(..., description="ID of the plan to base the subscription on")
25
+ expiration_time: str = Field(
26
+ ...,
27
+ description="The expiration time of the subscription in 'day/month' format (e.g., 12/1 for 12th January)",
28
+ )
29
+
30
+
31
  class UpdateUsageRequest(BaseModel):
32
  download_mb: float = Field(0.0, ge=0, description="Download usage in megabytes")
33
  upload_mb: float = Field(0.0, ge=0, description="Upload usage in megabytes")
App/Subscriptions/SubscriptionRoutes.py CHANGED
@@ -9,6 +9,7 @@ from .Schema import (
9
  SubscriptionResponse,
10
  BaseResponse,
11
  UpdateUsageRequest,
 
12
  SubscriptionListResponse,
13
  )
14
 
@@ -171,3 +172,75 @@ async def deactivate_subscription(subscription_id: str):
171
  return BaseResponse(
172
  code=200, message="Subscription and user deactivated successfully"
173
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  SubscriptionResponse,
10
  BaseResponse,
11
  UpdateUsageRequest,
12
+ CreateCustomSubscriptionRequest,
13
  SubscriptionListResponse,
14
  )
15
 
 
172
  return BaseResponse(
173
  code=200, message="Subscription and user deactivated successfully"
174
  )
175
+
176
+
177
+ from datetime import datetime, time
178
+
179
+
180
+ from datetime import datetime
181
+
182
+
183
+ @subscription_router.post("/subscription/custom", response_model=BaseResponse)
184
+ async def create_subscription(request: CreateCustomSubscriptionRequest):
185
+ # Find user by user_id or phone_number
186
+ user = None
187
+ if request.user_id: # Check if user_id is provided
188
+ user = await User.get_or_none(id=request.user_id)
189
+ elif request.phone_number: # Check if phone_number is provided
190
+ user = await User.get_or_none(phoneNumber=request.phone_number)
191
+
192
+ if not user:
193
+ raise HTTPException(
194
+ status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
195
+ )
196
+
197
+ # Check if plan exists
198
+ plan = await Plan.get_or_none(id=request.plan_id)
199
+ if not plan:
200
+ raise HTTPException(
201
+ status_code=status.HTTP_404_NOT_FOUND, detail="Plan not found"
202
+ )
203
+
204
+ # Parse expiration_time
205
+ try:
206
+ # Expecting expiration_time in "day/month" format (e.g., "12/1" for 12th January)
207
+ day, month = map(int, request.expiration_time.split("/"))
208
+ today = datetime.now()
209
+ expiration_date = datetime(
210
+ year=today.year, # Always use the current year
211
+ month=month,
212
+ day=day,
213
+ hour=12, # Fixed time at 12 PM
214
+ minute=0,
215
+ second=0,
216
+ )
217
+ except (ValueError, IndexError):
218
+ raise HTTPException(
219
+ status_code=status.HTTP_400_BAD_REQUEST,
220
+ detail="Invalid expiration_time format. Use 'day/month' (e.g., '12/1').",
221
+ )
222
+
223
+ # Validate expiration_date is not in the past
224
+ if expiration_date < datetime.now():
225
+ raise HTTPException(
226
+ status_code=status.HTTP_400_BAD_REQUEST,
227
+ detail="Expiration time cannot be in the past",
228
+ )
229
+
230
+ # Create new subscription based on plan details
231
+ subscription = await Subscription.create(
232
+ user=user,
233
+ plan=plan,
234
+ duration=plan.duration,
235
+ download_mb=plan.download_speed * 1024, # Converting Mbps to MB
236
+ upload_mb=plan.upload_speed * 1024, # Converting Mbps to MB
237
+ expiration_time=expiration_date,
238
+ active=True,
239
+ )
240
+ await subscription.save()
241
+
242
+ return BaseResponse(
243
+ code=200,
244
+ message="Subscription created successfully",
245
+ payload={"subscription_id": str(subscription.id)},
246
+ )
App/modelInit.py CHANGED
@@ -1,4 +1,4 @@
1
- import ssl
2
  from App.discovery import discover_models
3
 
4
  # Set up SSL context for secure database connections
@@ -13,17 +13,40 @@ print("Discovered models:", models)
13
  ## for migrations
14
  models.append("aerich.models")
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  TORTOISE_ORM = {
17
  "connections": {
18
  "default": {
19
  "engine": "tortoise.backends.asyncpg",
20
  "credentials": {
21
- "host": "ep-patient-darkness-a5bmmt9r.us-east-2.aws.neon.tech",
22
  "port": "5432",
23
- "user": "neondb_owner",
24
- "password": "l2kE5dbMyqfx",
25
- "database": "neondb",
26
- "ssl": ssl_context, # Pass the SSL context here
27
  },
28
  }
29
  },
 
1
+ import ssl, os
2
  from App.discovery import discover_models
3
 
4
  # Set up SSL context for secure database connections
 
13
  ## for migrations
14
  models.append("aerich.models")
15
 
16
+ # TORTOISE_ORM = {
17
+ # "connections": {
18
+ # "default": {
19
+ # "engine": "tortoise.backends.asyncpg",
20
+ # "credentials": {
21
+ # "host": "ep-patient-darkness-a5bmmt9r.us-east-2.aws.neon.tech",
22
+ # "port": "5432",
23
+ # "user": "neondb_owner",
24
+ # "password": "l2kE5dbMyqfx",
25
+ # "database": "neondb",
26
+ # "ssl": ssl_context, # Pass the SSL context here
27
+ # },
28
+ # }
29
+ # },
30
+ # "apps": {
31
+ # "models": {
32
+ # "models": models,
33
+ # "default_connection": "default",
34
+ # }
35
+ # },
36
+ # }
37
+
38
+
39
  TORTOISE_ORM = {
40
  "connections": {
41
  "default": {
42
  "engine": "tortoise.backends.asyncpg",
43
  "credentials": {
44
+ "host": "aws-0-us-west-1.pooler.supabase.com",
45
  "port": "5432",
46
+ "user": os.getenv("DB_USER"),
47
+ "user": os.getenv("DB_PASSWORD"),
48
+ "database": "postgres",
49
+ # "ssl": ssl_context, # Uncomment this if SSL is required
50
  },
51
  }
52
  },