99using Microsoft . Extensions . Hosting ;
1010using Microsoft . Extensions . Logging ;
1111using Remotely . Server . Data ;
12+ using Remotely . Server . Extensions ;
1213using Remotely . Server . Models ;
1314using Remotely . Shared ;
1415using Remotely . Shared . Dtos ;
@@ -118,7 +119,7 @@ public interface IDataService
118119
119120 Task < Result < Device > > GetDevice (
120121 string deviceId ,
121- Action < IQueryable < Device > > ? includesBuilder = null ) ;
122+ Action < IQueryable < Device > > ? queryBuilder = null ) ;
122123
123124 Task < Result < Device > > GetDevice ( string orgId , string deviceId ) ;
124125
@@ -178,7 +179,7 @@ Task<Result<DeviceGroup>> GetDeviceGroup(
178179
179180 Task < Result < RemotelyUser > > GetUserByName (
180181 string userName ,
181- Action < IQueryable < RemotelyUser > > ? includesBuilder = null ) ;
182+ Action < IQueryable < RemotelyUser > > ? queryBuilder = null ) ;
182183
183184 Task < Result < RemotelyUserOptions > > GetUserOptions ( string userName ) ;
184185
@@ -947,7 +948,9 @@ public async Task<Result> DeleteUser(string orgId, string targetUserId)
947948 . Include ( x => x . Organization )
948949 . Include ( x => x . Alerts )
949950 . Include ( x => x . SavedScripts )
951+ . ThenInclude ( x => x . ScriptRuns )
950952 . Include ( x => x . ScriptSchedules )
953+ . ThenInclude ( x => x . ScriptRuns )
951954 . FirstOrDefault ( x =>
952955 x . Id == targetUserId &&
953956 x . OrganizationID == orgId ) ;
@@ -962,11 +965,22 @@ public async Task<Result> DeleteUser(string orgId, string targetUserId)
962965 deviceGroup . Users . Remove ( target ) ;
963966 }
964967
965- foreach ( var alert in target . Alerts )
968+ foreach ( var run in target . ScriptSchedules . SelectMany ( x => x . ScriptRuns ) )
966969 {
967- dbContext . Alerts . Remove ( alert ) ;
970+ dbContext . ScriptRuns . Remove ( run ) ;
968971 }
969972
973+ foreach ( var script in target . SavedScripts )
974+ {
975+ if ( script . ScriptRuns is not null )
976+ {
977+ dbContext . ScriptRuns . RemoveRange ( script . ScriptRuns ) ;
978+ }
979+ }
980+
981+ dbContext . Alerts . RemoveRange ( target . Alerts ) ;
982+ dbContext . ScriptSchedules . RemoveRange ( target . ScriptSchedules ) ;
983+
970984 target . Organization = null ;
971985 org . RemotelyUsers . Remove ( target ) ;
972986 dbContext . Users . Remove ( target ) ;
@@ -1298,17 +1312,14 @@ public async Task<Result<Device>> GetDevice(string orgId, string deviceId)
12981312
12991313 public async Task < Result < Device > > GetDevice (
13001314 string deviceId ,
1301- Action < IQueryable < Device > > ? includesBuilder = null )
1315+ Action < IQueryable < Device > > ? queryBuilder = null )
13021316 {
13031317 using var dbContext = _appDbFactory . GetContext ( ) ;
1304-
1305- var query = dbContext . Devices
1306- . AsNoTracking ( )
1307- . AsQueryable ( ) ;
13081318
1309- includesBuilder ? . Invoke ( query ) ;
1310-
1311- var device = await query . FirstOrDefaultAsync ( x => x . ID == deviceId ) ;
1319+ var device = await dbContext . Devices
1320+ . AsNoTracking ( )
1321+ . Apply ( queryBuilder )
1322+ . FirstOrDefaultAsync ( x => x . ID == deviceId ) ;
13121323
13131324 if ( device is null )
13141325 {
@@ -1762,7 +1773,7 @@ public async Task<Result<RemotelyUser>> GetUserById(string userId)
17621773
17631774 public async Task < Result < RemotelyUser > > GetUserByName (
17641775 string userName ,
1765- Action < IQueryable < RemotelyUser > > ? includesBuilder = null )
1776+ Action < IQueryable < RemotelyUser > > ? queryBuilder = null )
17661777 {
17671778 if ( string . IsNullOrWhiteSpace ( userName ) )
17681779 {
@@ -1771,14 +1782,11 @@ public async Task<Result<RemotelyUser>> GetUserByName(
17711782
17721783 using var dbContext = _appDbFactory . GetContext ( ) ;
17731784
1774- var query = dbContext . Users
1785+ var user = await dbContext . Users
17751786 . AsNoTracking ( )
1776- . AsQueryable ( ) ;
1777-
1778- includesBuilder ? . Invoke ( query ) ;
1779-
1780- var user = await query . FirstOrDefaultAsync ( x =>
1781- x . UserName ! . ToLower ( ) . Trim ( ) == userName . ToLower ( ) . Trim ( ) ) ;
1787+ . Apply ( queryBuilder )
1788+ . FirstOrDefaultAsync ( x =>
1789+ x . UserName ! . ToLower ( ) . Trim ( ) == userName . ToLower ( ) . Trim ( ) ) ;
17821790
17831791 if ( user is null )
17841792 {
0 commit comments