Skip to content

Commit

Permalink
Merge pull request #189 from ZIFODS/188-create-user-when-skills-added…
Browse files Browse the repository at this point in the history
…-if-user-doesnt-exist

Create user when assigning skill if user doesn't exist
  • Loading branch information
Joseph Smith authored Nov 21, 2023
2 parents df0a0d7 + a289483 commit 5000a54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ExternalUser(BaseModel):
id: str
email: str
username: str
display_name: str


class InternalUser(BaseModel):
Expand Down
31 changes: 28 additions & 3 deletions app/routers/user_skills.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ async def create_user_skill(
) -> SkillList:
"""
Creates a relationship between a skill and the current user.
If the user does not exist, they are created using their Microsoft Azure info.
Parameters
----------
Expand All @@ -131,23 +132,46 @@ async def create_user_skill(
"""
if not config.PROD_ENV:
email = "[email protected]"
display_name = "Anthony Hopkins"

else:
provider = AzureAuthProvider()
external_user = await provider.get_user(
external_access_token=external_access_token
)
email = external_user.email
display_name = external_user.display_name

skills_dict = [s.dict() for s in skills]

conn = Neo4jConnection()
exists_query = """

user_exists_query = """
MATCH (s:Consultant {email: $email})
RETURN s
"""
result = conn.query(user_exists_query, email=email)
if not result:
create_user_query = """
MERGE (c:Consultant {uid: $uid, name: $name, email: $email})
WITH {name: c.name, type: labels(c)[0], email: c.email} as consultantOut
RETURN consultantOut
"""
result = conn.query(
create_user_query, uid=str(uuid.uuid4()), name=display_name, email=email
)
if not result:
raise HTTPException(
status_code=409,
detail=f"An error occurred creating the consultant {display_name}",
)

user_skills_exist_query = """
UNWIND $skills as skills
MATCH (c:Consultant {email: $email})-[:KNOWS]->(s:Skill {name: skills.name})
RETURN s
"""
result = conn.query(exists_query, email=email, skills=skills_dict)
result = conn.query(user_skills_exist_query, email=email, skills=skills_dict)
conn.close()
if result:
skill_names_linked = ", ".join([r[0]["name"] for r in result])
Expand All @@ -158,7 +182,8 @@ async def create_user_skill(

query = """
UNWIND $skills as skill
MATCH (s:Skill {name: skill.name}), (c:Consultant {email: $email})
MATCH (s:Skill {name: skill.name})
MERGE (c:Consultant {email: $email})
MERGE (c)-[:KNOWS {uid: $uid}]->(s)
WITH {name: s.name, type: labels(s)[0], category: s.category} as skillsMerged
RETURN COLLECT(skillsMerged) as skillsOut
Expand Down
5 changes: 4 additions & 1 deletion app/utils/security/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ async def get_user(self, external_access_token: str) -> ExternalUser:

user_id = response_data.get("id")
username = response_data.get("userPrincipalName")
display_name = response_data.get("displayName")
email = response_data.get("mail")

if email:
Expand All @@ -85,7 +86,9 @@ async def get_user(self, external_access_token: str) -> ExternalUser:
if not user_id or not username or not email:
raise UnauthorizedUser("User account not verified by Azure.")

external_user = ExternalUser(id=user_id, email=email, username=username)
external_user = ExternalUser(
id=user_id, email=email, username=username, display_name=display_name
)

return external_user

Expand Down

0 comments on commit 5000a54

Please sign in to comment.