Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions chalicelib/services/ApplicantService.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from chalicelib.db import db
from chalicelib.services.EventsRushService import events_rush_service
from chalice import BadRequestError
import json


class ApplicantService:
Expand All @@ -14,8 +17,40 @@ def get_all(self):
return data

def get_all_from_listing(self, id: str):
listing = db.get_item(table_name="zap-listings", key={"listingId": id})
data = db.get_applicants(table_name="zap-applications", listing_id=id)

# automated rush event logic (NOTE: does not override existing rush event logic)
rush_category_id = listing.get("rushCategoryId", None)
if rush_category_id:
analytics = self._get_rush_analytics(rush_category_id)
attendees = analytics.get("attendees", {})
events = analytics.get("events", [])

for applicant in data:
applicant["events"] = self._get_applicant_events(
email=applicant["email"], attendees=attendees, events=events
)

return data

def _get_rush_analytics(self, rush_category_id: str) -> dict:
"""Helper method for `get_all_from_listing`"""
analytics = events_rush_service.get_rush_category_analytics(rush_category_id)
try:
return json.loads(analytics)
except json.JSONDecodeError as e:
raise BadRequestError(f"Error decoding JSON: {e}")

def _get_applicant_events(self, email: str, attendees: dict, events: list) -> dict:
"""Helper method for `get_all_from_listing`"""
return {
event["eventName"]: any(
event_attended["eventId"] == event["eventId"]
for event_attended in attendees.get(email, {}).get("eventsAttended", [])
)
for event in events
}


applicant_service = ApplicantService()
4 changes: 2 additions & 2 deletions chalicelib/services/EventsRushService.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def get_rush_categories_and_events(self):
def create_rush_category(self, data: dict):
data["dateCreated"] = datetime.datetime.now()
data["events"] = []
self.mongo_module.insert_document(f"{self.collection_prefix}rush", data)
return
id = self.mongo_module.insert_document(f"{self.collection_prefix}rush", data)
return id

def create_rush_event(self, data: dict):
event_id = ObjectId()
Expand Down
12 changes: 11 additions & 1 deletion chalicelib/services/ListingService.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from chalicelib.s3 import s3
from chalicelib.utils import get_file_extension_from_base64
from chalicelib.modules.ses import ses, SesDestination
from chalicelib.services.EventsRushService import events_rush_service

import json
import uuid
Expand All @@ -17,11 +18,19 @@ class ListingService:
def __init__(self):
pass

def create(self, data):
# TODO: prevent duplicate names... (also for rush-category)
def create(self, data: dict):
listing_id = str(uuid.uuid4())
data["listingId"] = listing_id
data["isVisible"] = True

# TODO: check for dup name BEFORE going to rush-category creation
# if includeEventsAttended, create corresponding rush category (and create foreign-key)
if data.get("includeEventsAttended", None):
events_rush_data = {"name": data["title"], "defaultRushCategory": False}
rush_category_id = events_rush_service.create_rush_category(data=events_rush_data)
data["rushCategoryId"] = str(rush_category_id)

db.put_data(table_name="zap-listings", data=data)

return {"msg": True}
Expand Down Expand Up @@ -109,6 +118,7 @@ def get_all(self):
data = db.get_all(table_name="zap-listings")
return data

# TODO: also delete corresponding rush-category
def delete(self, id: str):
try:
# Perform delete operation in the database
Expand Down
1 change: 1 addition & 0 deletions tests/services/test_applicant_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def test_get_all_applicants_from_listing(service):
{"applicantId": "sample_id2", "name": "Bob"},
]
mock_db.get_applicants.return_value = sample_data
mock_db.get_item.return_value = {}

result = applicant_service.get_all_from_listing(listing_id)
mock_db.get_applicants.assert_called_once_with(
Expand Down