Skip to content

Commit

Permalink
Merge pull request #130 from DostEducation/feature/126/default-progra…
Browse files Browse the repository at this point in the history
…m-for-user

#126 Default program for user who didn't optin for any
  • Loading branch information
Satendra-SR authored Jun 23, 2021
2 parents 2c99eb0 + 4a8c03c commit 918070d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 33 deletions.
26 changes: 1 addition & 25 deletions api/helpers/db_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from api import models, db, helpers
from api import models, db

# TODO: Need to refactor this and try using ORM
def get_partner_id_by_system_phone(system_phone):
Expand All @@ -12,30 +12,6 @@ def get_partner_id_by_system_phone(system_phone):
return None


def get_program_prompt_id(jsonData):
"""
Expected program_details categories format (for ex): INTRO_1-SIGNUP_1-SELECTION_PROGRAM-OPTIN_2
Value returned by this function: 2
"""
if "program_details" in jsonData:
program_categories = helpers.fetch_by_key(
"categories", jsonData["program_details"]
)
if len(program_categories) > 0:
split_prompt_by_hyphen = helpers.split_prompt_by_hyphen(
program_categories[0]
)
split_prompt_by_underscore = helpers.split_prompt_by_underscore(
split_prompt_by_hyphen[-1]
)
return (
split_prompt_by_underscore[1]
if len(split_prompt_by_underscore) > 1
else None
)
return None


def save(data):
db.session.add(data)
db.session.commit()
Expand Down
27 changes: 27 additions & 0 deletions api/helpers/prompt_helper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from api import helpers


def split_prompt_by_hyphen(data):
split_prompt = data.split("-")
return split_prompt
Expand All @@ -6,3 +9,27 @@ def split_prompt_by_hyphen(data):
def split_prompt_by_underscore(data):
program_sub_prompt = data.split("_")
return program_sub_prompt


def get_program_prompt_id(jsonData):
"""
Expected program_details categories format (for ex): INTRO_1-SIGNUP_1-SELECTION_PROGRAM-OPTIN_2
Value returned by this function: 2
"""
if "program_details" in jsonData:
program_categories = helpers.fetch_by_key(
"categories", jsonData["program_details"]
)
if len(program_categories) > 0:
split_prompt_by_hyphen = helpers.split_prompt_by_hyphen(
program_categories[0]
)
split_prompt_by_underscore = helpers.split_prompt_by_underscore(
split_prompt_by_hyphen[-1]
)
return (
split_prompt_by_underscore[1]
if len(split_prompt_by_underscore) > 1
else None
)
return None
6 changes: 6 additions & 0 deletions api/models/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def get_latest_by_phone(self, phone):
class Registration(TimestampMixin, db.Model):
query_class = RegistrationQuery
__tablename__ = "registration"

class RegistrationStatus(object):
PENDING = "pending"
INCOMPLETE = "incomplete"
COMPLETE = "complete"

id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
user_phone = db.Column(db.String(50), nullable=False)
Expand Down
2 changes: 1 addition & 1 deletion api/models/system_phone.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class SystemPhoneQuery(BaseQuery):
def get_by_phone(self, phone):
return self.filter(SystemPhone.phone == phone).first()
return self.filter(SystemPhone.phone.contains(phone[-10:])).first()


class SystemPhone(TimestampMixin, db.Model):
Expand Down
2 changes: 1 addition & 1 deletion api/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class UserQuery(BaseQuery):
def get_by_phone(self, phone):
return self.filter(User.phone == phone).first()
return self.filter(User.phone.contains(phone[-10:])).first()

def get_by_id(self, id):
return self.filter(User.id == id).first()
Expand Down
2 changes: 1 addition & 1 deletion api/services/content_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from api import models, db, helpers
from api import models, helpers


class ContentService(object):
Expand Down
19 changes: 14 additions & 5 deletions api/services/registration_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file is treated as service layer
from api import models, db, helpers
from api import models, db, helpers, app
from datetime import datetime


Expand All @@ -8,14 +8,18 @@ def __init__(self):
self.system_phone = None
self.user_phone = None
self.user_id = None
self.selected_program_id = None
self.selected_program_id = app.config["DEFAULT_PROGRAM_ID"]
self.has_default_program_selection = True
self.selected_time_slot = None

def set_init_data(self, jsonData):
user_phone = helpers.fetch_by_key("urn", jsonData["contact"])
self.system_phone = helpers.fetch_by_key("address", jsonData["channel"])
self.user_phone = helpers.sanitize_phone_string(user_phone)
self.selected_program_id = helpers.get_program_prompt_id(jsonData)
selected_program_id = helpers.get_program_prompt_id(jsonData)
if selected_program_id:
self.selected_program_id = selected_program_id
self.has_default_program_selection = False

def handle_registration(self, jsonData):
try:
Expand Down Expand Up @@ -51,13 +55,17 @@ def register(self, jsonData):
if self.selected_program_id:
self.user_id = self.create_user(jsonData)

registration_status = "in-progress" if self.selected_program_id else "pending"
registration_status = (
models.Registration.RegistrationStatus.INCOMPLETE
if self.has_default_program_selection
else models.Registration.RegistrationStatus.COMPLETE
)
if system_phone_details:
registrant = models.Registration(
user_phone=self.user_phone,
system_phone=self.system_phone,
state=system_phone_details.state,
status="complete" if self.user_id else registration_status,
status=registration_status,
program_id=self.selected_program_id,
partner_id=helpers.get_partner_id_by_system_phone(self.system_phone),
user_id=self.user_id,
Expand All @@ -83,6 +91,7 @@ def update_registration(self, registration, jsonData):
registration.program_id = self.selected_program_id
registration.signup_date = datetime.now()
registration.user_id = self.user_id
registration.has_received_callback = True
registration.status = "complete"
db.session.commit()

Expand Down
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@
SQLALCHEMY_TRACK_MODIFICATIONS = True
WTF_CSRF_ENABLED = True
SECRET_KEY = os.environ.get("SECRET_KEY")

DEFAULT_PROGRAM_ID = 2

0 comments on commit 918070d

Please sign in to comment.