Skip to content

Commit

Permalink
a fix for the fix of 2649 (#1238)
Browse files Browse the repository at this point in the history
# Description

This PR includes the following proposed change(s):

- spdbt-2649(fix tracking - save and update should not apply to all
fields)
- this is a fix for the fix of 2649
  • Loading branch information
peggy-quartech authored Jul 19, 2024
1 parent de741f1 commit f2eb128
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/Spd.Resource.Repository/User/Mappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public Mappings()
.ForMember(d => d.spd_phonenumber, opt => opt.MapFrom(s => s.PhoneNumber))
.ForMember(d => d.spd_servicecategory, opt => opt.MapFrom(s => (int)PortalUserServiceCategoryOptionSet.Screening))
.ReverseMap()
.ForMember(d => d.OrganizationId, opt => opt.MapFrom(s => s._spd_organizationid_value))
.ForMember(d => d.ContactAuthorizationTypeCode, opt => opt.MapFrom(s => GetAuthorizationTypeCode(s.spd_spd_role_spd_portaluser)))
.ForMember(d => d.OrganizationId, opt => opt.MapFrom(s => s._spd_organizationid_value));

Expand Down
30 changes: 10 additions & 20 deletions src/Spd.Resource.Repository/User/OrgUserRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net;
using AutoMapper;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Dynamics.CRM;
Expand All @@ -7,6 +6,7 @@
using Spd.Utilities.Dynamics;
using Spd.Utilities.Shared;
using Spd.Utilities.Shared.Exceptions;
using System.Net;

namespace Spd.Resource.Repository.User
{
Expand Down Expand Up @@ -166,9 +166,9 @@ private async Task<OrgUserManageResult> AddUserAsync(UserCreateCmd createUserCmd

await _dynaContext.SaveChangesAsync(ct);

user._spd_organizationid_value = createUserCmd.User.OrganizationId;
user.spd_spd_role_spd_portaluser = new DataServiceCollection<spd_role> { new() { spd_roleid = role.spd_roleid } };

UserResult userResult = _mapper.Map<UserResult>(user);
userResult.OrganizationId = createUserCmd.User.OrganizationId;
userResult.ContactAuthorizationTypeCode = createUserCmd.User.ContactAuthorizationTypeCode;
return new OrgUserManageResult(_mapper.Map<UserResult>(user));
}

Expand All @@ -186,13 +186,13 @@ private async Task<OrgUserManageResult> UpdateUserAsync(UserUpdateCmd updateUser
}

spd_role existingRole = user.spd_spd_role_spd_portaluser.First();
spd_role newRole = existingRole;

string existingRoleName = _dynaContext.LookupRoleKeyById((Guid)existingRole.spd_roleid);
if (existingRoleName != updateUserCmd.User.ContactAuthorizationTypeCode.ToString()) //role changed
{
_dynaContext.DeleteLink(existingRole, nameof(existingRole.spd_spd_role_spd_portaluser), user);

newRole = _dynaContext.LookupRole(updateUserCmd.User.ContactAuthorizationTypeCode.ToString());
spd_role newRole = _dynaContext.LookupRole(updateUserCmd.User.ContactAuthorizationTypeCode.ToString());
if (newRole != null)
{
_dynaContext.AddLink(newRole, nameof(newRole.spd_spd_role_spd_portaluser), user);
Expand All @@ -201,8 +201,9 @@ private async Task<OrgUserManageResult> UpdateUserAsync(UserUpdateCmd updateUser
_dynaContext.UpdateObject(user);
await _dynaContext.SaveChangesAsync(cancellationToken);

user.spd_spd_role_spd_portaluser = new DataServiceCollection<spd_role> { new() { spd_roleid = newRole.spd_roleid } };
return new OrgUserManageResult(_mapper.Map<UserResult>(user));
UserResult userResult = _mapper.Map<UserResult>(user);
userResult.ContactAuthorizationTypeCode = updateUserCmd.User.ContactAuthorizationTypeCode;
return new OrgUserManageResult(userResult);
}

private async Task<OrgUserManageResult> DeleteUserAsync(Guid userId, CancellationToken cancellationToken)
Expand Down Expand Up @@ -247,18 +248,7 @@ private async Task<OrgUsersResult> SearchUsers(Guid? organizationId, Guid? ident
if (identityId != null)
users = users.Where(a => a._spd_identityid_value == identityId);

var userList = users.ToList();
await Parallel.ForEachAsync(userList, cancellationToken, async (user, cancellationToken) =>
{
var role = _dynaContext
.spd_spd_role_spd_portaluserset
.Where(r => r.spd_portaluserid == user.spd_portaluserid)
.FirstOrDefault();
if (role != null)
user.spd_spd_role_spd_portaluser = new DataServiceCollection<spd_role> { new() { spd_roleid = role.spd_roleid } };
});

return new OrgUsersResult(_mapper.Map<IEnumerable<UserResult>>(userList));
return new OrgUsersResult(_mapper.Map<IEnumerable<UserResult>>(users));
}

private async Task<OrgUserManageResult> UpdateUserLoginAsync(Guid userId, CancellationToken cancellationToken)
Expand Down

0 comments on commit f2eb128

Please sign in to comment.