feat(companies): show member + yacht counts on list page
listCompanies returns memberCount (active companyMemberships) and yachtCount (yachts where currentOwnerType=company), each fetched as a parallel grouped count after the main page query. Two new badge columns in company-columns render them between the tax-id and status columns. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { and, eq, ilike, or, sql } from 'drizzle-orm';
|
||||
import { and, count, eq, ilike, inArray, isNull, or, sql } from 'drizzle-orm';
|
||||
import { db } from '@/lib/db';
|
||||
import { companies, companyTags } from '@/lib/db/schema/companies';
|
||||
import { companies, companyMemberships, companyTags } from '@/lib/db/schema/companies';
|
||||
import type { Company } from '@/lib/db/schema/companies';
|
||||
import { yachts } from '@/lib/db/schema/yachts';
|
||||
import { withTransaction } from '@/lib/db/utils';
|
||||
import { buildListQuery } from '@/lib/db/query-builder';
|
||||
import { createAuditLog } from '@/lib/audit';
|
||||
@@ -238,7 +239,41 @@ export async function listCompanies(portId: string, query: ListCompaniesInput) {
|
||||
archivedAtColumn: companies.archivedAt,
|
||||
});
|
||||
|
||||
return result;
|
||||
if (result.data.length === 0) return result;
|
||||
|
||||
const ids = result.data.map((r) => r.id);
|
||||
|
||||
const [memberCounts, yachtCounts] = await Promise.all([
|
||||
db
|
||||
.select({ companyId: companyMemberships.companyId, count: count() })
|
||||
.from(companyMemberships)
|
||||
.where(and(inArray(companyMemberships.companyId, ids), isNull(companyMemberships.endDate)))
|
||||
.groupBy(companyMemberships.companyId),
|
||||
db
|
||||
.select({ ownerId: yachts.currentOwnerId, count: count() })
|
||||
.from(yachts)
|
||||
.where(
|
||||
and(
|
||||
eq(yachts.portId, portId),
|
||||
eq(yachts.currentOwnerType, 'company'),
|
||||
inArray(yachts.currentOwnerId, ids),
|
||||
isNull(yachts.archivedAt),
|
||||
),
|
||||
)
|
||||
.groupBy(yachts.currentOwnerId),
|
||||
]);
|
||||
|
||||
const memberCountMap = new Map(memberCounts.map((r) => [r.companyId, r.count]));
|
||||
const yachtCountMap = new Map(yachtCounts.map((r) => [r.ownerId, r.count]));
|
||||
|
||||
return {
|
||||
...result,
|
||||
data: result.data.map((row) => ({
|
||||
...row,
|
||||
memberCount: memberCountMap.get(row.id) ?? 0,
|
||||
yachtCount: yachtCountMap.get(row.id) ?? 0,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// ─── Autocomplete ────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user