diff --git a/src/main/java/com/jobtracker/service/status/LegacyStatusMapper.java b/src/main/java/com/jobtracker/service/status/LegacyStatusMapper.java index c651027..91b73f9 100644 --- a/src/main/java/com/jobtracker/service/status/LegacyStatusMapper.java +++ b/src/main/java/com/jobtracker/service/status/LegacyStatusMapper.java @@ -87,15 +87,16 @@ private static Optional rejected(String previous, LocalDate applicat } private static Optional ghosting(String previous, LocalDate applicationDate) { - if (applicationDate == null) { - return Optional.empty(); - } + // A closing status under Ghosting decides the conversion on its own, with or without a date. if (previous != null && REJECTED.contains(previous)) { return rejected(null, applicationDate); } if (previous != null && (HIRED.contains(previous) || WITHDRAWN.contains(previous))) { return closingOrOpen(previous, applicationDate); } + if (applicationDate == null) { + return Optional.empty(); + } ApplicationStage last = previous == null ? null : openStage(previous, applicationDate); return Optional.of(new StageState(last != null ? last : ApplicationStage.APPLIED, null, WaitingOn.COMPANY)); } diff --git a/src/main/resources/db/migration/postgresql/V41__retry_unconverted_legacy_statuses.sql b/src/main/resources/db/migration/postgresql/V41__retry_unconverted_legacy_statuses.sql new file mode 100644 index 0000000..aa6e879 --- /dev/null +++ b/src/main/resources/db/migration/postgresql/V41__retry_unconverted_legacy_statuses.sql @@ -0,0 +1,7 @@ +-- Ghosting over a closing status without an application date was left unconverted by mistake. +-- Clearing the marker makes the startup backfill attempt these rows again; rows that still +-- cannot be converted get their legacy value back. Converted rows are untouched. +UPDATE job_applications +SET legacy_status = NULL +WHERE stage IS NULL + AND legacy_status IS NOT NULL; diff --git a/src/main/resources/db/migration/postgresql/V42__repair_expired_draft_application_dates.sql b/src/main/resources/db/migration/postgresql/V42__repair_expired_draft_application_dates.sql new file mode 100644 index 0000000..c2ab844 --- /dev/null +++ b/src/main/resources/db/migration/postgresql/V42__repair_expired_draft_application_dates.sql @@ -0,0 +1,45 @@ +-- One-off data repair approved by the maintainer (issue #108 follow-up, 2026-12-03). +-- +-- Before the stage cutover, applications created without a status were stored as drafts and the API +-- cleared their application date; the old nightly job then turned them into "Rejected" and later into +-- "Ghosting". They were real applications, registered on the day they were sent, so the creation day +-- is used as the application date and they become open applications waiting on the company. +-- +-- Only rows with that exact shape are touched: no application date, no outcome chosen since the +-- cutover, and either "Rejected" with no previous status or "Ghosting" over "Rejected". +-- Missed deadlines (Tarde demais) and other legacy rows are left alone. updated_at is not changed. +-- The previous values are kept in application_date_repair_backup_20261203 for audit and rollback. + +CREATE TABLE IF NOT EXISTS application_date_repair_backup_20261203 AS +SELECT id, status, previous_status, stage, outcome, waiting_on, legacy_status, application_date, + to_send_later, created_at, updated_at, NOW() AS backed_up_at +FROM job_applications +WHERE FALSE; + +CREATE TEMPORARY TABLE application_date_repair_targets ON COMMIT DROP AS +SELECT id +FROM job_applications +WHERE application_date IS NULL + AND outcome IS NULL + AND (stage IS NULL OR stage = 'DRAFT') + AND ( + (LOWER(TRIM(status)) IN ('rejected', 'rejeitado') AND previous_status IS NULL) + OR (LOWER(TRIM(status)) = 'ghosting' AND LOWER(TRIM(previous_status)) IN ('rejected', 'rejeitado')) + ); + +INSERT INTO application_date_repair_backup_20261203 +SELECT j.id, j.status, j.previous_status, j.stage, j.outcome, j.waiting_on, j.legacy_status, j.application_date, + j.to_send_later, j.created_at, j.updated_at, NOW() +FROM job_applications j +JOIN application_date_repair_targets t ON t.id = j.id +WHERE NOT EXISTS (SELECT 1 FROM application_date_repair_backup_20261203 b WHERE b.id = j.id); + +UPDATE job_applications j +SET application_date = j.created_at::date, + stage = 'APPLIED', + outcome = NULL, + waiting_on = 'COMPANY', + to_send_later = FALSE, + legacy_status = j.status +FROM application_date_repair_targets t +WHERE t.id = j.id; diff --git a/src/test/java/com/jobtracker/integration/ExpiredDraftDateRepairIT.java b/src/test/java/com/jobtracker/integration/ExpiredDraftDateRepairIT.java new file mode 100644 index 0000000..006d6fd --- /dev/null +++ b/src/test/java/com/jobtracker/integration/ExpiredDraftDateRepairIT.java @@ -0,0 +1,115 @@ +package com.jobtracker.integration; + +import com.jobtracker.entity.User; +import com.jobtracker.repository.UserRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.nio.charset.StandardCharsets; +import java.sql.Date; +import java.sql.Timestamp; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.Map; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Runs the V42 data repair against real rows shaped like the production ones it targets. */ +class ExpiredDraftDateRepairIT extends AbstractIntegrationTest { + + private static final LocalDateTime CREATED = LocalDateTime.of(2026, 10, 25, 14, 30); + private static final LocalDateTime UPDATED = LocalDateTime.of(2026, 11, 2, 0, 0); + + @Autowired private JdbcTemplate jdbc; + @Autowired private UserRepository userRepository; + + private UUID userId; + + @BeforeEach + void setUp() { + jdbc.update("DROP TABLE IF EXISTS application_date_repair_backup_20261203"); + jdbc.update("DELETE FROM job_applications"); + User user = new User(); + user.setName("Repair"); + user.setEmail("repair-" + UUID.randomUUID() + "@example.com"); + user.setPasswordHash("x"); + userId = userRepository.save(user).getId(); + } + + @Test + void expiredDraftsGetTheirCreationDayAsApplicationDateAndAreBackedUp() throws Exception { + UUID rejectedDraft = insert("Rejected", null, "DRAFT", null, "Rejected"); + UUID ghostingOverRejected = insert("Ghosting", "Rejected", null, null, "Ghosting"); + UUID missedDeadline = insert("Ghosting", "TARDE_DEMAIS", null, null, "Ghosting"); + UUID ghostingOverGhosting = insert("Ghosting", "GHOSTING", null, null, "Ghosting"); + UUID plainDraft = insert(null, null, "DRAFT", null, "TO_SEND_LATER"); + UUID rejectedWithDate = insert("Rejected", null, "APPLIED", LocalDate.of(2026, 9, 1), "Rejected"); + + runRepair(); + + for (UUID repaired : new UUID[]{rejectedDraft, ghostingOverRejected}) { + Map row = row(repaired); + assertThat(row.get("application_date")).isEqualTo(Date.valueOf(CREATED.toLocalDate())); + assertThat(row.get("stage")).isEqualTo("APPLIED"); + assertThat(row.get("outcome")).isNull(); + assertThat(row.get("waiting_on")).isEqualTo("COMPANY"); + assertThat(row.get("to_send_later")).isEqualTo(false); + assertThat(row.get("updated_at")).isEqualTo(Timestamp.valueOf(UPDATED)); + assertThat(row.get("legacy_status")).isNotNull(); + } + assertThat(row(missedDeadline).get("stage")).isNull(); + assertThat(row(ghostingOverGhosting).get("stage")).isNull(); + assertThat(row(plainDraft).get("stage")).isEqualTo("DRAFT"); + assertThat(row(missedDeadline).get("application_date")).isNull(); + assertThat(row(ghostingOverGhosting).get("application_date")).isNull(); + assertThat(row(plainDraft).get("application_date")).isNull(); + assertThat(row(rejectedWithDate).get("application_date")).isEqualTo(Date.valueOf(LocalDate.of(2026, 9, 1))); + + assertThat(jdbc.queryForList("SELECT id FROM application_date_repair_backup_20261203", UUID.class)) + .containsExactlyInAnyOrder(rejectedDraft, ghostingOverRejected); + Map backup = jdbc.queryForMap( + "SELECT stage, application_date, to_send_later FROM application_date_repair_backup_20261203 WHERE id = ?", + rejectedDraft); + assertThat(backup.get("stage")).isEqualTo("DRAFT"); + assertThat(backup.get("application_date")).isNull(); + } + + @Test + void runningTheRepairTwiceChangesNothingMore() throws Exception { + UUID rejectedDraft = insert("Rejected", null, "DRAFT", null, "Rejected"); + runRepair(); + Map first = row(rejectedDraft); + + runRepair(); + + assertThat(row(rejectedDraft)).isEqualTo(first); + assertThat(jdbc.queryForObject("SELECT COUNT(*) FROM application_date_repair_backup_20261203", Long.class)) + .isEqualTo(1L); + } + + private void runRepair() throws Exception { + String sql = new ClassPathResource("db/migration/postgresql/V42__repair_expired_draft_application_dates.sql") + .getContentAsString(StandardCharsets.UTF_8); + jdbc.execute(sql); + } + + private UUID insert(String status, String previous, String stage, LocalDate date, String legacy) { + UUID id = UUID.randomUUID(); + jdbc.update(""" + INSERT INTO job_applications (id, user_id, vacancy_name, organization, status, previous_status, stage, + legacy_status, application_date, to_send_later, archived, rh_accepted_connection, + interview_scheduled, recruiter_dm_reminder_enabled, interview_count, created_at, updated_at) + VALUES (?, ?, 'Engineer', 'Acme', ?, ?, ?, ?, ?, ?, false, false, false, false, 0, ?, ?)""", + id, userId, status, previous, stage, legacy, date == null ? null : Date.valueOf(date), + "DRAFT".equals(stage), Timestamp.valueOf(CREATED), Timestamp.valueOf(UPDATED)); + return id; + } + + private Map row(UUID id) { + return jdbc.queryForMap("SELECT * FROM job_applications WHERE id = ?", id); + } +} diff --git a/src/test/java/com/jobtracker/unit/status/LegacyStatusMapperTest.java b/src/test/java/com/jobtracker/unit/status/LegacyStatusMapperTest.java index 427b158..3424130 100644 --- a/src/test/java/com/jobtracker/unit/status/LegacyStatusMapperTest.java +++ b/src/test/java/com/jobtracker/unit/status/LegacyStatusMapperTest.java @@ -122,6 +122,18 @@ void ghostingWithoutAMappablePreviousFallsBackToApplied(String previous) { assertState(map("Ghosting", previous), APPLIED, null, COMPANY); } + @Test + void ghostingOverAClosingStatusWithoutDateStillAppliesTheClosingRule() { + // Production rows: drafts the old nightly job moved to Rejected/Tarde demais and then to Ghosting. + assertState(LegacyStatusMapper.map("Ghosting", "TARDE_DEMAIS", false, null), DRAFT, WITHDREW, null); + assertState(LegacyStatusMapper.map("Ghosting", "Rejected", false, null), DRAFT, null, null); + } + + @Test + void ghostingOverGhostingWithoutDateIsNotConverted() { + assertThat(LegacyStatusMapper.map("Ghosting", "GHOSTING", false, null)).isEmpty(); + } + @Test void ghostingWithoutDateIsNotConverted() { assertThat(LegacyStatusMapper.map("Ghosting", null, false, null)).isEmpty();