Skip to content

Commit

Permalink
MDEXP-110 - Return list of users who run the completed jobs (#441)
Browse files Browse the repository at this point in the history
* MDEXP-110 Squashed commit
  • Loading branch information
obozhko-folio authored Mar 6, 2024
1 parent 1ed5a42 commit 4435df4
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 1 deletion.
19 changes: 18 additions & 1 deletion descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,17 @@
],
"modulePermissions": [
]
},
{
"methods": [
"GET"
],
"pathPattern": "/data-export/related-users",
"permissionsRequired": [
"data-export.related-users.collection.get"
],
"modulePermissions": [
]
}
]
},
Expand Down Expand Up @@ -590,6 +601,11 @@
"displayName": "Data Export - export all records",
"description": "Entry point to export all records of the specific type"
},
{
"permissionName": "data-export.related-users.collection.get",
"displayName": "Data Export - related users",
"description": "To return only users which executed export"
},
{
"permissionName": "data-export.all",
"displayName": "Data Export - all permissions",
Expand Down Expand Up @@ -618,7 +634,8 @@
"data-export.clean-up-files.post",
"data-export.quick.export.post",
"data-export.configuration.post",
"data-export.export.all"
"data-export.export.all",
"data-export.related-users.collection.get"
],
"visible": false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.folio.dataexp.controllers;

import static java.util.Objects.nonNull;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.folio.dataexp.domain.dto.JobExecution;
import org.folio.dataexp.domain.dto.RelatedUser;
import org.folio.dataexp.domain.dto.RelatedUserCollection;
import org.folio.dataexp.domain.entity.JobExecutionEntity;
import org.folio.dataexp.repository.JobExecutionEntityCqlRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.folio.dataexp.rest.resource.RelatedUsersApi;

import java.util.ArrayList;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@Log4j2
@RequestMapping("/data-export")
public class RelatedUsersController implements RelatedUsersApi {

private final JobExecutionEntityCqlRepository jobExecutionEntityCqlRepository;

@Override
public ResponseEntity<RelatedUserCollection> getRelatedUsers() {
log.info("GET related users");
var relatedUsers = jobExecutionEntityCqlRepository.findAll().stream()
.map(JobExecutionEntity::getJobExecution)
.filter(job -> nonNull(job.getRunBy()))
.map(JobExecution::getRunBy)
.map(runBy -> new RelatedUser().userId(runBy.getUserId()).firstName(runBy.getFirstName()).lastName(runBy.getLastName()))
.collect(Collectors.toSet());
log.info("Related users size: {}", relatedUsers.size());
var relatedUserCollection = new RelatedUserCollection().relatedUsers(new ArrayList<>(relatedUsers))
.totalRecords(relatedUsers.size());
return new ResponseEntity<>(relatedUserCollection, HttpStatus.OK);
}
}
25 changes: 25 additions & 0 deletions src/main/resources/swagger.api/data-export.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,29 @@ paths:
content:
text/plain:
example: "Internal server error"
/related-users:
get:
description: Get only users who have ever started export
operationId: getRelatedUsers
responses:
'200':
description: Related user
content:
application/json:
schema:
$ref: '#/components/schemas/relatedUserCollection'
example:
$ref: 'examples/relatedUserCollection.json'
'404':
description: Not Found
content:
text/plain:
example: "Related users not found"
'500':
description: Internal server errors, e.g. due to misconfiguration
content:
text/plain:
example: "Internal server error"
components:
schemas:
uuid:
Expand Down Expand Up @@ -903,6 +926,8 @@ components:
$ref: 'schemas/configuration/configurationEntry.json'
configurationEntryCollection:
$ref: 'schemas/configuration/configurationEntryCollection.json'
relatedUserCollection:
$ref: 'schemas/relatedUserCollection.json'

parameters:
trait_queryable:
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/swagger.api/examples/relatedUser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"firstName": "John",
"lastName": "Doe",
"userId": "6b380aa8-4070-4497-a466-22345fde64c0"
}
10 changes: 10 additions & 0 deletions src/main/resources/swagger.api/examples/relatedUserCollection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"relatedUsers":[
{
"firstName": "John",
"lastName": "Doe",
"userId": "6b380aa8-4070-4497-a466-22345fde64c0"
}
],
"totalRecords": 1
}
19 changes: 19 additions & 0 deletions src/main/resources/swagger.api/schemas/relatedUser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"description": "Related user who have ever started export",
"properties": {
"firstName": {
"description": "First name",
"type": "string"
},
"lastName": {
"description": "Last name",
"type": "string"
},
"userId": {
"description": "User id",
"type": "string"
}
}
}
20 changes: 20 additions & 0 deletions src/main/resources/swagger.api/schemas/relatedUserCollection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Collection of related users",
"type": "object",
"additionalProperties": false,
"properties": {
"relatedUsers": {
"description": "List of related users",
"type": "array",
"id": "relatedUsersList",
"items": {
"type": "object",
"$ref": "relatedUser.json"
}
},
"totalRecords": {
"type": "integer"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.folio.dataexp.controllers;

import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import lombok.SneakyThrows;
import org.folio.dataexp.BaseDataExportInitializer;
import org.folio.dataexp.domain.dto.JobExecution;
import org.folio.dataexp.domain.dto.JobExecutionRunBy;
import org.folio.dataexp.domain.entity.JobExecutionEntity;
import org.folio.dataexp.repository.JobExecutionEntityCqlRepository;
import org.folio.spring.scope.FolioExecutionContextSetter;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.util.List;
import java.util.UUID;

class RelatedUsersControllerTest extends BaseDataExportInitializer {

@Autowired
private JobExecutionEntityCqlRepository jobExecutionEntityCqlRepository;

@Test
@SneakyThrows
void getRelatedUsersTest() {

try (var context = new FolioExecutionContextSetter(folioExecutionContext)) {

var userId = UUID.randomUUID();
var userId2 = UUID.randomUUID();

var entities = List.of(
new JobExecutionEntity().withJobExecution(new JobExecution().runBy(
new JobExecutionRunBy().userId(userId.toString()).firstName("first name").lastName("last name")))
.withId(UUID.randomUUID()),
new JobExecutionEntity().withJobExecution(new JobExecution().runBy(
new JobExecutionRunBy().userId(userId2.toString()).firstName("first name 2").lastName("last name 2")))
.withId(UUID.randomUUID()),
new JobExecutionEntity().withJobExecution(new JobExecution().runBy(
new JobExecutionRunBy().userId(userId.toString()).firstName("first name").lastName("last name")))
.withId(UUID.randomUUID()));

jobExecutionEntityCqlRepository.saveAll(entities);

mockMvc.perform(MockMvcRequestBuilders
.get("/data-export/related-users")
.headers(defaultHeaders())
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json("{\n" +
" \"relatedUsers\": [{\n" +
" \"firstName\": \"first name\",\n" +
" \"lastName\": \"last name\",\n" +
" \"userId\": \"" + userId + "\"\n" +
" }," +
"{\n" +
" \"firstName\": \"first name 2\",\n" +
" \"lastName\": \"last name 2\",\n" +
" \"userId\": \"" + userId2 + "\"\n" +
" }],\n" +
" \"totalRecords\": 2\n" +
"}"));
}
}
}

0 comments on commit 4435df4

Please sign in to comment.