Skip to content

Issue: Enforce Mandatory ExternalReference for Media Entities #33

@vitorhugo-java

Description

@vitorhugo-java

Description

Currently, the API allows Media entities to be persisted without an associated ExternalReference. This lack of referential integrity prevents the system from precisely linking media records with external services, leading to orphaned data and synchronization issues.

To ensure data consistency, the system must be updated to require at least one ExternalReference for every Media record saved.


Technical Requirements

1. Service Layer Validation

The MediaService (or equivalent service layer) must validate the existence of a reference before persistence. If no reference is found, a ValidationException should be thrown.

Proposed Logic:

public Media save(Media media) {
    if (!externalsRepo.existsByMediasIdMedia(media.getIdMedia())) {
        throw new ValidationException("Referência externa obrigatória");
    }
    return mediaRepo.save(media);
}

2. Spring Batch Integration & Error Handling

To prevent data corruption from stopping bulk processes, the Spring Batch jobs must handle this validation failure gracefully:

  • Catch Exception: If a ValidationException is thrown during the item writing/processing phase, the job should not terminate.
  • Retry Logic: Attempt to re-extract or recover the ID from the source JSON if possible.
  • Skip Policy: If the ID cannot be recovered or the reference is truly missing, the specific media record should be ignored/skipped.
  • Logging: Log the skipped record for later auditing without stopping the overall job execution.

Acceptance Criteria

  • Media entities can no longer be saved via the API without an associated ExternalReference.
  • The Service Layer throws a ValidationException with the message "Referência externa obrigatória" when validation fails.
  • Spring Batch jobs continue running when encountering invalid media records.
  • Skipped records in Batch processes are logged with enough context to identify the failing source data.

Metadata

Metadata

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions