diff --git a/sda/cmd/api/api.md b/sda/cmd/api/api.md index 5816d74e0..b5972b123 100644 --- a/sda/cmd/api/api.md +++ b/sda/cmd/api/api.md @@ -103,3 +103,18 @@ Admin endpoints are only available to a set of whitelisted users specified in th - `200` Query execute ok. - `401` User is not in the list of admins. - `500` Internal error due to DB failure. + +- `/users/:username/files` + - accepts `GET` requests` + - Returns all files for a user with active uploads as a JSON array + + Example: + + ```bash + curl -H "Authorization: Bearer $token" -X GET https://HOSTNAME/users + ``` + +- Error codes + - `200` Query execute ok. + - `401` User is not in the list of admins. + - `500` Internal error due to DB failure. diff --git a/sda/cmd/api/api_test.go b/sda/cmd/api/api_test.go index 583861471..4df520e3a 100644 --- a/sda/cmd/api/api_test.go +++ b/sda/cmd/api/api_test.go @@ -1181,5 +1181,5 @@ func (suite *TestSuite) TestListUserFiles() { files := []database.SubmissionFileInfo{} err = json.NewDecoder(okResponse.Body).Decode(&files) assert.NoError(suite.T(), err, "failed to list users from DB") - assert.Equal(suite.T(), 5, len(files)) + assert.Equal(suite.T(), 2, len(files)) } diff --git a/sda/internal/database/db_functions.go b/sda/internal/database/db_functions.go index 499b4e9e2..8794a8e28 100644 --- a/sda/internal/database/db_functions.go +++ b/sda/internal/database/db_functions.go @@ -635,14 +635,9 @@ func (dbs *SDAdb) getUserFiles(userID string) ([]*SubmissionFileInfo, error) { db := dbs.DB // select all files of the user, each one annotated with its latest event - const query = "SELECT f.submission_file_path, e.event, f.created_at " + - "FROM sda.files f " + - "LEFT JOIN ( " + - "SELECT DISTINCT ON (file_id) file_id, started_at, event " + - "FROM sda.file_event_log " + - "ORDER BY file_id, started_at DESC" + - ") e ON f.id = e.file_id " + - "WHERE f.submission_user = $1; " + const query = "SELECT f.submission_file_path, e.event, f.created_at FROM sda.files f " + + "LEFT JOIN (SELECT DISTINCT ON (file_id) file_id, started_at, event FROM sda.file_event_log ORDER BY file_id, started_at DESC) e ON f.id = e.file_id WHERE f.submission_user = $1 " + + "AND f.id NOT IN (SELECT f.id FROM sda.files f RIGHT JOIN sda.file_dataset d ON f.id = d.file_id); " // nolint:rowserrcheck rows, err := db.Query(query, userID)