Skip to content

Commit 00d7d45

Browse files
committed
Save just in case2
1 parent 21af602 commit 00d7d45

15 files changed

+227
-429
lines changed

components/drift/actions/create-contact/create-contact.mjs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import drift from "../../drift.app.mjs";
22
import { removeNullEntries } from "../../common/utils.mjs";
33

4-
export default {
4+
export default {
55
key: "drift-create-contact-test",
66
name: "Create Contact",
77
description: "Creates a contact in Drift. [See the docs](https://devdocs.drift.com/docs/creating-a-contact).",
8-
version: "0.0.4",
8+
version: "0.0.10",
99
type: "action",
1010
props: {
1111
drift,
@@ -64,21 +64,16 @@ export default {
6464
throw new Error (`Contact ${email} already exists`);
6565
};
6666

67-
let response;
67+
const response = await drift.createContact({
68+
$,
69+
data: {
70+
attributes,
71+
},
72+
});
6873

69-
try {
70-
response = await drift.createContact({
71-
$,
72-
data: {
73-
attributes,
74-
},
75-
});
76-
} catch (error) {
77-
drift.throwCustomError("Unable to create new contact", error, warnings);
78-
}
74+
console.log(response.data.id);
7975

80-
$.export("$summary", `Contact "${email}" created with ID "${response.data.id}".`
81-
+ "\n- " + warnings.join("\n- "));
76+
$.export("$summary", `Contact ${email} created.` + "\n- " + warnings.join("\n- "));
8277
return response;
8378
},
8479
};

components/drift/actions/delete-contact/delete-contact.mjs

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "drift-delete-contact-test",
55
name: "Delete Contact",
66
description: "Deletes a contact in Drift by ID or email. [See the docs](https://devdocs.drift.com/docs/removing-a-contact).",
7-
version: "0.0.10",
7+
version: "0.0.12",
88
type: "action",
99
props: {
1010
drift,
@@ -25,54 +25,18 @@ export default {
2525

2626
warnings.push(...drift.checkEmailOrId(emailOrId));
2727

28-
let contactId;
29-
let email;
28+
let contact = await drift.getContactByEmailOrId($, emailOrId);
29+
contact = contact.data[0] || contact.data;
3030

31-
if (drift.isIdNumber(Number(emailOrId))) {
32-
33-
contactId = Number(emailOrId);
34-
// Drift's response always returns 204 (No Content) on both successful and failed deletions.
35-
// So fetch the contact first and throw early if it is not found.
36-
try {
37-
await drift.getContactById({
38-
$,
39-
contactId,
40-
});
41-
} catch (error) {
42-
if (error.status === 404) {
43-
throw new Error(`No contact found with ID: ${contactId}`);
44-
} else {
45-
throw error;
46-
};
47-
}
48-
49-
} else {
50-
email = emailOrId;
51-
// returns { data: [] } if not found.
52-
const response = await drift.getContactByEmail({
53-
$,
54-
params: {
55-
email,
56-
},
57-
});
58-
if (!response?.data?.length) {
59-
throw new Error(`No contact found with email: ${email}` +
60-
"\n- " + warnings.join("\n- "));
61-
};
62-
63-
contactId = response.data[0].id;
64-
};
31+
const contactId = contact.id;
32+
const contactEmail = contact.email;
6533

6634
const response = await drift.deleteContactById({
6735
$,
6836
contactId,
6937
});
7038

71-
email = (email)
72-
? "\"" + email + "\""
73-
: "";
74-
75-
$.export("$summary", `Contact ${email} ID "${contactId}" deleted successfully.`);
39+
$.export("$summary", `Contact ${contactEmail} ID "${contactId}" deleted successfully.`);
7640

7741
return response;
7842
},
Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import drift from "../../drift.app.mjs";
22

3-
export default {
4-
key: "drift-get-contact-test",
5-
name: "Delete Contact",
6-
description: "Deletes a contact in Drift by ID or email. [See the docs](https://devdocs.drift.com/docs/retrieving-contact).",
7-
version: "0.0.3",
3+
export default {
4+
key: "drift-delete-contact-test",
5+
name: "Get Contact",
6+
description: "Retrieves a contact in Drift by ID or email. [See the docs](https://devdocs.drift.com/docs/retrieving-contact)",
7+
version: "0.0.14",
88
type: "action",
99
props: {
1010
drift,
@@ -25,43 +25,9 @@ export default {
2525

2626
warnings.push(...drift.checkEmailOrId(emailOrId));
2727

28-
let contact;
28+
const response = await drift.getContactByEmailOrId($, emailOrId);
2929

30-
if (drift.isIdNumber(Number(emailOrId))) {
31-
32-
const contactId = Number(emailOrId);
33-
34-
let response;
35-
try {
36-
response = await drift.getContactById({
37-
$,
38-
contactId,
39-
});
40-
} catch (error) {
41-
if (error.status === 404) {
42-
throw new Error(`No contact found with ID: ${contactId}`);
43-
} else {
44-
throw error;
45-
};
46-
}
47-
48-
contact = response.data;
49-
50-
} else {
51-
const email = emailOrId;
52-
const response = await drift.getContactByEmail({
53-
$,
54-
params: {
55-
email,
56-
},
57-
});
58-
if (!response?.data?.length) {
59-
throw new Error(`No contact found with email: ${email}` +
60-
"\n- " + warnings.join("\n- "));
61-
};
62-
63-
contact = response.data[0];
64-
};
30+
const contact = response.data[0] || response.data;
6531

6632
console.log(contact);
6733

@@ -71,4 +37,3 @@ export default {
7137
return contact;
7238
},
7339
};
74-
Lines changed: 29 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import drift from "../../drift.app.mjs";
2+
import { removeNullEntries } from "../../common/utils.mjs";
23

34
export default {
45
key: "drift-update-contact",
56
name: "Update Contact",
67
description: "Updates a contact in Drift using ID or email. Only changed attributes will be updated. [See Drift API docs](https://devdocs.drift.com/docs/updating-a-contact)",
7-
version: "0.0.2",
8+
version: "0.0.5",
89
type: "action",
910
props: {
1011
drift,
@@ -15,8 +16,8 @@ export default {
1516
},
1617
email: {
1718
type: "string",
18-
label: "New Email",
19-
description: "The new email address to assign to this contact.",
19+
label: "Email",
20+
description: "The contact’s email address",
2021
optional: true,
2122
},
2223
name: {
@@ -41,77 +42,42 @@ export default {
4142

4243
async run({ $ }) {
4344
const warnings = [];
44-
const { drift } = this;
45-
const emailOrId = drift.trimIfString(this.emailOrId);
46-
warnings.push(...drift.checkEmailOrId(emailOrId));
45+
const {
46+
drift, name, email, phone,
47+
} = this;
4748

48-
let contactId;
49+
const customAttributes = drift.parseIfJSONString(this.customAttributes);
4950

50-
// Resolve contact by ID or email
51-
if (drift.isIdNumber(Number(emailOrId))) {
52-
contactId = Number(emailOrId);
53-
try {
54-
await drift.getContactById({
55-
$,
56-
contactId,
57-
}); // validate
58-
} catch (error) {
59-
if (error.status === 404) {
60-
throw new Error(`No contact found with ID: ${contactId}`);
61-
} else {
62-
throw error;
63-
}
64-
}
65-
} else {
66-
const response = await drift.getContactByEmail({
67-
$,
68-
params: {
69-
email: emailOrId,
70-
},
71-
});
72-
if (!response?.data?.length) {
73-
throw new Error(`No contact found with email: ${emailOrId}` +
74-
"\n- " + warnings.join("\n- "));
75-
}
76-
contactId = response.data[0].id;
77-
}
78-
79-
// Safely merge attributes
80-
const attributes = {
81-
...(this.email && {
82-
email: this.email,
83-
}),
84-
...(this.name && {
85-
name: this.name,
86-
}),
87-
...(this.phone && {
88-
phone: this.phone,
89-
}),
90-
...this.customAttributes,
91-
};
92-
93-
// Remove conflicts where top-level fields exist in customAttributes
94-
[
95-
"email",
96-
"name",
97-
"phone",
98-
].forEach((key) => {
99-
if (this.customAttributes?.[key]) {
100-
warnings.push(`Warning: Custom attribute "${key}" is ignored because it’s already handled as a top-level prop.`);
101-
delete attributes[key];
102-
}
51+
const attributes = removeNullEntries({
52+
name,
53+
phone,
54+
email,
55+
...customAttributes,
10356
});
10457

10558
if (!Object.keys(attributes).length) {
10659
throw new Error("No attributes provided to update.");
107-
}
60+
};
10861

109-
const response = await drift.updateContactById(contactId, {
62+
const emailOrId = drift.trimIfString(this.emailOrId);
63+
64+
warnings.push(...drift.checkEmailOrId(emailOrId));
65+
warnings.push(...drift.checkIfEmailValid(emailOrId));
66+
67+
let contact = await drift.getContactByEmailOrId($, emailOrId);
68+
69+
const contactId = contact.data[0]?.id || contact.data.id;
70+
71+
const response = await drift.updateContact({
11072
$,
111-
attributes,
73+
contactId,
74+
data: {
75+
attributes,
76+
},
11277
});
11378

11479
$.export("$summary", `Contact ID ${contactId} updated successfully.`);
11580
return response;
11681
},
11782
};
83+

components/drift/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"access": "public"
1414
},
1515
"dependencies": {
16-
"@pipedream/platform": "^3.0.0"
16+
"@pipedream/platform": "^3.0.0",
17+
"node-persist": "^4.0.4"
1718
}
1819
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import drift from "../../drift.app.mjs";
2+
3+
export default {
4+
key: "drift-new-conversation-instant",
5+
name: "New Conversation",
6+
description: "Emits an event every time a new conversation is started in Drift.",
7+
version: "0.0.2",
8+
type: "source",
9+
dedupe: "unique",
10+
props: {
11+
drift,
12+
db: "$.service.db",
13+
timer: {
14+
type: "$.interface.timer",
15+
label: "Polling Interval",
16+
description: "How often to poll Drift for new conversations.",
17+
},
18+
limit: {
19+
type: "integer",
20+
label: "Maximum Conversations to Fetch",
21+
description: "The number of most recent conversations to retrieve each time the source runs.",
22+
default: 50,
23+
optional: true,
24+
min: 1,
25+
max: 500,
26+
},
27+
},
28+
29+
async run({ $ }) {
30+
const lastTimestamp = this.db.get("lastCreatedAt") || 0;
31+
32+
const res = await this.drift._makeRequest({
33+
$,
34+
path: `/conversations?limit=${this.limit}&sort=createdAt`,
35+
});
36+
37+
const conversations = res?.data || [];
38+
39+
for (const conversation of conversations) {
40+
const createdAt = conversation.createdAt || 0;
41+
42+
if (createdAt > lastTimestamp) {
43+
this.$emit(conversation, {
44+
id: conversation.id,
45+
summary: `New conversation with ID ${conversation.id}`,
46+
ts: createdAt,
47+
});
48+
}
49+
}
50+
51+
const newest = conversations.reduce((max, c) =>
52+
Math.max(max, c.createdAt || 0), lastTimestamp);
53+
54+
this.db.set("lastCreatedAt", newest);
55+
},
56+
};

components/drift/tests/dev-db/2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae

Lines changed: 0 additions & 1 deletion
This file was deleted.

components/drift/tests/dev-db/ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"key":"lastCreatedAt","value":1746293843698}

components/drift/tests/sources-test/dev-db/6cf209679013426bada69ce89a388ed5ed598c6a752353d368f15c2f5fd7e819

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)