Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix(privacy): keep holding statuses when an organization is edited

[._.]/ Adam Eivy530f2f6c9b45
diff --git a/client/src/services/apiPrivacy.js b/client/src/services/apiPrivacy.js
index 3e32ec1247..1e088d06b9 100644
--- a/client/src/services/apiPrivacy.js
+++ b/client/src/services/apiPrivacy.js
@@ -107,7 +107,8 @@ export const deletePrivacyOrg = (id, options) => request(`/privacy/orgs/${id}`,
// ── Org holdings (which vault records an org holds) ─────────────────────────
export const getOrgHoldings = (id, options) => request(`/privacy/orgs/${id}/holdings`, options);
-// Replace-set semantics: `holdings` is the FULL list of { vaultRecordId, status }.
+// Replace-set semantics: `holdings` is the FULL list of { vaultRecordId, status? };
+// an omitted status keeps a held record's stored one (new rows start 'current').
export const setOrgHoldings = (id, holdings, options) => request(`/privacy/orgs/${id}/holdings`, {
method: 'PUT',
body: JSON.stringify({ holdings }),
diff --git a/server/services/privacyOrgs.db.test.js b/server/services/privacyOrgs.db.test.js
index c0ded141dc..e1a7ef6270 100644
--- a/server/services/privacyOrgs.db.test.js
+++ b/server/services/privacyOrgs.db.test.js
@@ -98,6 +98,20 @@ describe.skipIf(!runDb)('privacy orgs DB round-trip', () => {
expect(after[0]).toMatchObject({ vaultRecordId: r1.id, status: 'update_pending' });
});
+ it('keeps a held record\'s stored status when the replace-set omits it', async () => {
+ const held = await vault.createVaultRecord({ type: 'address', label: 'Old home', value: '2 Example Rd' });
+ const added = await vault.createVaultRecord({ type: 'email', label: 'Alt', value: '[email protected]' });
+ createdVaultRecords.push(held.id, added.id);
+ const org = await orgs.createOrg({ name: 'Example Insurer' });
+ createdOrgs.push(org.id);
+
+ await orgs.setOrgHoldings(org.id, [{ vaultRecordId: held.id, status: 'update_pending' }]);
+ // The org editor sends ids only — an unrelated edit must not reset the mark.
+ await orgs.setOrgHoldings(org.id, [{ vaultRecordId: held.id }, { vaultRecordId: added.id }]);
+ const byRecord = Object.fromEntries((await orgs.getHoldingsForOrg(org.id)).map((h) => [h.vaultRecordId, h.status]));
+ expect(byRecord).toEqual({ [held.id]: 'update_pending', [added.id]: 'current' });
+ });
+
it('cascades: deleting the org drops its holdings', async () => {
const record = await vault.createVaultRecord({ type: 'custom', label: 'Misc', value: 'x' });
createdVaultRecords.push(record.id);
diff --git a/server/services/privacyOrgs.js b/server/services/privacyOrgs.js
index 20c112618c..8ceccb2362 100644
--- a/server/services/privacyOrgs.js
+++ b/server/services/privacyOrgs.js
@@ -221,12 +221,15 @@ export async function setOrgHoldings(orgId, holdings) {
// A vaultRecordId that doesn't exist (stale UI id, typo, raced with a
// deletion) trips the FK constraint — surface it as a clean 400
// instead of a raw Postgres constraint-violation 500.
+ // An omitted status keeps a held record's stored one: the org editor
+ // sends ids only, and resetting to 'current' there erased the
+ // change-of-address workflow's update_pending/updated/removed marks.
await client.query(
`INSERT INTO privacy_org_holdings (org_id, subject_id, vault_record_id, status, noted_at, updated_at)
- VALUES ($1, $2, $3, $4, NOW(), NOW())
+ VALUES ($1, $2, $3, COALESCE($4::text, 'current'), NOW(), NOW())
ON CONFLICT (org_id, vault_record_id)
- DO UPDATE SET status = EXCLUDED.status, subject_id = EXCLUDED.subject_id, updated_at = NOW()`,
- [orgId, subjectId, h.vaultRecordId, h.status ?? 'current'],
+ DO UPDATE SET status = COALESCE($4::text, privacy_org_holdings.status), subject_id = EXCLUDED.subject_id, updated_at = NOW()`,
+ [orgId, subjectId, h.vaultRecordId, h.status ?? null],
).catch((err) => {
if (err?.code === '23503') {
throw new ServerError(`Vault record ${h.vaultRecordId} not found`, { status: 400, code: 'VAULT_RECORD_NOT_FOUND' });
diff --git a/server/services/privacyOrgs.test.js b/server/services/privacyOrgs.test.js
index 732a27fa5d..66d87e3385 100644
--- a/server/services/privacyOrgs.test.js
+++ b/server/services/privacyOrgs.test.js
@@ -206,7 +206,8 @@ describe('setOrgHoldings', () => {
expect(deleteCall[1]).toEqual(['o1', ['v1', 'v2']]);
const insertCalls = client.query.mock.calls.filter(([sql]) => /INSERT INTO privacy_org_holdings/.test(sql));
expect(insertCalls).toHaveLength(2);
- expect(insertCalls[1][1]).toEqual(['o1', SELF, 'v2', 'current']); // subject stamped + default status applied
+ expect(insertCalls[0][1]).toEqual(['o1', SELF, 'v1', 'current']);
+ expect(insertCalls[1][1]).toEqual(['o1', SELF, 'v2', null]); // subject stamped; omitted status left to the SQL default/keep
expect(withTransactionMock).toHaveBeenCalledTimes(1);
});