@@ -15,10 +15,54 @@ namespace Altinn.Broker.Persistence.Repositories;
1515
1616public class FileTransferRepository ( NpgsqlDataSource dataSource , IActorRepository actorRepository , ExecuteDBCommandWithRetries commandExecutor ) : IFileTransferRepository
1717{
18- public async Task < FileTransferEntity ? > GetFileTransfer ( Guid fileTransferId , CancellationToken cancellationToken )
19- {
20- await using var command = dataSource . CreateCommand (
21- @"
18+ #region constants
19+ const string overviewCommandMultiple = @"
20+ SELECT
21+ f.file_transfer_id_pk,
22+ f.resource_id,
23+ f.filename,
24+ f.checksum,
25+ f.sender_actor_id_fk,
26+ f.external_file_transfer_reference,
27+ f.created,
28+ f.file_location,
29+ f.file_transfer_size,
30+ f.expiration_time,
31+ f.hangfire_job_id,
32+ f.use_virus_scan,
33+ sender.actor_external_id as senderActorExternalReference,
34+ fs_latest.file_transfer_status_description_id_fk,
35+ fs_latest.file_transfer_status_date,
36+ fs_latest.file_transfer_status_detailed_description
37+ FROM
38+ broker.file_transfer f
39+ INNER JOIN
40+ broker.actor sender ON sender.actor_id_pk = f.sender_actor_id_fk
41+ LEFT JOIN
42+ (
43+ SELECT
44+ fs.file_transfer_id_fk,
45+ fs.file_transfer_status_description_id_fk,
46+ fs.file_transfer_status_date,
47+ fs.file_transfer_status_detailed_description
48+ FROM
49+ broker.file_transfer_status fs
50+ INNER JOIN
51+ (
52+ SELECT
53+ file_transfer_id_fk,
54+ MAX(file_transfer_status_date) as max_date
55+ FROM
56+ broker.file_transfer_status
57+ GROUP BY
58+ file_transfer_id_fk
59+ ) fs_max ON fs.file_transfer_id_fk = fs_max.file_transfer_id_fk AND fs.file_transfer_status_date = fs_max.max_date
60+ WHERE
61+ fs.file_transfer_id_fk IN (select unnest(@fileTransferIds))
62+ ) fs_latest ON f.file_transfer_id_pk = fs_latest.file_transfer_id_fk
63+ WHERE
64+ f.file_transfer_id_pk IN (select unnest(@fileTransferIds));" ;
65+ const string overviewCommandSingle = @"
2266 SELECT
2367 f.file_transfer_id_pk,
2468 f.resource_id,
@@ -63,16 +107,82 @@ GROUP BY
63107 fs.file_transfer_id_fk = @fileTransferId
64108 ) fs_latest ON f.file_transfer_id_pk = fs_latest.file_transfer_id_fk
65109 WHERE
66- f.file_transfer_id_pk = @fileTransferId;" ) ;
110+ f.file_transfer_id_pk = @fileTransferId;" ;
111+ #endregion
112+ public async Task < IReadOnlyList < FileTransferEntity > > GetFileTransfers ( IReadOnlyCollection < Guid > fileTransferIds , CancellationToken cancellationToken )
113+ {
114+ if ( fileTransferIds is null || fileTransferIds . Count == 0 )
115+ {
116+ return new List < FileTransferEntity > ( 0 ) ;
117+ }
67118
119+ await using var command = dataSource . CreateCommand ( overviewCommandMultiple ) ;
120+
121+ var parameter = new NpgsqlParameter ( "@fileTransferIds" , NpgsqlDbType . Array | NpgsqlDbType . Uuid )
122+ {
123+ Value = fileTransferIds . ToArray ( )
124+ } ;
125+
126+ command . Parameters . Add ( parameter ) ;
127+ return await commandExecutor . ExecuteWithRetry ( async ( ct ) =>
128+ {
129+ List < FileTransferEntity > fileTransferEntities = new List < FileTransferEntity > ( ) ;
130+ FileTransferEntity ? fileTransfer = null ;
131+
132+ await using var reader = await command . ExecuteReaderAsync ( ct ) ;
133+ while ( await reader . ReadAsync ( ct ) )
134+ {
135+ var fileTransferId = reader . GetGuid ( reader . GetOrdinal ( "file_transfer_id_pk" ) ) ;
136+ fileTransfer = new FileTransferEntity
137+ {
138+ FileTransferId = fileTransferId ,
139+ ResourceId = reader . GetString ( reader . GetOrdinal ( "resource_id" ) ) ,
140+ FileName = reader . GetString ( reader . GetOrdinal ( "filename" ) ) ,
141+ Checksum = reader . IsDBNull ( reader . GetOrdinal ( "checksum" ) ) ? null : reader . GetString ( reader . GetOrdinal ( "checksum" ) ) ,
142+ SendersFileTransferReference = reader . GetString ( reader . GetOrdinal ( "external_file_transfer_reference" ) ) ,
143+ HangfireJobId = reader . IsDBNull ( reader . GetOrdinal ( "hangfire_job_id" ) ) ? null : reader . GetString ( reader . GetOrdinal ( "hangfire_job_id" ) ) ,
144+ FileTransferStatusEntity = new FileTransferStatusEntity ( )
145+ {
146+ FileTransferId = fileTransferId ,
147+ Status = ( FileTransferStatus ) reader . GetInt32 ( reader . GetOrdinal ( "file_transfer_status_description_id_fk" ) ) ,
148+ Date = reader . GetDateTime ( reader . GetOrdinal ( "file_transfer_status_date" ) ) ,
149+ DetailedStatus = reader . IsDBNull ( reader . GetOrdinal ( "file_transfer_status_detailed_description" ) ) ? null : reader . GetString ( reader . GetOrdinal ( "file_transfer_status_detailed_description" ) )
150+ } ,
151+ FileTransferStatusChanged = reader . GetDateTime ( reader . GetOrdinal ( "file_transfer_status_date" ) ) ,
152+ Created = reader . GetDateTime ( reader . GetOrdinal ( "created" ) ) ,
153+ ExpirationTime = reader . GetDateTime ( reader . GetOrdinal ( "expiration_time" ) ) ,
154+ FileLocation = reader . IsDBNull ( reader . GetOrdinal ( "file_location" ) ) ? null : reader . GetString ( reader . GetOrdinal ( "file_location" ) ) ,
155+ FileTransferSize = reader . IsDBNull ( reader . GetOrdinal ( "file_transfer_size" ) ) ? 0 : reader . GetInt64 ( reader . GetOrdinal ( "file_transfer_size" ) ) ,
156+ Sender = new ActorEntity ( )
157+ {
158+ ActorId = reader . GetInt64 ( reader . GetOrdinal ( "sender_actor_id_fk" ) ) ,
159+ ActorExternalId = reader . GetString ( reader . GetOrdinal ( "senderActorExternalReference" ) )
160+ } ,
161+ RecipientCurrentStatuses = await GetLatestRecipientFileTransferStatuses ( fileTransferId , ct ) ,
162+ PropertyList = await GetMetadata ( fileTransferId , ct ) ,
163+ UseVirusScan = reader . GetBoolean ( reader . GetOrdinal ( "use_virus_scan" ) )
164+ } ;
165+
166+ fileTransferEntities . Add ( fileTransfer ) ;
167+ EnrichLogs ( fileTransfer ) ;
168+ }
169+
170+ return fileTransferEntities ;
171+ } , cancellationToken ) ;
172+ }
173+
174+ public async Task < FileTransferEntity ? > GetFileTransfer ( Guid fileTransferId , CancellationToken cancellationToken )
175+ {
176+ await using var command = dataSource . CreateCommand ( overviewCommandSingle ) ;
177+
68178 command . Parameters . AddWithValue ( "@fileTransferId" , fileTransferId ) ;
69-
70- return await commandExecutor . ExecuteWithRetry ( async ( ct ) =>
179+
180+ return await commandExecutor . ExecuteWithRetry ( async ( ct ) =>
71181 {
72182 FileTransferEntity ? fileTransfer = null ;
73-
183+
74184 await using var reader = await command . ExecuteReaderAsync ( ct ) ;
75-
185+
76186 if ( await reader . ReadAsync ( ct ) )
77187 {
78188 fileTransfer = new FileTransferEntity
@@ -104,7 +214,7 @@ GROUP BY
104214 PropertyList = await GetMetadata ( fileTransferId , ct ) ,
105215 UseVirusScan = reader . GetBoolean ( reader . GetOrdinal ( "use_virus_scan" ) )
106216 } ;
107-
217+
108218 EnrichLogs ( fileTransfer ) ;
109219 }
110220
0 commit comments