Skip to content

Commit 71f1d89

Browse files
committed
Refactor OrganizationUserRepository and update stored procedures
- Renamed stored procedure from OrganizationUser_UpdateManyConfirmByIds to OrganizationUser_UpdateManySetStatusKey to better reflect its functionality. - Updated OrganizationUserRepository to use the new stored procedure for confirming user statuses. - Introduced a new stored procedure OrganizationUser_ReadByOrganizationIdStatus to replace the previous OrganizationUser_ReadManyByOrganizationIdStatus, enhancing clarity and consistency in naming. - Adjusted related SQL migration scripts to align with the new procedure names and parameters.
1 parent eb0539f commit 71f1d89

5 files changed

Lines changed: 39 additions & 33 deletions

File tree

src/Infrastructure.Dapper/AdminConsole/Repositories/OrganizationUserRepository.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,15 +721,17 @@ public async Task<ICollection<Guid>> ConfirmManyOrganizationUsersAsync(
721721
await using var connection = new SqlConnection(ConnectionString);
722722

723723
var confirmedIds = await connection.QueryAsync<Guid>(
724-
$"[{Schema}].[OrganizationUser_UpdateManyConfirmByIds]",
724+
$"[{Schema}].[OrganizationUser_UpdateManySetStatusKey]",
725725
new
726726
{
727-
UsersToConfirmJson = JsonSerializer.Serialize(usersToConfirm.Select(u => new
727+
UsersJson = JsonSerializer.Serialize(usersToConfirm.Select(u => new
728728
{
729729
Id = u.OrganizationUserId,
730730
u.UserId,
731731
u.Key,
732732
})),
733+
NewStatus = (short)OrganizationUserStatusType.Confirmed,
734+
RequiredCurrentStatus = (short)OrganizationUserStatusType.Accepted,
733735
RevisionDate = DateTime.UtcNow
734736
},
735737
commandType: CommandType.StoredProcedure);
@@ -771,7 +773,7 @@ public async Task<ICollection<OrganizationUser>> GetManyByOrganizationIdWithStat
771773
using (var connection = new SqlConnection(ConnectionString))
772774
{
773775
var results = await connection.QueryAsync<OrganizationUser>(
774-
"[dbo].[OrganizationUser_ReadManyByOrganizationIdStatus]",
776+
"[dbo].[OrganizationUser_ReadByOrganizationIdStatus]",
775777
new { OrganizationId = organizationId, Status = (short)status },
776778
commandType: CommandType.StoredProcedure);
777779

src/Sql/dbo/Stored Procedures/OrganizationUser_ReadManyByOrganizationIdStatus.sql renamed to src/Sql/dbo/Stored Procedures/OrganizationUser_ReadByOrganizationIdStatus.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE PROCEDURE [dbo].[OrganizationUser_ReadManyByOrganizationIdStatus]
1+
CREATE PROCEDURE [dbo].[OrganizationUser_ReadByOrganizationIdStatus]
22
@OrganizationId UNIQUEIDENTIFIER,
33
@Status TINYINT
44
AS

src/Sql/dbo/Stored Procedures/OrganizationUser_UpdateManyConfirmByIds.sql renamed to src/Sql/dbo/Stored Procedures/OrganizationUser_UpdateManySetStatusKey.sql

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
1-
CREATE PROCEDURE [dbo].[OrganizationUser_UpdateManyConfirmByIds]
2-
@UsersToConfirmJson NVARCHAR(MAX),
3-
@RevisionDate DATETIME2(7)
1+
CREATE PROCEDURE [dbo].[OrganizationUser_UpdateManySetStatusKey]
2+
@UsersJson NVARCHAR(MAX),
3+
@NewStatus TINYINT,
4+
@RequiredCurrentStatus TINYINT,
5+
@RevisionDate DATETIME2(7)
46
AS
57
BEGIN
68
SET NOCOUNT ON
79

8-
DECLARE @UsersToConfirm AS TABLE (
10+
DECLARE @UsersToUpdate AS TABLE (
911
[Id] UNIQUEIDENTIFIER NOT NULL,
1012
[UserId] UNIQUEIDENTIFIER NOT NULL,
1113
[Key] NVARCHAR(MAX) NULL
1214
)
1315

14-
INSERT INTO @UsersToConfirm
16+
INSERT INTO @UsersToUpdate
1517
SELECT
1618
[Id],
1719
[UserId],
1820
[Key]
19-
FROM OPENJSON(@UsersToConfirmJson)
21+
FROM OPENJSON(@UsersJson)
2022
WITH (
2123
[Id] UNIQUEIDENTIFIER '$.Id',
2224
[UserId] UNIQUEIDENTIFIER '$.UserId',
2325
[Key] NVARCHAR(MAX) '$.Key'
2426
)
2527

26-
DECLARE @ConfirmedIds [dbo].[GuidIdArray]
28+
DECLARE @UpdatedIds [dbo].[GuidIdArray]
2729

2830
UPDATE OU
2931
SET
30-
OU.[Status] = 2, -- Confirmed
31-
OU.[Key] = UTC.[Key],
32+
OU.[Status] = @NewStatus,
33+
OU.[Key] = U.[Key],
3234
OU.[RevisionDate] = @RevisionDate
3335
OUTPUT
34-
INSERTED.[Id] INTO @ConfirmedIds
36+
INSERTED.[Id] INTO @UpdatedIds
3537
FROM
3638
[dbo].[OrganizationUser] OU
3739
INNER JOIN
38-
@UsersToConfirm UTC ON UTC.[Id] = OU.[Id]
40+
@UsersToUpdate U ON U.[Id] = OU.[Id]
3941
WHERE
40-
OU.[Status] = 1 -- Only update rows that are still Accepted
42+
OU.[Status] = @RequiredCurrentStatus
4143

42-
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @ConfirmedIds
44+
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @UpdatedIds
4345

4446
-- Return the IDs that were actually updated so the caller can track idempotency
45-
SELECT [Id] FROM @ConfirmedIds
47+
SELECT [Id] FROM @UpdatedIds
4648
END

util/Migrator/DbScripts/2026-04-23_00_UpdateOrganizationUser_ReadByOrganizationId_AddStatusParam.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUser_ReadManyByOrganizationIdStatus]
1+
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUser_ReadByOrganizationIdStatus]
22
@OrganizationId UNIQUEIDENTIFIER,
33
@Status TINYINT
44
AS
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
1-
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUser_UpdateManyConfirmByIds]
2-
@UsersToConfirmJson NVARCHAR(MAX),
3-
@RevisionDate DATETIME2(7)
1+
CREATE OR ALTER PROCEDURE [dbo].[OrganizationUser_UpdateManySetStatusKey]
2+
@UsersJson NVARCHAR(MAX),
3+
@NewStatus TINYINT,
4+
@RequiredCurrentStatus TINYINT,
5+
@RevisionDate DATETIME2(7)
46
AS
57
BEGIN
68
SET NOCOUNT ON
79

8-
DECLARE @UsersToConfirm AS TABLE (
10+
DECLARE @UsersToUpdate AS TABLE (
911
[Id] UNIQUEIDENTIFIER NOT NULL,
1012
[UserId] UNIQUEIDENTIFIER NOT NULL,
1113
[Key] NVARCHAR(MAX) NULL
1214
)
1315

14-
INSERT INTO @UsersToConfirm
16+
INSERT INTO @UsersToUpdate
1517
SELECT
1618
[Id],
1719
[UserId],
1820
[Key]
19-
FROM OPENJSON(@UsersToConfirmJson)
21+
FROM OPENJSON(@UsersJson)
2022
WITH (
2123
[Id] UNIQUEIDENTIFIER '$.Id',
2224
[UserId] UNIQUEIDENTIFIER '$.UserId',
2325
[Key] NVARCHAR(MAX) '$.Key'
2426
)
2527

26-
DECLARE @ConfirmedIds [dbo].[GuidIdArray]
28+
DECLARE @UpdatedIds [dbo].[GuidIdArray]
2729

2830
UPDATE OU
2931
SET
30-
OU.[Status] = 2, -- Confirmed
31-
OU.[Key] = UTC.[Key],
32+
OU.[Status] = @NewStatus,
33+
OU.[Key] = U.[Key],
3234
OU.[RevisionDate] = @RevisionDate
3335
OUTPUT
34-
INSERTED.[Id] INTO @ConfirmedIds
36+
INSERTED.[Id] INTO @UpdatedIds
3537
FROM
3638
[dbo].[OrganizationUser] OU
3739
INNER JOIN
38-
@UsersToConfirm UTC ON UTC.[Id] = OU.[Id]
40+
@UsersToUpdate U ON U.[Id] = OU.[Id]
3941
WHERE
40-
OU.[Status] = 1 -- Only update rows that are still Accepted
42+
OU.[Status] = @RequiredCurrentStatus
4143

42-
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @ConfirmedIds
44+
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserIds] @UpdatedIds
4345

4446
-- Return the IDs that were actually updated so the caller can track idempotency
45-
SELECT [Id] FROM @ConfirmedIds
47+
SELECT [Id] FROM @UpdatedIds
4648
END
4749
GO

0 commit comments

Comments
 (0)