Skip to content

Commit a71c059

Browse files
authored
fix(organizations): return a real zero daily usage as 0 (#9449)
getOrganizationMembers mapped the per-member daily usage with a truthiness test while the limit field next to it used an explicit null test, so a usage row holding exactly zero collapsed to null and the members card fell back to its limit-only rendering. Map with != null so an existing zero row arrives as 0 and only a missing row stays null, and cover both states with a regression test.
1 parent 2d5df8f commit a71c059

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎packages/web-shared/src/lib/organizations/organization-usage.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,36 @@ describe('Organization Usage Functions', () => {
983983
expect(result.balance).toBe(-0.000001); // -1 microdollar = -0.000001 USD (0 - 1)
984984
});
985985

986+
test('should return a real zero daily usage as 0, not null', async () => {
987+
const owner = await insertTestUser();
988+
const zeroUsageMember = await insertTestUser();
989+
const noUsageRowMember = await insertTestUser();
990+
991+
const organization = await createTestOrganization('Test Org', owner.id, 100000, {}, false);
992+
await addUserToOrganization(organization.id, zeroUsageMember.id, 'member');
993+
await addUserToOrganization(organization.id, noUsageRowMember.id, 'member');
994+
995+
await updateOrganizationUserLimit(organization.id, zeroUsageMember.id, 5); // $5.00 limit
996+
997+
await db.insert(organization_user_usage).values({
998+
organization_id: organization.id,
999+
kilo_user_id: zeroUsageMember.id,
1000+
usage_date: sql`CURRENT_DATE`, // Today
1001+
limit_type: 'daily',
1002+
microdollar_usage: 0, // Row exists, nothing spent
1003+
});
1004+
1005+
const members = await getOrganizationMembers(organization.id);
1006+
const zeroUsage = members.find(m => m.status === 'active' && m.id === zeroUsageMember.id);
1007+
const noUsageRow = members.find(m => m.status === 'active' && m.id === noUsageRowMember.id);
1008+
1009+
// A row with exactly 0 must reach the consumer as 0 so it can render
1010+
// "$0.00 / $5.00"; only a missing row is unknown.
1011+
expect(zeroUsage?.dailyUsageLimitUsd).toBe(5);
1012+
expect(zeroUsage?.currentDailyUsageUsd).toBe(0);
1013+
expect(noUsageRow?.currentDailyUsageUsd).toBeNull();
1014+
});
1015+
9861016
test('should handle null limit (unlimited) correctly', async () => {
9871017
const user = await insertTestUser();
9881018
const organization = await createTestOrganization('Test Org', user.id, 100000);

‎packages/web-shared/src/lib/organizations/organizations.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,10 @@ export async function getOrganizationMembers(
665665
member.dailyUsageLimitUsdMicrodollars != null
666666
? fromMicrodollars(member.dailyUsageLimitUsdMicrodollars)
667667
: null,
668-
currentDailyUsageUsd: member.currentDailyUsageUsdMicrodollars
669-
? fromMicrodollars(member.currentDailyUsageUsdMicrodollars)
670-
: null,
668+
currentDailyUsageUsd:
669+
member.currentDailyUsageUsdMicrodollars != null
670+
? fromMicrodollars(member.currentDailyUsageUsdMicrodollars)
671+
: null,
671672
})),
672673
...pendingInvitations.map(invitation => ({
673674
email: invitation.email,

0 commit comments

Comments
 (0)