cecon-interfaces
Version:
Interfaces de Projetos Cecon
78 lines (77 loc) • 3.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.N8nChatApiCallbackEntity = void 0;
var N8nChatApiCallbackEntity = /** @class */ (function () {
// #endregion Properties (12)
function N8nChatApiCallbackEntity(data) {
if (data === void 0) { data = {}; }
var _a, _b, _c, _d, _e;
this.callApi = (_a = data.callApi) !== null && _a !== void 0 ? _a : false;
this.callbackId = (_b = data.callbackId) !== null && _b !== void 0 ? _b : '';
this.apiUrl = (_c = data.apiUrl) !== null && _c !== void 0 ? _c : '';
this.method = (_d = data.method) !== null && _d !== void 0 ? _d : 'POST';
this.headers = (_e = data.headers) !== null && _e !== void 0 ? _e : {};
this.payload = data.payload;
this.requestConfig = data.requestConfig;
this.responseHandling = data.responseHandling;
this.metadata = data.metadata;
this.conditions = data.conditions;
this.webhook = data.webhook;
}
/**
* Valida se o callback está configurado corretamente para execução
*/
N8nChatApiCallbackEntity.prototype.isValid = function () {
if (!this.callApi)
return true; // Se não vai chamar API, é válido
return !!(this.callbackId && this.apiUrl && this.method);
};
/**
* Valida se todas as condições são atendidas para executar a API
*/
N8nChatApiCallbackEntity.prototype.canExecute = function (formData) {
var _a, _b, _c;
if (!this.callApi || !this.isValid())
return false;
// Verifica campos obrigatórios
if (((_a = this.conditions) === null || _a === void 0 ? void 0 : _a.requireAllFields) && ((_b = this.metadata) === null || _b === void 0 ? void 0 : _b.requiredFields)) {
for (var _i = 0, _d = this.metadata.requiredFields; _i < _d.length; _i++) {
var field = _d[_i];
if (!(formData === null || formData === void 0 ? void 0 : formData[field]))
return false;
}
}
// Valida validações customizadas
if (((_c = this.conditions) === null || _c === void 0 ? void 0 : _c.customValidations) && formData) {
for (var _e = 0, _f = this.conditions.customValidations; _e < _f.length; _e++) {
var validation = _f[_e];
var value = formData[validation.field];
switch (validation.rule) {
case 'required':
if (!value)
return false;
break;
case 'email':
if (value && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value))
return false;
break;
case 'phone':
if (value && !/^\+?[\d\s\-\(\)]+$/.test(value))
return false;
break;
case 'date':
if (value && isNaN(Date.parse(value)))
return false;
break;
case 'regex':
if (value && validation.value && !new RegExp(validation.value).test(value))
return false;
break;
}
}
}
return true;
};
return N8nChatApiCallbackEntity;
}());
exports.N8nChatApiCallbackEntity = N8nChatApiCallbackEntity;