@ever-works/pipedrive-connector-plugin 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -0
- package/dist/index.cjs +488 -0
- package/dist/index.d.cts +168 -0
- package/dist/index.d.ts +168 -0
- package/dist/index.js +471 -0
- package/package.json +111 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
import { Configuration, UsersApi, RecentsApi, NotesApi } from 'pipedrive/v1';
|
|
3
|
+
import { Configuration as Configuration$1, DealsApi, PersonsApi, OrganizationsApi } from 'pipedrive/v2';
|
|
4
|
+
import '@ever-works/contracts';
|
|
5
|
+
|
|
6
|
+
// src/pipedrive-connector-plugin.ts
|
|
7
|
+
var EventSourceNotConfiguredError = class extends Error {
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(message ?? "Event-source plugin is not configured in this runtime.");
|
|
10
|
+
this.name = "EventSourceNotConfiguredError";
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
var PLUGIN_CAPABILITIES = {
|
|
14
|
+
// Connectors ("Connector fabric") — first-party BIDIRECTIONAL
|
|
15
|
+
// communication-channel plugins. Each connector declares `CONNECTOR`
|
|
16
|
+
// (umbrella, for discovery/grouping) plus its `CONNECTOR_<provider>`
|
|
17
|
+
// constant. Superset of the outbound-only notification channels; see
|
|
18
|
+
// `capabilities/connector.interface.ts`.
|
|
19
|
+
CONNECTOR: "connector",
|
|
20
|
+
CONNECTOR_PIPEDRIVE: "connector-pipedrive",
|
|
21
|
+
// Event-ingest spine (Wave 6) — plugins that pull/push normalized
|
|
22
|
+
// external events into the platform ingest pipeline. See
|
|
23
|
+
// capabilities/event-source.interface.ts.
|
|
24
|
+
EVENT_SOURCE: "event-source"};
|
|
25
|
+
|
|
26
|
+
// src/pipedrive-connector-plugin.ts
|
|
27
|
+
var PIPEDRIVE_EVENT_TEXT_MAX_CHARS = 4e3;
|
|
28
|
+
var PIPEDRIVE_PULL_PAGE_SIZE = 50;
|
|
29
|
+
var PIPEDRIVE_BACKFILL_MAX_PAGES = 10;
|
|
30
|
+
var PIPEDRIVE_ENTITY_TYPES = ["deals", "persons", "organizations"];
|
|
31
|
+
var PIPEDRIVE_ENTITY_META = {
|
|
32
|
+
deals: {
|
|
33
|
+
kind: "pipedrive.deal",
|
|
34
|
+
recentsItem: "deal",
|
|
35
|
+
urlSegment: "deal",
|
|
36
|
+
titleField: "title",
|
|
37
|
+
noteField: "deal_id",
|
|
38
|
+
payloadFields: ["title", "status", "stage_id", "pipeline_id", "value", "currency", "add_time", "update_time"]
|
|
39
|
+
},
|
|
40
|
+
persons: {
|
|
41
|
+
kind: "pipedrive.person",
|
|
42
|
+
recentsItem: "person",
|
|
43
|
+
urlSegment: "person",
|
|
44
|
+
titleField: "name",
|
|
45
|
+
noteField: "person_id",
|
|
46
|
+
payloadFields: ["name", "org_id", "owner_id", "add_time", "update_time"]
|
|
47
|
+
},
|
|
48
|
+
organizations: {
|
|
49
|
+
kind: "pipedrive.organization",
|
|
50
|
+
recentsItem: "organization",
|
|
51
|
+
urlSegment: "organization",
|
|
52
|
+
titleField: "name",
|
|
53
|
+
noteField: "org_id",
|
|
54
|
+
payloadFields: ["name", "address", "owner_id", "people_count", "add_time", "update_time"]
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
function clampBackfillDays(value) {
|
|
58
|
+
const num = typeof value === "number" ? value : Number(value);
|
|
59
|
+
if (!Number.isFinite(num) || num <= 0) return 0;
|
|
60
|
+
return Math.min(Math.floor(num), 90);
|
|
61
|
+
}
|
|
62
|
+
function truncateText(text) {
|
|
63
|
+
return text.length > PIPEDRIVE_EVENT_TEXT_MAX_CHARS ? text.slice(0, PIPEDRIVE_EVENT_TEXT_MAX_CHARS) : text;
|
|
64
|
+
}
|
|
65
|
+
function resolveEntityTypes(settings) {
|
|
66
|
+
const raw = settings?.entityTypes;
|
|
67
|
+
if (typeof raw !== "string" || raw.trim().length === 0) return [...PIPEDRIVE_ENTITY_TYPES];
|
|
68
|
+
const requested = raw.split(/[\s,]+/).map((t) => t.trim().toLowerCase()).filter((t) => PIPEDRIVE_ENTITY_TYPES.includes(t));
|
|
69
|
+
return requested.length > 0 ? [...PIPEDRIVE_ENTITY_TYPES].filter((t) => requested.includes(t)) : [...PIPEDRIVE_ENTITY_TYPES];
|
|
70
|
+
}
|
|
71
|
+
function pipedriveTimeToIso(value) {
|
|
72
|
+
if (value instanceof Date) return value.toISOString();
|
|
73
|
+
if (typeof value === "string" && value.trim().length > 0) {
|
|
74
|
+
const raw = value.trim();
|
|
75
|
+
const normalized = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(raw) ? `${raw.replace(" ", "T")}Z` : raw;
|
|
76
|
+
const ms = Date.parse(normalized);
|
|
77
|
+
if (Number.isFinite(ms)) return new Date(ms).toISOString();
|
|
78
|
+
}
|
|
79
|
+
return (/* @__PURE__ */ new Date(0)).toISOString();
|
|
80
|
+
}
|
|
81
|
+
function isoToPipedriveTime(iso) {
|
|
82
|
+
const ms = Date.parse(iso);
|
|
83
|
+
const date = new Date(Number.isFinite(ms) ? ms : 0);
|
|
84
|
+
return date.toISOString().replace("T", " ").slice(0, 19);
|
|
85
|
+
}
|
|
86
|
+
function pipedriveErrorMessage(err) {
|
|
87
|
+
const e = err;
|
|
88
|
+
return e.response?.data?.error ?? e.message ?? (typeof e.response?.status === "number" ? `HTTP ${e.response.status}` : "unknown error");
|
|
89
|
+
}
|
|
90
|
+
function parsePullCursor(cursor) {
|
|
91
|
+
if (!cursor) return void 0;
|
|
92
|
+
try {
|
|
93
|
+
const parsed = JSON.parse(cursor);
|
|
94
|
+
if (parsed && typeof parsed.s === "string" && PIPEDRIVE_ENTITY_TYPES.includes(parsed.t)) {
|
|
95
|
+
return parsed;
|
|
96
|
+
}
|
|
97
|
+
} catch {
|
|
98
|
+
}
|
|
99
|
+
return void 0;
|
|
100
|
+
}
|
|
101
|
+
function resolveApiToken(config, options) {
|
|
102
|
+
const candidates = [config.apiToken, options.settings?.apiToken];
|
|
103
|
+
for (const c of candidates) {
|
|
104
|
+
if (typeof c === "string" && c.length > 0) return c;
|
|
105
|
+
}
|
|
106
|
+
return void 0;
|
|
107
|
+
}
|
|
108
|
+
function firstString(candidates) {
|
|
109
|
+
for (const c of candidates) {
|
|
110
|
+
if (typeof c === "string" && c.trim().length > 0) return c.trim();
|
|
111
|
+
if (typeof c === "number" && Number.isFinite(c)) return String(c);
|
|
112
|
+
}
|
|
113
|
+
return void 0;
|
|
114
|
+
}
|
|
115
|
+
function asEntityType(value) {
|
|
116
|
+
if (typeof value !== "string") return void 0;
|
|
117
|
+
const candidate = value.trim().toLowerCase();
|
|
118
|
+
return PIPEDRIVE_ENTITY_TYPES.includes(candidate) ? candidate : void 0;
|
|
119
|
+
}
|
|
120
|
+
function resolveNoteTarget(config, options) {
|
|
121
|
+
const explicitId = firstString([config.recordId, config.dealId]);
|
|
122
|
+
if (explicitId) {
|
|
123
|
+
const entityType = asEntityType(config.recordType) ?? "deals";
|
|
124
|
+
return { entityType, recordId: explicitId };
|
|
125
|
+
}
|
|
126
|
+
const defaultDealId = firstString([config.defaultDealId, options.settings?.defaultDealId]);
|
|
127
|
+
if (defaultDealId) {
|
|
128
|
+
return { entityType: "deals", recordId: defaultDealId };
|
|
129
|
+
}
|
|
130
|
+
throw new Error(
|
|
131
|
+
"pipedrive-connector: a record id is required for an outbound note (targetConfig.recordId or settings.defaultDealId)"
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
var PipedriveConnectorPlugin = class {
|
|
135
|
+
constructor() {
|
|
136
|
+
this.id = "pipedrive-connector";
|
|
137
|
+
this.name = "Pipedrive Connector";
|
|
138
|
+
this.version = "1.0.0";
|
|
139
|
+
this.category = "connector";
|
|
140
|
+
this.capabilities = [
|
|
141
|
+
PLUGIN_CAPABILITIES.CONNECTOR,
|
|
142
|
+
PLUGIN_CAPABILITIES.CONNECTOR_PIPEDRIVE,
|
|
143
|
+
PLUGIN_CAPABILITIES.EVENT_SOURCE
|
|
144
|
+
];
|
|
145
|
+
this.providerName = "pipedrive";
|
|
146
|
+
this.connector = {
|
|
147
|
+
direction: "outbound",
|
|
148
|
+
transport: "poll",
|
|
149
|
+
flags: {
|
|
150
|
+
outboundMessage: true,
|
|
151
|
+
outboundRecord: true,
|
|
152
|
+
inbound: false,
|
|
153
|
+
reply: false,
|
|
154
|
+
pairing: false,
|
|
155
|
+
richOutbound: false
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
this.settingsSchema = {
|
|
159
|
+
type: "object",
|
|
160
|
+
required: ["apiToken"],
|
|
161
|
+
properties: {
|
|
162
|
+
apiToken: {
|
|
163
|
+
type: "string",
|
|
164
|
+
title: "Pipedrive API token",
|
|
165
|
+
"x-secret": true,
|
|
166
|
+
"x-envVar": "PIPEDRIVE_API_TOKEN"
|
|
167
|
+
},
|
|
168
|
+
entityTypes: {
|
|
169
|
+
type: "string",
|
|
170
|
+
title: "Entity types to ingest (comma-separated; deals, persons, organizations when empty)",
|
|
171
|
+
default: "deals,persons,organizations"
|
|
172
|
+
},
|
|
173
|
+
companyDomain: {
|
|
174
|
+
type: "string",
|
|
175
|
+
title: "Pipedrive company domain (e.g. `acme` for acme.pipedrive.com) \u2014 enables deep links"
|
|
176
|
+
},
|
|
177
|
+
backfillDays: {
|
|
178
|
+
type: "number",
|
|
179
|
+
title: "Historical backfill window in days on first pull (0 = off, max 90)",
|
|
180
|
+
default: 0,
|
|
181
|
+
minimum: 0,
|
|
182
|
+
maximum: 90
|
|
183
|
+
},
|
|
184
|
+
defaultDealId: {
|
|
185
|
+
type: "string",
|
|
186
|
+
title: "Default deal id outbound notes are attached to"
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
this.idempotencyCache = /* @__PURE__ */ new Map();
|
|
191
|
+
this.recordIdempotencyCache = /* @__PURE__ */ new Map();
|
|
192
|
+
}
|
|
193
|
+
async onLoad() {
|
|
194
|
+
}
|
|
195
|
+
async onUnload() {
|
|
196
|
+
this.idempotencyCache.clear();
|
|
197
|
+
this.recordIdempotencyCache.clear();
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Testable seam — specs stub this; production adapts the official
|
|
201
|
+
* SDK's per-resource API classes onto the narrow surface above.
|
|
202
|
+
*/
|
|
203
|
+
createClient(apiToken) {
|
|
204
|
+
const v1 = new Configuration({ apiKey: apiToken });
|
|
205
|
+
const v2 = new Configuration$1({ apiKey: apiToken });
|
|
206
|
+
const users = new UsersApi(v1);
|
|
207
|
+
const recents = new RecentsApi(v1);
|
|
208
|
+
const notes = new NotesApi(v1);
|
|
209
|
+
const deals = new DealsApi(v2);
|
|
210
|
+
const persons = new PersonsApi(v2);
|
|
211
|
+
const organizations = new OrganizationsApi(v2);
|
|
212
|
+
return {
|
|
213
|
+
getCurrentUser: () => users.getCurrentUser(),
|
|
214
|
+
getRecents: (params) => recents.getRecents(
|
|
215
|
+
params
|
|
216
|
+
),
|
|
217
|
+
// The generated SDK wraps every request body in a property
|
|
218
|
+
// named after its schema — `addNote({ AddNoteRequest: {…} })`.
|
|
219
|
+
addNote: (payload) => notes.addNote({
|
|
220
|
+
AddNoteRequest: payload
|
|
221
|
+
}),
|
|
222
|
+
addRecord: (entityType, fields) => {
|
|
223
|
+
if (entityType === "persons") {
|
|
224
|
+
return persons.addPerson({
|
|
225
|
+
AddPersonRequest: fields
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
if (entityType === "organizations") {
|
|
229
|
+
return organizations.addOrganization({
|
|
230
|
+
AddOrganizationRequest: fields
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
return deals.addDeal({
|
|
234
|
+
AddDealRequest: fields
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
async verifyConnection(config, options) {
|
|
240
|
+
const apiToken = resolveApiToken(config, options);
|
|
241
|
+
if (!apiToken) {
|
|
242
|
+
return { valid: false, message: "apiToken is required" };
|
|
243
|
+
}
|
|
244
|
+
try {
|
|
245
|
+
const me = await this.createClient(apiToken).getCurrentUser();
|
|
246
|
+
const user = me?.data ?? void 0;
|
|
247
|
+
return {
|
|
248
|
+
valid: true,
|
|
249
|
+
details: {
|
|
250
|
+
...user?.id !== void 0 ? { userId: String(user.id) } : {},
|
|
251
|
+
...user?.name ? { userName: user.name } : {},
|
|
252
|
+
...user?.company_name ? { companyName: user.company_name } : {},
|
|
253
|
+
...user?.company_domain ? { companyDomain: user.company_domain } : {}
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
} catch (err) {
|
|
257
|
+
return { valid: false, message: `Pipedrive getCurrentUser failed: ${pipedriveErrorMessage(err)}` };
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/** Outbound leg — append a note to a deal / person / organization. */
|
|
261
|
+
async send(payload, options) {
|
|
262
|
+
const config = payload.target ?? options.target ?? {};
|
|
263
|
+
const apiToken = resolveApiToken(config, options);
|
|
264
|
+
if (!apiToken) {
|
|
265
|
+
throw new Error(
|
|
266
|
+
"pipedrive-connector: an API token is required (targetConfig.apiToken or settings.apiToken)"
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
const { entityType, recordId } = resolveNoteTarget(config, options);
|
|
270
|
+
const noteField = PIPEDRIVE_ENTITY_META[entityType].noteField;
|
|
271
|
+
const cacheKey = `${options.connectorId ?? ""}\0${entityType}\0${recordId}\0${payload.messageRef}`;
|
|
272
|
+
const cached = this.idempotencyCache.get(cacheKey);
|
|
273
|
+
if (cached) return cached;
|
|
274
|
+
let noteId;
|
|
275
|
+
try {
|
|
276
|
+
const numericId = Number(recordId);
|
|
277
|
+
const res = await this.createClient(apiToken).addNote({
|
|
278
|
+
content: payload.text,
|
|
279
|
+
[noteField]: Number.isFinite(numericId) ? numericId : recordId
|
|
280
|
+
});
|
|
281
|
+
const id = res?.data?.id;
|
|
282
|
+
noteId = id === void 0 || id === null ? void 0 : String(id);
|
|
283
|
+
} catch (err) {
|
|
284
|
+
throw new Error(`Pipedrive addNote failed: ${pipedriveErrorMessage(err)}`);
|
|
285
|
+
}
|
|
286
|
+
const result = {
|
|
287
|
+
provider: this.id,
|
|
288
|
+
providerMessageId: noteId ?? `pipedrive-${payload.messageRef}`,
|
|
289
|
+
deliveredAt: /* @__PURE__ */ new Date()
|
|
290
|
+
};
|
|
291
|
+
this.idempotencyCache.set(cacheKey, result);
|
|
292
|
+
if (this.idempotencyCache.size > 500) {
|
|
293
|
+
const firstKey = this.idempotencyCache.keys().next().value;
|
|
294
|
+
if (firstKey) this.idempotencyCache.delete(firstKey);
|
|
295
|
+
}
|
|
296
|
+
return result;
|
|
297
|
+
}
|
|
298
|
+
/** Outbound record leg — create a deal / person / organization. */
|
|
299
|
+
async createRecord(record, options) {
|
|
300
|
+
const config = options.target ?? {};
|
|
301
|
+
const apiToken = resolveApiToken(config, options);
|
|
302
|
+
if (!apiToken) {
|
|
303
|
+
throw new Error(
|
|
304
|
+
"pipedrive-connector: an API token is required (targetConfig.apiToken or settings.apiToken)"
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
const entityType = asEntityType(record.collection);
|
|
308
|
+
if (!entityType) {
|
|
309
|
+
throw new Error(
|
|
310
|
+
`pipedrive-connector: unsupported collection '${record.collection}' \u2014 use one of ${PIPEDRIVE_ENTITY_TYPES.join(", ")}`
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
const cacheKey = `${options.connectorId ?? ""}\0${entityType}\0${record.idempotencyKey}`;
|
|
314
|
+
const cached = this.recordIdempotencyCache.get(cacheKey);
|
|
315
|
+
if (cached) return cached;
|
|
316
|
+
let recordId;
|
|
317
|
+
try {
|
|
318
|
+
const res = await this.createClient(apiToken).addRecord(entityType, { ...record.fields });
|
|
319
|
+
const id = res?.data?.id;
|
|
320
|
+
recordId = id === void 0 || id === null ? void 0 : String(id);
|
|
321
|
+
} catch (err) {
|
|
322
|
+
throw new Error(`Pipedrive ${entityType} create failed: ${pipedriveErrorMessage(err)}`);
|
|
323
|
+
}
|
|
324
|
+
if (!recordId) {
|
|
325
|
+
throw new Error(`Pipedrive ${entityType} create returned no record id`);
|
|
326
|
+
}
|
|
327
|
+
const result = { provider: this.id, recordId };
|
|
328
|
+
this.recordIdempotencyCache.set(cacheKey, result);
|
|
329
|
+
if (this.recordIdempotencyCache.size > 500) {
|
|
330
|
+
const firstKey = this.recordIdempotencyCache.keys().next().value;
|
|
331
|
+
if (firstKey) this.recordIdempotencyCache.delete(firstKey);
|
|
332
|
+
}
|
|
333
|
+
return result;
|
|
334
|
+
}
|
|
335
|
+
// ── Event source (pull) ─────────────────────────────────────────────
|
|
336
|
+
/**
|
|
337
|
+
* Pull one `/recents` page of the current entity-type phase since the
|
|
338
|
+
* effective watermark, normalized to envelopes. The returned cursor
|
|
339
|
+
* resumes the same phase (`next_start` offset) or advances to the
|
|
340
|
+
* next configured entity type; no cursor means the sweep is done.
|
|
341
|
+
*
|
|
342
|
+
* First pull (epoch/absent watermark, no cursor): the window is
|
|
343
|
+
* `now - backfillDays` when the opt-in backfill is on, otherwise
|
|
344
|
+
* `now` — history stays untouched unless the user asked for it.
|
|
345
|
+
*/
|
|
346
|
+
async pullEvents(input) {
|
|
347
|
+
const apiToken = input.settings?.apiToken;
|
|
348
|
+
if (typeof apiToken !== "string" || apiToken.length === 0) {
|
|
349
|
+
throw new EventSourceNotConfiguredError(
|
|
350
|
+
"pipedrive-connector: settings.apiToken is required to pull events"
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
const entityTypes = resolveEntityTypes(input.settings);
|
|
354
|
+
const cursor = parsePullCursor(input.cursor);
|
|
355
|
+
let entityType;
|
|
356
|
+
let start;
|
|
357
|
+
let since;
|
|
358
|
+
let backfill;
|
|
359
|
+
let pagesUsed;
|
|
360
|
+
if (cursor) {
|
|
361
|
+
entityType = cursor.t;
|
|
362
|
+
start = cursor.n;
|
|
363
|
+
since = cursor.s;
|
|
364
|
+
backfill = cursor.f === 1;
|
|
365
|
+
pagesUsed = cursor.b ?? 0;
|
|
366
|
+
} else {
|
|
367
|
+
const sinceMs = Date.parse(input.since);
|
|
368
|
+
const firstPull = !Number.isFinite(sinceMs) || sinceMs <= 0;
|
|
369
|
+
const backfillDays = clampBackfillDays(input.settings?.backfillDays);
|
|
370
|
+
if (firstPull) {
|
|
371
|
+
const anchor = backfillDays > 0 ? Date.now() - backfillDays * 864e5 : Date.now();
|
|
372
|
+
since = new Date(anchor).toISOString();
|
|
373
|
+
backfill = backfillDays > 0;
|
|
374
|
+
} else {
|
|
375
|
+
since = new Date(sinceMs).toISOString();
|
|
376
|
+
backfill = false;
|
|
377
|
+
}
|
|
378
|
+
entityType = entityTypes[0];
|
|
379
|
+
start = void 0;
|
|
380
|
+
pagesUsed = 0;
|
|
381
|
+
}
|
|
382
|
+
let typeIndex = entityTypes.indexOf(entityType);
|
|
383
|
+
if (typeIndex === -1) {
|
|
384
|
+
typeIndex = 0;
|
|
385
|
+
entityType = entityTypes[0];
|
|
386
|
+
start = void 0;
|
|
387
|
+
pagesUsed = 0;
|
|
388
|
+
}
|
|
389
|
+
const meta = PIPEDRIVE_ENTITY_META[entityType];
|
|
390
|
+
const companyDomain = typeof input.settings?.companyDomain === "string" ? input.settings.companyDomain.trim() : void 0;
|
|
391
|
+
const client = this.createClient(apiToken);
|
|
392
|
+
const page = await client.getRecents({
|
|
393
|
+
since_timestamp: isoToPipedriveTime(since),
|
|
394
|
+
items: meta.recentsItem,
|
|
395
|
+
limit: PIPEDRIVE_PULL_PAGE_SIZE,
|
|
396
|
+
...typeof start === "number" ? { start } : {}
|
|
397
|
+
});
|
|
398
|
+
const events = [];
|
|
399
|
+
for (const item of page.data ?? []) {
|
|
400
|
+
const envelope = this.normalizeRecent(item, entityType, since, companyDomain);
|
|
401
|
+
if (envelope) events.push(envelope);
|
|
402
|
+
}
|
|
403
|
+
const pagination = page.additional_data?.pagination;
|
|
404
|
+
const nextStart = pagination?.more_items_in_collection === true && typeof pagination.next_start === "number" ? pagination.next_start : void 0;
|
|
405
|
+
pagesUsed += 1;
|
|
406
|
+
const pageBudgetExhausted = backfill && pagesUsed >= PIPEDRIVE_BACKFILL_MAX_PAGES;
|
|
407
|
+
let next;
|
|
408
|
+
if (nextStart !== void 0 && !pageBudgetExhausted) {
|
|
409
|
+
next = { t: entityType, n: nextStart, s: since, ...backfill ? { f: 1 } : {}, b: pagesUsed };
|
|
410
|
+
} else if (typeIndex + 1 < entityTypes.length) {
|
|
411
|
+
next = { t: entityTypes[typeIndex + 1], s: since, ...backfill ? { f: 1 } : {}, b: 0 };
|
|
412
|
+
}
|
|
413
|
+
return next ? { events, nextCursor: JSON.stringify(next) } : { events };
|
|
414
|
+
}
|
|
415
|
+
normalizeRecent(item, entityType, since, companyDomain) {
|
|
416
|
+
const meta = PIPEDRIVE_ENTITY_META[entityType];
|
|
417
|
+
const data = item.data ?? {};
|
|
418
|
+
const rawId = item.id ?? data.id;
|
|
419
|
+
if (rawId === void 0 || rawId === null || rawId === "") return null;
|
|
420
|
+
const recordId = String(rawId);
|
|
421
|
+
const updatedAt = pipedriveTimeToIso(data.update_time ?? data.add_time);
|
|
422
|
+
const createdAt = pipedriveTimeToIso(data.add_time);
|
|
423
|
+
const changeType = Date.parse(createdAt) >= Date.parse(since) ? "created" : "updated";
|
|
424
|
+
const rawTitle = data[meta.titleField];
|
|
425
|
+
const title = typeof rawTitle === "string" && rawTitle.trim().length > 0 ? rawTitle.trim() : void 0;
|
|
426
|
+
const sourceUrl = companyDomain ? `https://${encodeURIComponent(companyDomain)}.pipedrive.com/${meta.urlSegment}/${encodeURIComponent(recordId)}` : void 0;
|
|
427
|
+
return {
|
|
428
|
+
id: randomUUID(),
|
|
429
|
+
source: this.id,
|
|
430
|
+
sourceEventId: `${entityType}:${recordId}:${updatedAt}`,
|
|
431
|
+
kind: meta.kind,
|
|
432
|
+
occurredAt: updatedAt,
|
|
433
|
+
subject: {
|
|
434
|
+
type: "crm-record",
|
|
435
|
+
externalId: recordId,
|
|
436
|
+
...title ? { title } : {}
|
|
437
|
+
},
|
|
438
|
+
...sourceUrl ? { sourceUrl } : {},
|
|
439
|
+
payload: {
|
|
440
|
+
entityType,
|
|
441
|
+
recordId,
|
|
442
|
+
changeType,
|
|
443
|
+
createdAt,
|
|
444
|
+
updatedAt,
|
|
445
|
+
fields: this.sanitizeFields(data, meta)
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Payload fields are limited to the per-entity whitelist and each
|
|
451
|
+
* string value is capped, so a record with a huge free-text field can
|
|
452
|
+
* never blow the envelope byte budget (and custom fields, which can
|
|
453
|
+
* hold anything, never ride along).
|
|
454
|
+
*/
|
|
455
|
+
sanitizeFields(data, meta) {
|
|
456
|
+
const out = {};
|
|
457
|
+
for (const key of meta.payloadFields) {
|
|
458
|
+
const value = data[key];
|
|
459
|
+
if (value === void 0 || value === null) continue;
|
|
460
|
+
if (typeof value === "string") {
|
|
461
|
+
out[key] = truncateText(value);
|
|
462
|
+
} else if (typeof value === "number" || typeof value === "boolean") {
|
|
463
|
+
out[key] = value;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return out;
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
var pipedriveConnectorPlugin = new PipedriveConnectorPlugin();
|
|
470
|
+
|
|
471
|
+
export { PIPEDRIVE_BACKFILL_MAX_PAGES, PIPEDRIVE_ENTITY_META, PIPEDRIVE_ENTITY_TYPES, PIPEDRIVE_EVENT_TEXT_MAX_CHARS, PIPEDRIVE_PULL_PAGE_SIZE, PipedriveConnectorPlugin, asEntityType, clampBackfillDays, PipedriveConnectorPlugin as default, isoToPipedriveTime, pipedriveConnectorPlugin, pipedriveTimeToIso, resolveEntityTypes, resolveNoteTarget };
|
package/package.json
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ever-works/pipedrive-connector-plugin",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Pipedrive CRM connector plugin for Ever Works platform (outbound notes + record creation via the official pipedrive Node SDK, and event-source pull of deal/person/organization changes into the event-ingest pipeline, with opt-in historical backfill)",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Ever Co. LTD",
|
|
7
|
+
"email": "ever@ever.co",
|
|
8
|
+
"url": "https://ever.co"
|
|
9
|
+
},
|
|
10
|
+
"license": "AGPL-3.0",
|
|
11
|
+
"private": false,
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./dist/index.cjs",
|
|
14
|
+
"module": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"require": "./dist/index.cjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc --noEmit && tsup",
|
|
28
|
+
"dev": "tsup --watch",
|
|
29
|
+
"clean": "rm -rf dist",
|
|
30
|
+
"type-check": "tsc --noEmit",
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"test:watch": "vitest"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@ever-works/contracts": "^1.0.0",
|
|
36
|
+
"@ever-works/plugin": "^1.2.0",
|
|
37
|
+
"pipedrive": "^33.5.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^22.0.0",
|
|
41
|
+
"tsup": "^8.0.0",
|
|
42
|
+
"typescript": "^5.9.3",
|
|
43
|
+
"vitest": "^4.1.0"
|
|
44
|
+
},
|
|
45
|
+
"everworks": {
|
|
46
|
+
"plugin": {
|
|
47
|
+
"id": "pipedrive-connector",
|
|
48
|
+
"name": "Pipedrive Connector",
|
|
49
|
+
"version": "1.0.0",
|
|
50
|
+
"category": "connector",
|
|
51
|
+
"capabilities": [
|
|
52
|
+
"connector",
|
|
53
|
+
"connector-pipedrive",
|
|
54
|
+
"event-source"
|
|
55
|
+
],
|
|
56
|
+
"description": "Pipedrive CRM connector. Outbound appends notes to deals/persons/organizations and creates records via the official pipedrive Node SDK. Event-source pull ingests records created/updated since the last watermark into the platform event-ingest pipeline, with opt-in historical backfill (backfillDays, max 90).",
|
|
57
|
+
"systemPlugin": false,
|
|
58
|
+
"builtIn": true,
|
|
59
|
+
"isDefault": false,
|
|
60
|
+
"autoEnable": false,
|
|
61
|
+
"settings": {
|
|
62
|
+
"type": "object",
|
|
63
|
+
"required": [
|
|
64
|
+
"apiToken"
|
|
65
|
+
],
|
|
66
|
+
"properties": {
|
|
67
|
+
"apiToken": {
|
|
68
|
+
"type": "string",
|
|
69
|
+
"title": "Pipedrive API token",
|
|
70
|
+
"x-secret": true,
|
|
71
|
+
"x-envVar": "PIPEDRIVE_API_TOKEN"
|
|
72
|
+
},
|
|
73
|
+
"entityTypes": {
|
|
74
|
+
"type": "string",
|
|
75
|
+
"title": "Entity types to ingest (comma-separated; deals, persons, organizations when empty)",
|
|
76
|
+
"default": "deals,persons,organizations"
|
|
77
|
+
},
|
|
78
|
+
"companyDomain": {
|
|
79
|
+
"type": "string",
|
|
80
|
+
"title": "Pipedrive company domain (e.g. `acme` for acme.pipedrive.com) — enables deep links"
|
|
81
|
+
},
|
|
82
|
+
"backfillDays": {
|
|
83
|
+
"type": "number",
|
|
84
|
+
"title": "Historical backfill window in days on first pull (0 = off, max 90)",
|
|
85
|
+
"default": 0,
|
|
86
|
+
"minimum": 0,
|
|
87
|
+
"maximum": 90
|
|
88
|
+
},
|
|
89
|
+
"defaultDealId": {
|
|
90
|
+
"type": "string",
|
|
91
|
+
"title": "Default deal id outbound notes are attached to"
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
"repository": {
|
|
98
|
+
"type": "git",
|
|
99
|
+
"url": "git+https://github.com/ever-works/ever-works.git",
|
|
100
|
+
"directory": "packages/plugins/pipedrive-connector"
|
|
101
|
+
},
|
|
102
|
+
"homepage": "https://github.com/ever-works/ever-works/tree/main/packages/plugins/pipedrive-connector#readme",
|
|
103
|
+
"bugs": {
|
|
104
|
+
"url": "https://github.com/ever-works/ever-works/issues"
|
|
105
|
+
},
|
|
106
|
+
"everworksRelease": {
|
|
107
|
+
"contentHash": "sha256:6e5320ebba54b7ce82fc35c4b5bb7fa92dd4033cd503e588a910c239cb57ce5b",
|
|
108
|
+
"commit": "6628882fbf102716a412f7d9a66d8387bd6a784c",
|
|
109
|
+
"source": "https://github.com/ever-works/ever-works/tree/6628882fbf102716a412f7d9a66d8387bd6a784c/packages/plugins/pipedrive-connector"
|
|
110
|
+
}
|
|
111
|
+
}
|