Skip to content

Commit

Permalink
enabled has-members edge condition
Browse files Browse the repository at this point in the history
[email protected] initiated a sync from Lightning
  • Loading branch information
openfn[bot] authored and github-actions[bot] committed Jan 21, 2025
1 parent bad6ab7 commit e158698
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 128 deletions.
13 changes: 8 additions & 5 deletions openfn-f07240d8-8bb7-4cff-9e20-cc8d8059892f-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ workflows:
name: Get deleted campaign members
adaptor: '@openfn/[email protected]'
credential: mribeirocruz@verasolutions.org-Sandbox---OpenFn-Mailchimp-integration-2
body:
body:
path: workflows/remove-members/fetch-deleted.js

Remove-deleted-members-tags:
name: Remove deleted members tags
adaptor: '@openfn/[email protected]'
credential: [email protected]
body:
body:
path: workflows/remove-members/remove-deleted.js

triggers:
Expand All @@ -69,23 +70,25 @@ workflows:
condition_label: has-members
condition_expression: |
state.members.length > 0 && !state.errors
enabled: false
enabled: true
Upsert-Members:
name: Upsert Members
jobs:
Get-members-for-upsert:
name: Get members for upsert
adaptor: '@openfn/[email protected]'
credential: [email protected]
body:
body:
path: workflows/upsert-members/fetch-members.js

Add-or-update-member-tags:
name: Add or update member tags
adaptor: '@openfn/[email protected]'
credential: [email protected]
body:
body:
path: workflows/upsert-members/upsert-tags.js

