Repository navigation
Delete a member's contact and its email/phone/address together, in one server-side transaction - #15
Conversation
Lock the link while deleting it, clean up only when the delete removed it, and check that a directly deleted communication is really gone.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| return Personcontact.count(byCom) | ||
| .then(others => others ? num : db.communication.destroy(byCom).then(() => num)); |
There was a problem hiding this comment.
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 👍 / 👎.
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 onedb.sequelize.transaction, it finds the link bycpc_ID, deletes it, then deletes itscommunicationrow if the link has acom_ID. This follows the pattern already used inPerson.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 thepersoncontactrows with thatcom_ID, then the communication. The responses are unchanged.frontend/src/components/ContactInfo.vue,deleteContact: the secondCommunicationService.deletecall 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:DELETE /api/communications/:iddirectly. 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 testinbackend: 3/3 pass.npm run lintandnpm run buildinfrontend: both clean.docker compose up --buildand the demo data, and rantests/browser/run.sh: every check in all 6 files passed, including the new one.communications(plural), notcommunicationas the issue's SQL has it.For the reviewer
personcontact.controller.js,numfrom destroying the link is what drives the response. The communication delete is chained after it and passesnumthrough.personContact.communicationinContactInfo.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.Codex review
backend/controllers/personcontact.controller.js:103— Multiple contact links can reference the samecom_ID. Deleting one link also deletes the shared communication. With Sequelize’s defaultON DELETE SET NULL, the remaining links lose their communication, andContactInfo.vuethrows when it readscommunication.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 itscom_IDbefore the delete, causing the request to remove the old communication and orphan the one the link referenced when it was deleted. Read the link withFOR UPDATEinside 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 thatgone.com_IDis absent from the communications API after deletion.Fixes after the review
I fixed all three findings, committed as
9d931b3.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 thatcom_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.Link read without a lock (
personcontact.controller.js:101): Fixed. The link is now read withlock: trueinside the transaction, which makes itSELECT … 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.Direct-delete check didn't confirm the row was gone (
families-groups-members.check.js:154): Fixed. The check now also asserts thatgone.com_IDis no longer returned byGET /communications.Checks run:
npm test: no failures.npm run lint -- --no-fix: no errors.families-groups-members.check.jsagainst a freshdocker composestack: 11/11 passed, including the new and updated checks.Not checked: