a1c1b3c8 by acetousk

Create service job and necessary data for it

1 parent 945aff82
...@@ -323,4 +323,75 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -323,4 +323,75 @@ along with this software (see the LICENSE.md file). If not, see
323 </actions> 323 </actions>
324 </service> 324 </service>
325 325
326 <service verb="remove" noun="ExcessMjmlEmailResourceHistories">
327 <in-parameters>
328 <parameter name="runDate" default="ec.user.nowTimestamp" required="true" type="Timestamp"/>
329 </in-parameters>
330 <out-parameters>
331 <parameter name="removedHistoryList"/>
332 </out-parameters>
333 <actions>
334 <entity-find entity-name="moqui.basic.email.EmailTemplate" list="emailTemplateList">
335 <econdition field-name="grapesLocation" operator="is-not-null"/>
336 <econdition field-name="htmlLocation" operator="is-not-null"/>
337 <select-field field-name="emailTemplateId,grapesLocation,htmlLocation,grapesPublishedVersionName,htmlPublishedVersionName"/>
338 <order-by field-name="-lastUpdatedStamp"/>
339 </entity-find>
340 <set field="resourceList" from="[]"/>
341 <iterate list="emailTemplateList" entry="emailTemplate">
342 <set field="grapesFile" from="ec.resource.getLocationReference(emailTemplate.grapesLocation)"/>
343 <if condition="grapesFile">
344 <script><![CDATA[
345 def versionWhitelist = []
346 if (grapesFile?.getCurrentVersion()?.getVersionName()) versionWhitelist.add(grapesFile.getCurrentVersion().getVersionName())
347 if (emailTemplate?.grapesPublishedVersionName) versionWhitelist.add(emailTemplate.grapesPublishedVersionName)
348 resourceList.add([location:grapesFile.location,resourceId:grapesFile.dbResourceId,versionWhitelist:versionWhitelist.size()>0?versionWhitelist:null])]]></script>
349 </if>
350 <set field="htmlFile" from="ec.resource.getLocationReference(emailTemplate.htmlLocation)"/>
351 <if condition="htmlFile">
352 <script><![CDATA[
353 def versionWhitelist = []
354 if (htmlFile?.getCurrentVersion()?.getVersionName()) versionWhitelist.add(htmlFile.getCurrentVersion().getVersionName())
355 if (emailTemplate?.htmlPublishedVersionName) versionWhitelist.add(emailTemplate.htmlPublishedVersionName)
356 resourceList.add([location:htmlFile.location,resourceId:htmlFile.dbResourceId,versionWhitelist:versionWhitelist.size()>0?versionWhitelist:null])]]></script>
357 </if>
358 </iterate>
359
360 <set field="oneHourAgo" from="Timestamp.from(runDate.toInstant().minus(1, java.time.temporal.ChronoUnit.HOURS))"/>
361 <set field="removedHistoryList" from="[]"/>
362
363 <iterate list="resourceList" entry="resource">
364 <entity-find entity-name="moqui.resource.DbResourceFileHistory" list="resourceHistoryPreList">
365 <econdition field-name="resourceId" from="resource.resourceId"/>
366 <econdition field-name="versionDate" operator="greater" from="oneHourAgo"/>
367 <econdition field-name="versionDate" operator="less-equals" from="runDate"/>
368 <select-field field-name="resourceId,versionName,versionDate,previousVersionName,userId"/>
369 <order-by field-name="-versionDate"/>
370 </entity-find>
371 <set field="skippedUserIds" from="[]"/>
372 <set field="resourceHistoryList" from="[]"/>
373 <iterate list="resourceHistoryPreList" entry="resourceHistory">
374 <!-- Add all but the single latest history per userId to resourceHistoryList -->
375 <if condition="resource.versionWhitelist.contains(resourceHistory.versionName) || !skippedUserIds.contains(resourceHistory.userId)">
376 <!-- <log level="warn" message="${resource.resourceId} ${resourceHistory.versionName} userId ${resourceHistory.userId}"/>-->
377 <set field="skippedUserIds" from="skippedUserIds + [resourceHistory.userId]"/><continue/></if>
378 <set field="resourceHistoryList" from="resourceHistoryList + [resourceHistory]"/>
379 </iterate>
380 <iterate list="resourceHistoryList" entry="resourceHistory">
381 <entity-find-one entity-name="moqui.resource.DbResourceFileHistory" value-field="usedResourceHistory" auto-field-map="[resourceId:resource.resourceId,previousVersionName:resourceHistory.versionName]" for-update="true">
382 <select-field field-name="resourceId,versionName,versionDate,previousVersionName,userId"/></entity-find-one>
383 <!-- <log level="warn" message="set versionName ${usedResourceHistory.versionName} of previousVersionName ${usedResourceHistory.previousVersionName} to ${resourceHistory.previousVersionName}"/>-->
384 <set field="usedResourceHistory.previousVersionName" from="resourceHistory.previousVersionName"/>
385
386 <log level="info" message="Removed resource history ${resourceHistory.resourceId} version ${resourceHistory.versionName}"/>
387 <set field="removedHistoryList" from="removedHistoryList + [resourceId:resource.resourceId, versionName:resourceHistory.versionName,versionDate:resourceHistory.versionDate,userId:resourceHistory.userId]"/>
388
389 <entity-update value-field="usedResourceHistory"/>
390 <entity-delete value-field="resourceHistory"/>
391 </iterate>
392 </iterate>
393
394 </actions>
395 </service>
396
326 </services> 397 </services>
......