Skip to content

Commit 8d2c273

Browse files
Merge pull request #150 from TechnologyEnhancedLearning/Develop/Fixes/TD-5407-SQL-Exception
TD-5407: Implemented a fix to avoid SQL exception on LinkExistingUser…
2 parents 4adf6ee + 429e9a4 commit 8d2c273

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

LearningHub.Nhs.UserApi.Repository.Interface/LH/IExternalSystemUserRepository.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace LearningHub.Nhs.UserApi.Repository.Interface.LH
22
{
33
using System.Threading.Tasks;
4+
using elfhHub.Nhs.Models.Entities;
45
using LearningHub.Nhs.Models.Entities.External;
56

67
/// <summary>
@@ -15,5 +16,12 @@ public interface IExternalSystemUserRepository : IGenericLHRepository<ExternalSy
1516
/// <param name="externalSystemId">The external system id.</param>
1617
/// <returns>The <see cref="ExternalSystemUser"/>.</returns>
1718
Task<ExternalSystemUser> GetByIdAsync(int userId, int externalSystemId);
19+
20+
/// <summary>
21+
/// Create External system user.
22+
/// </summary>
23+
/// <param name="userExternalSystem">The userExternalSystem.</param>
24+
/// <returns>The <see cref="ExternalSystemUser"/>.</returns>
25+
Task CreateExternalSystemUserAsync(ExternalSystemUser userExternalSystem);
1826
}
1927
}

LearningHub.Nhs.UserApi.Repository/LH/ExternalSystemUserRepository.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
namespace LearningHub.Nhs.UserApi.Repository.LH
22
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Data;
36
using System.Linq;
47
using System.Threading.Tasks;
8+
using elfhHub.Nhs.Models.Entities;
59
using LearningHub.Nhs.Models.Entities.External;
610
using LearningHub.Nhs.UserApi.Repository;
711
using LearningHub.Nhs.UserApi.Repository.Interface.LH;
12+
using Microsoft.Data.SqlClient;
813
using Microsoft.EntityFrameworkCore;
914

1015
/// <summary>
@@ -30,5 +35,22 @@ public async Task<ExternalSystemUser> GetByIdAsync(int userId, int externalSyste
3035
.AsNoTracking()
3136
.FirstOrDefaultWithNoLockAsync();
3237
}
38+
39+
/// <inheritdoc/>
40+
public async Task CreateExternalSystemUserAsync(ExternalSystemUser userExternalSystem)
41+
{
42+
try
43+
{
44+
var param0 = new SqlParameter("@p0", SqlDbType.Int) { Value = userExternalSystem.UserId };
45+
var param1 = new SqlParameter("@p1", SqlDbType.VarChar) { Value = userExternalSystem.ExternalSystemId };
46+
var param2 = new SqlParameter("@p2", SqlDbType.VarChar) { Value = userExternalSystem.UserId };
47+
var param3 = new SqlParameter("@p3", SqlDbType.Int) { Value = this.TimezoneOffsetManager.UserTimezoneOffset ?? (object)DBNull.Value };
48+
await this.DbContext.Database.ExecuteSqlRawAsync("[external].ExternalSystemUserCreate @p0, @p1, @p2, @p3", param0, param1, param2, param3);
49+
}
50+
catch (Exception ex)
51+
{
52+
throw new Exception(ex.Message);
53+
}
54+
}
3355
}
3456
}

LearningHub.Nhs.UserApi.Services/RegistrationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public async Task LinkExistingUserToSso(int userId, int externalSystemId)
185185
ExternalSystemId = externalSystemId,
186186
};
187187

188-
await this.externalSystemUserRepository.CreateAsync(userId, userExternalSystem);
188+
await this.externalSystemUserRepository.CreateExternalSystemUserAsync(userExternalSystem);
189189
}
190190

191191
/// <inheritdoc/>

0 commit comments

Comments
 (0)