Files
aiform_prod/docs/N8N_CODE_PROFILE_CONTACT_RESPONSE.js

52 lines
2.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ========================================
// Code Node: Формирование JSON для ответа N8N_CONTACT_WEBHOOK (профиль)
// Данные берутся из ноды select_user1 (SQL/запрос контакта).
// Выход этой ноды подаётся в "Respond to Webhook" как Response Body.
// ========================================
//
// Вход из ноды select_user1 (массив строк или один item на строку):
// contactid, firstname, lastname, email, mobile, phone, birthday, mailingstreet,
// middle_name, birthplace, inn, verification, bank
//
// Выход для вебхука: { "items": [ { ...поля в snake_case... } ] } или { "items": [] }
// ========================================
// Данные из ноды select_user1
const rawItems = $('select_user1').all();
let rows = [];
if (rawItems.length === 1 && Array.isArray(rawItems[0].json)) {
rows = rawItems[0].json;
} else if (rawItems.length === 1 && Array.isArray(rawItems[0].json?.items)) {
rows = rawItems[0].json.items;
} else if (rawItems.length === 1 && rawItems[0].json && !Array.isArray(rawItems[0].json)) {
rows = [rawItems[0].json];
} else {
rows = rawItems.map(i => i.json).filter(Boolean);
}
function mapRow(r) {
const v = (key) => {
const x = r[key];
return x !== undefined && x !== null && String(x).trim() !== '' ? String(x).trim() : '';
};
return {
contact_id: r.contactid ?? r.contact_id ?? '',
last_name: v('lastname') || v('last_name'),
first_name: v('firstname') || v('first_name'),
middle_name: v('middle_name') || v('middleName'),
birth_date: v('birthday') || v('birth_date') || v('birthDate'),
birth_place: v('birthplace') || v('birth_place') || v('birthPlace'),
inn: v('inn'),
email: v('email'),
registration_address: v('mailingstreet') || v('registration_address') || v('address'),
mailing_address: v('mailing_address') || v('postal_address'),
bank_for_compensation: v('bank') || v('bank_for_compensation'),
phone: v('mobile') || v('phone') || v('mobile_phone'),
};
}
const items = rows.map(mapRow);
// Один выходной item с телом ответа для Respond to Webhook
return [{ json: { items } }];