From e0b6a7150375a10fdea858e349f94d88c46088f9 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Fri, 24 Jul 2026 10:44:06 +0800 Subject: [PATCH] fix(frontend): correct optimistic organization state after review and detail loads Follow-up fixes for the organization state sync work: - reviewOrganizationJoinRequest no longer increments member_count when approving a role-upgrade request (only genuine "join" approvals add a member); callers pass the request_type so upgrades are excluded. - setCurrentOrganization now merges the detail payload onto the existing card instead of replacing it, so list-only aggregate badges (share_count / agent_share_count / pending_join_request_count) are not transiently blanked when a detail response omits them. - OrganizationSettingsModal clears the previous organization context when reopening in edit mode for a different org, avoiding a stale frame before the detail request resolves. Adds pure-helper unit tests for mergeById and reviewMemberCountDelta. --- frontend/src/stores/organization.ts | 29 +++++++++++++-- frontend/src/stores/organizationState.test.ts | 37 ++++++++++++++++++- frontend/src/stores/organizationState.ts | 27 ++++++++++++++ .../OrganizationSettingsModal.vue | 11 +++++- 4 files changed, 97 insertions(+), 7 deletions(-) diff --git a/frontend/src/stores/organization.ts b/frontend/src/stores/organization.ts index 838926cd..f7094352 100644 --- a/frontend/src/stores/organization.ts +++ b/frontend/src/stores/organization.ts @@ -44,7 +44,12 @@ import { requestRoleUpgrade as requestRoleUpgradeApi } from '@/api/organization' import { createVersionedRequestCoordinator } from './versionedRequest' -import { applyOrganizationResourceDelta, upsertById } from './organizationState' +import { + applyOrganizationResourceDelta, + upsertById, + mergeById, + reviewMemberCountDelta +} from './organizationState' export const useOrganizationStore = defineStore('organization', () => { // State @@ -148,6 +153,19 @@ export const useOrganizationStore = defineStore('organization', () => { } } + /** + * Merge a detail payload (which may omit list-only aggregate fields) onto the + * existing card instead of replacing it, so badges like share_count survive. + */ + function mergeOrganizationDetail(organization: Organization) { + organizationsRequest.invalidate() + organizationsLoadedAt = 0 + const next = mergeById(organizations.value, organization) + organizations.value = next + currentOrganization.value = + next.find(org => org.id === organization.id) ?? organization + } + function adjustOrganizationResourceCount( organizationId: string, resource: 'knowledge_bases' | 'agents', @@ -685,13 +703,16 @@ export const useOrganizationStore = defineStore('organization', () => { async function reviewOrganizationJoinRequest( organizationId: string, requestId: string, - request: ReviewJoinRequestRequest + request: ReviewJoinRequestRequest, + options?: { requestType?: 'join' | 'upgrade' } ): Promise> { const response = await reviewJoinRequestApi(organizationId, requestId, request) if (response.success) { const organization = organizations.value.find(org => org.id === organizationId) patchOrganization(organizationId, { - member_count: (organization?.member_count ?? 0) + (request.approved ? 1 : 0), + member_count: + (organization?.member_count ?? 0) + + reviewMemberCountDelta(request.approved, options?.requestType), pending_join_request_count: Math.max( 0, (organization?.pending_join_request_count ?? 0) - 1 @@ -719,7 +740,7 @@ export const useOrganizationStore = defineStore('organization', () => { */ function setCurrentOrganization(org: Organization | null) { if (org) { - upsertOrganization(org) + mergeOrganizationDetail(org) void fetchOrganizations({ force: true }) } else { currentOrganization.value = null diff --git a/frontend/src/stores/organizationState.test.ts b/frontend/src/stores/organizationState.test.ts index 6447d629..0cb54fbc 100644 --- a/frontend/src/stores/organizationState.test.ts +++ b/frontend/src/stores/organizationState.test.ts @@ -2,7 +2,9 @@ import assert from 'node:assert/strict' import test from 'node:test' import { applyOrganizationResourceDelta, - upsertById + upsertById, + mergeById, + reviewMemberCountDelta } from './organizationState.ts' test('upsert adds a newly joined organization to the front', () => { @@ -54,3 +56,36 @@ test('resource counts never become negative when a share is removed', () => { assert.equal(result.organizations[0].agent_share_count, 0) assert.equal(result.resourceCounts?.agents.by_organization['space-1'], 0) }) + +test('merge keeps list-only aggregate fields when a detail payload omits them', () => { + const result = mergeById( + [{ id: 'space-1', name: 'Old name', share_count: 5, agent_share_count: 2 }], + { id: 'space-1', name: 'New name' } + ) + + assert.deepEqual(result, [ + { id: 'space-1', name: 'New name', share_count: 5, agent_share_count: 2 } + ]) +}) + +test('merge inserts a brand-new organization at the front', () => { + const result = mergeById( + [{ id: 'existing', name: 'Existing' }], + { id: 'new', name: 'New' } + ) + + assert.deepEqual(result.map(organization => organization.id), ['new', 'existing']) +}) + +test('approving a new join request adds one member', () => { + assert.equal(reviewMemberCountDelta(true, 'join'), 1) +}) + +test('approving a role upgrade does not change member_count', () => { + assert.equal(reviewMemberCountDelta(true, 'upgrade'), 0) +}) + +test('rejecting any request does not change member_count', () => { + assert.equal(reviewMemberCountDelta(false, 'join'), 0) + assert.equal(reviewMemberCountDelta(false, 'upgrade'), 0) +}) diff --git a/frontend/src/stores/organizationState.ts b/frontend/src/stores/organizationState.ts index e3ee0fee..d475db28 100644 --- a/frontend/src/stores/organizationState.ts +++ b/frontend/src/stores/organizationState.ts @@ -5,6 +5,33 @@ export function upsertById(items: T[], item: T): T[] { : [item, ...items] } +/** + * Merge a (possibly partial) organization payload onto the existing card. + * + * Detail endpoints (e.g. GET /organizations/:id) may omit list-only aggregate + * fields such as share_count / agent_share_count / pending_join_request_count. + * A plain replace would blank those badges until the background refresh lands, + * so when a card already exists we shallow-merge instead of overwriting it. + */ +export function mergeById(items: T[], item: T): T[] { + const existing = items.find(entry => entry.id === item.id) + const merged = existing ? { ...existing, ...item } : item + return upsertById(items, merged) +} + +/** + * How much a join-request review changes the organization's member_count. + * + * Only brand-new "join" approvals add a member. Approving an "upgrade" request + * (an existing member asking for a higher role) must not change member_count. + */ +export function reviewMemberCountDelta( + approved: boolean, + requestType: 'join' | 'upgrade' | undefined +): number { + return approved && requestType !== 'upgrade' ? 1 : 0 +} + interface OrganizationResourceCounts { knowledge_bases: { by_organization: Record } agents: { by_organization: Record } diff --git a/frontend/src/views/organization/OrganizationSettingsModal.vue b/frontend/src/views/organization/OrganizationSettingsModal.vue index a0a78e80..06d50bcb 100644 --- a/frontend/src/views/organization/OrganizationSettingsModal.vue +++ b/frontend/src/views/organization/OrganizationSettingsModal.vue @@ -1447,7 +1447,8 @@ const handleApproveRequest = async (req: JoinRequestResponse, assignRole: 'viewe const res = await orgStore.reviewOrganizationJoinRequest( props.orgId, req.id, - { approved: true, role: assignRole } + { approved: true, role: assignRole }, + { requestType: req.request_type } ) if (res.success) { MessagePlugin.success(t('organization.settings.approveSuccess')) @@ -1472,7 +1473,8 @@ const handleRejectRequest = async (req: JoinRequestResponse) => { const res = await orgStore.reviewOrganizationJoinRequest( props.orgId, req.id, - { approved: false } + { approved: false }, + { requestType: req.request_type } ) if (res.success) { MessagePlugin.success(t('organization.settings.rejectSuccess')) @@ -1871,6 +1873,11 @@ watch(() => props.visible, (newVal) => { inviteCode.value = '' inviteCodeExpiresAt.value = null } else if (props.orgId) { + // 清空上一个组织的详情上下文,避免在 fetchOrgDetail 返回前短暂显示旧组织信息 + if (orgStore.currentOrganization?.id !== props.orgId) { + orgStore.clearCurrentOrganizationContext() + sharedKnowledgeBases.value = [] + } fetchOrgDetail() fetchMembers() fetchSharedKBs()