Skip to content

Commit 21af602

Browse files
committed
Save just in case
1 parent 29ac629 commit 21af602

File tree

10 files changed

+676
-87
lines changed

10 files changed

+676
-87
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import drift from "../../drift.app.mjs";
2+
import { removeNullEntries } from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "drift-create-contact-test",
6+
name: "Create Contact",
7+
description: "Creates a contact in Drift. [See the docs](https://devdocs.drift.com/docs/creating-a-contact).",
8+
version: "0.0.4",
9+
type: "action",
10+
props: {
11+
drift,
12+
email: {
13+
type: "string",
14+
label: "Email",
15+
description: "The contact's email address",
16+
},
17+
name: {
18+
type: "string",
19+
label: "Name",
20+
description: "The contact's full name",
21+
optional: true,
22+
},
23+
phone: {
24+
type: "string",
25+
label: "Phone",
26+
description: "The contact's phone number",
27+
optional: true,
28+
},
29+
customAttributes: {
30+
type: "object",
31+
label: "Custom Attributes",
32+
description: "Additional custom attributes to store on the contact",
33+
optional: true,
34+
},
35+
},
36+
37+
async run({ $ }) {
38+
39+
const warnings = [];
40+
41+
const {
42+
drift, email, name, phone,
43+
} = this;
44+
45+
warnings.push(...drift.checkIfEmailValid(email));
46+
47+
const customAttributes = drift.parseIfJSONString(this.customAttributes);
48+
49+
const attributes = removeNullEntries({
50+
email,
51+
name,
52+
phone,
53+
...customAttributes,
54+
});
55+
56+
const existingContact = await drift.getContactByEmail({
57+
$,
58+
params: {
59+
email,
60+
},
61+
});
62+
63+
if (existingContact && existingContact.data.length > 0) {
64+
throw new Error (`Contact ${email} already exists`);
65+
};
66+
67+
let response;
68+
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+
}
79+
80+
$.export("$summary", `Contact "${email}" created with ID "${response.data.id}".`
81+
+ "\n- " + warnings.join("\n- "));
82+
return response;
83+
},
84+
};
85+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import drift from "../../drift.app.mjs";
2+
3+
export default {
4+
key: "drift-delete-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/removing-a-contact).",
7+
version: "0.0.10",
8+
type: "action",
9+
props: {
10+
drift,
11+
emailOrId: {
12+
type: "string",
13+
label: "Email or Id",
14+
description: "The contact's email address or ID",
15+
},
16+
},
17+
18+
async run({ $ }) {
19+
20+
const warnings = [];
21+
22+
const { drift } = this;
23+
24+
const emailOrId = drift.trimIfString(this.emailOrId);
25+
26+
warnings.push(...drift.checkEmailOrId(emailOrId));
27+
28+
let contactId;
29+
let email;
30+
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+
};
65+
66+
const response = await drift.deleteContactById({
67+
$,
68+
contactId,
69+
});
70+
71+
email = (email)
72+
? "\"" + email + "\""
73+
: "";
74+
75+
$.export("$summary", `Contact ${email} ID "${contactId}" deleted successfully.`);
76+
77+
return response;
78+
},
79+
};
80+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import drift from "../../drift.app.mjs";
2+
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",
8+
type: "action",
9+
props: {
10+
drift,
11+
emailOrId: {
12+
type: "string",
13+
label: "Email or Id",
14+
description: "The contact's email address or ID",
15+
},
16+
},
17+
18+
async run({ $ }) {
19+
20+
const warnings = [];
21+
22+
const { drift } = this;
23+
24+
const emailOrId = drift.trimIfString(this.emailOrId);
25+
26+
warnings.push(...drift.checkEmailOrId(emailOrId));
27+
28+
let contact;
29+
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+
};
65+
66+
console.log(contact);
67+
68+
$.export("$summary", `Contact ${contact.attributes.email} ID "${contact.id}"`
69+
+ " fetched successfully.");
70+
71+
return contact;
72+
},
73+
};
74+
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import drift from "../../drift.app.mjs";
2+
3+
export default {
4+
key: "drift-update-contact",
5+
name: "Update Contact",
6+
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+
type: "action",
9+
props: {
10+
drift,
11+
emailOrId: {
12+
type: "string",
13+
label: "Email or ID",
14+
description: "The contact’s email address or numeric ID.",
15+
},
16+
email: {
17+
type: "string",
18+
label: "New Email",
19+
description: "The new email address to assign to this contact.",
20+
optional: true,
21+
},
22+
name: {
23+
type: "string",
24+
label: "Name",
25+
description: "The contact’s name.",
26+
optional: true,
27+
},
28+
phone: {
29+
type: "string",
30+
label: "Phone",
31+
description: "The contact’s phone number.",
32+
optional: true,
33+
},
34+
customAttributes: {
35+
type: "object",
36+
label: "Custom Attributes",
37+
description: "Any custom attributes to update (e.g. company, job title, etc).",
38+
optional: true,
39+
},
40+
},
41+
42+
async run({ $ }) {
43+
const warnings = [];
44+
const { drift } = this;
45+
const emailOrId = drift.trimIfString(this.emailOrId);
46+
warnings.push(...drift.checkEmailOrId(emailOrId));
47+
48+
let contactId;
49+
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+
}
103+
});
104+
105+
if (!Object.keys(attributes).length) {
106+
throw new Error("No attributes provided to update.");
107+
}
108+
109+
const response = await drift.updateContactById(contactId, {
110+
$,
111+
attributes,
112+
});
113+
114+
$.export("$summary", `Contact ID ${contactId} updated successfully.`);
115+
return response;
116+
},
117+
};

0 commit comments

Comments
 (0)