Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #8 from kougen/feature/adding-cancel-instead-of-de…
Browse files Browse the repository at this point in the history
…lete

Feature/adding cancel instead of delete
  • Loading branch information
joshika39 authored Sep 17, 2024
2 parents 651fe2b + fdc9ef0 commit 04cf6e6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ handler/.env
/**/__pycache__/
/**/dblib.py
!/dblib/dblib.py

.idea/
19 changes: 13 additions & 6 deletions handler/groups_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
groupsrouter = APIRouter()

cursor, connection = get_db_cursor_and_connection()

logger = Logger("HANDLER", cursor, "GROUPS_ROUTER")

stopMessages = ["stop", "stap", "stopp", "pauza"]

def is_stop_message(message: str):
for stopMessage in stopMessages:
if stopMessage in message.lower():
return True
return False

@groupsrouter.get("/", tags=["groups"])
async def get_groups():
cursor.execute('SELECT * FROM "Group"')
Expand Down Expand Up @@ -43,7 +50,7 @@ async def get_group_details(id: int):


@groupsrouter.post("/cancel", tags=["groups"])
def delete_recipient_from_group(request: CancelRequest):
def cancel_recipient(request: CancelRequest):
keys = request.dict().keys()
if not request or not 'phone_number' in keys or not 'message' in keys:
return {"message": "ERROR", "success": False}
Expand All @@ -55,7 +62,7 @@ def delete_recipient_from_group(request: CancelRequest):
return {"message": "ERROR", "success": False}


if not ("STOP" in message.upper() or "Stap" in message.upper() or "Stopp" in message.upper()):
if not is_stop_message(message):
print(f"Received: {phone}, but no STOP message (or similar) found: {message}")
logger.info(f"Received: {phone}, but no STOP message (or similar) found: {message}")
return { "message": "NO_ACTION_REQUIRED", "success": True}
Expand All @@ -66,15 +73,15 @@ def delete_recipient_from_group(request: CancelRequest):
logger.info(f"Recipient with phone number {phone} not found")
return {"message": "NO_ACTION_REQUIRED", "success": True}
else:
cursor.execute('DELETE FROM "Recipient" WHERE "phone" = (%s)', (phone,))
cursor.execute('UPDATE "Recipient" SET "isSubscribed" = FALSE WHERE "phone" = (%s)', (phone,))
connection.commit()
logger.info(f"Recipient with phone number {phone} deleted")
logger.info(f"Recipient with phone number {phone} cancelled")
return {"message": "OK", "success": True}
except InvalidTextRepresentation as e:
print(e)
logger.error(f"Invalid phone number: {phone}")
return {"message": "ERROR", "success": False}
except Exception as e:
print(e)
logger.error(f"Error deleting recipient: {phone}")
logger.error(f"Error cancelling recipient: {phone}")
return {"message": "Error Occurred: " + str(e), "success": False}
2 changes: 1 addition & 1 deletion handler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
* **Create users** (_not implemented_).
* **Read users** (_not implemented_).
"""
print(f"Starting FastAPI on port 8000 ({SELF_URL}), with base path: {BASE_PATH}")
print(f"Starting FastAPI on ({SELF_URL}), with base path: {BASE_PATH}")
app = FastAPI(
title="SMS Automation API",
description=description,
Expand Down
5 changes: 3 additions & 2 deletions reciever/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ def handler_data(data, conn: socket.socket, cursor: Cursor, connection: Connecti
count = cursor.fetchone()
if count is not None:
count = count[0]
print(f"Deleting {count} records with phone {phone}")
cursor.execute('DELETE FROM "Recipient" WHERE "phone" = (%s)', (phone,))
print(f"Unsubscribing {count} records with phone {phone}")
# Setting the 'isSubscribed' to False for all records with the given phone number
cursor.execute('UPDATE "Recipient" SET "isSubscribed" = FALSE WHERE "phone" = (%s)', (phone,))
connection.commit()
insert_log(cursor, "INFO", f"Deleted {count} records with phone {phone}", "RECEIVER")
conn.sendall(b"OK")
Expand Down

0 comments on commit 04cf6e6

Please sign in to comment.