Skip to content

Commit ccb4764

Browse files
fix: DTOSS-11698 - Add GUID primary key to ServiceNow cases (#1761)
* feat(migrations): Add GUID primary key to ServiceNow cases - Created a new migration to drop the existing primary key on SERVICENOW_CASES and add a new GUID primary key. - Updated the DataServicesContextModelSnapshot to reflect the new ID property. - Modified the ServicenowCase model to include the new GUID property. - Updated unit tests to initialize the new ID property for ServicenowCase. - Added Playwright tests to verify handling of duplicate ServiceNow case IDs. - Updated environment configuration to reflect changes in unique key for ServiceNow cases. - Created test payloads for duplicate ServiceNow case ID scenarios. * rename: post-go-live-tests (files deleted, dirs gone) - changed to maintenance-regression-tests * fix: attempt to diagnose disk issues during static analysis * fix: second attempt to diagnose sonar analysis * fix: removed problematic debugging task * chore: remove pipeline debugging code (now in main) * fix: regenerated migration for guid
1 parent ce6e0b7 commit ccb4764

File tree

12 files changed

+1299
-8
lines changed

12 files changed

+1299
-8
lines changed

application/CohortManager/src/Functions/ServiceNowIntegration/ServiceNowMessageHandler/ReceiveServiceNowMessageFunction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public async Task<HttpResponseData> Run(
7070

7171
var serviceNowCase = new ServicenowCase
7272
{
73+
Id = Guid.NewGuid(),
7374
ServicenowId = requestBody.ServiceNowCaseNumber,
7475
NhsNumber = nhsNumber,
7576
Status = ServiceNowStatus.New,

application/CohortManager/src/Functions/Shared/DataServices.Database/DataServicesContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
8686
.ToTable("NEMS_SUBSCRIPTION", "dbo");
8787

8888
modelBuilder.Entity<ServicenowCase>()
89-
.ToTable("SERVICENOW_CASES", "dbo");
89+
.ToTable("SERVICENOW_CASES", "dbo")
90+
.HasIndex(s => s.ServicenowId, "IX_SERVICENOW_CASES_SERVICENOW_ID");
9091
}
9192
}

application/CohortManager/src/Functions/Shared/DataServices.Migrations/Migrations/20251125133634_AddGuidPrimaryKeyToServiceNowCases.Designer.cs

Lines changed: 1074 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
4+
#nullable disable
5+
6+
namespace DataServices.Migrations.Migrations
7+
{
8+
/// <inheritdoc />
9+
public partial class AddGuidPrimaryKeyToServiceNowCases : Migration
10+
{
11+
/// <inheritdoc />
12+
protected override void Up(MigrationBuilder migrationBuilder)
13+
{
14+
migrationBuilder.DropPrimaryKey(
15+
name: "PK_SERVICENOW_CASES",
16+
schema: "dbo",
17+
table: "SERVICENOW_CASES");
18+
19+
migrationBuilder.AddColumn<Guid>(
20+
name: "ID",
21+
schema: "dbo",
22+
table: "SERVICENOW_CASES",
23+
type: "uniqueidentifier",
24+
nullable: false,
25+
defaultValueSql: "NEWID()");
26+
27+
migrationBuilder.AddPrimaryKey(
28+
name: "PK_SERVICENOW_CASES",
29+
schema: "dbo",
30+
table: "SERVICENOW_CASES",
31+
column: "ID");
32+
33+
migrationBuilder.CreateIndex(
34+
name: "IX_SERVICENOW_CASES_SERVICENOW_ID",
35+
schema: "dbo",
36+
table: "SERVICENOW_CASES",
37+
column: "SERVICENOW_ID");
38+
}
39+
40+
/// <inheritdoc />
41+
protected override void Down(MigrationBuilder migrationBuilder)
42+
{
43+
migrationBuilder.DropPrimaryKey(
44+
name: "PK_SERVICENOW_CASES",
45+
schema: "dbo",
46+
table: "SERVICENOW_CASES");
47+
48+
migrationBuilder.DropIndex(
49+
name: "IX_SERVICENOW_CASES_SERVICENOW_ID",
50+
schema: "dbo",
51+
table: "SERVICENOW_CASES");
52+
53+
migrationBuilder.DropColumn(
54+
name: "ID",
55+
schema: "dbo",
56+
table: "SERVICENOW_CASES");
57+
58+
migrationBuilder.AddPrimaryKey(
59+
name: "PK_SERVICENOW_CASES",
60+
schema: "dbo",
61+
table: "SERVICENOW_CASES",
62+
column: "SERVICENOW_ID");
63+
}
64+
}
65+
}

application/CohortManager/src/Functions/Shared/DataServices.Migrations/Migrations/DataServicesContextModelSnapshot.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,10 +1031,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
10311031

10321032
modelBuilder.Entity("Model.ServicenowCase", b =>
10331033
{
1034-
b.Property<string>("ServicenowId")
1035-
.HasMaxLength(10)
1036-
.HasColumnType("nvarchar(10)")
1037-
.HasColumnName("SERVICENOW_ID");
1034+
b.Property<Guid>("Id")
1035+
.ValueGeneratedOnAdd()
1036+
.HasColumnType("uniqueidentifier")
1037+
.HasColumnName("ID");
10381038

10391039
b.Property<long?>("NhsNumber")
10401040
.HasColumnType("bigint")
@@ -1048,12 +1048,20 @@ protected override void BuildModel(ModelBuilder modelBuilder)
10481048
.HasColumnType("datetime")
10491049
.HasColumnName("RECORD_UPDATE_DATETIME");
10501050

1051+
b.Property<string>("ServicenowId")
1052+
.IsRequired()
1053+
.HasMaxLength(10)
1054+
.HasColumnType("nvarchar(10)")
1055+
.HasColumnName("SERVICENOW_ID");
1056+
10511057
b.Property<string>("Status")
10521058
.HasMaxLength(10)
10531059
.HasColumnType("nvarchar(10)")
10541060
.HasColumnName("STATUS");
10551061

1056-
b.HasKey("ServicenowId");
1062+
b.HasKey("Id");
1063+
1064+
b.HasIndex(new[] { "ServicenowId" }, "IX_SERVICENOW_CASES_SERVICENOW_ID");
10571065

10581066
b.ToTable("SERVICENOW_CASES", "dbo");
10591067
});

application/CohortManager/src/Functions/Shared/DataServices.Migrations/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static IHostBuilder CreateHostBuilder(DatabaseConfig config) =>
5353
var sqlConnectionBuilder = new SqlConnectionStringBuilder(config.DtOsDatabaseConnectionString);
5454
var connection = new SqlConnection(sqlConnectionBuilder.ConnectionString);
5555

56-
if (config.SQL_IDENTITY_CLIENT_ID is not null)
56+
if (!string.IsNullOrWhiteSpace(config.SQL_IDENTITY_CLIENT_ID))
5757
{
5858
var credential = new ManagedIdentityCredential(config.SQL_IDENTITY_CLIENT_ID);
5959
var token = credential.GetToken(new Azure.Core.TokenRequestContext(TokenScopes));

application/CohortManager/src/Functions/Shared/Model/EFModels/ServicenowCase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ namespace Model;
66
public class ServicenowCase
77
{
88
[Key]
9+
[Column("ID")]
10+
public Guid Id { get; set; }
11+
12+
[Required]
913
[MaxLength(10)]
1014
[Column("SERVICENOW_ID")]
1115
public required string ServicenowId { get; set; }

tests/UnitTests/ServiceNowCohortLookupTests/ServiceNowCohortLookupTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public async Task Run_WithInvalidNhsNumber_LogsWarningAndSkips()
5050
// Arrange
5151
var invalidCase = new ServicenowCase
5252
{
53+
Id = Guid.NewGuid(),
5354
ServicenowId = ServiceNowId,
5455
NhsNumber = 0, // Invalid NHS number
5556
Status = ServiceNowStatus.New,
@@ -84,6 +85,7 @@ public async Task Run_WithNoYesterdayParticipants_LogsInformation()
8485
var validNhsNumberLong = long.Parse(ValidNhsNumber);
8586
var newCase = new ServicenowCase
8687
{
88+
Id = Guid.NewGuid(),
8789
ServicenowId = ServiceNowId,
8890
NhsNumber = validNhsNumberLong,
8991
Status = ServiceNowStatus.New,
@@ -118,6 +120,7 @@ public async Task Run_WithCaseAlreadyProcessed_DoesNotProcessAgain()
118120
var validNhsNumberLong = long.Parse(ValidNhsNumber);
119121
var processedCase = new ServicenowCase
120122
{
123+
Id = Guid.NewGuid(),
121124
ServicenowId = ServiceNowId,
122125
NhsNumber = validNhsNumberLong,
123126
Status = ServiceNowStatus.Complete, // Already processed
@@ -143,20 +146,23 @@ public async Task Run_WithMixedCases_ProcessesCorrectly()
143146
// Arrange
144147
var validCase1 = new ServicenowCase
145148
{
149+
Id = Guid.NewGuid(),
146150
ServicenowId = "SN1",
147151
NhsNumber = 123,
148152
Status = ServiceNowStatus.New
149153
};
150154

151155
var validCase2 = new ServicenowCase
152156
{
157+
Id = Guid.NewGuid(),
153158
ServicenowId = "SN2",
154159
NhsNumber = 456,
155160
Status = ServiceNowStatus.New
156161
};
157162

158163
var invalidCase = new ServicenowCase
159164
{
165+
Id = Guid.NewGuid(),
160166
ServicenowId = "SN3",
161167
NhsNumber = 0, // Invalid
162168
Status = ServiceNowStatus.New
@@ -220,13 +226,15 @@ public async Task Run_WithDuplicateNhsNumbers_ProcessesAll()
220226

221227
var case1 = new ServicenowCase
222228
{
229+
Id = Guid.NewGuid(),
223230
ServicenowId = "SN1",
224231
NhsNumber = testNhsNumber,
225232
Status = ServiceNowStatus.New
226233
};
227234

228235
var case2 = new ServicenowCase
229236
{
237+
Id = Guid.NewGuid(),
230238
ServicenowId = "SN2",
231239
NhsNumber = testNhsNumber, // Same NHS number
232240
Status = ServiceNowStatus.New

tests/playwright-tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"test:regression_e2e_epic4d": "cross-env TEST_TYPE=RegressionEpic4d npx playwright test --project=dev --config=src/config/playwright.config.ts --grep=@epic4d-",
1717
"test:regression_e2e_epic4b": "cross-env TEST_TYPE=RegressionEpic4d npx playwright test --project=dev --config=src/config/playwright.config.ts --grep=@epic4b-",
1818
"test:regression_e2e_epic4f": "cross-env TEST_TYPE=RegressionEpic4f npx playwright test --project=dev --config=src/config/playwright.config.ts --grep=@epic4f-",
19+
"test:maintenance_regression": "cross-env TEST_TYPE=Maintenance npx playwright test --project=dev --config=src/config/playwright.config.ts src/tests/e2e/maintenance-regression-tests/",
1920
"test": "cross-env TEST_TYPE=SMOKE npx playwright test --project=dev --config=src/config/playwright.config.ts --grep=\"@smoke @e2e\"",
2021
"test:validation-exceptions": "cross-env TEST_TYPE=VALIDATION npx playwright test tests/api/bsselect/validationExceptions/ --project=dev --config=src/config/playwright.config.ts",
2122
"test:service_now": "npx playwright test --project=dev --config=src/config/playwright.config.ts --grep=\"@service_now\"",

tests/playwright-tests/src/config/env.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ export const config = {
8282
uniqueKeyParticipantManagement: 'ParticipantId',
8383
uniqueKeyParticipantDemographic: 'ParticipantId',
8484
uniqueKeyExceptionManagement: 'ExceptionId',
85-
uniqueKeyServiceNowCases: 'ServicenowId',
85+
uniqueKeyServiceNowCases: 'Id',
8686
ignoreValidationKey: 'apiEndpoint'
8787
}

0 commit comments

Comments
 (0)