Skip to content

Commit

Permalink
Merge pull request #27 from hotwax/#26
Browse files Browse the repository at this point in the history
Improved: Cycle count bulk import to better handle the erroneous csv.
  • Loading branch information
dixitdeepak authored Dec 3, 2024
2 parents 8f63cca + e2826be commit 0b3ad53
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
78 changes: 73 additions & 5 deletions service/co/hotwax/cycleCount/InventoryCountServices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,16 @@
<return error="true" message="SystemMessage record not found for systemMessageId: ${systemMessageId}"/>
</if>
<set field="csvData" from="ec.resource.getLocationReference(systemMessage.messageText).getText()"/>
<if condition="!csvData">
<return error="true" message="Csv data not found for systemMessageId: ${systemMessageId}"/>
</if>
<script>
recordsLoaded = ec.entity.makeDataLoader().csvText(csvData).csvEntityName("co.hotwax.cycleCount.InventoryCountServices.import#InventoryCount").load()
</script>
<message>Loaded ${recordsLoaded} inventory count import items</message>
</actions>
</service>
<service verb="import" noun="InventoryCount" transaction="force-new">
<service verb="import" noun="InventoryCount">
<in-parameters>
<parameter name="countImportName" required="true"/>
<parameter name="facilityId"/>
Expand All @@ -402,10 +405,12 @@
</in-parameters>
<actions>

<!--Processing cycle count items in Draft and Assigned status only-->
<set field="allowedStatusIds" value="['INV_COUNT_CREATED', 'INV_COUNT_ASSIGNED']"/>
<if condition="statusId != null &amp;&amp; !allowedStatusIds.contains(statusId)">
<return/>
<service-call name="co.hotwax.cycleCount.InventoryCountServices.validate#InventoryCount"
in-map="context"
out-map="validationResult"/>
<if condition="!validationResult.errorList.isEmpty()">
<script>errorMessage = validationResult.errorList.join("; ")</script>
<return error="true" message="${errorMessage}"/>
</if>

<if condition="!facilityId &amp;&amp; externalFacilityId">
Expand Down Expand Up @@ -437,4 +442,67 @@
out-map="result"/>
</actions>
</service>
<service verb="validate" noun="InventoryCount">
<implements service="co.hotwax.cycleCount.InventoryCountServices.import#InventoryCount"/>
<out-parameters>
<parameter name="errorList"/>
</out-parameters>
<actions>
<set field="errorList" from="[]"/>
<script>
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException
</script>

<!--Processing cycle count items in Draft and Assigned status only-->
<set field="allowedStatusIds" value="['INV_COUNT_CREATED', 'INV_COUNT_ASSIGNED']"/>
<if condition="statusId != null &amp;&amp; !allowedStatusIds.contains(statusId)">
<script>errorList.add("Invalid statusId: ${statusId}. Allowed values are INV_COUNT_CREATED or INV_COUNT_ASSIGNED.")</script>
</if>

<!-- Validate facilityId -->
<if condition="facilityId">
<entity-find-one entity-name="org.apache.ofbiz.product.facility.Facility" value-field="facility"/>
<if condition="!facility">
<script>errorList.add("Invalid facilityId: ${facilityId}. No matching facility found.")</script>
</if>
</if>

<!-- Validate externalFacilityId -->
<if condition="!facilityId &amp;&amp; externalFacilityId">
<entity-find entity-name="org.apache.ofbiz.product.facility.Facility" list="facilities">
<econdition field-name="externalId" from="externalFacilityId"/>
</entity-find>
<if condition="!facilities">
<script>errorList.add("Invalid externalFacilityId: ${externalFacilityId}. No matching facility found.")</script>
</if>
</if>

<!-- Validate dueDate format: yyyy-MM-dd -->
<if condition="dueDate">
<script>
try {
LocalDate.parse(dueDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
} catch (DateTimeParseException e) {
errorList.add("Invalid dueDate format: ${dueDate}. Expected format is yyyy-MM-dd.")
}
</script>
</if>
</actions>
</service>
<service verb="get" noun="UploadedCsvFile">
<in-parameters>
<parameter name="systemMessageId" required="true"/>
</in-parameters>
<out-parameters>
<parameter name="csvData"/>
</out-parameters>
<actions>
<entity-find-one entity-name="moqui.service.message.SystemMessage" value-field="systemMessage"/>
<if condition="!systemMessage">
<return error="true" message="SystemMessage record not found for systemMessageId: ${systemMessageId}"/>
</if>
<set field="csvData" from="ec.resource.getLocationReference(systemMessage.messageText).getText()"/>
</actions>
</service>
</services>
6 changes: 6 additions & 0 deletions service/inventorycount.rest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@
<method type="get"><entity name="moqui.service.message.SystemMessage" operation="list"/></method>
<id name="systemMessageId">
<method type="post"><entity name="moqui.service.message.SystemMessage" operation="store"/></method>
<resource name="downloadFile">
<method type="get"><service name="co.hotwax.cycleCount.InventoryCountServices.get#UploadedCsvFile"/></method>
</resource>
<resource name="errors">
<method type="get"><entity name="moqui.service.message.SystemMessageError" operation="list"/></method>
</resource>
</id>
</resource>
</resource>
Expand Down

0 comments on commit 0b3ad53

Please sign in to comment.