Skip to content

Commit

Permalink
Merge pull request #205 from DostEducation/release/sprint-14-release-…
Browse files Browse the repository at this point in the history
…2021

[Release] Sprint 14 release 2021
  • Loading branch information
GauravGusain98 authored Nov 12, 2021
2 parents 2e517d6 + e7746d4 commit cd8ec85
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
24 changes: 24 additions & 0 deletions api/models/user_custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,38 @@ class UserCustomFieldsQuery(BaseQuery):
def get_by_user_phone(self, phone):
return self.filter(UserCustomFields.user_phone == phone).all()

def set_custom_fields_as_inactive_by_user_phone(self, phone):
self.filter(and_(UserCustomFields.user_phone == phone)).update(
{UserCustomFields.status: UserCustomFields.UserCustomFieldStatus.INACTIVE}
)

def set_custom_field_as_active(self, field_name, field_value, phone):
self.filter(
and_(
UserCustomFields.user_phone == phone,
UserCustomFields.field_name == field_name,
UserCustomFields.field_value == field_value,
)
).update(
{UserCustomFields.status: UserCustomFields.UserCustomFieldStatus.ACTIVE}
)


class UserCustomFields(TimestampMixin, db.Model):
query_class = UserCustomFieldsQuery
__tablename__ = "user_custom_fields"

class UserCustomFieldStatus(object):
ACTIVE = "active"
INACTIVE = "inactive"

id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
registration_id = db.Column(db.Integer, db.ForeignKey("registration.id"))
user_phone = db.Column(db.String(50), nullable=False)
flow_run_uuid = db.Column(db.String(255))
field_name = db.Column(db.String(255), nullable=False)
field_value = db.Column(db.String(500), nullable=False)
status = db.Column(
db.String(100), nullable=False, default=UserCustomFieldStatus.INACTIVE
)
21 changes: 19 additions & 2 deletions api/services/user_contact_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,35 @@ def handle_custom_fields(self, jsonData):
user_custom_field_data = models.UserCustomFields.query.get_by_user_phone(
self.user_phone
)

self.process_custom_fields(jsonData, user_custom_field_data)

def process_custom_fields(self, jsonData, user_custom_field_data=False):
custom_fields = jsonData["contact"]["fields"]
fields_key_values = {}

if user_custom_field_data:
fields_key_values = self.fetch_fields_key_value(user_custom_field_data)
self.mark_user_custom_fields_as_inactive()

user_custom_contact_data = []

for field_name, field_value in custom_fields.items():
custom_fields_conditions = self.custom_fields_contidions(
custom_fields_conditions = self.custom_fields_conditions(
field_name, field_value
)

if (
custom_fields_conditions
and self.check_if_exist(fields_key_values, field_name, field_value)
== False
):
userdata = self.get_user_custom_fields_object(field_name, field_value)
user_custom_contact_data.extend(userdata)
elif custom_fields_conditions:
models.UserCustomFields.query.set_custom_field_as_active(
field_name, field_value, self.user_phone
)

if user_custom_contact_data:
helpers.save_batch(user_custom_contact_data)
Expand Down Expand Up @@ -134,10 +144,11 @@ def get_user_custom_fields_object(self, field_name, field_value):
field_name=field_name,
field_value=field_value,
flow_run_uuid=self.flow_run_uuid,
status=models.UserCustomFields.UserCustomFieldStatus.ACTIVE,
),
]

def custom_fields_contidions(self, field_name, field_value):
def custom_fields_conditions(self, field_name, field_value):
return (
True
if field_value is not None
Expand All @@ -148,3 +159,9 @@ def custom_fields_contidions(self, field_name, field_value):
def mark_user_groups_as_inactive(self):
models.UserGroup.query.mark_user_groups_as_inactive(self.user_phone)
db.session.commit()

def mark_user_custom_fields_as_inactive(self):
models.UserCustomFields.query.set_custom_fields_as_inactive_by_user_phone(
self.user_phone
)
db.session.commit()

0 comments on commit cd8ec85

Please sign in to comment.