UNPKG

@digitalstories/n8n-nodes-infomaniak

Version:

n8n nodes to interact with Infomaniak services (Kchat)

107 lines 5.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KchatNodeOperations = void 0; const KchatService_1 = require("./KchatService"); class KchatNodeOperations { constructor(executeFunctions, credentials) { this.executeFunctions = executeFunctions; this.service = new KchatService_1.KchatService(executeFunctions, credentials); } async executeOperation(resource, operation, itemIndex) { switch (resource) { case 'channel': return this.executeChannelOperation(operation, itemIndex); case 'file': return this.executeFileOperation(operation, itemIndex); case 'user': return this.executeUserOperation(operation, itemIndex); default: throw new Error(`Ressource non supportée: ${resource}`); } } async executeChannelOperation(operation, itemIndex) { const executeFunctions = this.executeFunctions; switch (operation) { case 'createPost': { const channelId = executeFunctions.getNodeParameter('channelId', itemIndex); const postContent = executeFunctions.getNodeParameter('postContent', itemIndex); const addFile = executeFunctions.getNodeParameter('addFile', itemIndex, false); if (addFile) { const binaryPropertyName = executeFunctions.getNodeParameter('binaryPropertyName', itemIndex); const binaryData = executeFunctions.helpers.assertBinaryData(itemIndex, binaryPropertyName); const dataBuffer = await executeFunctions.helpers.getBinaryDataBuffer(itemIndex, binaryPropertyName); const fileResponse = await this.service.uploadFile(channelId, dataBuffer, binaryData.fileName || 'file', binaryData.mimeType || 'application/octet-stream'); let fileId; if (fileResponse && fileResponse.file_info && fileResponse.file_info.id) { fileId = fileResponse.file_info.id; } else { throw new Error('Upload de fichier échoué ou réponse inattendue.'); } return await this.executeCreatePost({ channelId, message: postContent, fileIds: [fileId], }); } else { return await this.executeCreatePost({ channelId, message: postContent, }); } } case 'listPosts': { const channelId = executeFunctions.getNodeParameter('channelId', itemIndex); return await this.service.listPosts(channelId); } default: throw new Error(`Opération non supportée pour Channel: ${operation}`); } } async executeCreatePost(params) { const channelId = params.channelId; const message = params.message; const fileIds = params.fileIds; try { const result = await this.service.createPost(channelId, message, fileIds); return result; } catch (error) { throw error; } } async executeFileOperation(operation, itemIndex) { const executeFunctions = this.executeFunctions; switch (operation) { case 'uploadFile': { const channelId = executeFunctions.getNodeParameter('channelId', itemIndex); const binaryPropertyName = executeFunctions.getNodeParameter('binaryPropertyName', itemIndex); const binaryData = executeFunctions.helpers.assertBinaryData(itemIndex, binaryPropertyName); const dataBuffer = await executeFunctions.helpers.getBinaryDataBuffer(itemIndex, binaryPropertyName); return await this.service.uploadFile(channelId, dataBuffer, binaryData.fileName || 'file', binaryData.mimeType || 'application/octet-stream'); } case 'getFileDetails': { const fileId = executeFunctions.getNodeParameter('fileId', itemIndex); return await this.service.getFileDetails(fileId); } default: throw new Error(`Opération non supportée pour File: ${operation}`); } } async executeUserOperation(operation, itemIndex) { switch (operation) { case 'listUsers': { return await this.service.listUsers(); } case 'getUser': { const userId = this.executeFunctions.getNodeParameter('userId', itemIndex); return await this.service.getUser(userId); } default: throw new Error(`Opération non supportée pour User: ${operation}`); } } } exports.KchatNodeOperations = KchatNodeOperations; //# sourceMappingURL=KchatNodeOperations.js.map