1
0
Fork 0
mirror of synced 2024-07-03 05:20:32 +12:00

Merge pull request #8394 from Budibase/bug/sev3/hbs-email-automation

Automations substitute 'loop' in step bindings fixes
This commit is contained in:
melohagan 2022-10-25 16:32:49 +01:00 committed by GitHub
commit 96acee84ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 14 deletions

View file

@ -120,7 +120,7 @@
allSteps[idx]?.stepId === ActionStepID.LOOP && allSteps[idx]?.stepId === ActionStepID.LOOP &&
allSteps.find(x => x.blockToLoop === block.id) allSteps.find(x => x.blockToLoop === block.id)
// If the previous block was a loop block, decerement the index so the following // If the previous block was a loop block, decrement the index so the following
// steps are in the correct order // steps are in the correct order
if (wasLoopBlock) { if (wasLoopBlock) {
loopBlockCount++ loopBlockCount++

View file

@ -1,6 +1,5 @@
const { getTable } = require("../api/controllers/table/utils") const { getTable } = require("../api/controllers/table/utils")
const { const {
findHBSBlocks,
decodeJSBinding, decodeJSBinding,
isJSBinding, isJSBinding,
encodeJSBinding, encodeJSBinding,
@ -82,24 +81,34 @@ exports.getError = err => {
} }
exports.substituteLoopStep = (hbsString, substitute) => { exports.substituteLoopStep = (hbsString, substitute) => {
let blocks = []
let checkForJS = isJSBinding(hbsString) let checkForJS = isJSBinding(hbsString)
let substitutedHbsString = ""
let open = checkForJS ? `$("` : "{{"
let closed = checkForJS ? `")` : "}}"
if (checkForJS) { if (checkForJS) {
hbsString = decodeJSBinding(hbsString) hbsString = decodeJSBinding(hbsString)
blocks.push(hbsString)
} else {
blocks = findHBSBlocks(hbsString)
} }
for (let block of blocks) { let pointer = 0,
block = block.replace(/loop/, substitute) openPointer = 0,
closedPointer = 0
while (pointer < hbsString.length) {
openPointer = hbsString.indexOf(open, pointer)
closedPointer = hbsString.indexOf(closed, pointer) + 2
if (openPointer < 0 || closedPointer < 0) {
substitutedHbsString += hbsString.substring(pointer)
break
}
let before = hbsString.substring(pointer, openPointer)
let block = hbsString
.substring(openPointer, closedPointer)
.replace(/loop/, substitute)
substitutedHbsString += before + block
pointer = closedPointer
}
if (checkForJS) { if (checkForJS) {
hbsString = encodeJSBinding(block) substitutedHbsString = encodeJSBinding(substitutedHbsString)
} else {
hbsString = block
} }
} return substitutedHbsString
return hbsString
} }
exports.stringSplit = value => { exports.stringSplit = value => {

View file

@ -0,0 +1,17 @@
const automationUtils = require("../automationUtils")
describe("automationUtils", () => {
test("substituteLoopStep should allow multiple loop binding substitutes", () => {
expect(automationUtils.substituteLoopStep(
`{{ loop.currentItem._id }} {{ loop.currentItem._id }} {{ loop.currentItem._id }}`,
"step.2"))
.toBe(`{{ step.2.currentItem._id }} {{ step.2.currentItem._id }} {{ step.2.currentItem._id }}`)
})
test("substituteLoopStep should handle not subsituting outside of curly braces", () => {
expect(automationUtils.substituteLoopStep(
`loop {{ loop.currentItem._id }}loop loop{{ loop.currentItem._id }}loop`,
"step.2"))
.toBe(`loop {{ step.2.currentItem._id }}loop loop{{ step.2.currentItem._id }}loop`)
})
})