10129 attach endpoint doesnt error when the contract is full#3293
Merged
cp-at-mit merged 7 commits intoFeb 19, 2026
Merged
Conversation
Extract helper methods from AttachContractApi (_get_valid_discount, _get_eligible_contracts, _attach_user_to_contracts, _get_active_user_contracts) to simplify the main flow and avoid using request inside helpers. Validate the enrollment code early, return 404 for invalid/expired codes, return 201 when user is attached to new contracts, 200 if already attached, and 409 if contracts are valid but have no available seats. Also normalize use of a local user variable and ensure user save/contract redemptions are recorded.
Modify the B2B enrollment attachment test to simulate a contract filled to capacity before attempting attachment. Create and add an existing user to the contract, expect a 409 response with "Contract is full.", and assert the attach_user handler is not called and the new user is not attached. Update assertions/messages to reflect the contract-full business logic.
OpenAPI ChangesShow/hide ## Changes for v0.yaml:Unexpected changes? Ensure your branch is up-to-date with |
Detect and return HTTP 409 when an enrollment code belongs to a contract that is full even if the code lookup would otherwise fail. Update AttachContractApi to catch missing Discount, inspect related contracts for active/full status, and return a 409 with a clear message when appropriate. Refactor helper signatures and behavior: _get_valid_discount now raises on miss (and centralizes the query), _get_eligible_contracts and _attach_user_to_contracts take user first, and a reusable _get_active_user_contracts helper is added to avoid repeated queries; responses now reuse a serialized_contracts result. Add a test (test_b2b_contract_attachment_full_contract_with_used_code) to cover the scenario where a code was already used and the contract is full. Also add debug print statements in b2b_codes management command to aid troubleshooting remaining discounts and learners.
for more information, see https://pre-commit.ci
…-full' of https://github.com/mitodl/mitxonline into 10129-attach-endpoint-doesnt-error-when-the-contract-is-full
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What are the relevant tickets?
https://github.com/mitodl/hq/issues/10129
Description (What does it do?)
The /api/v0/b2b/attach/ endpoint will now return a 409 status and message: "Contract is full." if the contract specified is full and has no capacity for the user.
How can this be tested?