From 2500f78a6358f7785ff3efab33aa03de2402fb23 Mon Sep 17 00:00:00 2001 From: James Morton Date: Fri, 20 Nov 2026 10:37:04 +0000 Subject: [PATCH 1/5] fix(people): keep vote counts and segment evaluation honest - Removing a person, or merging a lead or visitor whose vote collides with the person's, takes the vote off the post's stored vote_count (on the canonical post of a merged thread) in the same transaction. - Companies created in the admin are 'manual', not 'api'. - The user and company attribute dialogs show a refused create or edit (for example a duplicate key) and toast a refused delete. - vote_count and comment_count segment rules count post_votes and live post_comments instead of tables that do not exist. - Evaluating every segment isolates each one: a failure is logged and reported and the rest still run. - Evaluation no longer reports a person held by another source as added. - REST DELETE segment members counts only rows it removed. - A schedule is accepted only if the scheduler's cron parser accepts it. - A dynamic segment's rules cannot be cleared. --- .../__tests__/scalar-attribute-list.test.tsx | 54 +++++ .../admin/settings/scalar-attribute-list.tsx | 29 ++- .../__tests__/principal-merge-db-mock.ts | 8 + .../auth/__tests__/merge-anonymous.db.test.ts | 29 +++ .../server/domains/posts/post.merge-ids.ts | 42 +++- .../domains/principals/principal-repoint.ts | 30 ++- .../__tests__/segment-evaluation.db.test.ts | 224 ++++++++++++++++++ .../__tests__/segment-membership.test.ts | 16 +- .../__tests__/segment-service.test.ts | 4 +- .../segments/segment-membership.service.ts | 7 +- .../domains/segments/segment.evaluation.ts | 57 +++-- .../domains/segments/segment.service.ts | 12 + .../server/domains/segments/segment.types.ts | 6 + .../__tests__/remove-portal-user.db.test.ts | 29 +++ .../users/__tests__/user-merge.db.test.ts | 30 +++ .../lib/server/domains/users/user.service.ts | 4 + .../__tests__/companies-create.test.ts | 59 +++++ .../segment-condition-schema.test.ts | 29 +++ apps/web/src/lib/server/functions/admin.ts | 19 +- .../web/src/lib/server/functions/companies.ts | 3 +- .../routes/api/v1/segments/$slug.members.ts | 5 +- .../__tests__/segment-members.test.ts | 61 +++++ 22 files changed, 719 insertions(+), 38 deletions(-) create mode 100644 apps/web/src/lib/server/domains/segments/__tests__/segment-evaluation.db.test.ts create mode 100644 apps/web/src/lib/server/functions/__tests__/companies-create.test.ts create mode 100644 apps/web/src/routes/api/v1/segments/__tests__/segment-members.test.ts diff --git a/apps/web/src/components/admin/settings/__tests__/scalar-attribute-list.test.tsx b/apps/web/src/components/admin/settings/__tests__/scalar-attribute-list.test.tsx index 0d96804355..be9b38bd65 100644 --- a/apps/web/src/components/admin/settings/__tests__/scalar-attribute-list.test.tsx +++ b/apps/web/src/components/admin/settings/__tests__/scalar-attribute-list.test.tsx @@ -18,9 +18,12 @@ const m = vi.hoisted(() => { companyCreate: make(), companyUpdate: make(), companyDelete: make(), + toastError: vi.fn(), } }) +vi.mock('@/lib/client/error-notice', () => ({ toastError: m.toastError })) + vi.mock('@/lib/client/mutations', () => ({ useCreateUserAttribute: () => m.userCreate, useUpdateUserAttribute: () => m.userUpdate, @@ -104,6 +107,57 @@ describe.each(ENTITIES)('ScalarAttributeList for $name', (entity) => { await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) }) + it('keeps the dialog open and says why when the server refuses the key', async () => { + const { create } = entity.mocks() + create.mutateAsync.mockRejectedValue(new Error('An attribute with that key already exists')) + const user = userEvent.setup() + renderList([]) + await user.click(screen.getAllByRole('button', { name: /new attribute/i })[0]) + const dialog = await screen.findByRole('dialog') + await user.type(within(dialog).getByLabelText(/^Key/), 'seats') + await user.type(within(dialog).getByLabelText('Display label'), 'Seats again') + await user.click(within(dialog).getByRole('button', { name: 'Create attribute' })) + expect((await within(dialog).findByRole('alert')).textContent).toBe( + 'An attribute with that key already exists' + ) + expect(screen.getByRole('dialog')).toBeTruthy() + + // Reopening starts clean. + await user.click(within(dialog).getByRole('button', { name: 'Cancel' })) + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) + await user.click(screen.getAllByRole('button', { name: /new attribute/i })[0]) + expect(within(await screen.findByRole('dialog')).queryByRole('alert')).toBeNull() + }) + + it('says why when the server refuses an edit', async () => { + const { update } = entity.mocks() + update.mutateAsync.mockRejectedValue(new Error('Attribute not found')) + const user = userEvent.setup() + renderList() + await user.click(screen.getByRole('button', { name: 'Actions for Seats' })) + await user.click(await screen.findByRole('menuitem', { name: 'Edit' })) + const dialog = await screen.findByRole('dialog') + await user.click(within(dialog).getByRole('button', { name: 'Save changes' })) + expect((await within(dialog).findByRole('alert')).textContent).toBe('Attribute not found') + }) + + it('toasts a refused delete and keeps the row', async () => { + const { del } = entity.mocks() + del.mutateAsync.mockRejectedValue(new Error('Attribute not found')) + const user = userEvent.setup() + renderList() + await user.click(screen.getByRole('button', { name: 'Actions for Seats' })) + await user.click(await screen.findByRole('menuitem', { name: 'Delete' })) + await user.click( + within(await screen.findByRole('alertdialog')).getByRole('button', { + name: 'Delete attribute', + }) + ) + await waitFor(() => expect(m.toastError).toHaveBeenCalledTimes(1)) + expect(m.toastError.mock.calls[0][0]).toMatchObject({ message: 'Attribute not found' }) + expect(screen.getByText('Seats')).toBeTruthy() + }) + it('edits an attribute with the key locked and saves the new label', async () => { const { update } = entity.mocks() update.mutateAsync.mockResolvedValue(stored({ label: 'Active seats' })) diff --git a/apps/web/src/components/admin/settings/scalar-attribute-list.tsx b/apps/web/src/components/admin/settings/scalar-attribute-list.tsx index e546b7d187..e40c293ee2 100644 --- a/apps/web/src/components/admin/settings/scalar-attribute-list.tsx +++ b/apps/web/src/components/admin/settings/scalar-attribute-list.tsx @@ -18,6 +18,8 @@ import { DialogFooter, } from '@/components/ui/dialog' import { ConfirmDialog } from '@/components/shared/confirm-dialog' +import { errorText } from '@/lib/client/error-text' +import { toastError } from '@/lib/client/error-notice' import { AttributeList, type AttributeListItem } from './attribute-list' const ATTRIBUTE_TYPES = [ @@ -131,9 +133,11 @@ function AttributeFormDialog({ const [type, setType] = useState(initialValues?.type ?? 'string') const [currencyCode, setCurrencyCode] = useState(initialValues?.currencyCode ?? 'USD') const [externalKey, setExternalKey] = useState(initialValues?.externalKey ?? '') + const [error, setError] = useState(null) useEffect(() => { if (open) { + setError(null) setKey(initialValues?.key ?? '') setLabel(initialValues?.label ?? '') setDescription(initialValues?.description ?? '') @@ -146,7 +150,17 @@ function AttributeFormDialog({ const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() - await onSubmit({ key, label, description, type, currencyCode, externalKey }) + setError(null) + try { + await onSubmit({ key, label, description, type, currencyCode, externalKey }) + } catch (err) { + setError( + errorText( + err, + isEditing ? "Couldn't save the attribute." : "Couldn't create the attribute." + ) + ) + } } const canSubmit = key.trim().length > 0 && label.trim().length > 0 @@ -253,6 +267,12 @@ function AttributeFormDialog({

{copy.externalHint}

+ {error && ( +

+ {error} +

+ )} +