UNPKG

n8n

Version:

n8n Workflow Automation Tool

127 lines 6.28 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ImportService = void 0; const typedi_1 = require("typedi"); const uuid_1 = require("uuid"); const Logger_1 = require("../Logger"); const Db = __importStar(require("../Db")); const credentials_repository_1 = require("../databases/repositories/credentials.repository"); const tag_repository_1 = require("../databases/repositories/tag.repository"); const SharedWorkflow_1 = require("../databases/entities/SharedWorkflow"); const WorkflowHelpers_1 = require("../WorkflowHelpers"); const Project_1 = require("../databases/entities/Project"); const WorkflowEntity_1 = require("../databases/entities/WorkflowEntity"); const WorkflowTagMapping_1 = require("../databases/entities/WorkflowTagMapping"); let ImportService = class ImportService { constructor(logger, credentialsRepository, tagRepository) { this.logger = logger; this.credentialsRepository = credentialsRepository; this.tagRepository = tagRepository; this.dbCredentials = []; this.dbTags = []; } async initRecords() { this.dbCredentials = await this.credentialsRepository.find(); this.dbTags = await this.tagRepository.find(); } async importWorkflows(workflows, projectId) { await this.initRecords(); for (const workflow of workflows) { workflow.nodes.forEach((node) => { this.toNewCredentialFormat(node); if (!node.id) node.id = (0, uuid_1.v4)(); }); const hasInvalidCreds = workflow.nodes.some((node) => { var _a; return !((_a = node.credentials) === null || _a === void 0 ? void 0 : _a.id); }); if (hasInvalidCreds) await this.replaceInvalidCreds(workflow); } await Db.transaction(async (tx) => { var _a, _b; for (const workflow of workflows) { if (workflow.active) { workflow.active = false; this.logger.info(`Deactivating workflow "${workflow.name}". Remember to activate later.`); } const exists = workflow.id ? await tx.existsBy(WorkflowEntity_1.WorkflowEntity, { id: workflow.id }) : false; const upsertResult = await tx.upsert(WorkflowEntity_1.WorkflowEntity, workflow, ['id']); const workflowId = (_a = upsertResult.identifiers.at(0)) === null || _a === void 0 ? void 0 : _a.id; const personalProject = await tx.findOneByOrFail(Project_1.Project, { id: projectId }); if (!exists) { await tx.upsert(SharedWorkflow_1.SharedWorkflow, { workflowId, projectId: personalProject.id, role: 'workflow:owner' }, ['workflowId', 'projectId']); } if (!((_b = workflow.tags) === null || _b === void 0 ? void 0 : _b.length)) continue; await this.tagRepository.setTags(tx, this.dbTags, workflow); for (const tag of workflow.tags) { await tx.upsert(WorkflowTagMapping_1.WorkflowTagMapping, { tagId: tag.id, workflowId }, [ 'tagId', 'workflowId', ]); } } }); } async replaceInvalidCreds(workflow) { try { await (0, WorkflowHelpers_1.replaceInvalidCredentials)(workflow); } catch (e) { const error = e instanceof Error ? e : new Error(`${e}`); this.logger.error('Failed to replace invalid credential', error); } } toNewCredentialFormat(node) { if (!node.credentials) return; for (const [type, name] of Object.entries(node.credentials)) { if (typeof name !== 'string') continue; const nodeCredential = { id: null, name }; const match = this.dbCredentials.find((c) => c.name === name && c.type === type); if (match) nodeCredential.id = match.id; node.credentials[type] = nodeCredential; } } }; exports.ImportService = ImportService; exports.ImportService = ImportService = __decorate([ (0, typedi_1.Service)(), __metadata("design:paramtypes", [Logger_1.Logger, credentials_repository_1.CredentialsRepository, tag_repository_1.TagRepository]) ], ImportService); //# sourceMappingURL=import.service.js.map