Price Book
Category Attributes
Define custom attributes per price book category for structured item data.
7 min readCategory Attributes Feature Implementation
Overview
This feature allows category markups to store default attributes that will be automatically prepopulated when a category is selected for new items. When items are saved with attributes, those attributes are automatically merged into the category data structure for future use.
Implementation Summary
1. Updated CategoryMarkup Data Structure
File:
HomeImprovment/apps/web/src/pages/settings/CategoryMarkupSettings.tsxAdded
attributes field to the CategoryMarkup interface:interface CategoryMarkup {
id: string
category: string
markup: number
description?: string
attributes?: Record<string, string> // NEW: Category-level attributes that will be prepopulated
isActive: boolean
createdAt?: string
context: 'inventory' | 'price_book' | 'services' | 'all'
}
2. Category Settings UI Enhancements
File:
HomeImprovment/apps/web/src/pages/settings/CategoryMarkupSettings.tsxAdded AttributesManager to Edit Modal
- Added
AttributesManagercomponent import - Added "Default Attributes (Optional)" field to the category edit form
- Users can now add, edit, and remove default attributes for each category
Added Attributes Column to Category Table
- Displays up to 2 attributes inline with tags
- Shows "+X more" indicator when more than 2 attributes exist
- Shows "No attributes" when category has no attributes
3. Inventory Item Attribute Prepopulation & Merging
File:
HomeImprovment/apps/web/src/pages/inventory/Inventory.tsxPrepopulate Attributes on Category Selection
When a user selects a category for a new inventory item:
- Checks if the category has default attributes defined
- Prepopulates the attributes field with category defaults (only for new items, not when editing)
- Shows info message: "Prepopulated attributes from category [name]"
Merge Attributes When Saving Items
When a user saves an inventory item with attributes:
- Checks if the item has a category and attributes
- Finds the matching category markup (inventory or all context)
- Merges the item's attributes with existing category attributes
- Updates the category markup in business settings automatically
- This enriches the category's attribute collection over time
4. Price Book Item Attribute Prepopulation & Merging
File:
HomeImprovment/apps/web/src/pages/priceBook/PriceBook.tsxPrepopulate Attributes on Category Selection
When a user selects a category for a new price book item:
- Checks if the category has default attributes defined
- Prepopulates the attributes field with category defaults (only for new items)
- Shows info message: "Prepopulated attributes from category [name]"
Merge Attributes When Saving Items
When a user saves a price book item with attributes:
- Checks if the item has a category and attributes
- Finds the matching category markup (price_book or all context)
- Merges the item's attributes with existing category attributes
- Updates the category markup in business settings automatically
- This enriches the category's attribute collection over time
5. Added Required Imports
- Added
AttributesManagercomponent to CategoryMarkupSettings - Added
useUpdateBusinessSettingsMutationto Inventory.tsx - Added
useUpdateBusinessSettingsMutationto PriceBook.tsx
How It Works (User Flow)
Setting Up Category Attributes
- Go to Settings → Category Markup Settings
- Edit or create a category
- Add default attributes in the "Default Attributes (Optional)" section
- Save the category
Using Category Attributes in Inventory
- Create a new inventory item
- Select a category from the dropdown
- Attributes field auto-populates with category defaults
- User can edit or add more attributes
- When saved, any new attributes are merged back into the category
Using Category Attributes in Price Book
- Create a new price book item
- Select a category from the dropdown
- Attributes field auto-populates with category defaults
- User can edit or add more attributes
- When saved, any new attributes are merged back into the category
Viewing Category Attributes
- Go to Settings → Category Markup Settings
- View the "Attributes" column in the table
- See which attributes are associated with each category
- Click edit to modify attributes directly
Benefits
- Consistency: Ensures consistent attributes across items in the same category
- Efficiency: Reduces manual data entry for common attributes
- Flexibility: Users can still override or add custom attributes per item
- Learning System: Attributes automatically accumulate as items are created
- Centralized Management: Edit attributes for all future items from category settings
Technical Details
Attribute Merging Strategy
- Uses spread operator to merge attributes:
{ ...existingAttributes, ...newAttributes } - New attribute values override existing ones with the same key
- Preserves all existing attributes that don't conflict
- Updates happen silently in the background (no user notification for merge)
Context-Aware Category Matching
- Inventory items only use markups with context: 'inventory' or 'all'
- Price book items only use markups with context: 'price_book' or 'all'
- Services would use markups with context: 'services' or 'all'
Performance Considerations
- Attribute prepopulation only happens for new items (empty attributes object)
- Attribute merging happens after successful item creation/update
- Failed attribute merges are logged but don't block item creation
- Business settings update is async and doesn't block the UI
Future Enhancements (Optional)
- Attribute Templates: Create reusable attribute templates for common patterns
- Attribute Validation: Set required attributes per category
- Attribute Type Constraints: Define data types (text, number, date, etc.)
- Bulk Attribute Update: Update attributes across multiple items in a category
- Attribute History: Track changes to category attributes over time
- Attribute Suggestions: AI-powered suggestions based on item names/descriptions
Testing Checklist
- Create category with default attributes in settings
- Create inventory item with that category
- Verify attributes are prepopulated
- Save inventory item with additional attributes
- Verify category attributes updated
- Create price book item with same category
- Verify attributes include both original and merged ones
- Edit category attributes directly in settings
- Verify changes appear for new items
- Verify attributes display correctly in category table
Files Modified
-
HomeImprovment/apps/web/src/pages/settings/CategoryMarkupSettings.tsx- Added attributes field to CategoryMarkup interface
- Added AttributesManager to edit form
- Added attributes column to table
-
HomeImprovment/apps/web/src/pages/inventory/Inventory.tsx- Updated handleCategoryChange to prepopulate attributes
- Updated handleItemSubmit to merge attributes into category
- Added useUpdateBusinessSettingsMutation import
-
HomeImprovment/apps/web/src/pages/priceBook/PriceBook.tsx- Updated handleCategoryChange to prepopulate attributes
- Updated handleModalOk to merge attributes into category
- Added useUpdateBusinessSettingsMutation import
Database/API Changes
No database schema changes required. The feature uses the existing
categoryMarkups field in business settings, which is a JSON field that can accommodate the new attributes property.Deployment Notes
- No migration required
- Existing categories without attributes will continue to work normally
- Feature is backward compatible
- No breaking changes to existing functionality