-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from DostEducation/feature/prod-bug-fixes
#70 Changes to monitor user group switching
- Loading branch information
Showing
9 changed files
with
118 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from api.mixins import TimestampMixin | ||
from api import db, helpers | ||
from flask_sqlalchemy import BaseQuery | ||
from sqlalchemy import desc, and_ | ||
|
||
|
||
class UserGroupQuery(BaseQuery): | ||
def get_by_uuid(self, uuid): | ||
return self.filter(UserGroup.group_uuid == uuid).first() | ||
|
||
def get_unique(self, uuid, phone): | ||
return ( | ||
self.filter( | ||
and_( | ||
UserGroup.group_uuid == uuid, | ||
UserGroup.user_phone == phone, | ||
) | ||
) | ||
.order_by(desc(UserGroup.created_on)) | ||
.first() | ||
) | ||
|
||
|
||
class UserGroup(TimestampMixin, db.Model): | ||
query_class = UserGroupQuery | ||
__tablename__ = "user_group" | ||
id = db.Column(db.Integer, primary_key=True) | ||
user_phone = db.Column(db.String(50), nullable=False) | ||
user_id = db.Column(db.Integer, db.ForeignKey("user.id")) | ||
registration_id = db.Column(db.Integer, db.ForeignKey("registration.id")) | ||
group_name = db.Column(db.String(255), nullable=False) | ||
group_uuid = db.Column(db.String(255)) | ||
status = db.Column(db.String(100)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from api import models, db, helpers | ||
|
||
|
||
class UserGroupService(object): | ||
def __init__(self): | ||
self.user_phone = None | ||
self.user_data = None | ||
self.registration_data = None | ||
|
||
def handle(self, jsonData): | ||
user_phone = helpers.fetch_by_key("urn", jsonData["contact"]) | ||
self.user_phone = helpers.sanitize_phone_string(user_phone) | ||
contact_groups = jsonData["contact"]["groups"] | ||
self.user_data = helpers.get_user_by_phone(self.user_phone) | ||
self.registration_data = helpers.get_registrant_by_phone(self.user_phone) | ||
|
||
for group in contact_groups: | ||
group_data = models.UserGroup.query.get_unique( | ||
group["uuid"], self.user_phone | ||
) | ||
if group_data: | ||
self.udpate(group_data) | ||
else: | ||
self.add(group) | ||
|
||
def add(self, group): | ||
user_group_data = models.UserGroup( | ||
user_id=self.user_data.id if self.user_data else None, | ||
registration_id=self.registration_data.id | ||
if self.registration_data | ||
else None, | ||
user_phone=self.user_phone, | ||
group_name=group["name"], | ||
group_uuid=group["uuid"], | ||
status="active", | ||
) | ||
helpers.save(user_group_data) | ||
|
||
def udpate(self, data): | ||
data.status = "active" | ||
db.session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters