Skip to content

Commit

Permalink
user [email protected] initiated a sync from Lightning
Browse files Browse the repository at this point in the history
  • Loading branch information
openfn[bot] authored and github-actions[bot] committed Jan 21, 2025
1 parent b47abd7 commit 6e8065d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions openfn-f07240d8-8bb7-4cff-9e20-cc8d8059892f-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ workflows:
jobs:
Get-deleted-campaign-members:
name: Get deleted campaign members
adaptor: '@openfn/language-salesforce@5.0.3'
adaptor: '@openfn/language-salesforce@4.8.6'
credential: mribeirocruz@verasolutions.org-Sandbox---OpenFn-Mailchimp-integration-2
body: |
// // To test with manual cursor simply create a new input with lastRunTime as the manual cursor
Expand Down Expand Up @@ -122,7 +122,7 @@ workflows:
})
Remove-deleted-members-tags:
name: Remove deleted members tags
adaptor: '@openfn/language-mailchimp@latest'
adaptor: '@openfn/language-mailchimp@1.0.11'
credential: [email protected]
body: |
//Sync contacts and create only active campaign tags
Expand Down Expand Up @@ -203,7 +203,7 @@ workflows:
jobs:
Get-members-for-upsert:
name: Get members for upsert
adaptor: '@openfn/language-salesforce@latest'
adaptor: '@openfn/language-salesforce@4.8.6'
credential: [email protected]
body: |
// Setup lastSyncTime
Expand Down Expand Up @@ -316,7 +316,7 @@ workflows:
Add-or-update-member-tags:
name: Add or update member tags
adaptor: '@openfn/language-mailchimp@latest'
adaptor: '@openfn/language-mailchimp@1.0.11'
credential: [email protected]
body: |
// Add or Update members to Mailchimp
Expand Down
22 changes: 11 additions & 11 deletions openfn-f07240d8-8bb7-4cff-9e20-cc8d8059892f-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"inserted_at": "2024-03-19T13:09:53Z",
"updated_at": "2024-11-06T19:14: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 @@ -62,15 +65,12 @@
"owner": "[email protected]"
}
},
"history_retention_period": 180,
"dataclip_retention_period": 180,
"retention_policy": "retain_all",
"workflows": {
"Upsert-Members": {
"id": "c1873753-c335-40ad-8fcc-bc3a347ca0e0",
"name": "Upsert Members",
"inserted_at": "2024-12-18T14:41:21.248990Z",
"lock_version": 25,
"inserted_at": "2025-01-21T10:00:05.324224Z",
"lock_version": 31,
"triggers": {
"cron": {
"enabled": true,
Expand All @@ -84,14 +84,14 @@
"id": "767da658-51fe-49e6-a3a8-a90695c51dc7",
"name": "Get members for upsert",
"body": "// Setup lastSyncTime\nfn(state => {\n const manualCursor = '2024-08-10T15:30:00.000Z';\n console.log(manualCursor, 'manualCursor');\n\n const lastSyncTime = state.lastRunTime || manualCursor;\n const lastRunTime = new Date().toISOString();\n console.log('time at job start:' + lastRunTime);\n\n return { ...state, lastSyncTime, lastRunTime };\n});\n\nbulkQuery(\n state => `\nSELECT Id, Name, FirstName, LastName, Email, CreatedDate,\n Contact.AccountId, Contact.LastModifiedDate, Contact.CreatedDate,\n Campaign.Name, Campaign.Nome_da_Tag__c\nFROM CampaignMember\nWHERE Campaign.RecordType.Name = 'Grupos, RTs ou Áreas Temáticas'\n AND Campaign.IsActive = true\n AND (Contact.LastModifiedDate > ${state.lastSyncTime} OR CreatedDate > ${state.lastSyncTime})\n`\n);\n\n//Seperate members for each batch\nfn(state => {\n const campaignMembers = state.data;\n const membersToCreate = [];\n const membersToUpdate = [];\n\n console.log(campaignMembers.length, 'campaignMembers');\n\n for (const member of campaignMembers) {\n const mappedMember = {\n email_address: member.Email,\n full_name: member.Name,\n merge_fields: {\n FNAME: member.FirstName,\n LNAME: member.LastName,\n MMERGE4: member[\"Contact.AccountId\"],\n },\n tags: [member[\"Campaign.Nome_da_tag__c\"]],\n };\n if ((member[\"Contact.LastModifiedDate\"] > state.lastSyncTime) || (member.CreatedDate > state.lastSyncTime)) {\n membersToCreate.push({ ...mappedMember, status: 'subscribed' });\n } else {\n membersToUpdate.push(mappedMember);\n }\n }\n\n console.log(membersToCreate.length, 'membersToCreate before merge tags');\n console.log(membersToUpdate.length, 'membersToUpdate before merge tags');\n\n let mergeCreateMemberTags = [];\n let mergeUpdateMemberTags = [];\n\n if (membersToCreate.length > 0) {\n mergeCreateMemberTags = membersToCreate.reduce((result, item) => {\n const existingItem = result.find(\n existing => existing.email_address === item.email_address\n );\n\n if (existingItem) {\n existingItem.tags = [...new Set([...existingItem.tags, ...item.tags])];\n } else {\n result.push(item);\n }\n\n return result;\n }, []);\n }\n\n if (membersToUpdate.length > 0) {\n mergeUpdateMemberTags = membersToUpdate.reduce((result, item) => {\n const existingItem = result.find(\n existing => existing.email_address === item.email_address\n );\n\n if (existingItem) {\n existingItem.tags = [...new Set([...existingItem.tags, ...item.tags])];\n } else {\n result.push(item);\n }\n return result;\n }, []);\n }\n\n console.log(\n mergeCreateMemberTags.length,\n 'mergeCreateMemberTags after merge tags'\n );\n\n console.log(\n mergeUpdateMemberTags.length,\n 'mergeUpdateMemberTags after merge tags'\n );\n\n return {\n ...state,\n references: [],\n members: [\n ...chunk(mergeCreateMemberTags, 500),\n ...chunk(mergeUpdateMemberTags, 500),\n ],\n chunkErrors: [],\n };\n});\n",
"adaptor": "@openfn/language-salesforce@latest",
"adaptor": "@openfn/language-salesforce@4.8.6",
"project_credential_id": "447cb045-646a-4b01-a7d4-182bc566363a"
},
"Add-or-update-member-tags": {
"id": "b302b4b7-aa10-499c-b91a-cc9f5cf3a50b",
"name": "Add or update member tags",
"body": "// Add or Update members to Mailchimp\neach(\n '$.members[*]',\n post(\n '/lists/43fead6cd7',\n \n state => {\n console.log(`Upserting ${state.data.length} members...`);\n return {\n sync_tags: false,\n update_existing: true,\n email_type: 'html',\n members: state.data,\n };\n },\n {},\n state => {\n\n if (state.response.total_created > 0)\n console.log(`Added ${state.response.total_created} members`);\n if (state.response.total_updated > 0)\n console.log(`Updated ${state.response.total_updated} members`);\n\n if (state.response.error_count > 0)\n state.chunkErrors.push(state.response.errors);\n return state;\n }\n )\n);\n\n// Alert admin if response has errors\nfn(state => {\n // Check if chunks response has errors\n const chunkErrors = state.chunkErrors.flat();\n if (chunkErrors.length > 0) {\n throw new Error(JSON.stringify(chunkErrors, null, 2));\n }\n return state;\n});\n",
"adaptor": "@openfn/language-mailchimp@latest",
"adaptor": "@openfn/language-mailchimp@1.0.11",
"project_credential_id": "b2f164e7-3e80-4aaf-8184-b4eb49b740e4"
}
},
Expand All @@ -115,8 +115,8 @@
"Remove-Deleted-Campaign-Members/Tags": {
"id": "273e2c2a-a97d-49eb-bebb-2dad0666a716",
"name": "Remove Deleted Campaign Members/Tags",
"inserted_at": "2025-01-16T13:47:39.167855Z",
"lock_version": 143,
"inserted_at": "2025-01-21T10:00:12.323872Z",
"lock_version": 145,
"triggers": {
"cron": {
"enabled": false,
Expand All @@ -130,14 +130,14 @@
"id": "de821b2b-f7f8-4cc3-ad8f-c746c8698b0f",
"name": "Get deleted campaign members",
"body": "// // To test with manual cursor simply create a new input with lastRunTime as the manual cursor\n// cursor($.lastRunTime || \"2023-08-16T15:30:00.000Z\", { key: \"lastSyncTime\" });\n// cursor(\"now\", { key: \"lastRunTime\", format: (c) => new Date(c).toISOString() });\nfn(state => {\n const manualCursor = '2023-08-16T15:30:00.000Z'; // SF timestamp\n console.log(manualCursor, 'manualCursor');\n state.lastSyncTime = state.lastRunTime || manualCursor;\n state.lastRunTime = new Date().toISOString();\n return state\n});\n\n//Get Deleted Campaign Member Records in Salesforce since last run\nbulkQuery(\n `SELECT Contact__r.Id, Contact__r.Name, Email__c, Nome_da_Tag__c FROM Deleted_Campaign_Member__c WHERE CreatedDate > ${$.lastSyncTime}`\n).then((state) => {\n state.contactIdsList = state.data\n .map((contact) => contact[\"Contact__r.Id\"])\n .filter((id) => id)\n .map((id) => `'${id}'`)\n .join(\", \");\n return state;\n});\n\nfnIf(!$.contactIdsList, (state) => {\n console.log(\"No contact IDs found. Skipping second bulk query.\");\n return state;\n});\n\n//Get corresponding Campaign Member details for Deleted Campaign Members\nfnIf(\n $.contactIdsList,\n bulkQuery(\n `SELECT Contact.Id, Contact.Name, Contact.Email, Campaign_Tag_Name__c FROM CampaignMember WHERE Campaign.RecordType.Name = 'Grupos, RTs ou Áreas Temáticas' AND Campaign.IsActive = true AND Contact.Id IN (${$.contactIdsList})`\n ).then(state => {\n state.dcMembers = state.data\n return state;\n })\n);\n\nfn((state) => {\n const dcMembers = state;\n \n console.log(dcMembers?.length, \"Deleted Campaign Members\");\n if (!dcMembers?.length > 0) {\n console.log(\n \"No campaing members found for contacts ids: \",\n state.contactIdsList\n );\n return state;\n }\n\n let mergeMemberTags = [];\n for (const member of dcMembers) {\n const email = member[\"Contact.Email\"];\n const campaignName = member.Campaign_Tag_Name__c;\n // Find the existing mapped member for this email\n const existingMember = mergeMemberTags.find(\n (item) => item.email_address === email\n );\n if (existingMember) {\n // If the email already exists, add the campaign name to its tags array\n existingMember.tags.push(campaignName);\n } else {\n // If the email doesn't exist, create a new mapped member\n const newMember = {\n email_address: email,\n tags: [campaignName].map((str) => str.replace(/\\n/g, \"\")),\n email_type: \"html\",\n };\n mergeMemberTags.push(newMember);\n }\n }\n console.log(mergeMemberTags.length, \"Retrieved mapped members\");\n\n const members = [...chunk(mergeMemberTags, 500)];\n return { members };\n})",
"adaptor": "@openfn/language-salesforce@5.0.3",
"adaptor": "@openfn/language-salesforce@4.8.6",
"project_credential_id": "becd5822-0191-4800-8a19-c7e24ff3589c"
},
"Remove-deleted-members-tags": {
"id": "d839d64f-78f7-4178-ab9d-482bbedd8866",
"name": "Remove deleted members tags",
"body": "//Sync contacts and create only active campaign tags\neach(\n $.members,\n // post(\n // //'/lists/43fead6cd7',\n // '/lists/8eec4f86ed',\n // {\n // body: {\n // sync_tags: true,\n // update_existing: true,\n // email_type: 'html',\n // members: $.data,\n // },\n // headers: { 'content-type': 'application/json' },\n // },\n // state => {\n // state.chunkErrors.push(state.response.errors);\n // return state;\n // }\n // )\n post(\n // '/lists/25299978a7 Mailchimp Shreya's Test' 8eec4f86ed Will's Mailchimp Test,\n \"/lists/8eec4f86ed\",\n // '/lists/a2ff510317', //will enviroment test 3\n {\n sync_tags: true,\n update_existing: true,\n members: $.data,\n },\n {},\n (state) => {\n //console.log(JSON.stringify(state.response.errors), 'Empty');\n state.chunkErrors = state.chunkErrors || [];\n state.chunkErrors.push(state.response.errors);\n return state;\n }\n ).catch((error, state) => {\n console.log(error);\n return state;\n })\n);\n\n// Alert admin if response has errors\nfn((state) => {\n const { lastSyncTime, lastRunTime } = state\n //state.chunkErrors = state.chunkErrors || [];\n // Check if response has errors\n const chunkErrors = state.chunkErrors.flat().filter(Boolean);\n if (chunkErrors.length > 0) {\n throw new Error(JSON.stringify(chunkErrors, null, 2));\n }\n return { lastSyncTime, lastRunTime };\n});\n",
"adaptor": "@openfn/language-mailchimp@latest",
"adaptor": "@openfn/language-mailchimp@1.0.11",
"project_credential_id": "d3c0c70f-b20f-42f0-ad6b-8c70eeb03f65"
}
},
Expand Down

0 comments on commit 6e8065d

Please sign in to comment.