Skip to content

Admin and User Metadata for transactional ressources and users (#5897)#6118

Merged
tvdeyen merged 8 commits intosolidusio:mainfrom
gms-electronics:metadata-draft-pr
Feb 18, 2025
Merged

Admin and User Metadata for transactional ressources and users (#5897)#6118
tvdeyen merged 8 commits intosolidusio:mainfrom
gms-electronics:metadata-draft-pr

Conversation

@fthobe
Copy link
Copy Markdown
Contributor

@fthobe fthobe commented Feb 12, 2025

Closes #5897
Concerns #5951

Breaking Changes

This feature breaks sqlite versions predating to December 2023, verify your requirements before migrating.

Introduction

Overview

This PR introduces the concept of metadata fields to solidus. The purpose of metadata fields is to allow customers and admins alike to provide additional information to transactions to either extend the informations contained or allow to insert additional data to correlate transactions with third party systems.

Examples for data can be:

  • any transaction itself such as orders, RMAs, payments and stock movements;
  • individual items contained inside an order (line items);
  • match users with 3rd party systems.

Possible Applications on a very high level are:

  • customers that want to add Purchase Order information to an order;
  • administrators that want to add a ticket number to an RMA.

The current scope of meta data are following ressources:

  • Users
  • Orders
  • Customer Returns and any from of correlated transactions
  • Payments
  • Shipments
  • Line Items

How is the additional information stored?

for each of the above resources two columns have been created:

  • customer_metadata
  • admin_metadata

The resources store the information as jsonb.

What is the access model?

Cancancan has been used to allow users to write data with the current scope just extended by the additional fields:

  • Users can create / delete / alter metadata on orders only till the payment itself is processed,
  • Admins have universal access R/W access to metadata at all stages of transactions.

Testing

Extensive test cases have been added.

Detailed Overview

Types of Meta Data and usage suggestions

Overview

Meta data can be broken down in three large topics visible in the table below:

Use Case
Administrative Metadata Information required to correlate all transactions to 3rd party systems.
Customer Metadata Information allowing customers to customize orders (eg options to store information like a text string or link to an image) or correlate transactions (such as POs).

Administrative Metadata

Transactional resources are either by nature integrated with 3rd party systems (eg Payments, Billing in Italy with eInvoicing) or by operational requirement (ERP, CRMs like Salesforce,...). Meta data as an extension on admin site for all transactional resources to connect the transactional resources to any of such systems. Given that more and more countries will switch to a form of eInvoicing in the coming years meta data allows to fetch or store additional information in those transactions.

Customer Meta Data

Customer meta data allows users to customize a cart with either administrative (PO numbers,...) or customisation information (eg an URL to a picture to customize a shirt, text strings for incisions or in our case IMEIs to relate to a subscription) allowing to significantly extend the features without changing the underlying model. Fields should be accessible on front-end and backend.

Schema

Meta Data should net a reliable outcome:
"key1" : "value1" should be as applicable as "key2" : "value2", without restriction other than the quantity of storable key value pairs and limitations of the dimension of the value given that json as a format does not pose limits we should do that server side.

Future models to reserve or block certain key names might be possible.

Scopes & Arrays

Nature of the Values

The system is agnostic to the value contained. A decision has been made to store the data in additional columns in a jsonb which by now is relatively widely supported. Some preexisting installations might have to upgrade DBs, older sqlite versions dating before Jan 2024 are therefor not supported and need to be updated.

"key1" : "value1" should lead to the same behaviour as "key2" : "value2" without requiring modification inside the code. Reasonable provisions should be taken inside the code base to edit the quantities and content size of key value pairs and appropriate errors should be returned where rate or size limits are exceeded.

Administrative Meta Data

Allow appropriate roles to customize resources with private fields not end-user accessible.
R/W = Read and Write Access

Admin Use Case
Order R/W Add 3rd party system reference fields such as PO-Order or Dropshipping reference
Line Item R/W Add information to customize object such as link to an image for customization of a product or relate to a 3rd party system reference
RMA R/W Add 3rd party system reference fields such as PO-Order or Dropshipping reference
Payment R/W Add 3rd party system reference fields such as PO-Order or Dropshipping reference
Order R/W Add 3rd party system reference fields such as PO-Order or Dropshipping reference
Shipment R/W Add 3rd party system reference fields such as a delivery note
Payment R/W Add 3rd party system reference fields for payment conciliation
User R/W Add a 3rd party reference

Customer Meta Data

Allowing users to customize an incomplete order (or I'd say a cart) and call the information after a completed order. Admins should in an ideal case be allowed to edit those information after purchase, but I feel that can wait if technical limitations are currently in place.

User Admin Use Case
Order R/W R/W Add 3rd party system reference fields such as PO-Order,
Line Item R/W R/W Add information to customize object such as link to an image for customization of a product or relate to a 3rd party system reference
User R/W R/W Add additional User Fields or References

@github-actions github-actions Bot added changelog:solidus_api Changes to the solidus_api gem changelog:solidus_core Changes to the solidus_core gem labels Feb 12, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 70.24070% with 136 lines in your changes missing coverage. Please review.

Project coverage is 88.73%. Comparing base (0ef2e29) to head (3dfbeb6).
Report is 46 commits behind head on main.

Files with missing lines Patch % Lines
...els/spree/permission_sets/configuration_display.rb 0.00% 30 Missing ⚠️
.../models/spree/permission_sets/dashboard_display.rb 0.00% 19 Missing ⚠️
core/lib/spree/permission_sets.rb 0.00% 5 Missing ⚠️
core/lib/spree/permission_sets/base.rb 0.00% 5 Missing ⚠️
...lib/spree/permission_sets/configuration_display.rb 0.00% 5 Missing ⚠️
.../spree/permission_sets/configuration_management.rb 0.00% 5 Missing ⚠️
...ore/lib/spree/permission_sets/dashboard_display.rb 0.00% 5 Missing ⚠️
core/lib/spree/permission_sets/default_customer.rb 0.00% 5 Missing ⚠️
core/lib/spree/permission_sets/order_display.rb 0.00% 5 Missing ⚠️
core/lib/spree/permission_sets/order_management.rb 0.00% 5 Missing ⚠️
... and 11 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6118      +/-   ##
==========================================
- Coverage   89.28%   88.73%   -0.55%     
==========================================
  Files         813      830      +17     
  Lines       17906    18062     +156     
==========================================
+ Hits        15987    16028      +41     
- Misses       1919     2034     +115     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fthobe fthobe marked this pull request as ready for review February 12, 2025 18:04
@fthobe fthobe requested a review from a team as a code owner February 12, 2025 18:04
@JustShah JustShah force-pushed the metadata-draft-pr branch 4 times, most recently from 6a23bc0 to dc6114a Compare February 13, 2025 13:58
@tvdeyen tvdeyen changed the title feat[Core]: Admin and User Metadata for transactional ressources and users (#5897) Admin and User Metadata for transactional ressources and users (#5897) Feb 14, 2025
@tvdeyen tvdeyen added this to the 4.5 milestone Feb 14, 2025
Copy link
Copy Markdown
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

Thanks. I like the feature. The only request I have is the coordination of commits in this PR. Tests should be included with the commit that makes the introduction/change. You could also add the tests first, make them pending and then implement commit by commit, removing the pending flag, but this is your decision.

Comment thread core/spec/lib/spree/app_configuration_spec.rb Outdated
Comment thread core/spec/lib/spree/app_configuration_spec.rb Outdated
Comment thread core/spec/lib/spree/app_configuration_spec.rb Outdated
Comment thread core/app/models/concerns/spree/metadata.rb Outdated
Comment thread core/app/models/concerns/spree/metadata.rb
Comment thread api/app/controllers/spree/api/orders_controller.rb
Comment thread api/app/controllers/spree/api/base_controller.rb
Comment thread api/spec/requests/spree/api/customer_returns_spec.rb
Comment thread core/lib/spree/app_configuration.rb
Comment thread core/spec/models/spree/order_spec.rb Outdated
@fthobe
Copy link
Copy Markdown
Contributor Author

fthobe commented Feb 17, 2025

@shahmayur001 it was discussed with Thomas to squash everything at the end.

@tvdeyen
Copy link
Copy Markdown
Member

tvdeyen commented Feb 17, 2025

@shahmayur001 it was discussed with Thomas to squash everything at the end.

To be fully transparent I offered to squash merge this if @shahmayur001 is not able to rewrite the git history to our standards.

@JustShah JustShah force-pushed the metadata-draft-pr branch 2 times, most recently from 403fa02 to 7131bae Compare February 18, 2025 05:53
@github-actions github-actions Bot added changelog:solidus_backend Changes to the solidus_backend gem changelog:solidus_admin changelog:solidus_legacy_promotions Changes to the solidus_legacy_promotions gem changelog:solidus_promotions Changes to the solidus_promotions gem and removed changelog:solidus_backend Changes to the solidus_backend gem changelog:solidus_admin changelog:solidus_legacy_promotions Changes to the solidus_legacy_promotions gem changelog:solidus_promotions Changes to the solidus_promotions gem labels Feb 18, 2025
@github-actions github-actions Bot added changelog:solidus_backend Changes to the solidus_backend gem changelog:solidus_admin changelog:solidus_legacy_promotions Changes to the solidus_legacy_promotions gem changelog:solidus_promotions Changes to the solidus_promotions gem labels Feb 18, 2025
This migration adds customer_metadata and admin_metadata to various tables
… and length

Introduced validations and configurable limits for metadata attributes
(`customer_metadata` and `admin_metadata`) to ensure consistency and prevent
overly large or complex structures.

**Implementation:**
- Set default values for `customer_metadata` and `admin_metadata` to empty hashes (`{}`) for proper initialization.
- Implemented validation to limit the number of keys, and the length of keys and values in metadata.
- Added configuration checks to ensure metadata does not exceed the limits defined in `Spree::Config`.
- Introduced preferences: `meta_data_max_keys`, `meta_data_max_key_length`, and `meta_data_max_value_length` to control metadata size:
  - `meta_data_max_keys`: Max keys allowed (default: 6).
  - `meta_data_max_key_length`: Max key length (default: 16).
  - `meta_data_max_value_length`: Max value length (default: 256).
- Created a shared concern to handle metadata validation logic.
- Added tests to ensure default values and validation logic work correctly.
- Created a shared example that can be used in other specs to ensure consistent testing of metadata validation across various parts of the system.
Call the metadata concern to various models to validate and add admin and customer metadata
@github-actions github-actions Bot removed changelog:solidus_backend Changes to the solidus_backend gem changelog:solidus_admin changelog:solidus_legacy_promotions Changes to the solidus_legacy_promotions gem changelog:solidus_promotions Changes to the solidus_promotions gem labels Feb 18, 2025
@JustShah JustShah force-pushed the metadata-draft-pr branch 2 times, most recently from b8ca440 to b08d5ee Compare February 18, 2025 06:37
…tadata handling

Update the API configuration to permit the `customer_metadata` attribute across various models,
such as orders, payments, shipments, and return authorizations. This change allows better handling of customer metadata within API requests,
ensuring consistent storage and validation of metadata attributes. Additionally, it updates the `preference` configurations for models to include `customer_metadata`.

**Implementation:**
- Added `customer_metadata` to the permitted attributes for several API resources.
- Updated various `preference` settings, including `order_attributes`, `payment_attributes`, `shipment_attributes`, etc., to include `customer_metadata`.
- Ensured that `customer_metadata` is correctly permitted in the API parameters for each relevant model, allowing admins and users to store custom metadata.
- This change improves the flexibility of metadata management within the API, enabling dynamic handling and easier customizations.
User cannot update customer_metadata after the status of order is complete
Introduce the `admin_metadata` attribute to support dynamic metadata handling for admins.
It enables configuration of metadata via API preferences, and ensures that metadata fields
are properly permitted for admin users through new helper methods.

**Implementation:**
- Added `admin_metadata` to resources for admins.
- Integrated `metadata_permit_parameters` and `metadata_api_parameters preference` in API configuration.
- Implemented `permitted_<resource>_attributes` and `<method_name>_attributes` methods for dynamic metadata permissions.
- Added `extract_metadata` in `line_items_controller` to handle metadata fields.
This configuration adds the ability to enable or disable metadata restrictions as needed.

Following configurations are possible:
- `meta_data_validation_enabled` : Enables or disables restrictions globally.
@fthobe
Copy link
Copy Markdown
Contributor Author

fthobe commented Feb 18, 2025

@tvdeyen this should be all, all comments are resolved, the failed tests are either related to

And are outside of the impact zone of this PR.

@JustShah
Copy link
Copy Markdown
Contributor

@kennyadsl @tvdeyen Thank you so much for your guidance and support in helping us become more familiar with the Solidus community. As you know, we are completely new contributors, and we’ve likely made quite a few mistakes but we truly appreciate your patience. We’re actively learning from your feedback, taking notes, and doing our best to avoid repeating the same mistakes in the future. Your patience and insights are truly appreciated!

Copy link
Copy Markdown
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

Thank you for your patience and the adjusting of your commits. This is very much appreciated

Comment thread core/app/models/concerns/spree/metadata.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog:solidus_api Changes to the solidus_api gem changelog:solidus_core Changes to the solidus_core gem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MetaData support for Products, Orders and other resources

3 participants