Mbonea commited on
Commit
fd01f78
·
1 Parent(s): 665318f

prevent double entry

Browse files
Files changed (1) hide show
  1. App/Messages/MessagesRoute.py +27 -22
App/Messages/MessagesRoute.py CHANGED
@@ -18,17 +18,19 @@ message_router = APIRouter(tags=["Messages"], prefix="/messages")
18
 
19
 
20
  @message_router.post("/sms_received", response_model=MessageResponse)
21
- async def receive_message(request: Request):
 
22
 
23
- message_raw = await request.json()
24
- message_data = MessageCreate(**message_raw)
25
  try:
26
  # Extract data from the message content using regex
27
  text = message_data.payload.message
28
  parsed_data = parse_message_content(text)
29
- # Process Mpesa Payments
30
- if parsed_data:
 
31
  user: User = User.get_or_none(phoneNumber=parsed_data.phone_number)
 
 
32
  data_plan: Plan = Plan.get_or_none(
33
  amount=Decimal(parsed_data.amount_received)
34
  )
@@ -40,23 +42,26 @@ async def receive_message(request: Request):
40
  transaction_id=parsed_data.transaction_id,
41
  )
42
 
43
- payment: Payment = await create_payment(payment_details)
44
- # Create a new message record with parsed_data
45
- new_message = await Message.create(
46
- device_id=message_data.deviceId,
47
- event=message_data.event,
48
- message_id=message_data.id,
49
- webhook_id=message_data.webhookId,
50
- message_content=text,
51
- phone_number=message_data.payload.phoneNumber,
52
- received_at=message_data.payload.receivedAt,
53
- sim_number=message_data.payload.simNumber,
54
- parsed_data=parsed_data,
55
- )
56
-
57
- data = MessageResponse.from_orm(new_message)
58
-
59
- await payment.create_subscription_or_balance()
 
 
 
60
  return data
61
  except Exception as e:
62
  raise HTTPException(status_code=400, detail=str(e))
 
18
 
19
 
20
  @message_router.post("/sms_received", response_model=MessageResponse)
21
+ async def receive_message(message_data: MessageCreate):
22
+ message = Message.get_or_none(message_id=message_data.id)
23
 
 
 
24
  try:
25
  # Extract data from the message content using regex
26
  text = message_data.payload.message
27
  parsed_data = parse_message_content(text)
28
+
29
+ # Process Mpesa Payments and prevent double entry from the sms gateway app (message_id must be unique)
30
+ if parsed_data and not message:
31
  user: User = User.get_or_none(phoneNumber=parsed_data.phone_number)
32
+
33
+ # if the user sent an exact amount
34
  data_plan: Plan = Plan.get_or_none(
35
  amount=Decimal(parsed_data.amount_received)
36
  )
 
42
  transaction_id=parsed_data.transaction_id,
43
  )
44
 
45
+ payment: Payment = await create_payment(payment_details)
46
+ await payment.create_subscription_or_balance()
47
+
48
+ # prevent double entries
49
+ if not message:
50
+ # Create a new message record with parsed_data
51
+ message = await Message.create(
52
+ device_id=message_data.deviceId,
53
+ event=message_data.event,
54
+ message_id=message_data.id,
55
+ webhook_id=message_data.webhookId,
56
+ message_content=text,
57
+ phone_number=message_data.payload.phoneNumber,
58
+ received_at=message_data.payload.receivedAt,
59
+ sim_number=message_data.payload.simNumber,
60
+ parsed_data=parsed_data,
61
+ )
62
+
63
+ data = MessageResponse.from_orm(message)
64
+
65
  return data
66
  except Exception as e:
67
  raise HTTPException(status_code=400, detail=str(e))