Skip to content
Merged
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
33 changes: 1 addition & 32 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
jobs:
web-lint-test:
name: Web - Lint & Test
runs-on: self-hosted
runs-on: ubuntu-latest
defaults:
run:
working-directory: apps/web
Expand All @@ -32,37 +32,6 @@ jobs:
- name: Build
run: npm run build

web-e2e:
name: Web - E2E Tests
runs-on: ubuntu-latest
defaults:
run:
working-directory: apps/web

steps:
- uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install dependencies
run: npm install

- name: Install Playwright browsers
run: npx playwright install chromium --with-deps

- name: Run E2E tests
run: npm run test:e2e

- name: Upload test report
uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-report
path: apps/web/playwright-report/
retention-days: 7

api-lint-test:
name: API - Lint & Test
Expand Down
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);
}
}
}
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
) {}
}
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);
}
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);
}
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());
}
}
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);
Comment on lines +63 to +65
Copy link

Copilot AI Jan 29, 2026

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.

Copilot uses AI. Check for mistakes.
Copy link
Owner Author

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


AuditEvent event = AuditEvent.budgetCreated(companyId, actorId, budget);
auditEventRepository.save(event);

return new SetBudgetResult(
budget.getId().toString(),
amount.amountCents(),
amount.currency().name()
);
}
}
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 apps/api/src/main/java/com/upkeep/domain/model/audit/AuditEvent.java
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;
}
}
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();
}
}
Loading