Skip to content

Delete a member's contact and its email/phone/address together, in one server-side transaction - #15

Merged
davis-brown merged 2 commits into
mainfrom
factory/issue-14
Nov 23, 2026
Merged

davis-brown merged 2 commits into
mainfrom
factory/issue-14

Conversation

@davis-brown

Copy link
Copy Markdown
Owner

Closes #14

Deleting a member's contact now removes both the link and the email/phone/address in one server-side transaction, so a failed second request can no longer leave private data behind. Fixes #14.

What changed

  • backend/controllers/personcontact.controller.js, exports.delete: inside one db.sequelize.transaction, it finds the link by cpc_ID, deletes it, then deletes its communication row if the link has a com_ID. This follows the pattern already used in Person.delete. The responses are unchanged: a success message when one link was deleted, "Cannot delete…" when none was found, and 500 on error.
  • backend/controllers/communication.controller.js, exports.delete: inside one transaction, it deletes the personcontact rows with that com_ID, then the communication. The responses are unchanged.
  • frontend/src/components/ContactInfo.vue, deleteContact: the second CommunicationService.delete call is gone, so deleting a contact is one request. The import stays because the add-email, add-phone and add-address code still uses it.
  • tests/browser/families-groups-members.check.js:
    • The existing "deleted phone and address disappear" check now reloads the page and confirms both are still gone.
    • A new check calls DELETE /api/communications/:id directly. It confirms the link is removed, a second contact on the same person survives, and the member's edit page still loads it.

Verification

  • npm test in backend: 3/3 pass.
  • npm run lint and npm run build in frontend: both clean.
  • I started the stack with docker compose up --build and the demo data, and ran tests/browser/run.sh: every check in all 6 files passed, including the new one.
  • The issue's two orphan queries returned 0 rows each after the full run. The table is named communications (plural), not communication as the issue's SQL has it.

For the reviewer

  • In personcontact.controller.js, num from destroying the link is what drives the response. The communication delete is chained after it and passes num through.
  • I didn't add the null check on personContact.communication in ContactInfo.vue (around lines 235–237). The issue didn't ask for it, and with these changes the server shouldn't create new dangling links. Any dangling links already in a production database would still break that person's contact list, so a one-off cleanup query may be worth running.
  • I didn't open the browser's network tab to watch the delete become a single request. The code removes the second call, and the browser check confirms both rows end up deleted.

Codex review

  • backend/controllers/personcontact.controller.js:103 — Multiple contact links can reference the same com_ID. Deleting one link also deletes the shared communication. With Sequelize’s default ON DELETE SET NULL, the remaining links lose their communication, and ContactInfo.vue throws when it reads communication.vvg_ID. Delete the communication only when no links remain, and add a regression test with two people sharing one communication.
  • backend/controllers/personcontact.controller.js:101 — The transaction reads the link without locking it. A concurrent update can change its com_ID before the delete, causing the request to remove the old communication and orphan the one the link referenced when it was deleted. Read the link with FOR UPDATE inside the transaction and run communication cleanup only if the link delete affected one row.
  • tests/browser/families-groups-members.check.js:154 — The direct-delete check can pass if the endpoint removes the contact link but leaves the communication row behind. Assert that gone.com_ID is absent from the communications API after deletion.

Fixes after the review

I fixed all three findings, committed as 9d931b3.

  1. Shared communication deleted with one link (personcontact.controller.js:103): Fixed. The UI always creates its own communication for each link, but the API lets several links share one, so the bug could happen. After deleting the link, the controller now counts the links that still point at that com_ID. It deletes the communication only when none are left. New browser check "Deleting one of two links to a shared communication keeps it for the other": two people share one communication; deleting one person's link leaves the other link and the communication in place, and deleting the last link removes the communication.

  2. Link read without a lock (personcontact.controller.js:101): Fixed. The link is now read with lock: true inside the transaction, which makes it SELECT … FOR UPDATE. Cleanup only runs when the delete actually removed one row (num == 1), so a missing or already-deleted link never touches a communication.

  3. Direct-delete check didn't confirm the row was gone (families-groups-members.check.js:154): Fixed. The check now also asserts that gone.com_ID is no longer returned by GET /communications.

Checks run:

  • Backend npm test: no failures.
  • Frontend npm run lint -- --no-fix: no errors.
  • families-groups-members.check.js against a fresh docker compose stack: 11/11 passed, including the new and updated checks.

Not checked:

  • I didn't confirm the new check fails against the old code.
  • I only ran that one browser check file, not the other check files.
  • A link added to the same communication by another request while a delete is running could still lose its communication. Fully closing that would need a database-level foreign-key rule.

Lock the link while deleting it, clean up only when the delete removed it, and
check that a directly deleted communication is really gone.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T20:11:33.170377Z 9d931b3 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9d931b3ee9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +106 to +107
return Personcontact.count(byCom)
.then(others => others ? num : db.communication.destroy(byCom).then(() => num));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Serialize cleanup of links sharing a communication

If two requests concurrently delete the last two links to the same communication, each transaction locks only its own contact row. Both can delete their respective rows and then have this nonlocking count see the other transaction's still-uncommitted link, so both skip communication deletion and commit successfully. The email/phone/address is then left orphaned permanently; the frontend no longer sends a separate communication-delete request to remove it. Serialize cleanup on the shared communication row before deleting/counting its links, and add a regression test for concurrent deletion of the last two links.

Useful? React with 👍 / 👎.

@davis-brown
davis-brown merged commit 4036022 into main Nov 23, 2026
3 checks passed
@davis-brown
davis-brown deleted the factory/issue-14 branch November 23, 2026 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Delete a member's contact and its email/phone/address together, in one server-side transaction

1 participant