- "jsCode": "const input = $input.first().json;\nconst text = (input.text || '').toLowerCase();\nconst now = new Date();\nconst fmt = d => d.toISOString().split('T')[0];\nconst addDays = (n) => { const d = new Date(now); d.setDate(d.getDate() + n); return fmt(d); };\nconst dow = now.getDay();\nconst daysToMon = dow === 0 ? 6 : dow - 1;\nconst monday = new Date(now);\nmonday.setDate(now.getDate() - daysToMon);\n\n// clientMap como array para evitar chave duplicada e suportar multiplos aliases\nconst clientMap = [\n { keys: ['antonio neto', 'antonio'], id: 'act_497795903676192', nome: 'Antonio Neto' },\n { keys: ['gabriel jacinto', 'gabriel'], id: 'act_2493856707309984', nome: 'Gabriel Jacinto' },\n { keys: ['dani escudero', 'dani'], id: 'act_2145598605527232', nome: 'Dani Escudero' },\n { keys: ['agro'], id: 'act_929521557422139', nome: 'Agro' },\n { keys: ['escola de musica', 'escola de m\\u00fasica', 'escola'], id: 'act_544347952854095', nome: 'Escola de M\\u00fasica' },\n { keys: ['panmalhas'], id: 'act_1509005182799187', nome: 'Panmalhas Assessoria T\\u00eaxtil' },\n { keys: ['gfix store', 'gfix'], id: 'act_2811791829124905', nome: 'GFiX Store' },\n { keys: ['arte em gelo', 'arte em'], id: 'act_5585082641598366', nome: 'Arte em Gelo' },\n { keys: ['itag tecnologia', 'itag'], id: 'act_738466861151636', nome: 'ITAG Tecnologia' },\n { keys: ['hi dogz', 'dogz'], id: 'act_873195821346004', nome: 'Hi Dogz' },\n { keys: ['batata bistro', 'batata bistr\\u00f4', 'batata'], id: 'act_1673012226810411', nome: 'Batata Bistr\\u00f4' },\n { keys: ['caribbean bronze', 'caribbean'], id: 'act_1082397873705862', nome: 'Caribbean Bronze' },\n { keys: ['larissa kelleter', 'larissa'], id: 'act_738280412237298', nome: 'Larissa Kelleter' }\n];\n\n// Ordena por comprimento da primeira chave (mais especifico primeiro)\nclientMap.sort((a, b) => b.keys[0].length - a.keys[0].length);\n\n// Detecta pedido de relatorio\nconst isRelatorio = text.includes('relat');\n\nlet clientFound = null;\nfor (let i = 0; i < clientMap.length; i++) {\n const entry = clientMap[i];\n for (let j = 0; j < entry.keys.length; j++) {\n if (text.includes(entry.keys[j])) {\n clientFound = entry;\n break;\n }\n }\n if (clientFound) break;\n}\n\nconst today = fmt(now);\nconst yesterday = addDays(-1);\n// Corrigido: ultimos 7 dias = D-7 ate D-1 (igual ao Meta Ads)\nconst last7End = addDays(-1);\nconst last7Start = addDays(-7);\n\nconst isRelatorioWithClient = isRelatorio && clientFound !== null;\n// Modo todos: pede relatorio sem especificar cliente\nconst isRelatorioTodos = isRelatorio && clientFound === null;\n\n// Detecta periodo solicitado e mapeia para date_preset do Meta Ads\nconst hasMesPassado = text.includes('m\\u00eas passado') || text.includes('mes passado');\nconst hasEsseMes = text.includes('esse m\\u00eas') || text.includes('este m\\u00eas') || text.includes('esse mes') || text.includes('este mes');\n\nlet datePreset = 'last_7d';\nlet periodoLabel = '\\u00faltimos 7 dias';\nlet periodStart = last7Start;\nlet periodEnd = last7End;\n\nif (hasMesPassado) {\n datePreset = 'last_month';\n periodoLabel = 'm\\u00eas passado';\n const firstOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);\n const lastOfPrevMonth = new Date(firstOfMonth.getTime() - 86400000);\n const firstOfPrevMonth = new Date(lastOfPrevMonth.getFullYear(), lastOfPrevMonth.getMonth(), 1);\n periodStart = fmt(firstOfPrevMonth);\n periodEnd = fmt(lastOfPrevMonth);\n} else if (text.includes('ontem')) {\n datePreset = 'yesterday';\n periodoLabel = 'ontem';\n periodStart = yesterday;\n periodEnd = yesterday;\n} else if (text.includes('hoje')) {\n datePreset = 'today';\n periodoLabel = 'hoje';\n periodStart = today;\n periodEnd = today;\n} else if (text.includes('essa semana') || text.includes('esta semana')) {\n datePreset = 'this_week_mon_today';\n periodoLabel = 'essa semana';\n periodStart = fmt(monday);\n periodEnd = today;\n} else if (text.includes('14 dias')) {\n datePreset = 'last_14d';\n periodoLabel = '\\u00faltimos 14 dias';\n periodStart = addDays(-14);\n periodEnd = addDays(-1);\n} else if (text.includes('30 dias') || hasEsseMes) {\n datePreset = 'last_30d';\n periodoLabel = '\\u00faltimos 30 dias';\n periodStart = addDays(-30);\n periodEnd = addDays(-1);\n}\n\nreturn [{\n json: {\n text: input.text,\n phone: input.phone,\n remoteJid: input.remoteJid,\n instance: input.instance,\n today: today,\n yesterday: yesterday,\n weekStart: fmt(monday),\n last7: last7Start,\n last30: addDays(-30),\n last7Start: last7Start,\n last7End: last7End,\n metaToken: 'EAAeVLzZANk8oBQ79Ue5UimCCCYL9uNZCH9EZA7vZAJtqB5B9aQ6mO53EnUI1b8Cskpq9tRsYZAaDKvZB1kLZABjbqgwi3aenhyPnAtQkeineasNmaZA7vue9ndlaErc59UFZBaMBeDnBC8ZBpXtn9PdUP4OMQvkd53QZAL1jI9SC49WGZCvgopdZABkhGKCEuVStyYGRfYPrlhksx7HHkqZBNWo5gMLhREik5hvkp1JA7X',\n isRelatorio: isRelatorio,\n isRelatorioWithClient: isRelatorioWithClient,\n isRelatorioTodos: isRelatorioTodos,\n clientFound: clientFound ? clientFound.id : null,\n clientNome: clientFound ? clientFound.nome : null,\n datePreset: datePreset,\n periodoLabel: periodoLabel,\n periodStart: periodStart,\n periodEnd: periodEnd\n }\n}];"
0 commit comments