From 429e9a44bd512a93c2a45b861025821957cfc808 Mon Sep 17 00:00:00 2001 From: Swapnamol Abraham Date: Mon, 7 Apr 2025 17:16:32 +0100 Subject: [PATCH] TD-5407: Implemented a fix to avoid SQL exception on LinkExistingUserToSso method --- .../LH/IExternalSystemUserRepository.cs | 8 +++++++ .../LH/ExternalSystemUserRepository.cs | 22 +++++++++++++++++++ .../RegistrationService.cs | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/LearningHub.Nhs.UserApi.Repository.Interface/LH/IExternalSystemUserRepository.cs b/LearningHub.Nhs.UserApi.Repository.Interface/LH/IExternalSystemUserRepository.cs index d84ded7..6f76875 100644 --- a/LearningHub.Nhs.UserApi.Repository.Interface/LH/IExternalSystemUserRepository.cs +++ b/LearningHub.Nhs.UserApi.Repository.Interface/LH/IExternalSystemUserRepository.cs @@ -1,6 +1,7 @@ namespace LearningHub.Nhs.UserApi.Repository.Interface.LH { using System.Threading.Tasks; + using elfhHub.Nhs.Models.Entities; using LearningHub.Nhs.Models.Entities.External; /// @@ -15,5 +16,12 @@ public interface IExternalSystemUserRepository : IGenericLHRepositoryThe external system id. /// The . Task GetByIdAsync(int userId, int externalSystemId); + + /// + /// Create External system user. + /// + /// The userExternalSystem. + /// The . + Task CreateExternalSystemUserAsync(ExternalSystemUser userExternalSystem); } } diff --git a/LearningHub.Nhs.UserApi.Repository/LH/ExternalSystemUserRepository.cs b/LearningHub.Nhs.UserApi.Repository/LH/ExternalSystemUserRepository.cs index 0e03129..c02b97d 100644 --- a/LearningHub.Nhs.UserApi.Repository/LH/ExternalSystemUserRepository.cs +++ b/LearningHub.Nhs.UserApi.Repository/LH/ExternalSystemUserRepository.cs @@ -1,10 +1,15 @@ namespace LearningHub.Nhs.UserApi.Repository.LH { + using System; + using System.Collections.Generic; + using System.Data; using System.Linq; using System.Threading.Tasks; + using elfhHub.Nhs.Models.Entities; using LearningHub.Nhs.Models.Entities.External; using LearningHub.Nhs.UserApi.Repository; using LearningHub.Nhs.UserApi.Repository.Interface.LH; + using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; /// @@ -30,5 +35,22 @@ public async Task GetByIdAsync(int userId, int externalSyste .AsNoTracking() .FirstOrDefaultWithNoLockAsync(); } + + /// + public async Task CreateExternalSystemUserAsync(ExternalSystemUser userExternalSystem) + { + try + { + var param0 = new SqlParameter("@p0", SqlDbType.Int) { Value = userExternalSystem.UserId }; + var param1 = new SqlParameter("@p1", SqlDbType.VarChar) { Value = userExternalSystem.ExternalSystemId }; + var param2 = new SqlParameter("@p2", SqlDbType.VarChar) { Value = userExternalSystem.UserId }; + var param3 = new SqlParameter("@p3", SqlDbType.Int) { Value = this.TimezoneOffsetManager.UserTimezoneOffset ?? (object)DBNull.Value }; + await this.DbContext.Database.ExecuteSqlRawAsync("[external].ExternalSystemUserCreate @p0, @p1, @p2, @p3", param0, param1, param2, param3); + } + catch (Exception ex) + { + throw new Exception(ex.Message); + } + } } } diff --git a/LearningHub.Nhs.UserApi.Services/RegistrationService.cs b/LearningHub.Nhs.UserApi.Services/RegistrationService.cs index ea3d67b..7dccbaf 100644 --- a/LearningHub.Nhs.UserApi.Services/RegistrationService.cs +++ b/LearningHub.Nhs.UserApi.Services/RegistrationService.cs @@ -185,7 +185,7 @@ public async Task LinkExistingUserToSso(int userId, int externalSystemId) ExternalSystemId = externalSystemId, }; - await this.externalSystemUserRepository.CreateAsync(userId, userExternalSystem); + await this.externalSystemUserRepository.CreateExternalSystemUserAsync(userExternalSystem); } ///