Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Giveaway] Added functionality to support giveaways #95

Merged
merged 1 commit into from
Oct 7, 2024
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
67 changes: 66 additions & 1 deletion api/messages/messages_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,8 @@ def get_user_by_id_old(id):
db = get_db() # this connects to our Firestore database
collection = db.collection('users')
doc = collection.document(id)
if not doc.get().exists:
return {}
doc_get = doc.get()
res = doc_get.to_dict()
# Only keep these fields since this is a public api
Expand Down Expand Up @@ -2174,4 +2176,67 @@ def get_user_feedback(propel_user_id):
feedback.pop("feedback_giver_id", None)
feedback_list.append(feedback)

return {"feedback": feedback_list}
return {"feedback": feedback_list}


def save_giveaway(propel_user_id, json):
db = get_db()
logger.info("Submitting Giveaway")
send_slack_audit(action="submit_giveaway", message="Submitting", payload=json)

slack_user = get_slack_user_from_propel_user_id(propel_user_id)
user_db_id = get_user_from_slack_id(slack_user["sub"]).id
giveaway_id = json.get("giveaway_id")
giveaway_data = json.get("giveaway_data", {})
entries = json.get("entries", 0)

collection = db.collection('giveaways')

doc_id = uuid.uuid1().hex
insert_res = collection.document(doc_id).set({
"user_id": user_db_id,
"giveaway_id": giveaway_id,
"entries": entries,
"giveaway_data": giveaway_data,
"timestamp": datetime.now().isoformat()
})

logger.info(f"Insert Result: {insert_res}")

return Message("Giveaway submitted successfully")

def get_user_giveaway(propel_user_id):
logger.info(f"Getting giveaway for propel_user_id: {propel_user_id}")
db = get_db()

slack_user = get_slack_user_from_propel_user_id(propel_user_id)
db_user_id = get_user_from_slack_id(slack_user["sub"]).id

giveaway_docs = db.collection('giveaways').where("user_id", "==", db_user_id).stream()

giveaway_list = []
for doc in giveaway_docs:
giveaway = doc.to_dict()
giveaway_list.append(giveaway)

return { "giveaways" : giveaway_list}

def get_all_giveaways():
logger.info("Getting all giveaways")
db = get_db()
# Order by timestamp in descending order
docs = db.collection('giveaways').order_by("timestamp", direction=firestore.Query.DESCENDING).stream()

# Get the most recent giveaway for each user
giveaways = {}
for doc in docs:
giveaway = doc.to_dict()
user_id = giveaway["user_id"]
if user_id not in giveaways:
user = get_user_by_id_old(user_id)
giveaway["user"] = user
giveaways[user_id] = giveaway



return { "giveaways" : list(giveaways.values()) }
11 changes: 10 additions & 1 deletion api/messages/messages_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
save_feedback,
get_user_feedback,
get_volunteer_by_event,
get_github_repos
get_github_repos,
get_user_giveaway,
save_giveaway,
get_all_giveaways,
)

logger = logging.getLogger("myapp")
Expand Down Expand Up @@ -444,4 +447,10 @@ def get_giveaway():
return get_user_giveaway(auth_user.user_id)
else:
return {"error": "Unauthorized"}, 401

@bp.route("/giveaway/admin", methods=["GET"])
@auth.require_user
@auth.require_org_member_with_permission("volunteer.admin", req_to_org_id=getOrgId)
def admin_get_all_giveaways():
return get_all_giveaways()

Loading