Files
aiform_prod/docs/n8n_CODE_FLATTEN_DATA.js
Fedor 66a0065df8 Consultations, CRM dashboard, Back button in support and consultations
- Consultations: list from DraftsContext, ticket-detail webhook, response card
- Back button in bar on consultations and in support chat (miniapp:goBack)
- BottomBar: back enabled on /support; Support: goBack subscription
- n8n: CRM normalize (n8n_CODE_CRM_NORMALIZE), flatten data (n8n_CODE_FLATTEN_DATA)
- Dashboard: filter by category for CRM items, draft card width
- Backend: consultations.py, ticket-detail, n8n_ticket_form_podrobnee_webhook
- CHANGELOG_MINIAPP.md: section 2026-02-25
2026-03-01 10:49:38 +03:00

30 lines
1.0 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.

/**
* n8n Code node: развернуть data в плоский список.
* Если в data попал объект вида { "crm_items": [...] }, он заменяется на сами элементы crm_items.
*
* Вход: один элемент с полем data (массив), где часть элементов могут быть { crm_items: [...] }.
* Выход: один элемент { data: [...] } — плоский массив только карточек (заявки Postgres + элементы CRM).
*/
const input = $input.first().json;
let data = input.data;
if (data == null) data = input.items || input.drafts || [];
if (!Array.isArray(data)) data = [data];
const flattened = [];
for (const item of data) {
if (
item &&
typeof item === 'object' &&
item.crm_items &&
Array.isArray(item.crm_items) &&
Object.keys(item).length === 1
) {
flattened.push(...item.crm_items);
} else {
flattened.push(item);
}
}
return [{ json: { ...input, data: flattened } }];