Skip to content

Commit

Permalink
Merge branch 'main' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
wderocco8 committed Aug 16, 2024
2 parents 4b4203f + 7ca1190 commit 9777281
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
19 changes: 11 additions & 8 deletions chalicelib/modules/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class MongoModule:

def __init__(self, use_mock=False):
"""Establishes connection to MongoDB server"""
self.use_mock = use_mock
if use_mock:
self.mongo_client = mongomock.MongoClient()
return

self.is_prod = os.environ.get("ENV") == "prod"
self.ssm_client = boto3.client("ssm")
Expand All @@ -21,21 +25,20 @@ def __init__(self, use_mock=False):
)["Parameter"]["Value"]
self.uri = f"mongodb+srv://{self.user}:{self.password}@cluster0.9gtht.mongodb.net/?retryWrites=true&w=majority"

# if use_mock is true -> use monogmock to execute tests with fake db
self.mongo_client = mongomock.MongoClient() if use_mock else MongoClient(self.uri)
self.mongo_client = MongoClient(self.uri)

def add_env_suffix(func):
def wrapper(self, collection: str, *args, **kwargs):
def wrapper(self, collection_name: str, *args, **kwargs):
# users collection is dependent on vault so suffix should not be appended
if collection == "users":
return func(self, collection, *args, **kwargs)
if collection_name == "users":
return func(self, collection_name, *args, **kwargs)

if self.is_prod:
collection += "-prod"
collection_name += "-prod"
else:
collection += "-dev"
collection_name += "-dev"

return func(self, collection, *args, **kwargs)
return func(self, collection_name, *args, **kwargs)

return wrapper

Expand Down
38 changes: 18 additions & 20 deletions chalicelib/services/EventsMemberService.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@ def default(self, o):
return str(o)
return super().default(o)

def __init__(self, mongo_module=mongo_module):
self.mongo_module = mongo_module
def __init__(self):
self.collection_prefix = "events-"

def create_timeframe(self, timeframe_data: dict):
timeframe_data["dateCreated"] = datetime.datetime.now()
self.mongo_module.insert_document(
collection=f"{self.collection_prefix}timeframe", data=timeframe_data
mongo_module.insert_document(
f"{self.collection_prefix}timeframe", timeframe_data
)
return {"msg": True}

def get_timeframe(self, timeframe_id: str):
timeframe = self.mongo_module.get_document_by_id(
collection=f"{self.collection_prefix}timeframe", document_id=timeframe_id
timeframe = mongo_module.get_document_by_id(
f"{self.collection_prefix}timeframe", timeframe_id
)

return json.dumps(timeframe, cls=self.BSONEncoder)
Expand All @@ -46,7 +44,7 @@ def get_all_timeframes(self):

def delete_timeframe(self, timeframe_id: str):
# Check if timeframe exists and if it doesn't return errors
timeframe = self.mongo_module.get_document_by_id(
timeframe = mongo_module.get_document_by_id(
f"{self.collection_prefix}timeframe", timeframe_id
)

Expand All @@ -58,12 +56,12 @@ def delete_timeframe(self, timeframe_id: str):

# Delete all the events in the timeframe
for event_id in event_ids:
self.mongo_module.delete_document_by_id(
mongo_module.delete_document_by_id(
f"{self.collection_prefix}event", event_id
)

# If timeframe exists, delete the timeframe document
self.mongo_module.delete_document_by_id(
mongo_module.delete_document_by_id(
f"{self.collection_prefix}timeframe", timeframe_id
)

Expand All @@ -75,7 +73,7 @@ def create_event(self, timeframe_id: str, event_data: dict):
event_data["usersAttended"] = []

# Get Google Spreadsheet ID from timeframe
timeframe_doc = self.mongo_module.get_document_by_id(
timeframe_doc = mongo_module.get_document_by_id(
f"{self.collection_prefix}timeframe", timeframe_id
)
spreadsheet_id = timeframe_doc["spreadsheetId"]
Expand All @@ -89,14 +87,14 @@ def create_event(self, timeframe_id: str, event_data: dict):
event_data["spreadsheetCol"] = col

# Insert the event in event collection
event_id = self.mongo_module.insert_document(
event_id = mongo_module.insert_document(
f"{self.collection_prefix}event", event_data
)

event_data["eventId"] = str(event_id)

# Insert child event in timeframe collection
self.mongo_module.update_document(
mongo_module.update_document(
f"{self.collection_prefix}timeframe",
timeframe_id,
{"$push": {"events": event_data}},
Expand All @@ -105,7 +103,7 @@ def create_event(self, timeframe_id: str, event_data: dict):
return json.dumps(event_data, cls=self.BSONEncoder)

def get_event(self, event_id: str):
event = self.mongo_module.get_document_by_id(
event = mongo_module.get_document_by_id(
f"{self.collection_prefix}event", event_id
)

Expand All @@ -128,7 +126,7 @@ def checkin(self, event_id: str, user: dict) -> dict:

user_name = member["name"]

event = self.mongo_module.get_document_by_id(
event = mongo_module.get_document_by_id(
f"{self.collection_prefix}event", event_id
)

Expand All @@ -142,7 +140,7 @@ def checkin(self, event_id: str, user: dict) -> dict:
}

# Get timeframe document to get Google Sheets info
timeframe = self.mongo_module.get_document_by_id(
timeframe = mongo_module.get_document_by_id(
f"{self.collection_prefix}timeframe", event["timeframeId"]
)

Expand Down Expand Up @@ -176,7 +174,7 @@ def checkin(self, event_id: str, user: dict) -> dict:
)

# Update event collection with checkin data
self.mongo_module.update_document(
mongo_module.update_document(
f"{self.collection_prefix}event",
event_id,
{"$push": {"usersAttended": checkin_data}},
Expand Down Expand Up @@ -210,7 +208,7 @@ def checkin(self, event_id: str, user: dict) -> dict:

def delete(self, event_id: str):
# Check if event exists and if it doesn't return errors
event = self.mongo_module.get_document_by_id(
event = mongo_module.get_document_by_id(
f"{self.collection_prefix}event", event_id
)

Expand All @@ -221,7 +219,7 @@ def delete(self, event_id: str):
timeframe_id = event["timeframeId"]

# Remove event from timeframe
self.mongo_module.update_document(
mongo_module.update_document(
f"{self.collection_prefix}timeframe",
timeframe_id,
{"$pull": {"events": {"_id": ObjectId(event_id)}}},
Expand All @@ -233,7 +231,7 @@ def delete(self, event_id: str):
)

def get_timeframe_sheets(self, timeframe_id: str):
timeframe = self.mongo_module.get_document_by_id(
timeframe = mongo_module.get_document_by_id(
f"{self.collection_prefix}timeframe", timeframe_id
)

Expand Down

0 comments on commit 9777281

Please sign in to comment.