triggers:
cron:
type: cron
Expand Down
100 changes: 47 additions & 53 deletions openfn-f07240d8-8bb7-4cff-9e20-cc8d8059892f-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
"description": null,
"inserted_at": "2024-03-19T13:09:53Z",
"updated_at": "2025-01-21T10:41:24Z",
"scheduled_deletion": null,
"history_retention_period": 180,
"dataclip_retention_period": 180,
"retention_policy": "retain_all",
"project_credentials": {
"[email protected]": {
"id": "7085f9d0-6bc9-403d-8e9b-cea31d66593a",
Expand Down Expand Up @@ -65,29 +61,24 @@
"owner": "[email protected]"
}
},
"scheduled_deletion": null,
"history_retention_period": 180,
"dataclip_retention_period": 180,
"retention_policy": "retain_all",
"workflows": {
"Upsert-Members": {
"id": "c1873753-c335-40ad-8fcc-bc3a347ca0e0",
"name": "Upsert Members",
"edges": {
"cron->Get-members-for-upsert": {
"enabled": true,
"id": "e8588202-23da-42b3-8ee5-b04cff8342ee",
"source_trigger_id": "b22a60d5-7c93-4ebd-805b-772b9f738e76",
"condition_type": "always",
"target_job_id": "767da658-51fe-49e6-a3a8-a90695c51dc7"
},
"Get-members-for-upsert->Add-or-update-member-tags": {
"inserted_at": "2025-01-21T10:41:24.514105Z",
"lock_version": 32,
"triggers": {
"cron": {
"enabled": true,
"id": "11ccb755-d08e-4c78-ad2e-dbea16de18eb",
"source_job_id": "767da658-51fe-49e6-a3a8-a90695c51dc7",
"condition_type": "on_job_success",
"target_job_id": "b302b4b7-aa10-499c-b91a-cc9f5cf3a50b"
"id": "b22a60d5-7c93-4ebd-805b-772b9f738e76",
"type": "cron",
"cron_expression": "*/150 * * * *"
}
},
"concurrency": null,
"inserted_at": "2024-03-19T13:09:53Z",
"updated_at": "2025-01-21T10:41:24Z",
"jobs": {
"Get-members-for-upsert": {
"id": "767da658-51fe-49e6-a3a8-a90695c51dc7",
Expand All @@ -104,41 +95,36 @@
"project_credential_id": "b2f164e7-3e80-4aaf-8184-b4eb49b740e4"
}
},
"deleted_at": null,
"lock_version": 32,
"triggers": {
"cron": {
"edges": {
"cron->Get-members-for-upsert": {
"enabled": true,
"id": "b22a60d5-7c93-4ebd-805b-772b9f738e76",
"type": "cron",
"cron_expression": "*/150 * * * *"
"id": "e8588202-23da-42b3-8ee5-b04cff8342ee",
"source_trigger_id": "b22a60d5-7c93-4ebd-805b-772b9f738e76",
"condition_type": "always",
"target_job_id": "767da658-51fe-49e6-a3a8-a90695c51dc7"
},
"Get-members-for-upsert->Add-or-update-member-tags": {
"enabled": true,
"id": "11ccb755-d08e-4c78-ad2e-dbea16de18eb",
"source_job_id": "767da658-51fe-49e6-a3a8-a90695c51dc7",
"condition_type": "on_job_success",
"target_job_id": "b302b4b7-aa10-499c-b91a-cc9f5cf3a50b"
}
}
},
"Remove-Deleted-Campaign-Members/Tags": {
"id": "273e2c2a-a97d-49eb-bebb-2dad0666a716",
"name": "Remove Deleted Campaign Members/Tags",
"edges": {
"cron->Get-deleted-campaign-members": {
"enabled": true,
"id": "52a622f3-ab69-4453-9a42-698cc4bf0ff5",
"source_trigger_id": "cf67b9c5-e2e0-4ca1-8aca-8e7f32c79d98",
"condition_type": "always",
"target_job_id": "de821b2b-f7f8-4cc3-ad8f-c746c8698b0f"
},
"Get-deleted-campaign-members->Remove-deleted-members-tags": {
"inserted_at": "2025-01-21T10:42:42.218036Z",
"lock_version": 147,
"triggers": {
"cron": {
"enabled": false,
"id": "d0104c99-4efe-4783-9314-f10421927c65",
"source_job_id": "de821b2b-f7f8-4cc3-ad8f-c746c8698b0f",
"condition_expression": "state.members.length > 0 && !state.errors\n",
"condition_type": "js_expression",
"condition_label": "has-members",
"target_job_id": "d839d64f-78f7-4178-ab9d-482bbedd8866"
"id": "cf67b9c5-e2e0-4ca1-8aca-8e7f32c79d98",
"type": "cron",
"cron_expression": "*/5 * * * *"
}
},
"concurrency": null,
"inserted_at": "2024-08-19T15:35:54Z",
"updated_at": "2025-01-21T10:41:24Z",
"jobs": {
"Get-deleted-campaign-members": {
"id": "de821b2b-f7f8-4cc3-ad8f-c746c8698b0f",
Expand All @@ -155,14 +141,22 @@
"project_credential_id": "d3c0c70f-b20f-42f0-ad6b-8c70eeb03f65"
}
},
"deleted_at": null,
"lock_version": 146,
"triggers": {
"cron": {
"enabled": false,
"id": "cf67b9c5-e2e0-4ca1-8aca-8e7f32c79d98",
"type": "cron",
"cron_expression": "*/5 * * * *"
"edges": {
"cron->Get-deleted-campaign-members": {
"enabled": true,
"id": "52a622f3-ab69-4453-9a42-698cc4bf0ff5",
"source_trigger_id": "cf67b9c5-e2e0-4ca1-8aca-8e7f32c79d98",
"condition_type": "always",
"target_job_id": "de821b2b-f7f8-4cc3-ad8f-c746c8698b0f"
},
"Get-deleted-campaign-members->Remove-deleted-members-tags": {
"enabled": true,
"id": "d0104c99-4efe-4783-9314-f10421927c65",
"source_job_id": "de821b2b-f7f8-4cc3-ad8f-c746c8698b0f",
"condition_expression": "state.members.length > 0 && !state.errors\n",
"condition_type": "js_expression",
"condition_label": "has-members",
"target_job_id": "d839d64f-78f7-4178-ab9d-482bbedd8866"
}
}
}
Expand Down
66 changes: 33 additions & 33 deletions workflows/remove-members/remove-deleted.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
//Sync contacts and create only active campaign tags
//'/lists/25299978a7 Mailchimp Shreya's Test' 8eec4f86ed Will's Mailchimp Test,
//'/lists/a2ff510317', //will enviroment test 3
//'/lists/43fead6cd7',
each(
$.members,
post('/lists/8eec4f86ed', state => ({
sync_tags: true,
update_existing: true,
email_type: 'html',
members: state.data,
}))
.catch((error, state) => {
console.log(error);
return state;
})
.then(state => {
state.chunkErrors ??= [];
state.chunkErrors.push(state.response.errors);
return state;
})
);

// Alert admin if response has errors
fn(state => {
const { lastSyncTime, lastRunTime } = state;
// Check if response has errors
const chunkErrors = state.chunkErrors.flat().filter(Boolean);
if (chunkErrors.length > 0) {
throw new Error(JSON.stringify(chunkErrors, null, 2));
}
return { lastSyncTime, lastRunTime };
});
//Sync contacts and create only active campaign tags
//'/lists/25299978a7 Mailchimp Shreya's Test' 8eec4f86ed Will's Mailchimp Test,
//'/lists/a2ff510317', //will enviroment test 3
//'/lists/43fead6cd7',
each(
$.members,
post('/lists/8eec4f86ed', state => ({
sync_tags: true,
update_existing: true,
email_type: 'html',
members: state.data,
}))
.catch((error, state) => {
console.log(error);
return state;
})
.then(state => {
state.chunkErrors ??= [];
state.chunkErrors.push(state.response.errors);
return state;
})
);

// Alert admin if response has errors
fn(state => {
const { lastSyncTime, lastRunTime } = state;
// Check if response has errors
const chunkErrors = state.chunkErrors.flat().filter(Boolean);
if (chunkErrors.length > 0) {
throw new Error(JSON.stringify(chunkErrors, null, 2));
}
return { lastSyncTime, lastRunTime };
});
74 changes: 37 additions & 37 deletions workflows/upsert-members/upsert-tags.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
// Add or Update members to Mailchimp
each(
'$.members[*]',
post(
'/lists/43fead6cd7',
state => {
console.log(`Upserting ${state.data.length} members...`);
return {
sync_tags: false,
update_existing: true,
email_type: 'html',
members: state.data,
};
},
{},
state => {
if (state.response.total_created > 0)
console.log(`Added ${state.response.total_created} members`);
if (state.response.total_updated > 0)
console.log(`Updated ${state.response.total_updated} members`);

if (state.response.error_count > 0)
state.chunkErrors.push(state.response.errors);
return state;
}
)
);

// Alert admin if response has errors
fn(state => {
// Check if chunks response has errors
const chunkErrors = state.chunkErrors.flat();
if (chunkErrors.length > 0) {
throw new Error(JSON.stringify(chunkErrors, null, 2));
}
return state;
});
// Add or Update members to Mailchimp
each(
'$.members[*]',
post(
'/lists/43fead6cd7',
state => {
console.log(`Upserting ${state.data.length} members...`);
return {
sync_tags: false,
update_existing: true,
email_type: 'html',
members: state.data,
};
},
{},
state => {
if (state.response.total_created > 0)
console.log(`Added ${state.response.total_created} members`);
if (state.response.total_updated > 0)
console.log(`Updated ${state.response.total_updated} members`);

if (state.response.error_count > 0)
state.chunkErrors.push(state.response.errors);
return state;
}
)
);

// Alert admin if response has errors
fn(state => {
// Check if chunks response has errors
const chunkErrors = state.chunkErrors.flat();
if (chunkErrors.length > 0) {
throw new Error(JSON.stringify(chunkErrors, null, 2));
}
return state;
});

0 comments on commit e158698

Please sign in to comment.