Skip to content
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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

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 ein ConditionalOnBean zum Exporter packen?

Copy link
Contributor Author

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!

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());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.time.ZonedDateTime;
import java.util.Optional;

record TimeClock(Long id, UserId userId, ZonedDateTime startedAt, String comment, boolean isBreak, Optional<ZonedDateTime> stoppedAt) {
public record TimeClock(Long id, UserId userId, ZonedDateTime startedAt, String comment, boolean isBreak, Optional<ZonedDateTime> stoppedAt) {

TimeClock(UserId userId, ZonedDateTime startedAt) {
this(null, userId, startedAt, "", false, Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Optional;

interface TimeClockRepository extends CrudRepository<TimeClockEntity, Long> {

Optional<TimeClockEntity> findByOwnerAndStoppedAtIsNull(String owner);

List<TimeClockEntity> findAllByOwnerOrderByIdAsc(String owner);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;

@Service
class TimeClockService {
public class TimeClockService {

private final TimeClockRepository timeClockRepository;
private final TimeEntryService timeEntryService;
Expand Down Expand Up @@ -60,6 +61,12 @@ void stopTimeClock(UserId userId) {
});
}

public List<TimeClock> findAllTimeClocks(UserId userId) {
return timeClockRepository.findAllByOwnerOrderByIdAsc(userId.value()).stream()
.map(TimeClockService::toTimeClock)
.toList();
}

private static TimeClockEntity toEntity(TimeClock timeClock) {
return TimeClockEntity.builder()
.id(timeClock.id())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.stereotype.Repository;

import java.time.Instant;
import java.util.Collection;
import java.util.List;

@Repository
Expand All @@ -16,4 +17,6 @@ interface TimeEntryRepository extends CrudRepository<TimeEntryEntity, Long> {
List<TimeEntryEntity> findAllByOwnerAndStartGreaterThanEqualAndStartLessThan(String owner, Instant start, Instant endExclusive);

List<TimeEntryEntity> findAllByOwnerIsInAndStartGreaterThanEqualAndStartLessThan(List<String> owners, Instant start, Instant endExclusive);

List<TimeEntryEntity> findAllByOwnerOrderByIdAsc(String owner);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ public interface TimeEntryService {
*/
List<TimeEntry> getEntries(LocalDate from, LocalDate toExclusive, UserId userId);

/**
* {@linkplain TimeEntry}s for the given criteria sorted by {@linkplain TimeEntry#id()}.
*
* @param userId to get {@linkplain TimeEntry}s for
*
* @return sorted list of {@linkplain TimeEntry}s
*/
List<TimeEntry> getEntries(UserId userId);

/**
* {@linkplain TimeEntry}s for all users and the given interval.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public List<TimeEntry> getEntries(LocalDate from, LocalDate toExclusive, UserId
.toList();
}

@Override
public List<TimeEntry> getEntries(UserId userId) {
return timeEntryRepository.findAllByOwnerOrderByIdAsc(userId.value())
.stream()
.map(TimeEntryServiceImpl::toTimeEntry)
.toList();
}

@Override
public Map<UserLocalId, List<TimeEntry>> getEntriesForAllUsers(LocalDate from, LocalDate toExclusive) {

Expand Down