Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

update backup functionality to include contact roles and references

Khuram Niaz4d55536a67e4
diff --git a/__tests__/backupIdMap.spec.ts b/__tests__/backupIdMap.spec.ts
index 8f79db6..d0fd8dd 100644
--- a/__tests__/backupIdMap.spec.ts
+++ b/__tests__/backupIdMap.spec.ts
@@ -108,6 +108,27 @@ describe("buildCreateData", () => {
expect(data.coverLetterId).toBeNull();
});
+ it("nulls every dangling optional contact reference instead of aborting", () => {
+ const map = mapWith({ "old-contact": "new-contact" });
+ const data = buildCreateData(
+ "Contact",
+ {
+ id: "old-contact",
+ name: "Pat",
+ companyId: "ghost-co",
+ locationId: "ghost-loc",
+ workedAtCompanyId: "ghost-co",
+ roleId: "ghost-role",
+ },
+ map,
+ "u",
+ );
+ expect(data.companyId).toBeNull();
+ expect(data.locationId).toBeNull();
+ expect(data.workedAtCompanyId).toBeNull();
+ expect(data.roleId).toBeNull();
+ });
+
it("throws when a required foreign key is dangling", () => {
const map = mapWith({ "old-skill": "new-skill", "old-section": "new-section" });
expect(() =>
diff --git a/__tests__/backupOrdering.spec.ts b/__tests__/backupOrdering.spec.ts
index 6c5a3ce..6ddb3d5 100644
--- a/__tests__/backupOrdering.spec.ts
+++ b/__tests__/backupOrdering.spec.ts
@@ -119,6 +119,7 @@ describe("emptiness models", () => {
expect([...EMPTINESS_MODELS].sort()).toEqual([
"Activity",
"Automation",
+ "Contact",
"CoverLetter",
"Job",
"Note",
diff --git a/__tests__/backupPreflight.spec.ts b/__tests__/backupPreflight.spec.ts
index 0da9442..c3c9afa 100644
--- a/__tests__/backupPreflight.spec.ts
+++ b/__tests__/backupPreflight.spec.ts
@@ -14,6 +14,7 @@ vi.mock("@/lib/db", () => {
coverLetter: counted(),
job: counted(),
note: counted(),
+ contact: counted(),
task: counted(),
activity: counted(),
question: counted(),
diff --git a/__tests__/backupRoundTrip.spec.ts b/__tests__/backupRoundTrip.spec.ts
index b9677f9..be4cb18 100644
--- a/__tests__/backupRoundTrip.spec.ts
+++ b/__tests__/backupRoundTrip.spec.ts
@@ -2,7 +2,7 @@
import fs from "fs";
import path from "path";
import { PrismaClient } from "@prisma/client";
-import { APP_CONSTANTS } from "@/lib/constants";
+import { APP_CONSTANTS, CONTACT_ROLES } from "@/lib/constants";
import { buildBackupZip } from "@/lib/backup/export";
import { importBackup } from "@/lib/backup/import";
import { listSnapshots, readSnapshot } from "@/lib/backup/snapshot";
@@ -129,6 +129,23 @@ async function seedFullAccount() {
data: { jobId: job.id, userId, content: "a note" },
});
+ const recruiter = await prisma.contactRole.findFirstOrThrow({
+ where: { createdBy: userId, value: "recruiter" },
+ });
+ const contact = await prisma.contact.create({
+ data: {
+ name: "Pat Lee",
+ email: "[email protected]",
+ companyId: company.id,
+ workedAtCompanyId: company.id,
+ roleId: recruiter.id,
+ createdBy: userId,
+ },
+ });
+ await prisma.jobContact.create({
+ data: { jobId: job.id, contactId: contact.id, roleId: recruiter.id },
+ });
+
const question = await prisma.question.create({
data: { question: "Why us?", createdBy: userId, tags: { connect: { id: tag.id } } },
});
@@ -300,6 +317,52 @@ describe("backup round trip", () => {
expect(restored.industry).toBe("Widgets");
});
+ it("restores a contact with its company, standing role and job link remapped", async () => {
+ const contact = await prisma.contact.findFirstOrThrow({
+ where: { createdBy: userId },
+ include: {
+ Company: true,
+ WorkedAtCompany: true,
+ Role: true,
+ jobLinks: { include: { Job: true, Role: true } },
+ },
+ });
+
+ expect(contact.Company?.value).toBe("acme");
+ expect(contact.WorkedAtCompany?.value).toBe("acme");
+ expect(contact.Role?.value).toBe("recruiter");
+ expect(contact.jobLinks).toHaveLength(1);
+ expect(contact.jobLinks[0].Job.userId).toBe(userId);
+ expect(contact.jobLinks[0].Role.id).toBe(contact.Role?.id);
+
+ // The roles came back once, not doubled on top of anything.
+ expect(await prisma.contactRole.count({ where: { createdBy: userId } })).toBe(5);
+ });
+
+ it("reseeds the default contact roles when restoring a pre-contacts backup", async () => {
+ const { buffer } = await buildBackupZip(userId, "[email protected]");
+
+ // Reshape into what v1.1.19 wrote: no contact groups, no counts for them.
+ const JSZip = (await import("jszip")).default;
+ const zip = await JSZip.loadAsync(buffer);
+ const data = JSON.parse(await zip.file("data.json")!.async("string"));
+ const manifest = JSON.parse(await zip.file("manifest.json")!.async("string"));
+ for (const group of ["Contact", "ContactRole", "JobContact"]) {
+ delete data[group];
+ delete manifest.counts[group];
+ }
+ zip.file("data.json", JSON.stringify(data));
+ zip.file("manifest.json", JSON.stringify(manifest));
+ const legacy = await zip.generateAsync({ type: "nodebuffer" });
+
+ await importBackup(legacy, userId, "[email protected]", true);
+
+ const roles = await prisma.contactRole.findMany({ where: { createdBy: userId } });
+ expect(roles.map((r) => r.value).sort()).toEqual(
+ CONTACT_ROLES.map((r) => r.value).sort(),
+ );
+ }, 120_000);
+
it("does not demand confirmWipe on a freshly signed-up account", async () => {
const freshUserId = await seedAccount(prisma, "[email protected]");
const { buffer } = await buildBackupZip(userId, "[email protected]");
@@ -315,6 +378,19 @@ describe("backup round trip", () => {
expect(result.snapshotPath).toBeNull();
}, 120_000);
+ it("treats an account holding only contacts as data worth confirming", async () => {
+ const contactsOnlyId = await seedAccount(prisma, "[email protected]");
+ await prisma.contact.create({
+ data: { name: "Only Contact", createdBy: contactsOnlyId },
+ });
+ const { buffer } = await buildBackupZip(userId, "[email protected]");
+
+ await expect(
+ importBackup(buffer, contactsOnlyId, "[email protected]", false),
+ ).rejects.toThrow(/already holds data/);
+ expect(await prisma.contact.count({ where: { createdBy: contactsOnlyId } })).toBe(1);
+ }, 120_000);
+
it("rolls back to the snapshot an import took", async () => {
const rollbackUserId = await seedAccount(prisma, "[email protected]");
// A content row, not just the lookup below: EMPTINESS_MODELS deliberately
diff --git a/__tests__/helpers/backupTestDb.ts b/__tests__/helpers/backupTestDb.ts
index 2ee684e..2ab6481 100644
--- a/__tests__/helpers/backupTestDb.ts
+++ b/__tests__/helpers/backupTestDb.ts
@@ -4,7 +4,7 @@ import path from "path";
import { execFileSync } from "child_process";
import { randomUUID } from "crypto";
import type { PrismaClient } from "@prisma/client";
-import { JOB_SOURCES, JOB_STATUSES } from "@/lib/constants";
+import { CONTACT_ROLES, JOB_SOURCES, JOB_STATUSES } from "@/lib/constants";
export function makeTestDbUrl(): { url: string; dir: string } {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "jobsync-backup-"));
@@ -45,5 +45,13 @@ export async function seedAccount(
});
}
+ await prisma.contactRole.createMany({
+ data: CONTACT_ROLES.map((role) => ({
+ label: role.label,
+ value: role.value,
+ createdBy: user.id,
+ })),
+ });
+
return user.id;
}
diff --git a/src/lib/backup/idmap.ts b/src/lib/backup/idmap.ts
index f59db8f..f69019e 100644
--- a/src/lib/backup/idmap.ts
+++ b/src/lib/backup/idmap.ts
@@ -17,6 +17,10 @@ const OPTIONAL_FKS = new Set<string>([
"Job.coverLetterId",
"Job.automationId",
"Contact.interviewId",
+ "Contact.companyId",
+ "Contact.locationId",
+ "Contact.workedAtCompanyId",
+ "Contact.roleId",
"Task.activityTypeId",
"Activity.taskId",
]);
@@ -78,7 +82,7 @@ export function buildCreateData(
// The client supplies the payload, the server supplies the owner — and no
// model keeps an ownership column it does not declare. Both keys go
- // unconditionally: 13 of the 28 models own through a relation chain and have
+ // unconditionally: 14 of the 30 models own through a relation chain and have
// neither column, so a stray userId riding in on a hand-edited data.json
// would reach Prisma as an unknown argument and abort the whole import.
delete data.userId;
diff --git a/src/lib/backup/import.ts b/src/lib/backup/import.ts
index 7bea5db..4dea1ca 100644
--- a/src/lib/backup/import.ts
+++ b/src/lib/backup/import.ts
@@ -2,7 +2,7 @@ import fs from "fs/promises";
import path from "path";
import type JSZip from "jszip";
import db from "@/lib/db";
-import { APP_CONSTANTS } from "@/lib/constants";
+import { APP_CONSTANTS, CONTACT_ROLES } from "@/lib/constants";
import { IdMap, buildCreateData } from "./idmap";
import { BackupError, openBackupZip, readManifest } from "./manifest";
import {
@@ -203,7 +203,7 @@ export async function importBackup(
}
const zip = await openBackupZip(bytes);
- await readManifest(zip);
+ const manifest = await readManifest(zip);
const data = await readData(zip);
const targetCounts = await countTargetContent(userId);
@@ -307,6 +307,18 @@ export async function importBackup(
await wipe(tx, userId);
await insertLookups(tx, data, idMap, userId);
+ // A backup from before contacts carries no roles, and the wipe above
+ // just deleted the seeded ones — reseed rather than leave none.
+ if (manifest.counts.ContactRole === undefined && data.ContactRole.length === 0) {
+ await tx.contactRole.createMany({
+ data: CONTACT_ROLES.map((role) => ({
+ label: role.label,
+ value: role.value,
+ createdBy: userId,
+ })),
+ });
+ }
+
for (const model of INSERT_ORDER) {
if (MODEL_SPECS[model].lookup) continue;
const spec = MODEL_SPECS[model];
diff --git a/src/lib/backup/ordering.ts b/src/lib/backup/ordering.ts
index 8710083..c39709a 100644
--- a/src/lib/backup/ordering.ts
+++ b/src/lib/backup/ordering.ts
@@ -236,6 +236,7 @@ export const EMPTINESS_MODELS: BackupModel[] = [
"CoverLetter",
"Job",
"Note",
+ "Contact",
"Task",
"Activity",
"Question",