n8n-nodes-warmr
Version:
n8n community node for Warmr integration
245 lines (244 loc) • 10.7 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Contacts = void 0;
const ContactsService_1 = require("../services/ContactsService");
class Contacts {
constructor() {
this.description = {
displayName: "Warmr Contacts",
name: "warmrContacts",
group: ["transform"],
version: 1,
description: "Manage Warmr contacts via the v1 API",
defaults: { name: "Warmr Contacts", color: "#e4d103" },
icon: "file:icon.png",
inputs: ["main"],
outputs: ["main"],
credentials: [{ name: "warmrApi", required: true }],
properties: [
{
displayName: "Operation",
name: "operation",
type: "options",
options: [
{ name: "List Contacts", value: "listContacts" },
{ name: "Get Contact", value: "getContact" },
{ name: "Create Contact", value: "createContact" },
{ name: "Update Contact", value: "updateContact" },
{ name: "Delete Contact", value: "deleteContact" },
],
default: "listContacts",
description: "Operation to perform",
},
// --- List Contacts filters ---
{
displayName: "Page",
name: "page",
type: "number",
default: 1,
displayOptions: { show: { operation: ["listContacts"] } },
description: "Page number (default: 1)",
},
{
displayName: "Per Page",
name: "perPage",
type: "number",
default: 50,
displayOptions: { show: { operation: ["listContacts"] } },
description: "Results per page (max 100)",
},
{
displayName: "Search",
name: "search",
type: "string",
default: "",
displayOptions: { show: { operation: ["listContacts"] } },
description: "Search by first name, last name, or email",
},
{
displayName: "List UUID",
name: "listUuid",
type: "string",
default: "",
displayOptions: { show: { operation: ["listContacts"] } },
description: "Filter by list UUID",
},
{
displayName: "Archived",
name: "archived",
type: "boolean",
default: false,
displayOptions: { show: { operation: ["listContacts"] } },
description: "Whether to filter archived contacts",
},
{
displayName: "Is Favorite",
name: "isFavorite",
type: "boolean",
default: false,
displayOptions: { show: { operation: ["listContacts"] } },
description: "Whether to filter favorite contacts only",
},
// --- Get / Update / Delete by UUID ---
{
displayName: "Contact UUID",
name: "uuid",
type: "string",
default: "",
required: true,
displayOptions: {
show: { operation: ["getContact", "updateContact", "deleteContact"] },
},
description: "UUID of the contact",
},
// --- Create / Update fields ---
{
displayName: "First Name",
name: "firstName",
type: "string",
default: "",
displayOptions: { show: { operation: ["createContact", "updateContact"] } },
},
{
displayName: "Last Name",
name: "lastName",
type: "string",
default: "",
displayOptions: { show: { operation: ["createContact", "updateContact"] } },
},
{
displayName: "Email",
name: "email",
type: "string",
default: "",
displayOptions: { show: { operation: ["createContact", "updateContact"] } },
},
{
displayName: "Phone",
name: "phone",
type: "string",
default: "",
displayOptions: { show: { operation: ["createContact", "updateContact"] } },
},
{
displayName: "Website",
name: "website",
type: "string",
default: "",
displayOptions: { show: { operation: ["createContact", "updateContact"] } },
},
{
displayName: "Headline",
name: "headline",
type: "string",
default: "",
displayOptions: { show: { operation: ["createContact", "updateContact"] } },
},
{
displayName: "Location",
name: "location",
type: "string",
default: "",
displayOptions: { show: { operation: ["createContact", "updateContact"] } },
},
{
displayName: "Country ISO",
name: "countryIso",
type: "string",
default: "",
displayOptions: { show: { operation: ["createContact", "updateContact"] } },
description: "2-letter ISO country code",
},
],
};
}
execute() {
return __awaiter(this, void 0, void 0, function* () {
const items = this.getInputData();
const returnData = [];
const credentials = (yield this.getCredentials("warmrApi"));
for (let i = 0; i < items.length; i++) {
const operation = this.getNodeParameter("operation", i);
try {
if (operation === "listContacts") {
const result = yield handleListContacts.call(this, i, credentials.apiKey);
returnData.push({ json: result });
}
else if (operation === "getContact") {
const uuid = this.getNodeParameter("uuid", i);
const contact = yield ContactsService_1.ContactsService.getContact(uuid, credentials.apiKey);
returnData.push({ json: contact });
}
else if (operation === "createContact") {
const data = buildContactInput.call(this, i);
const contact = yield ContactsService_1.ContactsService.createContact(data, credentials.apiKey);
returnData.push({ json: contact });
}
else if (operation === "updateContact") {
const uuid = this.getNodeParameter("uuid", i);
const data = buildContactInput.call(this, i);
const contact = yield ContactsService_1.ContactsService.updateContact(uuid, data, credentials.apiKey);
returnData.push({ json: contact });
}
else if (operation === "deleteContact") {
const uuid = this.getNodeParameter("uuid", i);
yield ContactsService_1.ContactsService.deleteContact(uuid, credentials.apiKey);
returnData.push({ json: { success: true, uuid } });
}
}
catch (error) {
returnData.push({ json: { error: error.message } });
}
}
return this.prepareOutputData(returnData);
});
}
}
exports.Contacts = Contacts;
function handleListContacts(i, apiKey) {
return __awaiter(this, void 0, void 0, function* () {
const query = {};
const page = this.getNodeParameter("page", i);
const perPage = this.getNodeParameter("perPage", i);
const search = this.getNodeParameter("search", i);
const listUuid = this.getNodeParameter("listUuid", i);
const archived = this.getNodeParameter("archived", i);
const isFavorite = this.getNodeParameter("isFavorite", i);
if (page)
query.page = page;
if (perPage)
query.per_page = perPage;
if (search)
query.search = search;
if (listUuid)
query.list_uuid = listUuid;
if (archived)
query.archived = archived;
if (isFavorite)
query.is_favorite = isFavorite;
return ContactsService_1.ContactsService.getContacts(query, apiKey);
});
}
function buildContactInput(i) {
const fields = [
"firstName", "lastName", "email", "phone",
"website", "headline", "location", "countryIso",
];
const data = {};
for (const field of fields) {
const value = this.getNodeParameter(field, i, "");
if (value)
data[field] = value;
}
return data;
}
exports.default = Contacts;