-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tenant Exporter (für 1.6.2 Schema) #725
Open
grafjo
wants to merge
2
commits into
main
Choose a base branch
from
export-poc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
src/main/java/de/focusshift/zeiterfassung/export/TenantExportComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package de.focusshift.zeiterfassung.export; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import de.focusshift.zeiterfassung.security.SecurityRole; | ||
import de.focusshift.zeiterfassung.tenancy.configuration.single.SingleTenantConfigurationProperties; | ||
import de.focusshift.zeiterfassung.tenancy.user.TenantUser; | ||
import de.focusshift.zeiterfassung.tenancy.user.TenantUserService; | ||
import de.focusshift.zeiterfassung.timeclock.TimeClock; | ||
import de.focusshift.zeiterfassung.timeclock.TimeClockService; | ||
import de.focusshift.zeiterfassung.timeentry.TimeEntry; | ||
import de.focusshift.zeiterfassung.timeentry.TimeEntryService; | ||
import de.focusshift.zeiterfassung.user.UserId; | ||
import de.focusshift.zeiterfassung.usermanagement.OvertimeAccount; | ||
import de.focusshift.zeiterfassung.usermanagement.OvertimeAccountService; | ||
import de.focusshift.zeiterfassung.usermanagement.UserLocalId; | ||
import de.focusshift.zeiterfassung.usermanagement.WorkDay; | ||
import de.focusshift.zeiterfassung.usermanagement.WorkingTime; | ||
import de.focusshift.zeiterfassung.usermanagement.WorkingTimeService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.event.ApplicationStartedEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@ConditionalOnProperty(prefix = "zeiterfassung.tenant.export", name = "enabled", havingValue = "true") | ||
@Component | ||
public class TenantExportComponent { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(TenantExportComponent.class); | ||
|
||
private final String tenantId; | ||
private final TenantUserService tenantUserService; | ||
private final OvertimeAccountService overtimeAccountService; | ||
private final TimeClockService timeClockService; | ||
private final TimeEntryService timeEntryService; | ||
private final WorkingTimeService workingTimeService; | ||
private final ObjectMapper objectMapper; | ||
|
||
public TenantExportComponent(SingleTenantConfigurationProperties singleTenantConfigurationProperties, TenantUserService tenantUserService, OvertimeAccountService overtimeAccountService, TimeClockService timeClockService, TimeEntryService timeEntryService, WorkingTimeService workingTimeService, ObjectMapper objectMapper) { | ||
this.tenantId = singleTenantConfigurationProperties.getDefaultTenantId(); | ||
this.tenantUserService = tenantUserService; | ||
this.overtimeAccountService = overtimeAccountService; | ||
this.timeClockService = timeClockService; | ||
this.timeEntryService = timeEntryService; | ||
this.workingTimeService = workingTimeService; | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Async | ||
@EventListener(ApplicationStartedEvent.class) | ||
void runExport() { | ||
LOG.info("Running export for tenant '{}'", tenantId); | ||
|
||
List<UserExport> exports = tenantUserService.findAllUsers() | ||
.stream() | ||
.map(user -> { | ||
LOG.info("Exporting user localId={}, externalId={}", user.localId(), user.id()); | ||
|
||
final UserLocalId internalUserId = new UserLocalId(user.localId()); | ||
final UserId externalUserId = new UserId(user.id()); | ||
|
||
OvertimeAccount overtimeAccount = overtimeAccountService.getOvertimeAccount(internalUserId); | ||
WorkingTime workingTime = workingTimeService.getWorkingTimeByUser(internalUserId); | ||
List<TimeClock> timeClocks = timeClockService.findAllTimeClocks(externalUserId); | ||
List<TimeEntry> timeEntries = timeEntryService.getEntries(externalUserId); | ||
|
||
return UserExport.from(user, overtimeAccount, workingTime, timeClocks, timeEntries); | ||
}) | ||
.toList(); | ||
|
||
toJson(exports); | ||
|
||
LOG.info("Finished export for tenant '{}'", tenantId); | ||
} | ||
|
||
void toJson(List<UserExport> userExports) { | ||
try (FileOutputStream fos = new FileOutputStream(calcFileName())) { | ||
objectMapper.writeValue(fos, Map.of( | ||
"tenantId", tenantId, | ||
"exportedAt", Instant.now(), | ||
"users", userExports) | ||
); | ||
} catch (IOException e) { | ||
LOG.warn("skipping export - something went wrong exporting as json!", e); | ||
} | ||
} | ||
|
||
private File calcFileName() { | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"); | ||
String dateTimeString = LocalDateTime.now().format(formatter); | ||
String filename = "export_%s-%s.json".formatted(this.tenantId, dateTimeString); | ||
Path filePath = Paths.get(System.getProperty("java.io.tmpdir")).resolve(filename); | ||
LOG.info("Exporting to file '{}'", filePath.toAbsolutePath()); | ||
return filePath.toFile(); | ||
} | ||
|
||
record UserExport(UserDTO user, OvertimeAccountDTO overtimeAccount, WorkingTimeDTO workingTime, | ||
List<TimeClockDTO> timeClocks, List<TimeEntryDTO> timeEntries) { | ||
static UserExport from(TenantUser tenantUser, OvertimeAccount overtimeAccount, WorkingTime workingTime, List<TimeClock> timeClocks, List<TimeEntry> timeEntries) { | ||
return new UserExport( | ||
UserDTO.from(tenantUser), | ||
OvertimeAccountDTO.from(overtimeAccount), | ||
WorkingTimeDTO.from(workingTime), | ||
timeClocks.stream().map(TimeClockDTO::from).toList(), | ||
timeEntries.stream().map(TimeEntryDTO::from).toList() | ||
); | ||
} | ||
} | ||
|
||
record UserDTO(String externalId, String givenName, String familyName, String eMail, Instant firstLoginAt, | ||
Set<String> authorities) { | ||
static UserDTO from(TenantUser tenantUser) { | ||
return new UserDTO(tenantUser.id(), tenantUser.givenName(), tenantUser.familyName(), tenantUser.eMail().value(), tenantUser.firstLoginAt(), tenantUser.authorities().stream().map(SecurityRole::name).collect(Collectors.toSet())); | ||
} | ||
} | ||
|
||
record OvertimeAccountDTO(boolean allowed, Duration maxAllowedOvertime) { | ||
static OvertimeAccountDTO from(OvertimeAccount overtimeAccount) { | ||
return new OvertimeAccountDTO(overtimeAccount.isAllowed(), overtimeAccount.getMaxAllowedOvertime().orElse(null)); | ||
} | ||
} | ||
|
||
record WorkingTimeDTO(WorkDayDTO monday, WorkDayDTO tuesday, WorkDayDTO wednesday, WorkDayDTO thursday, | ||
WorkDayDTO friday, WorkDayDTO saturday, WorkDayDTO sunday) { | ||
static WorkingTimeDTO from(WorkingTime workingTime) { | ||
return new WorkingTimeDTO( | ||
WorkDayDTO.from(workingTime.getMonday()), | ||
WorkDayDTO.from(workingTime.getTuesday()), | ||
WorkDayDTO.from(workingTime.getWednesday()), | ||
WorkDayDTO.from(workingTime.getThursday()), | ||
WorkDayDTO.from(workingTime.getFriday()), | ||
WorkDayDTO.from(workingTime.getSaturday()), | ||
WorkDayDTO.from(workingTime.getSunday()) | ||
); | ||
} | ||
} | ||
|
||
record WorkDayDTO(String dayOfWeek, Duration duration) { | ||
static WorkDayDTO from(Optional<WorkDay> workDay) { | ||
return workDay.map(item -> new WorkDayDTO(item.dayOfWeek().name(), item.duration())).orElseGet(null); | ||
} | ||
} | ||
|
||
record TimeClockDTO(ZonedDateTime startedAt, String comment, boolean isBreak, | ||
Optional<ZonedDateTime> stoppedAt) { | ||
static TimeClockDTO from(TimeClock timeClock) { | ||
return new TimeClockDTO(timeClock.startedAt(), timeClock.comment(), timeClock.isBreak(), timeClock.stoppedAt()); | ||
} | ||
} | ||
|
||
record TimeEntryDTO(String comment, ZonedDateTime start, ZonedDateTime end, boolean isBreak) { | ||
static TimeEntryDTO from(TimeEntry timeEntry) { | ||
return new TimeEntryDTO(timeEntry.comment(), timeEntry.start(), timeEntry.end(), timeEntry.isBreak()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wenn das hier von der
SingleTenantConfigurationProperties
abhängt, wollen wir dann noch einConditionalOnBean
zum Exporter packen?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep hört sich gut an!