/// <summary> /// Process when professional invites a parent /// </summary> /// <param name="parentUser">Parent user</param> /// <returns>Nothing</returns> private async Task ProfessionalInvite(User parentUser) { if (FromEmail == null) { Logger.Log.LogError($"from email is missing"); return; } //create connection token for parent var connectionToken = await GetFrom(parentUser.Id, FromEmail); var canUpdateParent = true; if (connectionToken == null) { connectionToken = new ConnectionToken(); } // Create or update parent token with information if (canUpdateParent) { connectionToken.FromId = parentUser.Id; connectionToken.Email = FromEmail; connectionToken.FromEmail = Email; connectionToken.FirstName = FirstName; connectionToken.LastName = LastName; connectionToken.Type = "parent"; connectionToken.ChildFirstName = ChildFirstName; connectionToken.ChildLastName = ChildLastName; connectionToken.Permission = Permission; connectionToken.State = "pending"; connectionToken.FromFirstName = FromFirstName; connectionToken.FromLastName = FromLastName; connectionToken.IsFromParent = IsFromParent; await connectionToken.CreateOrUpdate(); } // create connection var connection = await Connection.QueryBy2Users(parentUser.Id, FromId); if (connection == null) { var newConnection = new Connection() { User1 = parentUser.Id, User2 = FromId, Permission = Permission ?? "read", Status = "pending", Partition = FromId, Table = "Report", Profiles = { } }; await newConnection.CreateOrUpdate(); } //else //{ // if (connection.Permission == null) // { // connection.Permission = "read"; // } // if (connection.Table == null) // { // connection.Table = "Report"; // } // connection.Partition = FromId; // connection.Status = "accepted"; // await connection.CreateOrUpdate(); //} }
/// <summary> /// Process when parent accepts the invitation /// It creates new Connection record for sharing a table /// </summary> /// <param name="professionalUser"></param> /// <returns>Nothing</returns> private async Task ParentAccepted(User professionalUser) { var profiles = await Profile.GetByUserId(FromId); var profileIds = profiles.Select(s => s.Id).ToList(); // update connect to accept var connection = await Connection.QueryBy2Users(FromId, professionalUser.Id); if (connection == null) { var newConnection = new Connection() { User1 = FromId, User2 = professionalUser.Id, Permission = Permission ?? "read", Status = "accepted", Profiles = profileIds, Partition = professionalUser.Id, Table = "Report" }; await newConnection.CreateOrUpdate(); } else { if (connection.Permission == null) { connection.Permission = "read"; } if (connection.Table == null) { connection.Table = "Report"; } if (connection.Profiles == null || connection.Profiles.Count == 0) { connection.Profiles = profileIds; } connection.Partition = professionalUser.Id; connection.Status = "accepted"; await connection.CreateOrUpdate(); } // update connection token of professional to accepted var connectionToken = await GetFrom(professionalUser.Id, FromEmail); if (connectionToken != null) { connectionToken.ToId = FromId; connectionToken.State = "accepted"; connectionToken.IsFromParent = IsFromParent; await connectionToken.CreateOrUpdate(); } else { var childFirstName = profiles.Count > 0 ? profiles[0].FirstName : FirstName; var childLastName = profiles.Count > 0 ? profiles[0].LastName : LastName; var newConnectionToken = new ConnectionToken() { ToId = FromId, Email = FromEmail, State = "accepted", FirstName = FirstName, LastName = LastName, ChildFirstName = childFirstName, ChildLastName = childLastName, FromEmail = professionalUser.Email, FromId = professionalUser.Id, Type = "professional", FromFirstName = FromFirstName, FromLastName = FromLastName, IsFromParent = IsFromParent }; await newConnectionToken.CreateOrUpdate(); } }