UNPKG

n8n-nodes-warmr

Version:
153 lines (152 loc) 7.2 kB
"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.Tags = void 0; const TagsService_1 = require("../services/TagsService"); class Tags { constructor() { this.description = { displayName: "Warmr Tags", name: "warmrTags", group: ["transform"], version: 1, description: "Manage Warmr tags and contact tag assignments via the v1 API", defaults: { name: "Warmr Tags", 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 Tags", value: "listTags" }, { name: "Create Tag", value: "createTag" }, { name: "Update Tag", value: "updateTag" }, { name: "Delete Tag", value: "deleteTag" }, { name: "Get Contact Tags", value: "getContactTags" }, { name: "Set Contact Tags", value: "setContactTags" }, ], default: "listTags", description: "Operation to perform", }, // --- Tag ID (integer) --- { displayName: "Tag ID", name: "tagId", type: "number", default: 0, required: true, displayOptions: { show: { operation: ["updateTag", "deleteTag"] } }, description: "Numeric ID of the tag", }, // --- Create / Update fields --- { displayName: "Name", name: "name", type: "string", default: "", displayOptions: { show: { operation: ["createTag", "updateTag"] } }, description: "Tag name (1-255 chars)", }, { displayName: "Color", name: "color", type: "string", default: "", displayOptions: { show: { operation: ["createTag", "updateTag"] } }, description: "Hex color (#rrggbb). Auto-generated if omitted on create.", }, // --- Contact tag operations --- { displayName: "Contact UUID", name: "contactUuid", type: "string", default: "", required: true, displayOptions: { show: { operation: ["getContactTags", "setContactTags"] } }, description: "UUID of the contact", }, { displayName: "Tag IDs", name: "tagIds", type: "string", default: "", required: true, displayOptions: { show: { operation: ["setContactTags"] } }, description: "Comma-separated tag IDs to assign (replaces all existing tags)", }, ], }; } 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 === "listTags") { const tags = yield TagsService_1.TagsService.listTags(credentials.apiKey); returnData.push({ json: { data: tags } }); } else if (operation === "createTag") { const data = buildTagInput.call(this, i); const tag = yield TagsService_1.TagsService.createTag(data, credentials.apiKey); returnData.push({ json: tag }); } else if (operation === "updateTag") { const id = this.getNodeParameter("tagId", i); const data = buildTagInput.call(this, i); const tag = yield TagsService_1.TagsService.updateTag(id, data, credentials.apiKey); returnData.push({ json: tag }); } else if (operation === "deleteTag") { const id = this.getNodeParameter("tagId", i); yield TagsService_1.TagsService.deleteTag(id, credentials.apiKey); returnData.push({ json: { success: true, id } }); } else if (operation === "getContactTags") { const contactUuid = this.getNodeParameter("contactUuid", i); const tags = yield TagsService_1.TagsService.getContactTags(contactUuid, credentials.apiKey); returnData.push({ json: { data: tags } }); } else if (operation === "setContactTags") { const contactUuid = this.getNodeParameter("contactUuid", i); const raw = this.getNodeParameter("tagIds", i); const tagIds = raw.split(",").map((s) => Number(s.trim())).filter(Boolean); yield TagsService_1.TagsService.replaceContactTags(contactUuid, tagIds, credentials.apiKey); returnData.push({ json: { success: true, contactUuid, tagIds } }); } } catch (error) { returnData.push({ json: { error: error.message } }); } } return this.prepareOutputData(returnData); }); } } exports.Tags = Tags; function buildTagInput(i) { const name = this.getNodeParameter("name", i, ""); const color = this.getNodeParameter("color", i, ""); const data = {}; if (name) data.name = name; if (color) data.color = color; return data; } exports.default = Tags;