Skip to content

Commit

Permalink
Fix version bug in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
lkleisa committed Apr 12, 2024
1 parent 0ca0812 commit 4683850
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public String getTargetIdByAlignedObjectiveId(Long alignedObjectiveId) {
}

public void createEntity(Objective alignedObjective) {
Alignment alignment = buildAlignmentModel(alignedObjective);
Alignment alignment = buildAlignmentModel(alignedObjective, 0);
alignmentValidationService.validateOnCreate(alignment);
alignmentPersistenceService.save(alignment);
}
Expand All @@ -72,15 +72,16 @@ public void updateEntity(Long objectiveId, Objective objective) {
Alignment savedAlignment = alignmentPersistenceService.findByAlignedObjectiveId(objectiveId);

if (savedAlignment == null) {
Alignment alignment = buildAlignmentModel(objective);
Alignment alignment = buildAlignmentModel(objective, 0);
alignmentValidationService.validateOnCreate(alignment);
alignmentPersistenceService.save(alignment);
} else {
if (objective.getAlignedEntityId() == null) {
alignmentValidationService.validateOnDelete(savedAlignment.getId());
alignmentPersistenceService.deleteById(savedAlignment.getId());
} else {
Alignment alignment = buildAlignmentModel(objective);
Alignment alignment = buildAlignmentModel(objective, savedAlignment.getVersion());

alignment.setId(savedAlignment.getId());
alignmentValidationService.validateOnUpdate(savedAlignment.getId(), alignment);
if (isAlignmentTypeChange(alignment, savedAlignment)) {
Expand All @@ -92,18 +93,17 @@ public void updateEntity(Long objectiveId, Objective objective) {
}
}

public Alignment buildAlignmentModel(Objective alignedObjective) {
public Alignment buildAlignmentModel(Objective alignedObjective, int version) {
if (alignedObjective.getAlignedEntityId().startsWith("O")) {
Objective targetObjective = objectivePersistenceService
.findById(Long.valueOf(alignedObjective.getAlignedEntityId().replace("O", "")));
return ObjectiveAlignment.Builder.builder().withAlignedObjective(alignedObjective)
.withTargetObjective(targetObjective).build();
.withTargetObjective(targetObjective).withVersion(version).build();
} else if (alignedObjective.getAlignedEntityId().startsWith("K")) {
KeyResult targetKeyResult = keyResultPersistenceService
.findById(Long.valueOf(alignedObjective.getAlignedEntityId().replace("K", "")));

return KeyResultAlignment.Builder.builder().withAlignedObjective(alignedObjective)
.withTargetKeyResult(targetKeyResult).build();
.withTargetKeyResult(targetKeyResult).withVersion(version).build();
} else {
throw new OkrResponseStatusException(HttpStatus.BAD_REQUEST, ErrorKey.ATTRIBUTE_NOT_SET,
List.of("alignedEntityId", alignedObjective.getAlignedEntityId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void shouldBuildAlignmentCorrectObjective() {

Alignment returnAlignment = ObjectiveAlignment.Builder.builder().withAlignedObjective(objectiveAlignedObjective)
.withTargetObjective(objective1).build();
Alignment alignment = alignmentBusinessService.buildAlignmentModel(objectiveAlignedObjective);
Alignment alignment = alignmentBusinessService.buildAlignmentModel(objectiveAlignedObjective, 0);

assertEquals(returnAlignment, alignment);
assertTrue(alignment instanceof ObjectiveAlignment);
Expand All @@ -180,7 +180,7 @@ void shouldBuildAlignmentCorrectKeyResult() {

Alignment returnAlignment = KeyResultAlignment.Builder.builder().withAlignedObjective(keyResultAlignedObjective)
.withTargetKeyResult(metricKeyResult).build();
Alignment alignment = alignmentBusinessService.buildAlignmentModel(keyResultAlignedObjective);
Alignment alignment = alignmentBusinessService.buildAlignmentModel(keyResultAlignedObjective, 0);

assertEquals(returnAlignment, alignment);
assertTrue(alignment instanceof KeyResultAlignment);
Expand All @@ -189,7 +189,7 @@ void shouldBuildAlignmentCorrectKeyResult() {
@Test
void shouldThrowErrorWhenAlignedEntityIdIsIncorrect() {
OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class,
() -> alignmentBusinessService.buildAlignmentModel(wrongAlignedObjective));
() -> alignmentBusinessService.buildAlignmentModel(wrongAlignedObjective, 0));

List<ErrorDto> expectedErrors = List.of(new ErrorDto("ATTRIBUTE_NOT_SET", List.of("alignedEntityId", "Hello")));

Expand Down

0 comments on commit 4683850

Please sign in to comment.