|
| 1 | +import pipedriveApp from "../../pipedrive.app.mjs"; |
| 2 | +import { decode } from "html-entities"; |
| 3 | + |
| 4 | +export default { |
| 5 | + key: "pipedrive-remove-duplicate-notes", |
| 6 | + name: "Remove Duplicate Notes", |
| 7 | + description: "Remove duplicate notes from an object in Pipedrive. See the documentation for [getting notes](https://developers.pipedrive.com/docs/api/v1/Notes#getNotes) and [deleting notes](https://developers.pipedrive.com/docs/api/v1/Notes#deleteNote)", |
| 8 | + version: "0.0.1", |
| 9 | + type: "action", |
| 10 | + props: { |
| 11 | + pipedriveApp, |
| 12 | + leadId: { |
| 13 | + propDefinition: [ |
| 14 | + pipedriveApp, |
| 15 | + "leadId", |
| 16 | + ], |
| 17 | + description: "The ID of the lead that the notes are attached to", |
| 18 | + }, |
| 19 | + dealId: { |
| 20 | + propDefinition: [ |
| 21 | + pipedriveApp, |
| 22 | + "dealId", |
| 23 | + ], |
| 24 | + description: "The ID of the deal that the notes are attached to", |
| 25 | + }, |
| 26 | + personId: { |
| 27 | + propDefinition: [ |
| 28 | + pipedriveApp, |
| 29 | + "personId", |
| 30 | + ], |
| 31 | + description: "The ID of the person that the notes are attached to", |
| 32 | + }, |
| 33 | + organizationId: { |
| 34 | + propDefinition: [ |
| 35 | + pipedriveApp, |
| 36 | + "organizationId", |
| 37 | + ], |
| 38 | + description: "The ID of the organization that the notes are attached to", |
| 39 | + }, |
| 40 | + userId: { |
| 41 | + propDefinition: [ |
| 42 | + pipedriveApp, |
| 43 | + "userId", |
| 44 | + ], |
| 45 | + description: "The ID of the user that the notes are attached to", |
| 46 | + }, |
| 47 | + projectId: { |
| 48 | + propDefinition: [ |
| 49 | + pipedriveApp, |
| 50 | + "projectId", |
| 51 | + ], |
| 52 | + description: "The ID of the project that the notes are attached to", |
| 53 | + }, |
| 54 | + keyword: { |
| 55 | + type: "string", |
| 56 | + label: "Keyword", |
| 57 | + description: "Only remove duplicate notes that contain the specified keyword(s)", |
| 58 | + optional: true, |
| 59 | + }, |
| 60 | + }, |
| 61 | + methods: { |
| 62 | + getDuplicateNotes(notes) { |
| 63 | + const seenContent = new Map(); |
| 64 | + const uniqueNotes = []; |
| 65 | + const duplicates = []; |
| 66 | + |
| 67 | + // Sort notes by add_time (ascending) to keep the oldest duplicate |
| 68 | + const sortedNotes = notes.sort((a, b) => { |
| 69 | + const dateA = new Date(a.add_time); |
| 70 | + const dateB = new Date(b.add_time); |
| 71 | + return dateA - dateB; |
| 72 | + }); |
| 73 | + |
| 74 | + for (const note of sortedNotes) { |
| 75 | + // Normalize content by removing extra whitespace and converting to lowercase |
| 76 | + const decodedContent = decode(note.content || ""); |
| 77 | + const normalizedContent = decodedContent?.replace(/^\s*<br\s*\/?>|<br\s*\/?>\s*$/gi, "").trim() |
| 78 | + .toLowerCase(); |
| 79 | + |
| 80 | + if (!normalizedContent) { |
| 81 | + // Skip notes with empty content |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + if (seenContent.has(normalizedContent)) { |
| 86 | + // This is a duplicate |
| 87 | + duplicates.push({ |
| 88 | + duplicate: note, |
| 89 | + original: seenContent.get(normalizedContent), |
| 90 | + }); |
| 91 | + } else { |
| 92 | + // This is the first occurrence |
| 93 | + seenContent.set(normalizedContent, note); |
| 94 | + uniqueNotes.push(note); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return { |
| 99 | + uniqueNotes, |
| 100 | + duplicates, |
| 101 | + duplicateCount: duplicates.length, |
| 102 | + }; |
| 103 | + }, |
| 104 | + }, |
| 105 | + async run({ $ }) { |
| 106 | + let notes = await this.pipedriveApp.getPaginatedResources({ |
| 107 | + fn: this.pipedriveApp.getNotes, |
| 108 | + params: { |
| 109 | + user_id: this.userId, |
| 110 | + lead_id: this.leadId, |
| 111 | + deal_id: this.dealId, |
| 112 | + person_id: this.personId, |
| 113 | + org_id: this.organizationId, |
| 114 | + project_id: this.projectId, |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + if (this.keyword) { |
| 119 | + notes = notes.filter((note) => |
| 120 | + note.content?.toLowerCase().includes(this.keyword.toLowerCase())); |
| 121 | + } |
| 122 | + |
| 123 | + let result = { |
| 124 | + notes, |
| 125 | + totalNotes: notes.length, |
| 126 | + }; |
| 127 | + |
| 128 | + const { |
| 129 | + uniqueNotes, duplicates, duplicateCount, |
| 130 | + } = this.getDuplicateNotes(notes); |
| 131 | + |
| 132 | + for (const note of duplicates) { |
| 133 | + await this.pipedriveApp.deleteNote(note.duplicate.id); |
| 134 | + } |
| 135 | + |
| 136 | + result = { |
| 137 | + notes: uniqueNotes, |
| 138 | + totalNotes: uniqueNotes.length, |
| 139 | + duplicatesFound: duplicateCount, |
| 140 | + duplicates: duplicates, |
| 141 | + originalCount: notes.length, |
| 142 | + }; |
| 143 | + |
| 144 | + $.export("$summary", `Found ${notes.length} total note(s), removed ${duplicateCount} duplicate(s), returning ${uniqueNotes.length} unique note(s)`); |
| 145 | + |
| 146 | + return result; |
| 147 | + }, |
| 148 | +}; |
0 commit comments