Skip to content

Conversation

@mitchelljkotler
Copy link
Member

@mitchelljkotler mitchelljkotler commented Dec 23, 2025

These are the same changes as the MuckRock repo, except for premium credits instead of requests. Also, some of the code is in the library squarelet-auth which DocumentCloud uses but MuckRock does not for historical reasons

  • Organizations now track parents, members and share resources. These are set on Accounts and are set via the API
  • When calculating how many credits an organization has left, or is using, credits from parents which have sharing enabled will be added in. The order is your own monthly credits, your own non-monthly credits, parent's monthly credits, parent's non-monthly credits, group's monthly credits, group's non-monthly credits

@mitchelljkotler mitchelljkotler marked this pull request as ready for review January 6, 2026 19:14
Comment on lines 172 to 206
ai_credit_count["monthly"] = min(amount, organization.monthly_ai_credits)
amount -= ai_credit_count["monthly"]

ai_credit_count["regular"] = min(amount, organization.number_ai_credits)
amount -= ai_credit_count["regular"]

if amount > 0:
raise InsufficientAICreditsError(amount)

organization.monthly_ai_credits -= ai_credit_count["monthly"]
organization.number_ai_credits -= ai_credit_count["regular"]
organization.save()

# Then deduct from parent resources
if parent:
parent_monthly = min(amount, parent.monthly_ai_credits)
ai_credit_count["monthly"] += parent_monthly
amount -= parent_monthly
parent.monthly_ai_credits -= parent_monthly

parent_regular = min(amount, parent.number_ai_credits)
ai_credit_count["regular"] += parent_regular
amount -= parent_regular
parent.number_ai_credits -= parent_regular
parent.save()

# Then deduct from group resources
for group in groups:
group_monthly = min(amount, group.monthly_ai_credits)
ai_credit_count["monthly"] += group_monthly
amount -= group_monthly
group.monthly_ai_credits -= group_monthly

group_regular = min(amount, group.number_ai_credits)
ai_credit_count["regular"] += group_regular
amount -= group_regular
group.number_ai_credits -= group_regular
group.save()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments about duplicated logic pointing to a refactoring opportunity. Seeing this logic shared between spending requests and AI credits makes that even more apparent! Doesn't need to get addressed now, but let's make sure we keep this in mind this during our payments refactor.

@duckduckgrayduck
Copy link
Contributor

duckduckgrayduck commented Jan 10, 2026

I correctly am able to see that MuckRock and MuckRock child are on the organization plan now, so I believe the latest squarelet fixed that integration. However, MuckRock child only shows 5000 AI credits though, instead of 10,000 (5k from itself and 5k from its parent). Requests does show the correct totals - child 100 requests, parent 50.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants