-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/epic 3 budget import #15
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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
This file contains hidden or 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
23 changes: 23 additions & 0 deletions
23
apps/api/src/main/java/com/upkeep/application/port/in/budget/GetBudgetSummaryUseCase.java
This file contains hidden or 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,23 @@ | ||
| package com.upkeep.application.port.in.budget; | ||
|
|
||
| public interface GetBudgetSummaryUseCase { | ||
|
|
||
| BudgetSummary execute(String companyId); | ||
|
|
||
| record BudgetSummary( | ||
| String budgetId, | ||
| long totalCents, | ||
| long allocatedCents, | ||
| long remainingCents, | ||
| String currency, | ||
| boolean exists | ||
| ) { | ||
| public static BudgetSummary empty() { | ||
| return new BudgetSummary(null, 0, 0, 0, "EUR", false); | ||
| } | ||
|
|
||
| public static BudgetSummary of(String budgetId, long totalCents, String currency) { | ||
| return new BudgetSummary(budgetId, totalCents, 0, totalCents, currency, true); | ||
| } | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
apps/api/src/main/java/com/upkeep/application/port/in/budget/SetCompanyBudgetUseCase.java
This file contains hidden or 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,21 @@ | ||
| package com.upkeep.application.port.in.budget; | ||
|
|
||
| import com.upkeep.domain.model.budget.Currency; | ||
|
|
||
| public interface SetCompanyBudgetUseCase { | ||
|
|
||
| SetBudgetResult execute(SetBudgetCommand command); | ||
|
|
||
| record SetBudgetCommand( | ||
| String companyId, | ||
| String actorUserId, | ||
| long amountCents, | ||
| Currency currency | ||
| ) {} | ||
|
|
||
| record SetBudgetResult( | ||
| String budgetId, | ||
| long amountCents, | ||
| String currency | ||
| ) {} | ||
| } |
12 changes: 12 additions & 0 deletions
12
apps/api/src/main/java/com/upkeep/application/port/out/audit/AuditEventRepository.java
This file contains hidden or 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,12 @@ | ||
| package com.upkeep.application.port.out.audit; | ||
|
|
||
| import com.upkeep.domain.model.audit.AuditEvent; | ||
| import com.upkeep.domain.model.audit.AuditEventId; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface AuditEventRepository { | ||
| void save(AuditEvent auditEvent); | ||
|
|
||
| Optional<AuditEvent> findById(AuditEventId id); | ||
| } |
20 changes: 20 additions & 0 deletions
20
apps/api/src/main/java/com/upkeep/application/port/out/budget/BudgetRepository.java
This file contains hidden or 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,20 @@ | ||
| package com.upkeep.application.port.out.budget; | ||
|
|
||
| import com.upkeep.domain.model.budget.Budget; | ||
| import com.upkeep.domain.model.budget.BudgetId; | ||
| import com.upkeep.domain.model.company.CompanyId; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.Optional; | ||
|
|
||
| public interface BudgetRepository { | ||
| void save(Budget budget); | ||
|
|
||
| Optional<Budget> findById(BudgetId id); | ||
|
|
||
| Optional<Budget> findByCompanyId(CompanyId companyId); | ||
|
|
||
| Optional<Budget> findByCompanyIdAndEffectiveFrom(CompanyId companyId, Instant effectiveFrom); | ||
|
|
||
| boolean existsByCompanyId(CompanyId companyId); | ||
| } |
31 changes: 31 additions & 0 deletions
31
apps/api/src/main/java/com/upkeep/application/usecase/GetBudgetSummaryUseCaseImpl.java
This file contains hidden or 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,31 @@ | ||
| package com.upkeep.application.usecase; | ||
|
|
||
| import com.upkeep.application.port.in.budget.GetBudgetSummaryUseCase; | ||
| import com.upkeep.application.port.out.budget.BudgetRepository; | ||
| import com.upkeep.domain.model.company.CompanyId; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| @ApplicationScoped | ||
| public class GetBudgetSummaryUseCaseImpl implements GetBudgetSummaryUseCase { | ||
|
|
||
| private final BudgetRepository budgetRepository; | ||
|
|
||
| @Inject | ||
| public GetBudgetSummaryUseCaseImpl(BudgetRepository budgetRepository) { | ||
| this.budgetRepository = budgetRepository; | ||
| } | ||
|
|
||
| @Override | ||
| public BudgetSummary execute(String companyId) { | ||
| CompanyId id = CompanyId.from(companyId); | ||
|
|
||
| return budgetRepository.findByCompanyId(id) | ||
| .map(budget -> BudgetSummary.of( | ||
| budget.getId().toString(), | ||
| budget.getAmount().amountCents(), | ||
| budget.getAmount().currency().name() | ||
| )) | ||
| .orElse(BudgetSummary.empty()); | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
apps/api/src/main/java/com/upkeep/application/usecase/SetCompanyBudgetUseCaseImpl.java
This file contains hidden or 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,76 @@ | ||
| package com.upkeep.application.usecase; | ||
|
|
||
| import com.upkeep.application.port.in.budget.SetCompanyBudgetUseCase; | ||
| import com.upkeep.application.port.out.audit.AuditEventRepository; | ||
| import com.upkeep.application.port.out.budget.BudgetRepository; | ||
| import com.upkeep.application.port.out.membership.MembershipRepository; | ||
| import com.upkeep.domain.exception.BudgetAlreadyExistsException; | ||
| import com.upkeep.domain.exception.MembershipNotFoundException; | ||
| import com.upkeep.domain.exception.UnauthorizedOperationException; | ||
| import com.upkeep.domain.model.audit.AuditEvent; | ||
| import com.upkeep.domain.model.budget.Budget; | ||
| import com.upkeep.domain.model.budget.Money; | ||
| import com.upkeep.domain.model.company.CompanyId; | ||
| import com.upkeep.domain.model.customer.CustomerId; | ||
| import com.upkeep.domain.model.membership.Membership; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.inject.Inject; | ||
| import jakarta.transaction.Transactional; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.YearMonth; | ||
| import java.time.ZoneOffset; | ||
|
|
||
| @ApplicationScoped | ||
| public class SetCompanyBudgetUseCaseImpl implements SetCompanyBudgetUseCase { | ||
|
|
||
| private final BudgetRepository budgetRepository; | ||
| private final AuditEventRepository auditEventRepository; | ||
| private final MembershipRepository membershipRepository; | ||
|
|
||
| @Inject | ||
| public SetCompanyBudgetUseCaseImpl(BudgetRepository budgetRepository, | ||
| AuditEventRepository auditEventRepository, | ||
| MembershipRepository membershipRepository) { | ||
| this.budgetRepository = budgetRepository; | ||
| this.auditEventRepository = auditEventRepository; | ||
| this.membershipRepository = membershipRepository; | ||
| } | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public SetBudgetResult execute(SetBudgetCommand command) { | ||
| CompanyId companyId = CompanyId.from(command.companyId()); | ||
| CustomerId actorId = CustomerId.from(command.actorUserId()); | ||
|
|
||
| Membership membership = membershipRepository.findByCustomerIdAndCompanyId(actorId, companyId) | ||
| .orElseThrow(() -> new MembershipNotFoundException(command.actorUserId(), command.companyId())); | ||
|
|
||
| if (!membership.isOwner()) { | ||
| throw new UnauthorizedOperationException("Only owners can set the company budget"); | ||
| } | ||
|
|
||
| // Check if a budget already exists for the current month | ||
| Instant currentMonthStart = YearMonth.now() | ||
| .atDay(1) | ||
| .atStartOfDay(ZoneOffset.UTC) | ||
| .toInstant(); | ||
|
|
||
| if (budgetRepository.findByCompanyIdAndEffectiveFrom(companyId, currentMonthStart).isPresent()) { | ||
| throw new BudgetAlreadyExistsException(command.companyId()); | ||
| } | ||
|
|
||
| Money amount = new Money(command.amountCents(), command.currency()); | ||
| Budget budget = Budget.create(companyId, amount); | ||
| budgetRepository.save(budget); | ||
|
|
||
| AuditEvent event = AuditEvent.budgetCreated(companyId, actorId, budget); | ||
| auditEventRepository.save(event); | ||
|
|
||
| return new SetBudgetResult( | ||
| budget.getId().toString(), | ||
| amount.amountCents(), | ||
| amount.currency().name() | ||
| ); | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
apps/api/src/main/java/com/upkeep/domain/exception/BudgetAlreadyExistsException.java
This file contains hidden or 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,18 @@ | ||
| package com.upkeep.domain.exception; | ||
|
|
||
| /** | ||
| * Thrown when attempting to set a monthly budget for a company that already has one for the current month. | ||
| */ | ||
| public class BudgetAlreadyExistsException extends DomainException { | ||
|
|
||
| private final String companyId; | ||
|
|
||
| public BudgetAlreadyExistsException(String companyId) { | ||
| super("A budget already exists for this company for the current month"); | ||
| this.companyId = companyId; | ||
| } | ||
|
|
||
| public String getCompanyId() { | ||
| return companyId; | ||
| } | ||
| } |
120 changes: 120 additions & 0 deletions
120
apps/api/src/main/java/com/upkeep/domain/model/audit/AuditEvent.java
This file contains hidden or 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,120 @@ | ||
| package com.upkeep.domain.model.audit; | ||
|
|
||
| import com.upkeep.domain.model.budget.Budget; | ||
| import com.upkeep.domain.model.company.CompanyId; | ||
| import com.upkeep.domain.model.customer.CustomerId; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Audit event tracking all important actions in the system for compliance and transparency (FR37). | ||
| */ | ||
| public class AuditEvent { | ||
| private final AuditEventId id; | ||
| private final CompanyId companyId; | ||
| private final AuditEventType eventType; | ||
| private final CustomerId actorId; | ||
| private final String targetType; | ||
| private final String targetId; | ||
| private final Map<String, Object> payload; | ||
| private final Instant timestamp; | ||
|
|
||
| private AuditEvent(AuditEventId id, | ||
| CompanyId companyId, | ||
| AuditEventType eventType, | ||
| CustomerId actorId, | ||
| String targetType, | ||
| String targetId, | ||
| Map<String, Object> payload, | ||
| Instant timestamp) { | ||
| this.id = id; | ||
| this.companyId = companyId; | ||
| this.eventType = eventType; | ||
| this.actorId = actorId; | ||
| this.targetType = targetType; | ||
| this.targetId = targetId; | ||
| this.payload = new HashMap<>(payload); | ||
| this.timestamp = timestamp; | ||
| } | ||
|
|
||
| public static AuditEvent budgetCreated(CompanyId companyId, CustomerId actorId, Budget budget) { | ||
| Map<String, Object> payload = new HashMap<>(); | ||
| payload.put("amountCents", budget.getAmount().amountCents()); | ||
| payload.put("currency", budget.getAmount().currency().name()); | ||
| payload.put("effectiveFrom", budget.getEffectiveFrom().toString()); | ||
|
|
||
| return new AuditEvent( | ||
| AuditEventId.generate(), | ||
| companyId, | ||
| AuditEventType.BUDGET_CREATED, | ||
| actorId, | ||
| "Budget", | ||
| budget.getId().toString(), | ||
| payload, | ||
| Instant.now() | ||
| ); | ||
| } | ||
|
|
||
| public static AuditEvent budgetUpdated(CompanyId companyId, CustomerId actorId, Budget budget, long previousAmountCents) { | ||
| Map<String, Object> payload = new HashMap<>(); | ||
| payload.put("previousAmountCents", previousAmountCents); | ||
| payload.put("newAmountCents", budget.getAmount().amountCents()); | ||
| payload.put("currency", budget.getAmount().currency().name()); | ||
|
|
||
| return new AuditEvent( | ||
| AuditEventId.generate(), | ||
| companyId, | ||
| AuditEventType.BUDGET_UPDATED, | ||
| actorId, | ||
| "Budget", | ||
| budget.getId().toString(), | ||
| payload, | ||
| Instant.now() | ||
| ); | ||
| } | ||
|
|
||
| public static AuditEvent reconstitute(AuditEventId id, | ||
| CompanyId companyId, | ||
| AuditEventType eventType, | ||
| CustomerId actorId, | ||
| String targetType, | ||
| String targetId, | ||
| Map<String, Object> payload, | ||
| Instant timestamp) { | ||
| return new AuditEvent(id, companyId, eventType, actorId, targetType, targetId, payload, timestamp); | ||
| } | ||
|
|
||
| public AuditEventId getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public CompanyId getCompanyId() { | ||
| return companyId; | ||
| } | ||
|
|
||
| public AuditEventType getEventType() { | ||
| return eventType; | ||
| } | ||
|
|
||
| public CustomerId getActorId() { | ||
| return actorId; | ||
| } | ||
|
|
||
| public String getTargetType() { | ||
| return targetType; | ||
| } | ||
|
|
||
| public String getTargetId() { | ||
| return targetId; | ||
| } | ||
|
|
||
| public Map<String, Object> getPayload() { | ||
| return new HashMap<>(payload); | ||
| } | ||
|
|
||
| public Instant getTimestamp() { | ||
| return timestamp; | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
apps/api/src/main/java/com/upkeep/domain/model/audit/AuditEventId.java
This file contains hidden or 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,22 @@ | ||
| package com.upkeep.domain.model.audit; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public record AuditEventId(UUID value) { | ||
| public static AuditEventId generate() { | ||
| return new AuditEventId(UUID.randomUUID()); | ||
| } | ||
|
|
||
| public static AuditEventId from(String value) { | ||
| return new AuditEventId(UUID.fromString(value)); | ||
| } | ||
|
|
||
| public static AuditEventId from(UUID value) { | ||
| return new AuditEventId(value); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return value.toString(); | ||
| } | ||
| } |
Oops, something went wrong.
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.
The SetCompanyBudgetUseCaseImpl always creates a new budget without checking if one already exists for the company in the current month. This will cause a database constraint violation (uk_budget_company_effective) if called twice in the same month via direct API access. While the UI prevents this by checking budget.exists before showing the form, the API should handle this scenario gracefully. Consider either: 1) Checking if a budget exists and throwing a descriptive domain exception, or 2) Updating the existing budget if one exists for the current month. Based on the story acceptance criteria AC1 "no budget is set", option 1 seems more appropriate for Story 3.1, with updates handled in Story 3.2.
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.
@copilot open a new pull request to apply changes based on this feedback