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

Nc feat/webhook conditions #7246

Merged
merged 11 commits into from
Dec 20, 2023
13 changes: 11 additions & 2 deletions packages/nc-gui/components/smartsheet/toolbar/ColumnFilter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,17 @@ const filtersCount = computed(() => {
}, 0)
})

const applyChanges = async (hookId?: string, _nested = false) => {
await sync(hookId, _nested)
const applyChanges = async (hookId?: string, nested = false, isConditionSupported = true) => {
// if condition is not supported, delete all filters present
// it's used for bulk webhooks with filters since bulk webhooks don't support conditions at the moment
if (!isConditionSupported) {
// iterate in reverse order and delete all filters, reverse order is for getting the correct index
for (let i = filters.value.length - 1; i >= 0; i--) {
await deleteFilter(filters.value[i], i)
}
}

await sync(hookId, nested)

if (!localNestedFilters.value?.length) return

Expand Down
10 changes: 7 additions & 3 deletions packages/nc-gui/components/webhook/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ async function loadPluginList() {
}
}

const isConditionSupport = computed(() => {
return hookRef.eventOperation && !hookRef.eventOperation.includes('bulk')
})

async function saveHooks() {
loading.value = true
try {
Expand Down Expand Up @@ -446,7 +450,7 @@ async function saveHooks() {
}

if (filterRef.value) {
await filterRef.value.applyChanges(hookRef.id)
await filterRef.value.applyChanges(hookRef.id, false, isConditionSupport.value)
}

// Webhook details updated successfully
Expand Down Expand Up @@ -770,8 +774,7 @@ onMounted(async () => {
</a-form-item>
</a-col>
</a-row>

<a-row class="mb-5" type="flex">
<a-row v-show="isConditionSupport" class="mb-5" type="flex">
<a-col :span="24">
<div class="rounded-lg border-1 p-6">
<a-checkbox
Expand All @@ -790,6 +793,7 @@ onMounted(async () => {
:show-loading="false"
:hook-id="hookRef.id"
:web-hook="true"
@update:filtersLength="hookRef.condition = $event > 0"
/>
</div>
</a-col>
Expand Down
36 changes: 31 additions & 5 deletions packages/nocodb/src/helpers/webhookHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,26 @@ export async function invokeWebhook(
}

if (hook.condition && !testHook) {
const filters = testFilters || (await hook.getFilters());

if (isBulkOperation) {
const filteredData = [];
for (const data of newData) {
for (let i = 0; i < newData.length; i++) {
const data = newData[i];

// disable until we have a way to extract prevData for bulk operations
// const pData = prevData[i] ? prevData[i] : null;
//
// // if condition is satisfied for prevData then return
// // if filters are not defined then skip the check
// if (
// pData &&
// filters.length &&
// (await validateCondition(filters, pData))
// ) {
// continue;
// }

if (
await validateCondition(
testFilters || (await hook.getFilters()),
Expand All @@ -312,12 +329,21 @@ export async function invokeWebhook(
) {
filteredData.push(data);
}
if (!filteredData.length) {
return;
}
newData = filteredData;
}
if (!filteredData.length) {
return;
}
newData = filteredData;
} else {
// if condition is satisfied for prevData then return
// if filters are not defined then skip the check
if (
prevData &&
filters.length &&
(await validateCondition(filters, prevData))
) {
return;
}
if (
!(await validateCondition(
testFilters || (await hook.getFilters()),
Expand Down