Skip to content

10129 attach endpoint doesnt error when the contract is full#3293

Merged
cp-at-mit merged 7 commits into
mainfrom
10129-attach-endpoint-doesnt-error-when-the-contract-is-full
Feb 19, 2026
Merged

10129 attach endpoint doesnt error when the contract is full#3293
cp-at-mit merged 7 commits into
mainfrom
10129-attach-endpoint-doesnt-error-when-the-contract-is-full

Conversation

@cp-at-mit
Copy link
Copy Markdown
Contributor

@cp-at-mit cp-at-mit commented Feb 11, 2026

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?

  1. Create all of the objects needed in order to have an active contract. In the Contract page in the CMS, ensure that the contract has a max capacity of 1.
  2. Run the following command in order to get the enrollment code for the contract: docker compose run --rm web python manage.py b2b_codes output --contract <contract_id>
  3. Through Django Admin, you can add the contract to one of your users.
  4. While logged in as a different user than the user from step 4, visit http://mitxonline.odl.local:8013/api/schema/swagger-ui/#/b2b/b2b_attach_create and send a request using the enrollment code from step 2.
  5. Verify that you receive a 409 status error and a message of "Contract is full."

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.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 11, 2026

OpenAPI Changes

Show/hide ## Changes for v0.yaml:
## Changes for v0.yaml:


## Changes for v1.yaml:


## Changes for v2.yaml:


Unexpected changes? Ensure your branch is up-to-date with main (consider rebasing).

cp-at-mit and others added 5 commits February 11, 2026 15:13
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.
…-full' of https://github.com/mitodl/mitxonline into 10129-attach-endpoint-doesnt-error-when-the-contract-is-full
@cp-at-mit cp-at-mit marked this pull request as ready for review February 18, 2026 17:16
Copy link
Copy Markdown
Contributor

@jkachel jkachel left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@cp-at-mit cp-at-mit merged commit a6c7b1a into main Feb 19, 2026
10 checks passed
@cp-at-mit cp-at-mit deleted the 10129-attach-endpoint-doesnt-error-when-the-contract-is-full branch February 19, 2026 18:25
@odlbot odlbot mentioned this pull request Feb 19, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants