Skip to content

Conversation

@dorner
Copy link
Collaborator

@dorner dorner commented Oct 17, 2025

This PR is the first step in the kit redesign. It moves line items from kits to the kits' item. This will set us up to make items the only table that has line items, which will make the next step (turning kit into a subclass of item) easier.

  • Add Itemizable to the Item class. This will only be used for items that represent a kit.
  • Rename the current line_items to used_line_items association inside Item - we need some way of differentiating between line items that belong to the item (as part of a kit) and line items that the item is part of (e.g. distributions, donations). There are very few uses of item.line_items so it's easier to switch the name of this association.
  • On create, attach the line items to the kit's item, not the kit. There is no way to edit a kit's line items once created.
  • Move the validation "kits must have at least one line item" to the creation service class. We can't add this to Item because there are plenty of items with no line items. Eventually we'll be able to add this back once Kit is a subclass of Item since we could look at the type.
  • Update all query code to look at items' line items, not kits'.
  • Add a migration to move all existing kits' line items to their items.

Note that this PR does not include a feature flag. Although this is a somewhat risky change, it is not difficult to switch back if we have to:

  • Every line item that belongs to an Item can only have originally been on a kit;
  • For every line item that belongs to an Item, that item will have a kit_id.

Using this, we can easily set the type and ID back to the kit if we need to roll back. Trying to feature flag this would be difficult because we are talking about changing the source of an association. Simply changing the flag won't help - we'd need a redeploy anyway if something is going wrong. If anything, it's more risky to feature flag this because the flag would have to line up with the data in the database. Anything out of sync would break things worse than the rollback.

@dorner dorner requested a review from awwaiid October 17, 2025 20:30
@dorner dorner marked this pull request as draft October 17, 2025 20:33
@dorner dorner changed the title 3652 #1: kits to items [WIP] 3652 #1: kits to items Oct 17, 2025
@dorner dorner marked this pull request as ready for review October 24, 2025 19:53
@dorner dorner changed the title [WIP] 3652 #1: kits to items 3652 #1: kits to items Oct 24, 2025
@awwaiid awwaiid self-assigned this Jan 25, 2026
@awwaiid
Copy link
Collaborator

awwaiid commented Feb 1, 2026

Note to self

Before:

graph LR
    kit[Kit A] --> kit_item[Item Kit A]
    kit --> line_item_1[Line Item 1, Qty 3]
    kit --> line_item_2[Line Item 2, Qty 7]
    line_item_1 --> item_x[Item X]
    line_item_2 --> item_y[Item Y]
Loading

After:

graph LR
    kit[Kit A] --> kit_item[Item Kit A]
    kit_item --> line_item_1[Line Item 1, Qty 3]
    kit_item --> line_item_2[Line Item 2, Qty 7]
    line_item_1 --> item_x[Item X]
    line_item_2 --> item_y[Item Y]
Loading

Copy link
Collaborator

@awwaiid awwaiid left a comment

Choose a reason for hiding this comment

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

A few fixes needed, and the big question -- can we remove include Itemizable from the Kit model?


def is_in_kit?(kits = nil)
if kits
kits.any? { |k| k.line_items.map(&:item_id).include?(id) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this need to be k.item.line_items?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm pretty sure it does need to change.

Toward this end -- how about we remove include Itemizable from the Kit model? That will stop anything from doing kit.line_items, which we shouldn't be doing.

AND items.kit_id IS NOT NULL
AND kit_items.reporting_category = 'disposable_diapers'
AND kit_line_items.itemizable_type = 'Kit';
AND kit_line_items.itemizable_type = 'Item';
Copy link
Collaborator

Choose a reason for hiding this comment

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

app/services/reports/adult_incontinence_report_service.rb def distributed_adult_incontinence_items_from_kits needs SQL updated like this

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.

2 participants