Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the services to create products, variants and the related records #10

Merged
merged 9 commits into from
Jan 21, 2025
148 changes: 148 additions & 0 deletions service/co/hotwax/oms/ProductServices.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?xml version="1.0" encoding="UTF-8"?>
<services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/service-definition-3.xsd">

<service verb="create" noun="ProductAndVariants" transaction-timeout="1800">
<description>Service to create the virtual and variant products.</description>
<in-parameters>
<parameter name="productJson" type="Map" required="true">
<description>The productJson containing product and its variants.</description>
</parameter>
</in-parameters>
<actions>
<!-- Set the productVariants field from the incoming productJson map -->
<set field="productVariants" from="productJson.variants"/>

<!-- Check if the productId is not null then only create ShopifyShopProduct record -->
<if condition="productJson.productId!=null">
<!-- Create ShopifyShopProduct record -->
<service-call name="store#co.hotwax.shopify.ShopifyShopProduct" in-map="[shopId:productJson.shopifyShopProduct.shopId,
productId:productJson.productId,shopifyProductId:productJson.shopifyShopProduct.shopifyProductId]"/>
<set field="parentProductId" from="productJson.productId"/>
<return/>
</if>
<!-- Set the shopifyShopProduct field -->
<set field="shopifyShopProduct" from="productJson.shopifyShopProduct"/>
<script>productJson.remove(shopifyShopProduct)</script>

<!-- Call the prepare#ProductCreate service to get the json for creating product -->
<service-call name="co.hotwax.oms.ProductServices.prepare#ProductCreate" in-map="[productJson:productJson]"
out-map="prepareProductCreateOut"/>

<!-- Call the create service on Product -->
<service-call name="create#org.apache.ofbiz.product.product.Product" in-map="prepareProductCreateOut.productJson"
out-map="createProductOut"/>

<!-- Set the parentProductId -->
<set field="parentProductId" from="createProductOut.productId"/>

<!-- Create ShopifyShopProduct record -->
<service-call name="create#co.hotwax.shopify.ShopifyShopProduct" in-map="[shopId:shopifyShopProduct.shopId,
productId:createProductOut.productId,shopifyProductId:shopifyShopProduct.shopifyProductId]" out-map="context"/>

<!-- Iterate the productVariants list -->
<iterate list="productVariants" entry="productVariant">
<!-- For each product variant call the create#ProductVariant service -->
<service-call name="co.hotwax.oms.ProductServices.create#ProductVariant" in-map="[productVariantJson:productVariant,
parentProductId:parentProductId]"/>
</iterate>
</actions>
</service>

<service verb="create" noun="ProductVariant" transaction-timeout="1800">
<description>Service to create Product Variant.</description>
<in-parameters>
<parameter name="productVariantJson" type="Map" required="true">
<description>The product variant json to be created.</description>
</parameter>
<parameter name="parentProductId" required="true">
<description>The parent product id for the variants.</description>
</parameter>
</in-parameters>
<actions>
<!-- Check if productId is not null -->
<if condition="productVariantJson.productId!=null">
<!-- Create ShopifyShopProduct record -->
<service-call name="create#co.hotwax.shopify.ShopifyShopProduct" in-map="[shopId:productVariantJson.shopifyShopProduct.shopId,
productId:productVariantJson.productId,shopifyProductId:productVariantJson.shopifyShopProduct.shopifyProductId,
shopifyInventoryItemId:productVariantJson.shopifyShopProduct.shopifyInventoryItemId]" out-map="context"/>

<!-- Create ProductAssoc record for the given productVariant and parentProductId -->
<service-call name="create#org.apache.ofbiz.product.product.ProductAssoc" in-map="[productId:parentProductId,productIdTo:productVariantJson.productId,productAssocTypeId:'PRODUCT_VARIANT']"/>
<return/>
</if>

<!-- Set the shopifyShopProductMap from productVariantJsonMap -->
<set field="shopifyShopProduct" from="productVariantJson.shopifyShopProduct"/>
<script>productVariantJson.remove(shopifyShopProduct)</script>

<!-- Call prepare#ProductCreate for the productVariantJson map -->
<service-call name="co.hotwax.oms.ProductServices.prepare#ProductCreate" in-map="[productJson:productVariantJson]"
out-map="prepareProductCreateOut"/>

<!-- Create Product record for the variant product -->
<service-call name="create#org.apache.ofbiz.product.product.Product" in-map="prepareProductCreateOut.productJson"
out-map="createProductOut"/>

<!-- Create ProductAssoc record for the variant product -->
<service-call name="create#org.apache.ofbiz.product.product.ProductAssoc" in-map="[productId:parentProductId,productIdTo:createProductOut.productId,productAssocTypeId:'PRODUCT_VARIANT']"/>

<!-- Create ShopifyShopProduct record for the variant product -->
<service-call name="create#co.hotwax.shopify.ShopifyShopProduct" in-map="[shopId:shopifyShopProduct.shopId,
productId:createProductOut.productId,shopifyProductId:shopifyShopProduct.shopifyProductId,
shopifyInventoryItemId:shopifyShopProduct.shopifyInventoryItemId]" out-map="context"/>
</actions>
</service>

<service verb="prepare" noun="ProductCreate" transaction-timeout="1800">
<description>Service to create the json map required for creating the product and its related records.</description>
<in-parameters>
<parameter name="productJson" type="Map" required="true">
<description>The input json map for product.</description>
</parameter>
</in-parameters>
<out-parameters>
<parameter name="productJson" type="Map">
<description>The output json map for the product.</description>
</parameter>
</out-parameters>
<actions>
<!-- Set the features list -->
<set field="features" from="productJson.features"/>
mridulpathak marked this conversation as resolved.
Show resolved Hide resolved

<!-- Remove the product features list -->
<script>productJson.remove(features)</script>

<!-- Check if features list is not null -->
<if condition="features!=null">
<!-- Initialize the featureAppls list -->
<set field="featureAppls" from="[]"/>

<!-- Iterate through the features list -->
<iterate list="features" entry="feature">
<!-- Check if productFeatureTypeId is null then create one and set the productFeatureTypeId -->
<if condition="feature.productFeatureTypeId == null">
<service-call name="create#org.apache.ofbiz.product.feature.ProductFeatureType" in-map="[description: feature.productFeatureTypeId]" out-map="createProductFeatureTypeOutput"/>
<set field="feature.productFeatureTypeId" from="createProductFeatureTypeOutput.productFeatureTypeId"/>
</if>
<!-- Create new productFeatureId if it doesn't exist -->
<if condition="feature.productFeatureId == null">
<service-call name="create#org.apache.ofbiz.product.feature.ProductFeature" in-map="feature" out-map="createProductFeatureOutput"/>
<set field="feature.productFeatureId" from="createProductFeatureOutput.productFeatureId"/>
</if>
<set field="productFeatureAppl" from="[:]"/>
<set field="productFeatureAppl.productFeatureId" from="feature.productFeatureId"/>
<set field="productFeatureAppl.productFeatureApplTypeId" from="feature.productFeatureApplTypeId"/>
<set field="productFeatureAppl.sequenceNum" from="feature.position"/>
<set field="productFeatureAppl.fromDate" from="ec.user.nowTimestamp"/>
<set field="featureAppls" from="featureAppls+productFeatureAppl"/>
<if condition="featureAppls!=null">
<script>productJson.put("featureAppls", featureAppls)</script>
mridulpathak marked this conversation as resolved.
Show resolved Hide resolved
</if>
</iterate>
</if>
<!-- Adding log for testing -->
<log message="======productJson=====${productJson}===="/>
</actions>
</service>
</services>