fix: move team credits to org on upgarde (#24386)

* move credits from team to org

* add test

---------

Co-authored-by: CarinaWolli <[email protected]>
Co-authored-by: sean-brydon <[email protected]>
This commit is contained in:
authored and GitHub committed 2025-10-13 09:30:22 +00:00
1 parent b881e025f6
commit 3bcaf0ec22
3 files changed
+116 -1

No files matched your search

@@ -689,4 +689,65 @@ export class CreditService {
totalCreditsUsedThisMonth,
};
}
async moveCreditsFromTeamToOrg({ teamId, orgId }: { teamId: number; orgId: number }) {
return await prisma.$transaction(async (tx) => {
// Get team's credit balance
const teamCreditBalance = await CreditsRepository.findCreditBalance({ teamId }, tx);
if (!teamCreditBalance || teamCreditBalance.additionalCredits <= 0) {
log.info("No credits to transfer from team to org", { teamId, orgId });
return;
}
// Get or create org's credit balance
let orgCreditBalance = await CreditsRepository.findCreditBalance({ teamId: orgId }, tx);
if (!orgCreditBalance) {
orgCreditBalance = await CreditsRepository.createCreditBalance(
{
teamId: orgId,
},
tx
);
}
const creditsToTransfer = teamCreditBalance.additionalCredits;
// Transfer credits from team to org
await CreditsRepository.updateCreditBalance(
{
teamId,
data: {
additionalCredits: 0,
},
},
tx
);
await CreditsRepository.updateCreditBalance(
{
teamId: orgId,
data: {
additionalCredits: {
increment: creditsToTransfer,
},
},
},
tx
);
log.info("Successfully transferred credits from team to org", {
teamId,
orgId,
creditsTransferred: creditsToTransfer,
});
return {
creditsTransferred: creditsToTransfer,
teamId,
orgId,
};
});
}
}