@merchi/sdk 1.40.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +43 -0
- package/.turbo/turbo-build.log +1 -0
- package/.turbo/turbo-test.log +849 -0
- package/LICENSE +21 -0
- package/README.md +244 -0
- package/coverage/clover.xml +5057 -0
- package/coverage/coverage-final.json +133 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +191 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov-report/src/constants/auto_assign_production_on_actions.ts.html +103 -0
- package/coverage/lcov-report/src/constants/call_to_actions.ts.html +127 -0
- package/coverage/lcov-report/src/constants/discount_types.ts.html +100 -0
- package/coverage/lcov-report/src/constants/domain_types.ts.html +121 -0
- package/coverage/lcov-report/src/constants/errors.ts.html +181 -0
- package/coverage/lcov-report/src/constants/event_types.ts.html +100 -0
- package/coverage/lcov-report/src/constants/field_types.ts.html +133 -0
- package/coverage/lcov-report/src/constants/index.html +551 -0
- package/coverage/lcov-report/src/constants/index.ts.html +238 -0
- package/coverage/lcov-report/src/constants/input_types.ts.html +124 -0
- package/coverage/lcov-report/src/constants/inventory_statuses.ts.html +103 -0
- package/coverage/lcov-report/src/constants/invoice_types.ts.html +100 -0
- package/coverage/lcov-report/src/constants/item_types.ts.html +100 -0
- package/coverage/lcov-report/src/constants/job/drafting_status.ts.html +106 -0
- package/coverage/lcov-report/src/constants/job/index.html +176 -0
- package/coverage/lcov-report/src/constants/job/payment_status.ts.html +106 -0
- package/coverage/lcov-report/src/constants/job/production_status.ts.html +121 -0
- package/coverage/lcov-report/src/constants/job/shipment_status.ts.html +118 -0
- package/coverage/lcov-report/src/constants/job/supply_chain_request_status.ts.html +103 -0
- package/coverage/lcov-report/src/constants/job_priorities.ts.html +103 -0
- package/coverage/lcov-report/src/constants/job_status.ts.html +232 -0
- package/coverage/lcov-report/src/constants/job_types.ts.html +133 -0
- package/coverage/lcov-report/src/constants/notification_sections.ts.html +124 -0
- package/coverage/lcov-report/src/constants/notification_types.ts.html +385 -0
- package/coverage/lcov-report/src/constants/notification_urgencies.ts.html +106 -0
- package/coverage/lcov-report/src/constants/payment_types.ts.html +115 -0
- package/coverage/lcov-report/src/constants/platform.ts.html +103 -0
- package/coverage/lcov-report/src/constants/product_types.ts.html +163 -0
- package/coverage/lcov-report/src/constants/rights.ts.html +100 -0
- package/coverage/lcov-report/src/constants/robots_meta_directives.ts.html +118 -0
- package/coverage/lcov-report/src/constants/roles.ts.html +223 -0
- package/coverage/lcov-report/src/constants/shipment_companies.ts.html +142 -0
- package/coverage/lcov-report/src/constants/shipment_services.ts.html +94 -0
- package/coverage/lcov-report/src/constants/system_roles.ts.html +94 -0
- package/coverage/lcov-report/src/constants/theme_status.ts.html +100 -0
- package/coverage/lcov-report/src/constants/user_types.ts.html +136 -0
- package/coverage/lcov-report/src/constants/white_label_accessibilities.ts.html +103 -0
- package/coverage/lcov-report/src/cookie.ts.html +133 -0
- package/coverage/lcov-report/src/entities/address.ts.html +280 -0
- package/coverage/lcov-report/src/entities/agent_conversation.ts.html +202 -0
- package/coverage/lcov-report/src/entities/agent_skill.ts.html +382 -0
- package/coverage/lcov-report/src/entities/agent_skill_approval.ts.html +163 -0
- package/coverage/lcov-report/src/entities/agent_skill_version.ts.html +277 -0
- package/coverage/lcov-report/src/entities/agent_token_usage.ts.html +349 -0
- package/coverage/lcov-report/src/entities/assignment.ts.html +382 -0
- package/coverage/lcov-report/src/entities/automatic_payment_relationship.ts.html +172 -0
- package/coverage/lcov-report/src/entities/bank.ts.html +220 -0
- package/coverage/lcov-report/src/entities/cart.ts.html +391 -0
- package/coverage/lcov-report/src/entities/cart_item.ts.html +340 -0
- package/coverage/lcov-report/src/entities/cart_shipment_group.ts.html +148 -0
- package/coverage/lcov-report/src/entities/cart_shipment_quote.ts.html +184 -0
- package/coverage/lcov-report/src/entities/category.ts.html +262 -0
- package/coverage/lcov-report/src/entities/company.ts.html +1276 -0
- package/coverage/lcov-report/src/entities/company_invitation.ts.html +184 -0
- package/coverage/lcov-report/src/entities/country_tax.ts.html +223 -0
- package/coverage/lcov-report/src/entities/discount.ts.html +235 -0
- package/coverage/lcov-report/src/entities/discount_group.ts.html +214 -0
- package/coverage/lcov-report/src/entities/domain.ts.html +3349 -0
- package/coverage/lcov-report/src/entities/domain_chat_settings.ts.html +331 -0
- package/coverage/lcov-report/src/entities/domain_invitation.ts.html +193 -0
- package/coverage/lcov-report/src/entities/domain_tag.ts.html +211 -0
- package/coverage/lcov-report/src/entities/draft.ts.html +322 -0
- package/coverage/lcov-report/src/entities/draft_comment.ts.html +274 -0
- package/coverage/lcov-report/src/entities/draft_preview.ts.html +196 -0
- package/coverage/lcov-report/src/entities/draft_preview_layer.ts.html +148 -0
- package/coverage/lcov-report/src/entities/draft_template.ts.html +328 -0
- package/coverage/lcov-report/src/entities/email_address.ts.html +157 -0
- package/coverage/lcov-report/src/entities/email_counter.ts.html +145 -0
- package/coverage/lcov-report/src/entities/enrolled_domain.ts.html +205 -0
- package/coverage/lcov-report/src/entities/exchange_rate.ts.html +151 -0
- package/coverage/lcov-report/src/entities/file.ts.html +580 -0
- package/coverage/lcov-report/src/entities/index.html +1271 -0
- package/coverage/lcov-report/src/entities/internal_tag.ts.html +238 -0
- package/coverage/lcov-report/src/entities/inventory.ts.html +274 -0
- package/coverage/lcov-report/src/entities/inventory_group.ts.html +193 -0
- package/coverage/lcov-report/src/entities/inventory_unit_variation.ts.html +172 -0
- package/coverage/lcov-report/src/entities/invoice.ts.html +661 -0
- package/coverage/lcov-report/src/entities/item.ts.html +262 -0
- package/coverage/lcov-report/src/entities/job.ts.html +1288 -0
- package/coverage/lcov-report/src/entities/job_comment.ts.html +244 -0
- package/coverage/lcov-report/src/entities/job_note.ts.html +205 -0
- package/coverage/lcov-report/src/entities/job_operation_log.ts.html +208 -0
- package/coverage/lcov-report/src/entities/matching_inventory.ts.html +172 -0
- package/coverage/lcov-report/src/entities/notification.ts.html +403 -0
- package/coverage/lcov-report/src/entities/payment.ts.html +316 -0
- package/coverage/lcov-report/src/entities/payment_device.ts.html +175 -0
- package/coverage/lcov-report/src/entities/phone_number.ts.html +193 -0
- package/coverage/lcov-report/src/entities/product.ts.html +1543 -0
- package/coverage/lcov-report/src/entities/product_form.ts.html +238 -0
- package/coverage/lcov-report/src/entities/product_form_version.ts.html +238 -0
- package/coverage/lcov-report/src/entities/product_review.ts.html +748 -0
- package/coverage/lcov-report/src/entities/production_comment.ts.html +244 -0
- package/coverage/lcov-report/src/entities/quote.ts.html +505 -0
- package/coverage/lcov-report/src/entities/quote_item.ts.html +298 -0
- package/coverage/lcov-report/src/entities/reminder.ts.html +307 -0
- package/coverage/lcov-report/src/entities/session.ts.html +169 -0
- package/coverage/lcov-report/src/entities/shipment.ts.html +673 -0
- package/coverage/lcov-report/src/entities/shipment_item.ts.html +130 -0
- package/coverage/lcov-report/src/entities/shipment_item_fulfillment.ts.html +130 -0
- package/coverage/lcov-report/src/entities/shipment_log.ts.html +196 -0
- package/coverage/lcov-report/src/entities/shipment_method.ts.html +268 -0
- package/coverage/lcov-report/src/entities/shipment_method_variation.ts.html +202 -0
- package/coverage/lcov-report/src/entities/short_url.ts.html +193 -0
- package/coverage/lcov-report/src/entities/subscription_plan.ts.html +304 -0
- package/coverage/lcov-report/src/entities/supply_domain.ts.html +166 -0
- package/coverage/lcov-report/src/entities/support_conversation.ts.html +280 -0
- package/coverage/lcov-report/src/entities/support_message.ts.html +175 -0
- package/coverage/lcov-report/src/entities/system_role.ts.html +136 -0
- package/coverage/lcov-report/src/entities/theme.ts.html +331 -0
- package/coverage/lcov-report/src/entities/theme_css_setting.ts.html +154 -0
- package/coverage/lcov-report/src/entities/user.ts.html +1369 -0
- package/coverage/lcov-report/src/entities/user_company.ts.html +166 -0
- package/coverage/lcov-report/src/entities/variation.ts.html +277 -0
- package/coverage/lcov-report/src/entities/variation_field.ts.html +889 -0
- package/coverage/lcov-report/src/entities/variation_fields_option.ts.html +406 -0
- package/coverage/lcov-report/src/entities/variation_option.ts.html +259 -0
- package/coverage/lcov-report/src/entities/variations_group.ts.html +208 -0
- package/coverage/lcov-report/src/entity.ts.html +3379 -0
- package/coverage/lcov-report/src/index.html +221 -0
- package/coverage/lcov-report/src/index.ts.html +652 -0
- package/coverage/lcov-report/src/merchi.ts.html +1420 -0
- package/coverage/lcov-report/src/pricing/discount.ts.html +136 -0
- package/coverage/lcov-report/src/pricing/estimate.ts.html +1015 -0
- package/coverage/lcov-report/src/pricing/index.html +206 -0
- package/coverage/lcov-report/src/pricing/index.ts.html +106 -0
- package/coverage/lcov-report/src/pricing/inventory.ts.html +352 -0
- package/coverage/lcov-report/src/pricing/matrix.ts.html +736 -0
- package/coverage/lcov-report/src/pricing/round.ts.html +166 -0
- package/coverage/lcov-report/src/pricing/visibility.ts.html +418 -0
- package/coverage/lcov-report/src/request.ts.html +673 -0
- package/coverage/lcov-report/src/test_util.ts.html +139 -0
- package/coverage/lcov-report/src/toasts.ts.html +187 -0
- package/coverage/lcov-report/src/util/float.ts.html +121 -0
- package/coverage/lcov-report/src/util/index.html +161 -0
- package/coverage/lcov-report/src/util/index.ts.html +106 -0
- package/coverage/lcov-report/src/util/query_string.ts.html +124 -0
- package/coverage/lcov-report/src/util/validation.ts.html +91 -0
- package/coverage/lcov-report/src/uuid.ts.html +109 -0
- package/coverage/lcov.info +7758 -0
- package/dist/constants/auto_assign_production_on_actions.js +7 -0
- package/dist/constants/call_to_actions.js +15 -0
- package/dist/constants/call_to_actions.test.js +4 -0
- package/dist/constants/discount_types.js +6 -0
- package/dist/constants/discount_types.test.js +4 -0
- package/dist/constants/domain_types.js +13 -0
- package/dist/constants/domain_types.test.js +4 -0
- package/dist/constants/errors.js +32 -0
- package/dist/constants/event_types.js +6 -0
- package/dist/constants/event_types.test.js +4 -0
- package/dist/constants/field_types.js +17 -0
- package/dist/constants/field_types.test.js +4 -0
- package/dist/constants/index.js +25 -0
- package/dist/constants/input_types.js +14 -0
- package/dist/constants/input_types.test.js +4 -0
- package/dist/constants/inventory_status.test.js +4 -0
- package/dist/constants/inventory_statuses.js +7 -0
- package/dist/constants/invoice_types.js +6 -0
- package/dist/constants/invoice_types.test.js +4 -0
- package/dist/constants/item_types.js +6 -0
- package/dist/constants/item_types.test.js +4 -0
- package/dist/constants/job/drafting_status.js +8 -0
- package/dist/constants/job/drafting_status.test.js +4 -0
- package/dist/constants/job/payment_status.js +8 -0
- package/dist/constants/job/payment_status.test.js +4 -0
- package/dist/constants/job/production_status.js +13 -0
- package/dist/constants/job/production_status.test.js +4 -0
- package/dist/constants/job/shipment_status.js +12 -0
- package/dist/constants/job/shipment_status.test.js +4 -0
- package/dist/constants/job/supply_chain_request_status.js +7 -0
- package/dist/constants/job/supply_chain_request_status.test.js +4 -0
- package/dist/constants/job_priorities.js +7 -0
- package/dist/constants/job_priorities.test.js +4 -0
- package/dist/constants/job_status.js +50 -0
- package/dist/constants/job_status.test.js +4 -0
- package/dist/constants/job_types.js +17 -0
- package/dist/constants/job_types.test.js +4 -0
- package/dist/constants/notification_sections.js +14 -0
- package/dist/constants/notification_sections.test.js +4 -0
- package/dist/constants/notification_types.js +101 -0
- package/dist/constants/notification_types.test.js +4 -0
- package/dist/constants/notification_urgencies.js +7 -0
- package/dist/constants/notification_urgencies.test.js +4 -0
- package/dist/constants/payment_types.js +11 -0
- package/dist/constants/payment_types.test.js +4 -0
- package/dist/constants/platform.js +5 -0
- package/dist/constants/platform.test.js +8 -0
- package/dist/constants/product_types.js +27 -0
- package/dist/constants/product_types.test.js +4 -0
- package/dist/constants/rights.js +6 -0
- package/dist/constants/rights.test.js +4 -0
- package/dist/constants/robots_meta_directives.js +11 -0
- package/dist/constants/robots_meta_directives.test.js +4 -0
- package/dist/constants/roles.js +64 -0
- package/dist/constants/roles.test.js +4 -0
- package/dist/constants/shipment_companies.js +20 -0
- package/dist/constants/shipment_companies.test.js +4 -0
- package/dist/constants/shipment_services.js +4 -0
- package/dist/constants/shipment_services.test.js +4 -0
- package/dist/constants/system_roles.js +4 -0
- package/dist/constants/system_roles.test.js +4 -0
- package/dist/constants/theme_status.js +6 -0
- package/dist/constants/theme_status.test.js +4 -0
- package/dist/constants/user_types.js +18 -0
- package/dist/constants/user_types.test.js +4 -0
- package/dist/constants/white_label_accessibilities.js +6 -0
- package/dist/constants/white_label_accessibilities.test.js +4 -0
- package/dist/cookie.js +15 -0
- package/dist/cookie.test.js +10 -0
- package/dist/entities/address.js +104 -0
- package/dist/entities/address.test.js +6 -0
- package/dist/entities/agent_conversation.js +78 -0
- package/dist/entities/agent_conversation.test.js +6 -0
- package/dist/entities/agent_skill.js +133 -0
- package/dist/entities/agent_skill.test.js +11 -0
- package/dist/entities/agent_skill_approval.js +61 -0
- package/dist/entities/agent_skill_version.js +99 -0
- package/dist/entities/agent_token_analytics.js +1 -0
- package/dist/entities/agent_token_usage.js +127 -0
- package/dist/entities/agent_token_usage.test.js +6 -0
- package/dist/entities/assignment.js +135 -0
- package/dist/entities/assignment.test.js +30 -0
- package/dist/entities/automatic_payment_relationship.js +65 -0
- package/dist/entities/automatic_payment_relationship.test.js +6 -0
- package/dist/entities/bank.js +85 -0
- package/dist/entities/bank.test.js +6 -0
- package/dist/entities/cart.js +173 -0
- package/dist/entities/cart.test.js +51 -0
- package/dist/entities/cart_item.js +125 -0
- package/dist/entities/cart_item.test.js +28 -0
- package/dist/entities/cart_shipment_group.js +53 -0
- package/dist/entities/cart_shipment_quote.js +70 -0
- package/dist/entities/category.js +101 -0
- package/dist/entities/category.test.js +52 -0
- package/dist/entities/company.js +509 -0
- package/dist/entities/company.test.js +6 -0
- package/dist/entities/company_invitation.js +70 -0
- package/dist/entities/company_invitation.test.js +6 -0
- package/dist/entities/country_tax.js +80 -0
- package/dist/entities/country_tax.test.js +11 -0
- package/dist/entities/discount.js +89 -0
- package/dist/entities/discount.test.js +34 -0
- package/dist/entities/discount_group.js +80 -0
- package/dist/entities/discount_group.test.js +6 -0
- package/dist/entities/domain.js +567 -0
- package/dist/entities/domain.test.js +356 -0
- package/dist/entities/domain_chat_settings.js +134 -0
- package/dist/entities/domain_chat_settings.test.js +15 -0
- package/dist/entities/domain_invitation.js +74 -0
- package/dist/entities/domain_invitation.test.js +6 -0
- package/dist/entities/domain_tag.js +77 -0
- package/dist/entities/domain_tag.test.js +6 -0
- package/dist/entities/draft.js +122 -0
- package/dist/entities/draft.test.js +26 -0
- package/dist/entities/draft_comment.js +107 -0
- package/dist/entities/draft_comment.test.js +6 -0
- package/dist/entities/draft_preview.js +74 -0
- package/dist/entities/draft_preview.test.js +36 -0
- package/dist/entities/draft_preview_layer.js +54 -0
- package/dist/entities/draft_preview_layer.test.js +6 -0
- package/dist/entities/draft_template.js +119 -0
- package/dist/entities/draft_template.test.js +12 -0
- package/dist/entities/email_address.js +56 -0
- package/dist/entities/email_address.test.js +6 -0
- package/dist/entities/email_counter.js +53 -0
- package/dist/entities/email_counter.test.js +6 -0
- package/dist/entities/enrolled_domain.js +74 -0
- package/dist/entities/enrolled_domain.test.js +6 -0
- package/dist/entities/exchange_rate.js +56 -0
- package/dist/entities/exchange_rate.test.js +6 -0
- package/dist/entities/file.js +218 -0
- package/dist/entities/file.test.js +38 -0
- package/dist/entities/internal_tag.js +84 -0
- package/dist/entities/internal_tag.test.js +6 -0
- package/dist/entities/inventory.js +97 -0
- package/dist/entities/inventory.test.js +22 -0
- package/dist/entities/inventory_group.js +69 -0
- package/dist/entities/inventory_group.test.js +6 -0
- package/dist/entities/inventory_unit_variation.js +61 -0
- package/dist/entities/inventory_unit_variation.test.js +6 -0
- package/dist/entities/invoice.js +268 -0
- package/dist/entities/invoice.test.js +45 -0
- package/dist/entities/item.js +96 -0
- package/dist/entities/item.test.js +17 -0
- package/dist/entities/job.js +519 -0
- package/dist/entities/job.test.js +83 -0
- package/dist/entities/job_comment.js +94 -0
- package/dist/entities/job_comment.test.js +6 -0
- package/dist/entities/job_note.js +78 -0
- package/dist/entities/job_operation_log.js +82 -0
- package/dist/entities/job_operation_log.test.js +6 -0
- package/dist/entities/matching_inventory.js +63 -0
- package/dist/entities/notification.js +163 -0
- package/dist/entities/notification.test.js +51 -0
- package/dist/entities/payment.js +122 -0
- package/dist/entities/payment.test.js +22 -0
- package/dist/entities/payment_device.js +65 -0
- package/dist/entities/payment_device.test.js +6 -0
- package/dist/entities/phone_number.js +72 -0
- package/dist/entities/phone_number.test.js +6 -0
- package/dist/entities/product.js +619 -0
- package/dist/entities/product.test.js +720 -0
- package/dist/entities/product_form.js +92 -0
- package/dist/entities/product_form_version.js +95 -0
- package/dist/entities/product_review.js +234 -0
- package/dist/entities/production_comment.js +94 -0
- package/dist/entities/production_comment.test.js +6 -0
- package/dist/entities/quote.js +168 -0
- package/dist/entities/quote.test.js +86 -0
- package/dist/entities/quote_item.js +99 -0
- package/dist/entities/quote_item.test.js +40 -0
- package/dist/entities/reminder.js +120 -0
- package/dist/entities/reminder.test.js +14 -0
- package/dist/entities/session.js +63 -0
- package/dist/entities/session.test.js +6 -0
- package/dist/entities/shipment.js +256 -0
- package/dist/entities/shipment.test.js +20 -0
- package/dist/entities/shipment_item.js +45 -0
- package/dist/entities/shipment_item.test.js +6 -0
- package/dist/entities/shipment_item_fulfillment.js +46 -0
- package/dist/entities/shipment_item_fulfillment.test.js +6 -0
- package/dist/entities/shipment_log.js +75 -0
- package/dist/entities/shipment_method.js +104 -0
- package/dist/entities/shipment_method.test.js +6 -0
- package/dist/entities/shipment_method_variation.js +77 -0
- package/dist/entities/shipment_method_variation.test.js +6 -0
- package/dist/entities/short_url.js +73 -0
- package/dist/entities/short_url.test.js +6 -0
- package/dist/entities/subscription_plan.js +122 -0
- package/dist/entities/subscription_plan.test.js +6 -0
- package/dist/entities/supply_domain.js +62 -0
- package/dist/entities/supply_domain.test.js +6 -0
- package/dist/entities/support_conversation.js +106 -0
- package/dist/entities/support_conversation.test.js +6 -0
- package/dist/entities/support_message.js +66 -0
- package/dist/entities/support_message.test.js +6 -0
- package/dist/entities/system_role.js +49 -0
- package/dist/entities/system_role.test.js +6 -0
- package/dist/entities/theme.js +125 -0
- package/dist/entities/theme.test.js +35 -0
- package/dist/entities/theme_css_setting.js +57 -0
- package/dist/entities/theme_css_setting.test.js +6 -0
- package/dist/entities/user.js +508 -0
- package/dist/entities/user.test.js +134 -0
- package/dist/entities/user_company.js +62 -0
- package/dist/entities/user_company.test.js +6 -0
- package/dist/entities/variation.js +108 -0
- package/dist/entities/variation.test.js +6 -0
- package/dist/entities/variation_field.js +381 -0
- package/dist/entities/variation_field.test.js +69 -0
- package/dist/entities/variation_fields_option.js +153 -0
- package/dist/entities/variation_fields_option.test.js +43 -0
- package/dist/entities/variation_option.js +104 -0
- package/dist/entities/variation_option.test.js +6 -0
- package/dist/entities/variations_group.js +79 -0
- package/dist/entities/variations_group.test.js +6 -0
- package/dist/entity.js +950 -0
- package/dist/index.js +86 -0
- package/dist/merchi.js +296 -0
- package/dist/merchi.test.js +21 -0
- package/dist/pricing/discount.js +11 -0
- package/dist/pricing/discount.test.js +35 -0
- package/dist/pricing/estimate.js +335 -0
- package/dist/pricing/estimate.test.js +403 -0
- package/dist/pricing/golden.test.js +61 -0
- package/dist/pricing/index.js +7 -0
- package/dist/pricing/index.test.js +8 -0
- package/dist/pricing/inventory.js +238 -0
- package/dist/pricing/inventory.test.js +188 -0
- package/dist/pricing/matrix.js +245 -0
- package/dist/pricing/matrix.test.js +437 -0
- package/dist/pricing/round.js +29 -0
- package/dist/pricing/round.test.js +27 -0
- package/dist/pricing/types.js +1 -0
- package/dist/pricing/visibility.js +227 -0
- package/dist/pricing/visibility.test.js +210 -0
- package/dist/request.js +169 -0
- package/dist/request.test.js +70 -0
- package/dist/test_util.js +17 -0
- package/dist/toasts.js +14 -0
- package/dist/toasts.test.js +28 -0
- package/dist/util/float.js +34 -0
- package/dist/util/index.js +3 -0
- package/dist/util/query_string.js +13 -0
- package/dist/util/query_string.test.js +6 -0
- package/dist/util/validation.js +3 -0
- package/dist/util/validation.test.js +12 -0
- package/dist/uuid.js +8 -0
- package/dist/uuid.test.js +11 -0
- package/jest.config.js +23 -0
- package/merchi_sdk_ts-1.13.1.tgz +0 -0
- package/package.json +41 -0
- package/src/constants/auto_assign_production_on_actions.ts +6 -0
- package/src/constants/call_to_actions.test.ts +5 -0
- package/src/constants/call_to_actions.ts +14 -0
- package/src/constants/discount_types.test.ts +5 -0
- package/src/constants/discount_types.ts +5 -0
- package/src/constants/domain_types.test.ts +5 -0
- package/src/constants/domain_types.ts +12 -0
- package/src/constants/errors.ts +32 -0
- package/src/constants/event_types.test.ts +5 -0
- package/src/constants/event_types.ts +5 -0
- package/src/constants/field_types.test.ts +5 -0
- package/src/constants/field_types.ts +16 -0
- package/src/constants/index.ts +51 -0
- package/src/constants/input_types.test.ts +5 -0
- package/src/constants/input_types.ts +13 -0
- package/src/constants/inventory_status.test.ts +5 -0
- package/src/constants/inventory_statuses.ts +6 -0
- package/src/constants/invoice_types.test.ts +5 -0
- package/src/constants/invoice_types.ts +5 -0
- package/src/constants/item_types.test.ts +5 -0
- package/src/constants/item_types.ts +5 -0
- package/src/constants/job/drafting_status.test.ts +5 -0
- package/src/constants/job/drafting_status.ts +7 -0
- package/src/constants/job/payment_status.test.ts +5 -0
- package/src/constants/job/payment_status.ts +7 -0
- package/src/constants/job/production_status.test.ts +5 -0
- package/src/constants/job/production_status.ts +12 -0
- package/src/constants/job/shipment_status.test.ts +5 -0
- package/src/constants/job/shipment_status.ts +11 -0
- package/src/constants/job/supply_chain_request_status.test.ts +5 -0
- package/src/constants/job/supply_chain_request_status.ts +6 -0
- package/src/constants/job_priorities.test.ts +5 -0
- package/src/constants/job_priorities.ts +6 -0
- package/src/constants/job_status.test.ts +5 -0
- package/src/constants/job_status.ts +49 -0
- package/src/constants/job_types.test.ts +5 -0
- package/src/constants/job_types.ts +16 -0
- package/src/constants/notification_sections.test.ts +5 -0
- package/src/constants/notification_sections.ts +13 -0
- package/src/constants/notification_types.test.ts +5 -0
- package/src/constants/notification_types.ts +100 -0
- package/src/constants/notification_urgencies.test.ts +5 -0
- package/src/constants/notification_urgencies.ts +7 -0
- package/src/constants/payment_types.test.ts +5 -0
- package/src/constants/payment_types.ts +10 -0
- package/src/constants/platform.test.ts +15 -0
- package/src/constants/platform.ts +6 -0
- package/src/constants/product_types.test.ts +6 -0
- package/src/constants/product_types.ts +26 -0
- package/src/constants/rights.test.ts +5 -0
- package/src/constants/rights.ts +5 -0
- package/src/constants/robots_meta_directives.test.ts +5 -0
- package/src/constants/robots_meta_directives.ts +11 -0
- package/src/constants/roles.test.ts +5 -0
- package/src/constants/roles.ts +46 -0
- package/src/constants/shipment_companies.test.ts +5 -0
- package/src/constants/shipment_companies.ts +19 -0
- package/src/constants/shipment_services.test.ts +5 -0
- package/src/constants/shipment_services.ts +3 -0
- package/src/constants/system_roles.test.ts +5 -0
- package/src/constants/system_roles.ts +3 -0
- package/src/constants/theme_status.test.ts +5 -0
- package/src/constants/theme_status.ts +5 -0
- package/src/constants/user_types.test.ts +5 -0
- package/src/constants/user_types.ts +17 -0
- package/src/constants/white_label_accessibilities.test.ts +5 -0
- package/src/constants/white_label_accessibilities.ts +6 -0
- package/src/cookie.test.ts +11 -0
- package/src/cookie.ts +16 -0
- package/src/entities/address.test.ts +7 -0
- package/src/entities/address.ts +65 -0
- package/src/entities/agent_conversation.test.ts +7 -0
- package/src/entities/agent_conversation.ts +39 -0
- package/src/entities/agent_skill.test.ts +13 -0
- package/src/entities/agent_skill.ts +99 -0
- package/src/entities/agent_skill_approval.ts +26 -0
- package/src/entities/agent_skill_version.ts +64 -0
- package/src/entities/agent_token_analytics.ts +49 -0
- package/src/entities/agent_token_usage.test.ts +7 -0
- package/src/entities/agent_token_usage.ts +88 -0
- package/src/entities/assignment.test.ts +39 -0
- package/src/entities/assignment.ts +99 -0
- package/src/entities/automatic_payment_relationship.test.ts +8 -0
- package/src/entities/automatic_payment_relationship.ts +29 -0
- package/src/entities/bank.test.ts +7 -0
- package/src/entities/bank.ts +45 -0
- package/src/entities/cart.test.ts +57 -0
- package/src/entities/cart.ts +102 -0
- package/src/entities/cart_item.test.ts +33 -0
- package/src/entities/cart_item.ts +85 -0
- package/src/entities/cart_shipment_group.ts +21 -0
- package/src/entities/cart_shipment_quote.ts +33 -0
- package/src/entities/category.test.ts +57 -0
- package/src/entities/category.ts +59 -0
- package/src/entities/company.test.ts +7 -0
- package/src/entities/company.ts +397 -0
- package/src/entities/company_invitation.test.ts +7 -0
- package/src/entities/company_invitation.ts +33 -0
- package/src/entities/country_tax.test.ts +13 -0
- package/src/entities/country_tax.ts +46 -0
- package/src/entities/discount.test.ts +36 -0
- package/src/entities/discount.ts +50 -0
- package/src/entities/discount_group.test.ts +7 -0
- package/src/entities/discount_group.ts +43 -0
- package/src/entities/domain.test.ts +393 -0
- package/src/entities/domain.ts +1088 -0
- package/src/entities/domain_chat_settings.test.ts +16 -0
- package/src/entities/domain_chat_settings.ts +82 -0
- package/src/entities/domain_invitation.test.ts +7 -0
- package/src/entities/domain_invitation.ts +36 -0
- package/src/entities/domain_tag.test.ts +7 -0
- package/src/entities/domain_tag.ts +42 -0
- package/src/entities/draft.test.ts +29 -0
- package/src/entities/draft.ts +79 -0
- package/src/entities/draft_comment.test.ts +7 -0
- package/src/entities/draft_comment.ts +63 -0
- package/src/entities/draft_preview.test.ts +42 -0
- package/src/entities/draft_preview.ts +37 -0
- package/src/entities/draft_preview_layer.test.ts +7 -0
- package/src/entities/draft_preview_layer.ts +21 -0
- package/src/entities/draft_template.test.ts +14 -0
- package/src/entities/draft_template.ts +81 -0
- package/src/entities/email_address.test.ts +7 -0
- package/src/entities/email_address.ts +24 -0
- package/src/entities/email_counter.test.ts +7 -0
- package/src/entities/email_counter.ts +20 -0
- package/src/entities/enrolled_domain.test.ts +7 -0
- package/src/entities/enrolled_domain.ts +40 -0
- package/src/entities/exchange_rate.test.ts +7 -0
- package/src/entities/exchange_rate.ts +22 -0
- package/src/entities/file.test.ts +43 -0
- package/src/entities/file.ts +165 -0
- package/src/entities/internal_tag.test.ts +7 -0
- package/src/entities/internal_tag.ts +51 -0
- package/src/entities/inventory.test.ts +28 -0
- package/src/entities/inventory.ts +63 -0
- package/src/entities/inventory_group.test.ts +7 -0
- package/src/entities/inventory_group.ts +36 -0
- package/src/entities/inventory_unit_variation.test.ts +7 -0
- package/src/entities/inventory_unit_variation.ts +29 -0
- package/src/entities/invoice.test.ts +50 -0
- package/src/entities/invoice.ts +192 -0
- package/src/entities/item.test.ts +19 -0
- package/src/entities/item.ts +59 -0
- package/src/entities/job.test.ts +95 -0
- package/src/entities/job.ts +401 -0
- package/src/entities/job_comment.test.ts +7 -0
- package/src/entities/job_comment.ts +53 -0
- package/src/entities/job_note.ts +40 -0
- package/src/entities/job_operation_log.test.ts +7 -0
- package/src/entities/job_operation_log.ts +41 -0
- package/src/entities/matching_inventory.ts +29 -0
- package/src/entities/notification.test.ts +58 -0
- package/src/entities/notification.ts +106 -0
- package/src/entities/payment.test.ts +27 -0
- package/src/entities/payment.ts +77 -0
- package/src/entities/payment_device.test.ts +7 -0
- package/src/entities/payment_device.ts +30 -0
- package/src/entities/phone_number.test.ts +7 -0
- package/src/entities/phone_number.ts +36 -0
- package/src/entities/product.test.ts +776 -0
- package/src/entities/product.ts +486 -0
- package/src/entities/product_form.ts +51 -0
- package/src/entities/product_form_version.ts +51 -0
- package/src/entities/product_review.ts +221 -0
- package/src/entities/production_comment.test.ts +7 -0
- package/src/entities/production_comment.ts +53 -0
- package/src/entities/quote.test.ts +98 -0
- package/src/entities/quote.ts +140 -0
- package/src/entities/quote_item.test.ts +50 -0
- package/src/entities/quote_item.ts +71 -0
- package/src/entities/reminder.test.ts +16 -0
- package/src/entities/reminder.ts +74 -0
- package/src/entities/session.test.ts +7 -0
- package/src/entities/session.ts +28 -0
- package/src/entities/shipment.test.ts +24 -0
- package/src/entities/shipment.ts +196 -0
- package/src/entities/shipment_item.test.ts +7 -0
- package/src/entities/shipment_item.ts +15 -0
- package/src/entities/shipment_item_fulfillment.test.ts +8 -0
- package/src/entities/shipment_item_fulfillment.ts +15 -0
- package/src/entities/shipment_log.ts +37 -0
- package/src/entities/shipment_method.test.ts +7 -0
- package/src/entities/shipment_method.ts +61 -0
- package/src/entities/shipment_method_variation.test.ts +7 -0
- package/src/entities/shipment_method_variation.ts +39 -0
- package/src/entities/short_url.test.ts +7 -0
- package/src/entities/short_url.ts +36 -0
- package/src/entities/subscription_plan.test.ts +7 -0
- package/src/entities/subscription_plan.ts +73 -0
- package/src/entities/supply_domain.test.ts +7 -0
- package/src/entities/supply_domain.ts +27 -0
- package/src/entities/support_conversation.test.ts +7 -0
- package/src/entities/support_conversation.ts +65 -0
- package/src/entities/support_message.test.ts +7 -0
- package/src/entities/support_message.ts +30 -0
- package/src/entities/system_role.test.ts +7 -0
- package/src/entities/system_role.ts +17 -0
- package/src/entities/theme.test.ts +38 -0
- package/src/entities/theme.ts +82 -0
- package/src/entities/theme_css_setting.test.ts +7 -0
- package/src/entities/theme_css_setting.ts +23 -0
- package/src/entities/user.test.ts +154 -0
- package/src/entities/user.ts +428 -0
- package/src/entities/user_company.test.ts +7 -0
- package/src/entities/user_company.ts +27 -0
- package/src/entities/variation.test.ts +7 -0
- package/src/entities/variation.ts +64 -0
- package/src/entities/variation_field.test.ts +76 -0
- package/src/entities/variation_field.ts +268 -0
- package/src/entities/variation_fields_option.test.ts +46 -0
- package/src/entities/variation_fields_option.ts +107 -0
- package/src/entities/variation_option.test.ts +7 -0
- package/src/entities/variation_option.ts +58 -0
- package/src/entities/variations_group.test.ts +7 -0
- package/src/entities/variations_group.ts +41 -0
- package/src/entity.ts +1098 -0
- package/src/index.ts +189 -0
- package/src/merchi.test.ts +25 -0
- package/src/merchi.ts +445 -0
- package/src/pricing/__fixtures__/conditional_field_hidden.json +112 -0
- package/src/pricing/__fixtures__/conditional_field_visible.json +113 -0
- package/src/pricing/__fixtures__/group_product.json +130 -0
- package/src/pricing/__fixtures__/non_selectable_number_field.json +108 -0
- package/src/pricing/__fixtures__/quantity_break_discount.json +104 -0
- package/src/pricing/__fixtures__/simple_selectable_field.json +110 -0
- package/src/pricing/discount.test.ts +47 -0
- package/src/pricing/discount.ts +17 -0
- package/src/pricing/estimate.test.ts +486 -0
- package/src/pricing/estimate.ts +310 -0
- package/src/pricing/golden.test.ts +51 -0
- package/src/pricing/index.test.ts +9 -0
- package/src/pricing/index.ts +7 -0
- package/src/pricing/inventory.test.ts +197 -0
- package/src/pricing/inventory.ts +89 -0
- package/src/pricing/matrix.test.ts +553 -0
- package/src/pricing/matrix.ts +217 -0
- package/src/pricing/round.test.ts +31 -0
- package/src/pricing/round.ts +27 -0
- package/src/pricing/types.ts +140 -0
- package/src/pricing/visibility.test.ts +223 -0
- package/src/pricing/visibility.ts +111 -0
- package/src/request.test.ts +79 -0
- package/src/request.ts +196 -0
- package/src/test_util.ts +18 -0
- package/src/toasts.test.ts +38 -0
- package/src/toasts.ts +34 -0
- package/src/util/float.ts +12 -0
- package/src/util/index.ts +7 -0
- package/src/util/query_string.test.ts +7 -0
- package/src/util/query_string.ts +13 -0
- package/src/util/validation.test.ts +16 -0
- package/src/util/validation.ts +2 -0
- package/src/uuid.test.ts +15 -0
- package/src/uuid.ts +8 -0
- package/tsconfig.json +29 -0
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PricingRules, PricingField, PricingOption, Selections, FieldSelection,
|
|
3
|
+
QuoteResult, UnsupportedResult, DiscountGroup,
|
|
4
|
+
} from './types.js';
|
|
5
|
+
import { applyDiscount } from './discount.js';
|
|
6
|
+
import { roundHalfEven } from './round.js';
|
|
7
|
+
import { resolveVisibleFields } from './visibility.js';
|
|
8
|
+
import { FieldType } from '../constants/field_types.js';
|
|
9
|
+
|
|
10
|
+
/** Form inputs often supply quantities as strings. Coerce before any arithmetic
|
|
11
|
+
* so `reduce((a,b)=>a+b)` cannot concatenate ("100"+"1" → "01001" → 1001). */
|
|
12
|
+
function toQuantity(value: unknown): number {
|
|
13
|
+
const n = Number(value);
|
|
14
|
+
return Number.isFinite(n) ? n : 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function parseAreaMm(
|
|
18
|
+
value: string | number | null | undefined
|
|
19
|
+
): { heightMm: number; widthMm: number } | null {
|
|
20
|
+
if (value === undefined || value === null || value === '') return null;
|
|
21
|
+
const parts = String(value).split(',').map((p) => p.trim());
|
|
22
|
+
if (parts.length !== 2) return null;
|
|
23
|
+
const heightMm = Number(parts[0]);
|
|
24
|
+
const widthMm = Number(parts[1]);
|
|
25
|
+
if (!Number.isFinite(heightMm) || !Number.isFinite(widthMm)) return null;
|
|
26
|
+
if (heightMm <= 0 || widthMm <= 0) return null;
|
|
27
|
+
return { heightMm, widthMm };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function unitPriceAt(rules: PricingRules, qty: number): number {
|
|
31
|
+
let unitPrice = applyDiscount(rules.product.unitPrice, qty, rules.product.discountGroup);
|
|
32
|
+
const mop = rules.product.minimumPrice;
|
|
33
|
+
if (qty > 0 && mop && unitPrice * qty < mop) {
|
|
34
|
+
unitPrice = mop / qty;
|
|
35
|
+
}
|
|
36
|
+
return unitPrice;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function variationSetupCost(
|
|
40
|
+
source: { variationCost: number; variationCostDiscountGroup: DiscountGroup | null },
|
|
41
|
+
totalQty: number
|
|
42
|
+
): number {
|
|
43
|
+
let cost = source.variationCost;
|
|
44
|
+
if (source.variationCostDiscountGroup) {
|
|
45
|
+
cost = applyDiscount(cost, totalQty, source.variationCostDiscountGroup);
|
|
46
|
+
}
|
|
47
|
+
return roundHalfEven(cost, 3);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function variationUnitCosts(
|
|
51
|
+
source: { variationUnitCost: number; variationUnitCostDiscountGroup: DiscountGroup | null },
|
|
52
|
+
groupQuantities: number[]
|
|
53
|
+
): number[] {
|
|
54
|
+
const uc = source.variationUnitCost;
|
|
55
|
+
const group = source.variationUnitCostDiscountGroup;
|
|
56
|
+
let list: number[];
|
|
57
|
+
if (group) {
|
|
58
|
+
if (group.groupRestricted) {
|
|
59
|
+
list = groupQuantities.map((q) => applyDiscount(uc, q, group));
|
|
60
|
+
} else {
|
|
61
|
+
const total = groupQuantities.reduce((a, b) => a + b, 0);
|
|
62
|
+
const discounted = applyDiscount(uc, total, group);
|
|
63
|
+
list = groupQuantities.map(() => discounted);
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
list = groupQuantities.map(() => uc);
|
|
67
|
+
}
|
|
68
|
+
return list.map((c) => roundHalfEven(c, 3));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function isEmpty(field: PricingField, sel: FieldSelection | undefined): boolean {
|
|
72
|
+
if (!sel) return true;
|
|
73
|
+
if (field.isSelectable) {
|
|
74
|
+
return !sel.selectedOptionIds || sel.selectedOptionIds.length === 0;
|
|
75
|
+
}
|
|
76
|
+
// Server `is_empty` non-selectable branch is `return not self.value`, so
|
|
77
|
+
// numeric 0 is empty (but string '0' stays non-empty, since `not '0'` is False).
|
|
78
|
+
return (
|
|
79
|
+
sel.value === undefined ||
|
|
80
|
+
sel.value === null ||
|
|
81
|
+
sel.value === '' ||
|
|
82
|
+
sel.value === 0
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function areaCostFactor(
|
|
87
|
+
field: PricingField,
|
|
88
|
+
sel: FieldSelection | undefined,
|
|
89
|
+
groupQuantities: number[]
|
|
90
|
+
): { setup: number; unitList: number[] } {
|
|
91
|
+
const n = groupQuantities.length;
|
|
92
|
+
const parsed = parseAreaMm(sel?.value ?? null);
|
|
93
|
+
if (!parsed) {
|
|
94
|
+
return { setup: 0, unitList: new Array(n).fill(0) };
|
|
95
|
+
}
|
|
96
|
+
const totalQty = groupQuantities.reduce((a, b) => a + b, 0);
|
|
97
|
+
const heightSetup = variationSetupCost(
|
|
98
|
+
{
|
|
99
|
+
variationCost: field.heightVariationCost || 0,
|
|
100
|
+
variationCostDiscountGroup: field.heightVariationCostDiscountGroup ?? null,
|
|
101
|
+
},
|
|
102
|
+
totalQty
|
|
103
|
+
);
|
|
104
|
+
const widthSetup = variationSetupCost(
|
|
105
|
+
{
|
|
106
|
+
variationCost: field.widthVariationCost || 0,
|
|
107
|
+
variationCostDiscountGroup: field.widthVariationCostDiscountGroup ?? null,
|
|
108
|
+
},
|
|
109
|
+
totalQty
|
|
110
|
+
);
|
|
111
|
+
const heightUcs = variationUnitCosts(
|
|
112
|
+
{
|
|
113
|
+
variationUnitCost: field.heightVariationUnitCost || 0,
|
|
114
|
+
variationUnitCostDiscountGroup:
|
|
115
|
+
field.heightVariationUnitCostDiscountGroup ?? null,
|
|
116
|
+
},
|
|
117
|
+
groupQuantities
|
|
118
|
+
);
|
|
119
|
+
const widthUcs = variationUnitCosts(
|
|
120
|
+
{
|
|
121
|
+
variationUnitCost: field.widthVariationUnitCost || 0,
|
|
122
|
+
variationUnitCostDiscountGroup:
|
|
123
|
+
field.widthVariationUnitCostDiscountGroup ?? null,
|
|
124
|
+
},
|
|
125
|
+
groupQuantities
|
|
126
|
+
);
|
|
127
|
+
const areaUnit = (field.areaUnit || 'mm').toLowerCase();
|
|
128
|
+
const mmPerUnit =
|
|
129
|
+
areaUnit === 'm' ? 1000
|
|
130
|
+
: areaUnit === 'cm' ? 10
|
|
131
|
+
: areaUnit === 'in' || areaUnit === 'inch' || areaUnit === 'inches' ? 25.4
|
|
132
|
+
: areaUnit === 'ft' || areaUnit === 'foot' || areaUnit === 'feet' ? 304.8
|
|
133
|
+
: 1;
|
|
134
|
+
const heightU = parsed.heightMm / mmPerUnit;
|
|
135
|
+
const widthU = parsed.widthMm / mmPerUnit;
|
|
136
|
+
const area = heightU * widthU;
|
|
137
|
+
// onceOff/unit = rateH × rateW × height_u × width_u (in field areaUnit)
|
|
138
|
+
const setup = roundHalfEven(heightSetup * widthSetup * area, 3);
|
|
139
|
+
const unitList = heightUcs.map((hu, i) =>
|
|
140
|
+
roundHalfEven(hu * widthUcs[i] * area, 3)
|
|
141
|
+
);
|
|
142
|
+
return { setup, unitList };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function costFactor(
|
|
146
|
+
field: PricingField,
|
|
147
|
+
sel: FieldSelection | undefined,
|
|
148
|
+
groupQuantities: number[]
|
|
149
|
+
): { setup: number; unitList: number[] } {
|
|
150
|
+
const n = groupQuantities.length;
|
|
151
|
+
// Coerce: JSON/APIs occasionally deliver fieldType as a string.
|
|
152
|
+
if (Number(field.fieldType) === FieldType.AREA) {
|
|
153
|
+
return areaCostFactor(field, sel, groupQuantities);
|
|
154
|
+
}
|
|
155
|
+
if (isEmpty(field, sel)) {
|
|
156
|
+
return { setup: 0, unitList: new Array(n).fill(0) };
|
|
157
|
+
}
|
|
158
|
+
const totalQty = groupQuantities.reduce((a, b) => a + b, 0);
|
|
159
|
+
if (field.isSelectable) {
|
|
160
|
+
let setup = 0;
|
|
161
|
+
const unitList = new Array(n).fill(0);
|
|
162
|
+
const optById = new Map<number, PricingOption>();
|
|
163
|
+
for (const o of field.options) {
|
|
164
|
+
optById.set(o.id, o);
|
|
165
|
+
if (o.originalId != null) optById.set(o.originalId, o);
|
|
166
|
+
}
|
|
167
|
+
for (const optId of sel!.selectedOptionIds!) {
|
|
168
|
+
const opt = optById.get(optId);
|
|
169
|
+
if (!opt) continue;
|
|
170
|
+
setup += variationSetupCost(opt, totalQty);
|
|
171
|
+
const ucs = variationUnitCosts(opt, groupQuantities);
|
|
172
|
+
for (let i = 0; i < n; i++) unitList[i] += ucs[i];
|
|
173
|
+
}
|
|
174
|
+
return { setup, unitList };
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
setup: variationSetupCost(field, totalQty),
|
|
178
|
+
unitList: variationUnitCosts(field, groupQuantities),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function estimateQuote(
|
|
183
|
+
rules: PricingRules,
|
|
184
|
+
selections: Selections
|
|
185
|
+
): QuoteResult | UnsupportedResult {
|
|
186
|
+
if (rules.unsupported) {
|
|
187
|
+
return { unsupported: rules.unsupported };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const groupCosts: number[] = [];
|
|
191
|
+
let cost = 0;
|
|
192
|
+
let costPerUnit: number;
|
|
193
|
+
let groupQuantities: number[];
|
|
194
|
+
|
|
195
|
+
// Mirror the server's `update_group_variations_cost_and_job_cost`
|
|
196
|
+
// (jobs.py ~3321): the group-vs-single branch keys off whether the *job*
|
|
197
|
+
// actually has variation groups (`if self.variations_groups:`), NOT the
|
|
198
|
+
// product's `hasGroups` capability. A group-capable product with no groups
|
|
199
|
+
// submitted is priced as a single non-group job (the server's `else` branch
|
|
200
|
+
// sets `group_quantities = [self.quantity or 0]`).
|
|
201
|
+
const groups = selections.groups || [];
|
|
202
|
+
if (groups.length > 0) {
|
|
203
|
+
groupQuantities = groups.map((g) => toQuantity(g.quantity));
|
|
204
|
+
const totalQty = groupQuantities.reduce((a, b) => a + b, 0);
|
|
205
|
+
const restricted = Boolean(rules.product.discountGroup?.groupRestricted);
|
|
206
|
+
const baseUnitPrice = unitPriceAt(rules, totalQty);
|
|
207
|
+
const perGroupCpu: number[] = [];
|
|
208
|
+
|
|
209
|
+
for (let gi = 0; gi < groups.length; gi++) {
|
|
210
|
+
const group = groups[gi];
|
|
211
|
+
const gQty = groupQuantities[gi];
|
|
212
|
+
const cpu = restricted ? unitPriceAt(rules, gQty) : baseUnitPrice;
|
|
213
|
+
perGroupCpu.push(cpu);
|
|
214
|
+
// Server `VariationsGroups.update_cost` (variations_groups.py ~238):
|
|
215
|
+
// `if not self.quantity: self.group_cost = 0; return` — a zero-qty group
|
|
216
|
+
// contributes exactly 0 and adds NO field/variation costs.
|
|
217
|
+
if (!gQty) {
|
|
218
|
+
groupCosts.push(0);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
// A group's field visibility is scoped to that group's own selections
|
|
222
|
+
// plus the job-level (independent) selections — NOT other groups'
|
|
223
|
+
// (server `VariationsGroups.selected_options` = group.variations +
|
|
224
|
+
// job.variations, variations_groups.py ~252).
|
|
225
|
+
const groupVisible = resolveVisibleFields(rules, {
|
|
226
|
+
fieldValues: selections.fieldValues,
|
|
227
|
+
groups: [group],
|
|
228
|
+
});
|
|
229
|
+
let groupVariationCost = 0;
|
|
230
|
+
for (const field of rules.groupFields) {
|
|
231
|
+
if (!groupVisible.has(field.id)) continue;
|
|
232
|
+
const { setup, unitList } = costFactor(field, group.fieldValues[field.id], [gQty]);
|
|
233
|
+
groupVariationCost += setup + unitList[0] * gQty;
|
|
234
|
+
}
|
|
235
|
+
const groupCost = gQty * cpu + groupVariationCost;
|
|
236
|
+
groupCosts.push(groupCost);
|
|
237
|
+
cost += groupCost;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
costPerUnit = totalQty > 0
|
|
241
|
+
? perGroupCpu.reduce((acc, cpu, i) => acc + cpu * groupQuantities[i], 0) / totalQty
|
|
242
|
+
: baseUnitPrice;
|
|
243
|
+
|
|
244
|
+
// Independent (job-level) field visibility is scoped to independent
|
|
245
|
+
// selections only (server `Jobs.selected_options` = job.variations,
|
|
246
|
+
// jobs.py ~1392), so other groups' selections never reveal them.
|
|
247
|
+
const independentVisible = resolveVisibleFields(rules, {
|
|
248
|
+
quantity: selections.quantity,
|
|
249
|
+
fieldValues: selections.fieldValues,
|
|
250
|
+
});
|
|
251
|
+
for (const field of rules.fields) {
|
|
252
|
+
if (!independentVisible.has(field.id)) continue;
|
|
253
|
+
const { setup, unitList } = costFactor(field, selections.fieldValues[field.id], groupQuantities);
|
|
254
|
+
const unitTotal = unitList.reduce((acc, uc, i) => acc + uc * groupQuantities[i], 0);
|
|
255
|
+
cost += setup + unitTotal;
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
const qty = toQuantity(selections.quantity);
|
|
259
|
+
groupQuantities = [qty];
|
|
260
|
+
costPerUnit = unitPriceAt(rules, qty);
|
|
261
|
+
cost = costPerUnit * qty;
|
|
262
|
+
// No groups: single-job scope (resolveVisibleFields with no groups uses
|
|
263
|
+
// only the top-level selections and considers only independent fields).
|
|
264
|
+
const visible = resolveVisibleFields(rules, selections);
|
|
265
|
+
for (const field of rules.fields) {
|
|
266
|
+
if (!visible.has(field.id)) continue;
|
|
267
|
+
const { setup, unitList } = costFactor(field, selections.fieldValues[field.id], groupQuantities);
|
|
268
|
+
cost += setup + unitList[0] * qty;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Server `Jobs.apply_product_setup_price`: product setup fee once per job,
|
|
273
|
+
// or once per non-empty group when setupPerGroup is true. With no groups the
|
|
274
|
+
// job is a single group, so setup is charged once either way.
|
|
275
|
+
const setupPrice = rules.product.setupPrice || 0;
|
|
276
|
+
if (setupPrice) {
|
|
277
|
+
if (groups.length > 0 && rules.product.setupPerGroup) {
|
|
278
|
+
for (let gi = 0; gi < groups.length; gi++) {
|
|
279
|
+
if (!groupQuantities[gi]) continue;
|
|
280
|
+
groupCosts[gi] += setupPrice;
|
|
281
|
+
cost += setupPrice;
|
|
282
|
+
}
|
|
283
|
+
} else {
|
|
284
|
+
cost += setupPrice;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Server `Jobs.update_cost` (jobs.py ~3452-3454) accumulates `self.cost` at
|
|
289
|
+
// full precision and computes `self.tax_amount = tax(self.cost, ...)` on the
|
|
290
|
+
// UNROUNDED cost (money_protocol.py `tax` does NOT round). `cost` and
|
|
291
|
+
// `taxAmount` are then rounded INDEPENDENTLY at serialization to 2dp
|
|
292
|
+
// (jobs.py ~2169-2171). So tax must be derived from the unrounded cost.
|
|
293
|
+
const unroundedCost = cost;
|
|
294
|
+
const roundedCost = roundHalfEven(unroundedCost, 2);
|
|
295
|
+
const unroundedTax = (unroundedCost * rules.taxPercent) / 100;
|
|
296
|
+
const taxAmount = roundHalfEven(unroundedTax, 2);
|
|
297
|
+
return {
|
|
298
|
+
costPerUnit: roundHalfEven(costPerUnit, 3),
|
|
299
|
+
cost: roundedCost,
|
|
300
|
+
taxAmount,
|
|
301
|
+
// `totalCost` mirrors `Jobs.all_total_cost` = `self.cost + self.tax_amount`
|
|
302
|
+
// (jobs.py ~1724), serialized UNROUNDED at jobs.py ~1899. We round to 3dp to
|
|
303
|
+
// strip float noise while matching the server's effective Numeric scale.
|
|
304
|
+
totalCost: roundHalfEven(unroundedCost + unroundedTax, 3),
|
|
305
|
+
// `result["groupCost"] = self.group_cost` is serialized UNROUNDED
|
|
306
|
+
// (variations_groups.py ~154); round to 3dp to match the Numeric scale.
|
|
307
|
+
groupCosts: groupCosts.map((c) => roundHalfEven(c, 3)),
|
|
308
|
+
currency: rules.currency,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import { estimateQuote } from './estimate.js';
|
|
4
|
+
import { resolveVisibleFields } from './visibility.js';
|
|
5
|
+
import { PricingRules, Selections, QuoteResult } from './types.js';
|
|
6
|
+
|
|
7
|
+
// Fixtures are vendored into the SDK so the golden parity test is
|
|
8
|
+
// self-contained in CI. They are generated by the merchi_api fixture
|
|
9
|
+
// generator (tests/tests/pricing_fixtures.py) from the real server estimate.
|
|
10
|
+
const FIXTURES_DIR = path.resolve(__dirname, '__fixtures__');
|
|
11
|
+
|
|
12
|
+
interface Fixture {
|
|
13
|
+
name: string;
|
|
14
|
+
bundle: PricingRules;
|
|
15
|
+
selections: Selections;
|
|
16
|
+
expected: {
|
|
17
|
+
cost: number; costPerUnit: number; taxAmount: number; totalCost: number;
|
|
18
|
+
groupCosts: number[]; visibleFieldIds: number[];
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function loadFixtures(): Fixture[] {
|
|
23
|
+
if (!fs.existsSync(FIXTURES_DIR)) return [];
|
|
24
|
+
return fs.readdirSync(FIXTURES_DIR)
|
|
25
|
+
.filter((f) => f.endsWith('.json'))
|
|
26
|
+
.map((f) => JSON.parse(fs.readFileSync(path.join(FIXTURES_DIR, f), 'utf-8')));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const fixtures = loadFixtures();
|
|
30
|
+
|
|
31
|
+
test('pricing fixtures exist', () => {
|
|
32
|
+
expect(fixtures.length).toBeGreaterThan(0);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe.each(fixtures)('golden parity: $name', (fixture: Fixture) => {
|
|
36
|
+
test('estimateQuote matches server', () => {
|
|
37
|
+
const result = estimateQuote(fixture.bundle, fixture.selections) as QuoteResult;
|
|
38
|
+
expect(result.cost).toBeCloseTo(fixture.expected.cost, 2);
|
|
39
|
+
expect(result.costPerUnit).toBeCloseTo(fixture.expected.costPerUnit, 3);
|
|
40
|
+
expect(result.taxAmount).toBeCloseTo(fixture.expected.taxAmount, 2);
|
|
41
|
+
expect(result.totalCost).toBeCloseTo(fixture.expected.totalCost, 2);
|
|
42
|
+
expect(result.groupCosts.length).toBe(fixture.expected.groupCosts.length);
|
|
43
|
+
result.groupCosts.forEach((c, i) =>
|
|
44
|
+
expect(c).toBeCloseTo(fixture.expected.groupCosts[i], 2));
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('resolveVisibleFields matches server', () => {
|
|
48
|
+
const visible = [...resolveVisibleFields(fixture.bundle, fixture.selections)].sort((a, b) => a - b);
|
|
49
|
+
expect(visible).toEqual(fixture.expected.visibleFieldIds);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { pricing } from '../index.js';
|
|
2
|
+
|
|
3
|
+
test('pricing namespace exposes calculator functions', () => {
|
|
4
|
+
expect(typeof pricing.estimateQuote).toBe('function');
|
|
5
|
+
expect(typeof pricing.buildPriceMatrix).toBe('function');
|
|
6
|
+
expect(typeof pricing.resolveVisibleFields).toBe('function');
|
|
7
|
+
expect(typeof pricing.applyDiscount).toBe('function');
|
|
8
|
+
expect(typeof pricing.roundHalfEven).toBe('function');
|
|
9
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export { roundHalfEven } from './round.js';
|
|
3
|
+
export { applyDiscount } from './discount.js';
|
|
4
|
+
export { resolveVisibleFields, resolveVisibleOptionIds } from './visibility.js';
|
|
5
|
+
export { resolveUnavailableOptionIds } from './inventory.js';
|
|
6
|
+
export { estimateQuote } from './estimate.js';
|
|
7
|
+
export { buildPriceMatrix } from './matrix.js';
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { resolveUnavailableOptionIds } from './inventory.js';
|
|
2
|
+
import { PricingRules } from './types.js';
|
|
3
|
+
|
|
4
|
+
// Size (24mm=801, 25mm=802) x Colour (Red=811, Green=812, Blue=813), with
|
|
5
|
+
// per-combination stock. 25mm-Blue = 0; 19mm/24mm-Blue has stock.
|
|
6
|
+
function rules(inventoryUnits: any[]): PricingRules {
|
|
7
|
+
const opt = (id: number) => ({
|
|
8
|
+
id, originalId: id, position: id, default: false,
|
|
9
|
+
variationCost: 0, variationUnitCost: 0,
|
|
10
|
+
variationCostDiscountGroup: null, variationUnitCostDiscountGroup: null,
|
|
11
|
+
selectedBy: [],
|
|
12
|
+
});
|
|
13
|
+
const f = (id: number, options: number[]): any => ({
|
|
14
|
+
id, originalId: id, position: 0, fieldType: 2, independent: false,
|
|
15
|
+
isSelectable: true, selectedBy: [],
|
|
16
|
+
variationCost: 0, variationUnitCost: 0,
|
|
17
|
+
variationCostDiscountGroup: null, variationUnitCostDiscountGroup: null,
|
|
18
|
+
options: options.map(opt),
|
|
19
|
+
});
|
|
20
|
+
return {
|
|
21
|
+
currency: 'AUD', taxPercent: 0,
|
|
22
|
+
product: { unitPrice: 0, minimumPrice: null, discountGroup: null },
|
|
23
|
+
fields: [], hasGroups: true, needsInventory: true,
|
|
24
|
+
groupFields: [f(800, [801, 802]), f(810, [811, 812, 813])],
|
|
25
|
+
inventoryUnits,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const units = [
|
|
30
|
+
{ optionIds: [801, 813], quantity: 5 }, // 24mm-Blue: in stock
|
|
31
|
+
{ optionIds: [802, 811], quantity: 7 }, // 25mm-Red: in stock
|
|
32
|
+
{ optionIds: [802, 812], quantity: 7 }, // 25mm-Green: in stock
|
|
33
|
+
{ optionIds: [802, 813], quantity: 0 }, // 25mm-Blue: OUT OF STOCK
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
test('no inventory units -> nothing unavailable', () => {
|
|
37
|
+
const r = rules([]);
|
|
38
|
+
expect(resolveUnavailableOptionIds(r, { fieldValues: {}, groups: [{ quantity: 1, fieldValues: {} }] }).size).toBe(0);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('with no other selection, an option with stock somewhere is available', () => {
|
|
42
|
+
const r = rules(units);
|
|
43
|
+
// Nothing selected: Blue has stock as 24mm-Blue, so not unavailable.
|
|
44
|
+
const u = resolveUnavailableOptionIds(r, { fieldValues: {}, groups: [{ quantity: 1, fieldValues: {} }] });
|
|
45
|
+
expect(u.has(813)).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('25mm makes Blue unavailable but keeps Red/Green available', () => {
|
|
49
|
+
const r = rules(units);
|
|
50
|
+
const u = resolveUnavailableOptionIds(r, {
|
|
51
|
+
fieldValues: {},
|
|
52
|
+
groups: [{ quantity: 1, fieldValues: { 800: { selectedOptionIds: [802] } } }],
|
|
53
|
+
});
|
|
54
|
+
expect(u.has(813)).toBe(true); // 25mm-Blue = 0 -> Blue disabled
|
|
55
|
+
expect(u.has(811)).toBe(false); // 25mm-Red ok
|
|
56
|
+
expect(u.has(812)).toBe(false); // 25mm-Green ok
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('selecting Blue makes 25mm unavailable (combination-aware both directions)', () => {
|
|
60
|
+
const r = rules(units);
|
|
61
|
+
const u = resolveUnavailableOptionIds(r, {
|
|
62
|
+
fieldValues: {},
|
|
63
|
+
groups: [{ quantity: 1, fieldValues: { 810: { selectedOptionIds: [813] } } }],
|
|
64
|
+
});
|
|
65
|
+
expect(u.has(802)).toBe(true); // 25mm + Blue = 0 -> 25mm disabled
|
|
66
|
+
expect(u.has(801)).toBe(false); // 24mm + Blue = 5 -> 24mm ok
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('per-group scope: one group selecting 25mm does not disable Blue in another', () => {
|
|
70
|
+
const r = rules(units);
|
|
71
|
+
const u = resolveUnavailableOptionIds(r, {
|
|
72
|
+
fieldValues: {},
|
|
73
|
+
groups: [{ quantity: 1, fieldValues: { 800: { selectedOptionIds: [801] } } }],
|
|
74
|
+
});
|
|
75
|
+
// 24mm selected here -> 24mm-Blue has stock -> Blue available.
|
|
76
|
+
expect(u.has(813)).toBe(false);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('omitted inventoryUnits -> nothing unavailable', () => {
|
|
80
|
+
const r = rules(units);
|
|
81
|
+
delete (r as any).inventoryUnits;
|
|
82
|
+
expect(resolveUnavailableOptionIds(r, { fieldValues: {} }).size).toBe(0);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('non-group product: top-level selections drive availability', () => {
|
|
86
|
+
const opt = (id: number) => ({
|
|
87
|
+
id, originalId: null, position: id, default: false,
|
|
88
|
+
variationCost: 0, variationUnitCost: 0,
|
|
89
|
+
variationCostDiscountGroup: null, variationUnitCostDiscountGroup: null,
|
|
90
|
+
selectedBy: [],
|
|
91
|
+
});
|
|
92
|
+
const f = (id: number, options: number[]): any => ({
|
|
93
|
+
id, originalId: id, position: 0, fieldType: 2, independent: true,
|
|
94
|
+
isSelectable: true, selectedBy: [],
|
|
95
|
+
variationCost: 0, variationUnitCost: 0,
|
|
96
|
+
variationCostDiscountGroup: null, variationUnitCostDiscountGroup: null,
|
|
97
|
+
options: options.map(opt),
|
|
98
|
+
});
|
|
99
|
+
const r: PricingRules = {
|
|
100
|
+
currency: 'AUD', taxPercent: 0,
|
|
101
|
+
product: { unitPrice: 0, minimumPrice: null, discountGroup: null },
|
|
102
|
+
hasGroups: false, needsInventory: true,
|
|
103
|
+
fields: [f(800, [801, 802]), f(810, [811, 813])],
|
|
104
|
+
groupFields: [],
|
|
105
|
+
inventoryUnits: [
|
|
106
|
+
{ optionIds: [802, 811], quantity: 7 }, // 25mm-Red
|
|
107
|
+
{ optionIds: [802, 813], quantity: 0 }, // 25mm-Blue OUT
|
|
108
|
+
],
|
|
109
|
+
};
|
|
110
|
+
const u = resolveUnavailableOptionIds(r, {
|
|
111
|
+
quantity: 1,
|
|
112
|
+
fieldValues: { 800: { selectedOptionIds: [802] } },
|
|
113
|
+
});
|
|
114
|
+
expect(u.has(813)).toBe(true); // 25mm-Blue unavailable
|
|
115
|
+
expect(u.has(811)).toBe(false); // 25mm-Red ok
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('non-inventory options and non-selection field values are ignored', () => {
|
|
119
|
+
const r = rules(units);
|
|
120
|
+
// Field 810 option 999 is not in any unit (not tracked); a value-only field
|
|
121
|
+
// and an undefined selection entry exercise the skip branches.
|
|
122
|
+
(r.groupFields[1].options as any).push({
|
|
123
|
+
id: 999, originalId: null, position: 9, default: false,
|
|
124
|
+
variationCost: 0, variationUnitCost: 0,
|
|
125
|
+
variationCostDiscountGroup: null, variationUnitCostDiscountGroup: null,
|
|
126
|
+
selectedBy: [],
|
|
127
|
+
});
|
|
128
|
+
const u = resolveUnavailableOptionIds(r, {
|
|
129
|
+
fieldValues: { 700: { value: 'text' }, 701: undefined as any },
|
|
130
|
+
groups: [{ quantity: 1, fieldValues: { 800: { selectedOptionIds: [802] }, 810: { selectedOptionIds: [999] } } }],
|
|
131
|
+
});
|
|
132
|
+
expect(u.has(999)).toBe(false); // untracked option is always available
|
|
133
|
+
expect(u.has(813)).toBe(true); // 25mm-Blue still unavailable
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('undefined top-level fieldValues and group fieldValues are tolerated', () => {
|
|
137
|
+
const r = rules(units);
|
|
138
|
+
const u = resolveUnavailableOptionIds(r, {
|
|
139
|
+
fieldValues: undefined as any,
|
|
140
|
+
groups: [{ quantity: 1, fieldValues: undefined as any }],
|
|
141
|
+
});
|
|
142
|
+
// No selections -> only options with zero stock everywhere would be flagged;
|
|
143
|
+
// every option here has stock in some combo, so none unavailable.
|
|
144
|
+
expect(u.size).toBe(0);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test('same field selected at top level and in a group merges selections', () => {
|
|
148
|
+
const r = rules(units);
|
|
149
|
+
const u = resolveUnavailableOptionIds(r, {
|
|
150
|
+
fieldValues: { 800: { selectedOptionIds: [802] } },
|
|
151
|
+
groups: [{ quantity: 1, fieldValues: { 800: { selectedOptionIds: [802] } } }],
|
|
152
|
+
});
|
|
153
|
+
expect(u.has(813)).toBe(true); // 25mm selected (both scopes) -> Blue unavailable
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test('unit referencing an unknown option id falls back to the raw id', () => {
|
|
157
|
+
const r = rules([
|
|
158
|
+
{ optionIds: [802, 813], quantity: 0 },
|
|
159
|
+
{ optionIds: [802, 5555], quantity: 9 }, // 5555 not a known option
|
|
160
|
+
]);
|
|
161
|
+
// 25mm + Blue still 0; the unknown id just never matches a selection.
|
|
162
|
+
const u = resolveUnavailableOptionIds(r, {
|
|
163
|
+
fieldValues: {},
|
|
164
|
+
groups: [{ quantity: 1, fieldValues: { 800: { selectedOptionIds: [802] } } }],
|
|
165
|
+
});
|
|
166
|
+
expect(u.has(813)).toBe(true);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('originalId is canonicalised when matching units', () => {
|
|
170
|
+
const opt = (id: number, originalId: number | null) => ({
|
|
171
|
+
id, originalId, position: id, default: false,
|
|
172
|
+
variationCost: 0, variationUnitCost: 0,
|
|
173
|
+
variationCostDiscountGroup: null, variationUnitCostDiscountGroup: null,
|
|
174
|
+
selectedBy: [],
|
|
175
|
+
});
|
|
176
|
+
const r: PricingRules = {
|
|
177
|
+
currency: 'AUD', taxPercent: 0,
|
|
178
|
+
product: { unitPrice: 0, minimumPrice: null, discountGroup: null },
|
|
179
|
+
fields: [], hasGroups: true, needsInventory: true,
|
|
180
|
+
groupFields: [
|
|
181
|
+
{
|
|
182
|
+
id: 800, originalId: 800, position: 0, fieldType: 2, independent: false,
|
|
183
|
+
isSelectable: true, selectedBy: [],
|
|
184
|
+
variationCost: 0, variationUnitCost: 0,
|
|
185
|
+
variationCostDiscountGroup: null, variationUnitCostDiscountGroup: null,
|
|
186
|
+
options: [opt(900, 802)], // cloned option 900 whose original is 802
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
// inventory references the original id 802
|
|
190
|
+
inventoryUnits: [{ optionIds: [802], quantity: 0 }],
|
|
191
|
+
};
|
|
192
|
+
const u = resolveUnavailableOptionIds(r, {
|
|
193
|
+
fieldValues: {},
|
|
194
|
+
groups: [{ quantity: 1, fieldValues: {} }],
|
|
195
|
+
});
|
|
196
|
+
expect(u.has(900)).toBe(true); // original 802 has 0 stock -> cloned 900 unavailable
|
|
197
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { PricingRules, PricingField, Selections } from './types.js';
|
|
2
|
+
|
|
3
|
+
// Returns the set of option ids that should be DISABLED because the combination
|
|
4
|
+
// of (current inventory-relevant selections + that option) has no matching
|
|
5
|
+
// inventory with stock. Mirrors the server's subset match
|
|
6
|
+
// (inventories.py `inventories_subset_match_strategy`): an inventory matches a
|
|
7
|
+
// selection when it contains every selected option; an option is available if
|
|
8
|
+
// any matching inventory has quantity > 0.
|
|
9
|
+
//
|
|
10
|
+
// Scope: pass the container's selections (independent fields use the top-level
|
|
11
|
+
// selections; a group uses its own + independent). Only options that appear in
|
|
12
|
+
// at least one inventory unit are inventory-tracked; all others are always
|
|
13
|
+
// available. Direct product inventories only (no shared inventory groups).
|
|
14
|
+
export function resolveUnavailableOptionIds(
|
|
15
|
+
rules: PricingRules,
|
|
16
|
+
selections: Selections
|
|
17
|
+
): Set<number> {
|
|
18
|
+
const unavailable = new Set<number>();
|
|
19
|
+
const units = rules.inventoryUnits || [];
|
|
20
|
+
if (units.length === 0) return unavailable;
|
|
21
|
+
|
|
22
|
+
const allFields: PricingField[] = [...rules.fields, ...rules.groupFields];
|
|
23
|
+
|
|
24
|
+
// Canonicalise id/originalId to a single id per option so bundle option ids,
|
|
25
|
+
// selected ids and inventory unit ids all compare consistently.
|
|
26
|
+
const canonical = new Map<number, number>();
|
|
27
|
+
const fieldIdByOption = new Map<number, number>();
|
|
28
|
+
for (const f of allFields) {
|
|
29
|
+
for (const o of f.options) {
|
|
30
|
+
canonical.set(o.id, o.id);
|
|
31
|
+
fieldIdByOption.set(o.id, f.id);
|
|
32
|
+
if (o.originalId != null) {
|
|
33
|
+
canonical.set(o.originalId, o.id);
|
|
34
|
+
fieldIdByOption.set(o.originalId, f.id);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const canon = (id: number): number => (canonical.has(id) ? (canonical.get(id) as number) : id);
|
|
39
|
+
|
|
40
|
+
const unitSets = units.map((u) => ({
|
|
41
|
+
quantity: u.quantity,
|
|
42
|
+
optionIds: new Set(u.optionIds.map(canon)),
|
|
43
|
+
}));
|
|
44
|
+
const inventoryOptionIds = new Set<number>();
|
|
45
|
+
for (const u of unitSets) {
|
|
46
|
+
for (const id of u.optionIds) inventoryOptionIds.add(id);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Collect currently-selected, inventory-tracked option ids grouped by field.
|
|
50
|
+
const selectedByField = new Map<number, Set<number>>();
|
|
51
|
+
const addSelections = (
|
|
52
|
+
fieldValues: Record<number, { selectedOptionIds?: number[] }>
|
|
53
|
+
) => {
|
|
54
|
+
for (const key of Object.keys(fieldValues)) {
|
|
55
|
+
const fieldId = Number(key);
|
|
56
|
+
const sel = fieldValues[fieldId];
|
|
57
|
+
if (!sel || !sel.selectedOptionIds) continue;
|
|
58
|
+
const set = selectedByField.get(fieldId) || new Set<number>();
|
|
59
|
+
for (const oid of sel.selectedOptionIds) {
|
|
60
|
+
const c = canon(oid);
|
|
61
|
+
if (inventoryOptionIds.has(c)) set.add(c);
|
|
62
|
+
}
|
|
63
|
+
selectedByField.set(fieldId, set);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
addSelections(selections.fieldValues || {});
|
|
67
|
+
for (const group of selections.groups || []) addSelections(group.fieldValues || {});
|
|
68
|
+
|
|
69
|
+
const hasGroups = Boolean(selections.groups && selections.groups.length > 0);
|
|
70
|
+
const fieldsToConsider = hasGroups ? allFields : rules.fields;
|
|
71
|
+
|
|
72
|
+
for (const f of fieldsToConsider) {
|
|
73
|
+
for (const o of f.options) {
|
|
74
|
+
const candidate = canon(o.id);
|
|
75
|
+
if (!inventoryOptionIds.has(candidate)) continue; // not inventory-tracked
|
|
76
|
+
// filter = candidate + selected inventory options from OTHER fields.
|
|
77
|
+
const filter: number[] = [candidate];
|
|
78
|
+
for (const [fieldId, set] of selectedByField) {
|
|
79
|
+
if (fieldId === f.id) continue;
|
|
80
|
+
for (const sid of set) filter.push(sid);
|
|
81
|
+
}
|
|
82
|
+
const available = unitSets.some(
|
|
83
|
+
(u) => u.quantity > 0 && filter.every((id) => u.optionIds.has(id))
|
|
84
|
+
);
|
|
85
|
+
if (!available) unavailable.add(o.id);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return unavailable;
|
|
89
|
+
}
|