Skip to content

Commit

Permalink
Add more events
Browse files Browse the repository at this point in the history
  • Loading branch information
sravfeyn committed Aug 15, 2024
1 parent c59a387 commit 51b7719
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 2 deletions.
6 changes: 5 additions & 1 deletion commcare_connect/events/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ class Migration(migrations.Migration):
models.ForeignKey(
null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
)
),
(
"metadata",
models.JSONField(default=dict),
),
],
),
]
1 change: 1 addition & 0 deletions commcare_connect/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Event(models.Model):
event_type = models.CharField(max_length=40, db_index=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
opportunity = models.ForeignKey(Opportunity, on_delete=models.PROTECT, null=True)
metadata = models.JSONField()

@classmethod
@quickcache([], timeout=60 * 60)
Expand Down
10 changes: 10 additions & 0 deletions commcare_connect/events/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Server/Web events
INVITE_SENT = _("Invite Sent")
INVITE_ACCEPTED = _("Invite Accepted")
RECORDS_APPROVED = _("Records Approved")
RECORDS_REJECTED = _("Records Rejected")
PAYMENT_APPROVED = _("Payment Approved")
Expand All @@ -10,6 +11,15 @@
NOTIFICATIONS_SENT = _("Notifications Sent")
ADDITIONAL_BUDGET_ADDED = _("Additional Budget Added")

MODULE_COMPLETED = _("Module Completed")
ALL_MODULES_COMPLETED = _("All Modules Completed")
ASSESSMENT_PASSED = _("Assessment Passed")
ASSESSMENT_FAILED = _("Assessment Failed")

JOB_CLAIMED = _("Job Claimed Successfully")
DELIVERY_FORM_SUBMITTED = _("Delivery Form Submitted")
PAYMENT_ACKNOWLEDGED = _("Payment Acknowledged")


# Inferred Events
RECORDS_FLAGGED = _("Records Flagged")
Expand Down
25 changes: 24 additions & 1 deletion commcare_connect/form_receiver/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from jsonpath_ng import JSONPathError
from jsonpath_ng.ext import parse

from commcare_connect.events.models import Event
from commcare_connect.form_receiver.const import CCC_LEARN_XMLNS
from commcare_connect.form_receiver.exceptions import ProcessingError
from commcare_connect.form_receiver.serializers import XForm
Expand Down Expand Up @@ -103,6 +104,15 @@ def process_learn_modules(user: User, xform: XForm, app: CommCareApp, opportunit

if not created:
raise ProcessingError("Learn Module is already completed")
else:
Event(
event_type=Event.Type.MODULE_COMPLETED,
user=user,
opportunity=opportunity,
metadata={"module_name": module.name},
).save()
if access.learn_progress == 100:
Event(event_type=Event.Type.ALL_MODULES_COMPLETED, user=user, opportunity=opportunity).save()


def process_assessments(user, xform: XForm, app: CommCareApp, opportunity: Opportunity, blocks: list[dict]):
Expand All @@ -121,6 +131,7 @@ def process_assessments(user, xform: XForm, app: CommCareApp, opportunity: Oppor
# TODO: should this move to the opportunity to allow better re-use of the app?
passing_score = app.passing_score
access = OpportunityAccess.objects.get(user=user, opportunity=opportunity)
passed = score >= passing_score
assessment, created = Assessment.objects.get_or_create(
user=user,
app=app,
Expand All @@ -131,14 +142,21 @@ def process_assessments(user, xform: XForm, app: CommCareApp, opportunity: Oppor
"date": xform.received_on,
"score": score,
"passing_score": passing_score,
"passed": score >= passing_score,
"passed": passed,
"app_build_id": xform.build_id,
"app_build_version": xform.metadata.app_build_version,
},
)

if not created:
return ProcessingError("Learn Assessment is already completed")
else:
Event(
event_type=Event.Type.ASSESSMENT_PASSED if passed else Event.Type.ASSESSMENT_FAILED,
user=user,
opportunity=opportunity,
metadata={"score": score, "passing_score": passing_score},
).save()


def process_deliver_form(user, xform: XForm, app: CommCareApp, opportunity: Opportunity):
Expand Down Expand Up @@ -229,6 +247,11 @@ def clean_form_submission(access: OpportunityAccess, user_visit: UserVisit, xfor


def process_deliver_unit(user, xform: XForm, app: CommCareApp, opportunity: Opportunity, deliver_unit_block: dict):
Event(
event_type=Event.Type.DELIVERY_FORM_SUBMITTED,
user=user,
opportunity=opportunity,
).save()
deliver_unit = get_or_create_deliver_unit(app, deliver_unit_block)
access = OpportunityAccess.objects.get(opportunity=opportunity, user=user)
counts = (
Expand Down
14 changes: 14 additions & 0 deletions commcare_connect/opportunity/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from rest_framework.response import Response
from rest_framework.views import APIView

from commcare_connect.events.models import Event
from commcare_connect.opportunity.api.serializers import (
CompletedWorkSerializer,
DeliveryProgressSerializer,
Expand Down Expand Up @@ -101,6 +102,11 @@ def post(self, *args, **kwargs):
return Response("Failed to create user", status=400)
cc_username = f"{self.request.user.username.lower()}@{domain}.commcarehq.org"
ConnectIDUserLink.objects.create(commcare_username=cc_username, user=self.request.user, domain=domain)
Event(
event_type=Event.Type.JOB_CLAIMED,
user=opportunity_access.user,
opportunity=opportunity,
).save()
return Response(status=201)


Expand All @@ -119,4 +125,12 @@ def post(self, *args, **kwargs):
payment.confirmed = confirmed
payment.confirmation_date = now()
payment.save()

Event(
event_type=Event.Type.PAYMENT_ACKNOWLEDGED,
user=payment.opportunity_access.user,
opportunity=payment.opportunity_access.opportunity,
metadata={"confirmed": confirmed},
).save()

return Response(status=200)
8 changes: 8 additions & 0 deletions commcare_connect/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from rest_framework.views import APIView

from commcare_connect.connect_id_client.main import fetch_demo_user_tokens
from commcare_connect.events.models import Event
from commcare_connect.opportunity.models import Opportunity, OpportunityAccess, UserInvite, UserInviteStatus

from .helpers import create_hq_user
Expand Down Expand Up @@ -134,6 +135,13 @@ def accept_invite(request, invite_id):
user_invite = UserInvite.objects.get(opportunity_access=o)
user_invite.status = UserInviteStatus.accepted
user_invite.save()

Event(
event_type=Event.Type.INVITE_ACCEPTED,
user=o.user,
opportunity=o.opportunity,
).save()

return HttpResponse(
"Thank you for accepting the invitation. Open your CommCare Connect App to "
"see more information about the opportunity and begin learning"
Expand Down
6 changes: 6 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ django==4.2.5
# -r requirements/base.in
# crispy-bootstrap5
# django-allauth
# django-autocomplete-light
# django-celery-beat
# django-cors-headers
# django-crispy-forms
# django-filter
# django-model-utils
# django-oauth-toolkit
# django-redis
Expand All @@ -91,6 +93,8 @@ django==4.2.5
# drf-spectacular
django-allauth==0.54.0
# via -r requirements/base.in
django-autocomplete-light==3.11.0
# via -r requirements/base.in
django-celery-beat==2.5.0
# via -r requirements/base.in
django-cors-headers==4.2.0
Expand All @@ -101,6 +105,8 @@ django-crispy-forms==2.0
# crispy-bootstrap5
django-environ==0.10.0
# via -r requirements/base.in
django-filter==24.3
# via -r requirements/base.in
django-model-utils==4.3.1
# via -r requirements/base.in
django-oauth-toolkit==2.3.0
Expand Down

0 comments on commit 51b7719

Please sign in to comment.