Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
lkleisa committed Jun 6, 2024
1 parent b34d987 commit 2ef92c9
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ public boolean isAlignmentTypeChange(Alignment alignment, Alignment savedAlignme
|| (alignment instanceof KeyResultAlignment && savedAlignment instanceof ObjectiveAlignment);
}

public void updateKeyResultIdOnIdChange(Long oldId, KeyResult keyResult) {
List<KeyResultAlignment> alignments = alignmentPersistenceService.findByKeyResultAlignmentId(oldId);
alignments.forEach(alignment -> {
public void updateKeyResultIdOnIdChange(Long oldKeyResultId, KeyResult keyResult) {
List<KeyResultAlignment> keyResultAlignmentList = alignmentPersistenceService
.findByKeyResultAlignmentId(oldKeyResultId);
keyResultAlignmentList.forEach(alignment -> {
alignment.setAlignmentTarget(keyResult);
alignmentValidationService.validateOnUpdate(alignment.getId(), alignment);
alignmentPersistenceService.save(alignment);
Expand All @@ -114,16 +115,18 @@ public void deleteAlignmentByObjectiveId(Long objectiveId) {
alignmentValidationService.validateOnDelete(alignment.getId());
alignmentPersistenceService.deleteById(alignment.getId());
}
List<ObjectiveAlignment> alignmentList = alignmentPersistenceService.findByObjectiveAlignmentId(objectiveId);
alignmentList.forEach(objectiveAlignment -> {
List<ObjectiveAlignment> objectiveAlignmentList = alignmentPersistenceService
.findByObjectiveAlignmentId(objectiveId);
objectiveAlignmentList.forEach(objectiveAlignment -> {
alignmentValidationService.validateOnDelete(objectiveAlignment.getId());
alignmentPersistenceService.deleteById(objectiveAlignment.getId());
});
}

public void deleteAlignmentByKeyResultId(Long keyResultId) {
List<KeyResultAlignment> alignmentList = alignmentPersistenceService.findByKeyResultAlignmentId(keyResultId);
alignmentList.forEach(keyResultAlignment -> {
List<KeyResultAlignment> keyResultAlignmentList = alignmentPersistenceService
.findByKeyResultAlignmentId(keyResultId);
keyResultAlignmentList.forEach(keyResultAlignment -> {
alignmentValidationService.validateOnDelete(keyResultAlignment.getId());
alignmentPersistenceService.deleteById(keyResultAlignment.getId());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ public List<AlignmentDto> getAlignmentPossibilities(Long quarterId) {
.filter(objective -> objective.getTeam().equals(team)).toList().stream()
.sorted(Comparator.comparing(Objective::getTitle)).toList();

List<AlignmentObjectDto> alignmentObjectDtos = new ArrayList<>();
List<AlignmentObjectDto> alignmentObjectDtoList = new ArrayList<>();

filteredObjectiveList.forEach(objective -> {
AlignmentObjectDto objectiveDto = new AlignmentObjectDto(objective.getId(),
"O - " + objective.getTitle(), "objective");
alignmentObjectDtos.add(objectiveDto);
alignmentObjectDtoList.add(objectiveDto);

List<KeyResult> keyResults = keyResultBusinessService.getAllKeyResultsByObjective(objective.getId())
List<KeyResult> keyResultList = keyResultBusinessService.getAllKeyResultsByObjective(objective.getId())
.stream().sorted(Comparator.comparing(KeyResult::getTitle)).toList();

keyResults.forEach(keyResult -> {
keyResultList.forEach(keyResult -> {
AlignmentObjectDto keyResultDto = new AlignmentObjectDto(keyResult.getId(),
"KR - " + keyResult.getTitle(), "keyResult");
alignmentObjectDtos.add(keyResultDto);
alignmentObjectDtoList.add(keyResultDto);
});
});
AlignmentDto alignmentDto = new AlignmentDto(team.getId(), team.getName(), alignmentObjectDtos);
AlignmentDto alignmentDto = new AlignmentDto(team.getId(), team.getName(), alignmentObjectDtoList);
alignmentDtoList.add(alignmentDto);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ public AlignmentValidationService(AlignmentPersistenceService alignmentPersisten
public void validateOnCreate(Alignment model) {
throwExceptionWhenModelIsNull(model);
throwExceptionWhenIdIsNotNull(model.getId());
throwExceptionWhenAlignedObjectIsNull(model);
throwExceptionWhenAlignmentObjectIsNull(model);
throwExceptionWhenAlignedIdIsSameAsTargetId(model);
throwExceptionWhenAlignmentIsInSameTeam(model);
throwExceptionWhenAlignedObjectiveAlreadyExists(model);
throwExceptionWhenAlignmentWithAlignedObjectiveAlreadyExists(model);
validate(model);
}

@Override
public void validateOnUpdate(Long id, Alignment model) {
throwExceptionWhenModelIsNull(model);
throwExceptionWhenIdIsNull(model.getId());
throwExceptionWhenAlignedObjectIsNull(model);
throwExceptionWhenAlignmentObjectIsNull(model);
throwExceptionWhenAlignedIdIsSameAsTargetId(model);
throwExceptionWhenAlignmentIsInSameTeam(model);
validate(model);
}

private void throwExceptionWhenAlignedObjectIsNull(Alignment model) {
private void throwExceptionWhenAlignmentObjectIsNull(Alignment model) {
if (model.getAlignedObjective() == null) {
throw new OkrResponseStatusException(HttpStatus.BAD_REQUEST, ErrorKey.ATTRIBUTE_NULL,
List.of("alignedObjectiveId"));
Expand All @@ -68,19 +68,20 @@ private void throwExceptionWhenAlignedObjectIsNull(Alignment model) {
}

private void throwExceptionWhenAlignmentIsInSameTeam(Alignment model) {
Team alignedTeam = teamPersistenceService.findById(model.getAlignedObjective().getTeam().getId());
Team targetTeam = null;
Team alignedObjectiveTeam = teamPersistenceService.findById(model.getAlignedObjective().getTeam().getId());
Team targetObjectTeam = null;

if (model instanceof ObjectiveAlignment objectiveAlignment) {
targetTeam = teamPersistenceService.findById(objectiveAlignment.getAlignmentTarget().getTeam().getId());
targetObjectTeam = teamPersistenceService
.findById(objectiveAlignment.getAlignmentTarget().getTeam().getId());
} else if (model instanceof KeyResultAlignment keyResultAlignment) {
targetTeam = teamPersistenceService
targetObjectTeam = teamPersistenceService
.findById(keyResultAlignment.getAlignmentTarget().getObjective().getTeam().getId());
}

if (alignedTeam.equals(targetTeam)) {
if (alignedObjectiveTeam.equals(targetObjectTeam)) {
throw new OkrResponseStatusException(HttpStatus.BAD_REQUEST, ErrorKey.NOT_LINK_IN_SAME_TEAM,
List.of("teamId", targetTeam.getId()));
List.of("teamId", targetObjectTeam.getId()));
}
}

Expand All @@ -94,7 +95,7 @@ private void throwExceptionWhenAlignedIdIsSameAsTargetId(Alignment model) {
}
}

private void throwExceptionWhenAlignedObjectiveAlreadyExists(Alignment model) {
private void throwExceptionWhenAlignmentWithAlignedObjectiveAlreadyExists(Alignment model) {
if (this.alignmentPersistenceService.findByAlignedObjectiveId(model.getAlignedObjective().getId()) != null) {
throw new OkrResponseStatusException(HttpStatus.BAD_REQUEST, ErrorKey.ALIGNMENT_ALREADY_EXISTS,
List.of("alignedObjectiveId", model.getAlignedObjective().getId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,22 @@
<div class="d-flex flex-column gap-2 col-6">
<label class="text-black">Bezug (optional)</label>
<input
#input
#alignmentInput
type="text"
formControlName="alignment"
class="alignment-input"
placeholder="{{
(this.filteredOptions$ | async)?.length == 0 ? 'Kein Alignment vorhanden' : 'Bezug wählen'
(this.filteredAlignmentOptions$ | async)?.length == 0 ? 'Kein Alignment vorhanden' : 'Bezug wählen'
}}"
[matAutocomplete]="auto"
[attr.data-testId]="'alignmentInput'"
(input)="filter()"
(focus)="filter(); input.select()"
(focus)="filter(); alignmentInput.select()"
(focusout)="scrollLeft()"
[value]="displayedValue"
/>
<mat-autocomplete requireSelection #auto="matAutocomplete" [displayWith]="displayWith">
@for (group of filteredOptions$ | async; track group) {
@for (group of filteredAlignmentOptions$ | async; track group) {
<mat-optgroup [label]="group.teamName" style="font-weight: bold">
@for (alignmentObject of group.alignmentObjectDtos; track alignmentObject) {
<mat-option [value]="alignmentObject">{{ alignmentObject.objectTitle }}</mat-option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ describe('ObjectiveDialogComponent', () => {
});

expect(alignmentPossibilities).toStrictEqual([alignmentPossibility1, alignmentPossibility2]);
expect(component.filteredOptions$.getValue()).toEqual([alignmentPossibility1, alignmentPossibility2]);
expect(component.filteredAlignmentOptions$.getValue()).toEqual([alignmentPossibility1, alignmentPossibility2]);
expect(component.objectiveForm.getRawValue().alignment).toEqual(null);
});

Expand All @@ -522,15 +522,15 @@ describe('ObjectiveDialogComponent', () => {
});

expect(alignmentPossibilities).toStrictEqual([alignmentPossibility2]);
expect(component.filteredOptions$.getValue()).toEqual([alignmentPossibility2]);
expect(component.filteredAlignmentOptions$.getValue()).toEqual([alignmentPossibility2]);
expect(component.objectiveForm.getRawValue().alignment).toEqual(null);
});

it('should return team and objective with same text in alignment possibilities', async () => {
component.input.nativeElement.value = 'puzzle';
component.alignmentInput.nativeElement.value = 'puzzle';
component.alignmentPossibilities$ = of([alignmentPossibility1, alignmentPossibility2]);
component.filter();
expect(component.filteredOptions$.getValue()).toEqual([alignmentPossibility1, alignmentPossibility2]);
expect(component.filteredAlignmentOptions$.getValue()).toEqual([alignmentPossibility1, alignmentPossibility2]);
});

it('should load existing objective alignment to objectiveForm', async () => {
Expand All @@ -542,7 +542,7 @@ describe('ObjectiveDialogComponent', () => {
});

expect(alignmentPossibilities).toStrictEqual([alignmentPossibility1, alignmentPossibility2]);
expect(component.filteredOptions$.getValue()).toEqual([alignmentPossibility1, alignmentPossibility2]);
expect(component.filteredAlignmentOptions$.getValue()).toEqual([alignmentPossibility1, alignmentPossibility2]);
expect(component.objectiveForm.getRawValue().alignment).toEqual(alignmentObject2);
});

Expand All @@ -556,35 +556,35 @@ describe('ObjectiveDialogComponent', () => {
});

expect(alignmentPossibilities).toStrictEqual([alignmentPossibility1, alignmentPossibility2]);
expect(component.filteredOptions$.getValue()).toEqual([alignmentPossibility1, alignmentPossibility2]);
expect(component.filteredAlignmentOptions$.getValue()).toEqual([alignmentPossibility1, alignmentPossibility2]);
expect(component.objectiveForm.getRawValue().alignment).toEqual(alignmentObject3);
});

it('should filter correct alignment possibilities', async () => {
// Search for one title
component.input.nativeElement.value = 'palm';
component.alignmentInput.nativeElement.value = 'palm';
component.alignmentPossibilities$ = of([alignmentPossibility1, alignmentPossibility2]);
component.filter();
let modifiedAlignmentPossibility: AlignmentPossibility = {
teamId: 1,
teamName: 'Puzzle ITC',
alignmentObjectDtos: [alignmentObject3],
};
expect(component.filteredOptions$.getValue()).toEqual([modifiedAlignmentPossibility]);
expect(component.filteredAlignmentOptions$.getValue()).toEqual([modifiedAlignmentPossibility]);

// Search for team name
component.input.nativeElement.value = 'Puzzle IT';
component.alignmentInput.nativeElement.value = 'Puzzle IT';
component.alignmentPossibilities$ = of([alignmentPossibility1, alignmentPossibility2]);
component.filter();
modifiedAlignmentPossibility = {
teamId: 1,
teamName: 'Puzzle ITC',
alignmentObjectDtos: [alignmentObject2, alignmentObject3],
};
expect(component.filteredOptions$.getValue()).toEqual([modifiedAlignmentPossibility]);
expect(component.filteredAlignmentOptions$.getValue()).toEqual([modifiedAlignmentPossibility]);

// Search for two objects
component.input.nativeElement.value = 'buy';
component.alignmentInput.nativeElement.value = 'buy';
component.alignmentPossibilities$ = of([alignmentPossibility1, alignmentPossibility2]);
component.filter();
let modifiedAlignmentPossibilities = [
Expand All @@ -599,18 +599,18 @@ describe('ObjectiveDialogComponent', () => {
alignmentObjectDtos: [alignmentObject1],
},
];
expect(component.filteredOptions$.getValue()).toEqual(modifiedAlignmentPossibilities);
expect(component.filteredAlignmentOptions$.getValue()).toEqual(modifiedAlignmentPossibilities);

// No match
component.input.nativeElement.value = 'findus';
component.alignmentInput.nativeElement.value = 'findus';
component.alignmentPossibilities$ = of([alignmentPossibility1, alignmentPossibility2]);
component.filter();
expect(component.filteredOptions$.getValue()).toEqual([]);
expect(component.filteredAlignmentOptions$.getValue()).toEqual([]);
});

it('should find correct alignment object', () => {
// objective
let alignmentObject = component.findAlignmentObject(
let alignmentObject = component.findAlignmentPossibilityObject(
[alignmentPossibility1, alignmentPossibility2],
1,
'objective',
Expand All @@ -619,38 +619,46 @@ describe('ObjectiveDialogComponent', () => {
expect(alignmentObject!.objectTitle).toEqual('We want to increase the income puzzle buy');

// keyResult
alignmentObject = component.findAlignmentObject([alignmentPossibility1, alignmentPossibility2], 1, 'keyResult');
alignmentObject = component.findAlignmentPossibilityObject(
[alignmentPossibility1, alignmentPossibility2],
1,
'keyResult',
);
expect(alignmentObject!.objectId).toEqual(1);
expect(alignmentObject!.objectTitle).toEqual('We buy 3 palms');

// no match
alignmentObject = component.findAlignmentObject([alignmentPossibility1, alignmentPossibility2], 133, 'keyResult');
alignmentObject = component.findAlignmentPossibilityObject(
[alignmentPossibility1, alignmentPossibility2],
133,
'keyResult',
);
expect(alignmentObject).toEqual(null);
});

it('should display kein alignment vorhanden when no alignment possibility', () => {
component.filteredOptions$.next([alignmentPossibility1, alignmentPossibility2]);
component.filteredAlignmentOptions$.next([alignmentPossibility1, alignmentPossibility2]);
fixture.detectChanges();
expect(component.input.nativeElement.getAttribute('placeholder')).toEqual('Bezug wählen');
expect(component.alignmentInput.nativeElement.getAttribute('placeholder')).toEqual('Bezug wählen');

component.filteredOptions$.next([]);
component.filteredAlignmentOptions$.next([]);
fixture.detectChanges();
expect(component.input.nativeElement.getAttribute('placeholder')).toEqual('Kein Alignment vorhanden');
expect(component.alignmentInput.nativeElement.getAttribute('placeholder')).toEqual('Kein Alignment vorhanden');
});

it('should update alignments on quarter change', () => {
objectiveService.getAlignmentPossibilities.mockReturnValue(of([alignmentPossibility1, alignmentPossibility2]));
component.updateAlignments();
expect(component.input.nativeElement.value).toEqual('');
expect(component.alignmentInput.nativeElement.value).toEqual('');
expect(component.objectiveForm.getRawValue().alignment).toEqual(null);
expect(objectiveService.getAlignmentPossibilities).toHaveBeenCalled();
});

it('should return correct displayedValue', () => {
component.input.nativeElement.value = 'O - Objective 1';
component.alignmentInput.nativeElement.value = 'O - Objective 1';
expect(component.displayedValue).toEqual('O - Objective 1');

component.input = new ElementRef(document.createElement('input'));
component.alignmentInput = new ElementRef(document.createElement('input'));
expect(component.displayedValue).toEqual('');
});
});
Expand Down
Loading

0 comments on commit 2ef92c9

Please sign in to comment.