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

Add Enterprise SMS Report #35437

Merged
merged 11 commits into from
Dec 4, 2024
2 changes: 2 additions & 0 deletions corehq/apps/enterprise/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
MobileUserResource,
ODataFeedResource,
WebUserResource,
SMSResource,
)

v1_api = Api(api_name='v1')
Expand All @@ -14,3 +15,4 @@
v1_api.register(MobileUserResource())
v1_api.register(FormSubmissionResource())
v1_api.register(ODataFeedResource())
v1_api.register(SMSResource())
39 changes: 39 additions & 0 deletions corehq/apps/enterprise/api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils.translation import gettext as _

from dateutil import tz
from datetime import timezone
from tastypie import fields, http
from tastypie.exceptions import ImmediateHttpResponse

Expand Down Expand Up @@ -314,6 +315,44 @@ def get_primary_keys(self):
return ('user_id',)


class SMSResource(ODataEnterpriseReportResource):
domain = fields.CharField()
num_sent = fields.IntegerField()
num_received = fields.IntegerField()
num_error = fields.IntegerField()

REPORT_SLUG = EnterpriseReport.SMS

def get_report_task(self, request):
start_date = request.GET.get('startdate', None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

util function for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if start_date:
start_date = str(datetime.fromisoformat(start_date).astimezone(timezone.utc))

end_date = request.GET.get('enddate', None)
if end_date:
end_date = str(datetime.fromisoformat(end_date).astimezone(timezone.utc))

account = BillingAccount.get_account_by_domain(request.domain)
return generate_enterprise_report.s(
self.REPORT_SLUG,
account.id,
request.couch_user.username,
start_date=start_date,
end_date=end_date
)

def dehydrate(self, bundle):
bundle.data['domain'] = bundle.obj[0]
bundle.data['num_sent'] = bundle.obj[1]
bundle.data['num_received'] = bundle.obj[2]
bundle.data['num_error'] = bundle.obj[3]

return bundle

def get_primary_keys(self):
return ('domain',)


class ODataFeedResource(ODataEnterpriseReportResource):
'''
A Resource for listing all Domain-level OData feeds which belong to the Enterprise.
Expand Down