{ "name": "Seguimiento de Impuestos GLM - Recordatorios Gmail y Google Chat", "nodes": [ { "parameters": { "rule": { "interval": [ { "field": "cronExpression", "expression": "0 8 * * *" } ] } }, "type": "n8n-nodes-base.scheduleTrigger", "typeVersion": 1.2, "position": [ 1008, 1472 ], "id": "28b8d854-b043-4a35-b047-4db8f919fb6f", "name": "Diario 8:00 AM" }, { "parameters": {}, "type": "n8n-nodes-base.manualTrigger", "typeVersion": 1, "position": [ 1008, 1632 ], "id": "be168026-4130-4486-8e9a-1766b3e219d4", "name": "Ejecutar manualmente" }, { "parameters": { "httpMethod": "POST", "path": "seguimiento-impuestos", "options": {} }, "type": "n8n-nodes-base.webhook", "typeVersion": 2.1, "position": [ 1008, 1824 ], "id": "07d8b63b-e322-492a-8241-ef9ccb273a31", "name": "Webhook seguimiento-impuestos", "webhookId": "seguimiento-impuestos-glm" }, { "parameters": { "jsCode": "function dateInTimeZone(timeZone) {\n const parts = new Intl.DateTimeFormat('en-US', {\n timeZone,\n year: 'numeric',\n month: '2-digit',\n day: '2-digit',\n }).formatToParts(new Date());\n const values = Object.fromEntries(parts.map((part) => [part.type, part.value]));\n return `${values.year}-${values.month}-${values.day}`;\n}\n\nconst input = $json ?? {};\nconst runDate = input.run_date || dateInTimeZone('America/Santo_Domingo');\n\nreturn [{\n json: {\n run_date: runDate,\n source: input.source || 'schedule',\n },\n}];" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1248, 1552 ], "id": "43f9136f-ecb0-4145-978a-a212a532f271", "name": "Preparar ejecución" }, { "parameters": { "jsCode": "const body = $json.body ?? $json;\nconst action = String(body.action ?? '').trim().toLowerCase();\n\nif (!['run_now', 'run_reminders', 'ejecutar_recordatorios'].includes(action)) {\n return [];\n}\n\nreturn [{\n json: {\n run_date: body.run_date || body.date || '',\n source: 'webhook',\n },\n}];" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1248, 1824 ], "id": "c640bba7-a14c-4d8f-a204-d73dbb43f77e", "name": "Validar ejecución por Webhook" }, { "parameters": { "method": "POST", "url": "https://dbit.digitalcompass.agency/rest/v1/rpc/tax_due_reminders", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "apikey", "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q" }, { "name": "Authorization", "value": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q" }, { "name": "Content-Type", "value": "application/json" } ] }, "sendBody": true, "specifyBody": "json", "jsonBody": "={{ { p_run_date: $json.run_date } }}", "options": {} }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.4, "position": [ 1504, 1632 ], "id": "ff4bb447-340e-405a-8db7-a6ef0e7f8fe3", "name": "Supabase - Recordatorios pendientes" }, { "parameters": { "jsCode": "const htmlEscape = (value) => String(value ?? '')\n .replace(/&/g, '&')\n .replace(//g, '>')\n .replace(/\"/g, '"')\n .replace(/'/g, ''');\n\nconst rows = [];\nfor (const item of $input.all()) {\n const value = item.json;\n if (Array.isArray(value)) rows.push(...value);\n else if (Array.isArray(value?.data)) rows.push(...value.data);\n else if (Array.isArray(value?.body)) rows.push(...value.body);\n else if (value?.obligation_id) rows.push(value);\n}\n\nconst logoUrl = 'https://home.digitalcompass.agency/glm-logo-completo.png';\nconst appUrl = 'https://home.digitalcompass.agency/';\n\nconst formatDate = (iso) => {\n const [year, month, day] = String(iso).split('-').map(Number);\n return new Intl.DateTimeFormat('es-DO', {\n timeZone: 'UTC',\n day: '2-digit',\n month: 'long',\n year: 'numeric',\n }).format(new Date(Date.UTC(year, month - 1, day)));\n};\n\nreturn rows.map((row) => {\n const dueDate = formatDate(row.due_date);\n const isSameDay = row.alert_type === 'same_day';\n const reminderLabel = isSameDay ? 'Vence hoy' : 'Recordatorio previo';\n const categoryLabel = row.category === 'nomina' ? 'Nómina' : 'Administración';\n const firstName = String(row.contact_name ?? '').trim().split(/\\s+/)[0] || 'equipo';\n const subject = `${isSameDay ? 'Vence hoy' : 'Recordatorio'} · ${row.country_name} · ${row.description}`;\n\n const emailHtml = `\n\n\n \n \n \n
\n \n \n \n \n \n
\"GomezLee
 
\n
${htmlEscape(reminderLabel)}
\n

Seguimiento de Impuestos GLM

\n

Hola ${htmlEscape(firstName)}, este es el recordatorio correspondiente a la siguiente obligación tributaria:

\n \n \n \n \n \n
País${htmlEscape(row.country_name)}
Categoría${htmlEscape(categoryLabel)}
Fecha límite${htmlEscape(dueDate)}
Obligación${htmlEscape(row.description)}
\n

Este aviso forma parte del seguimiento automático: miércoles anterior y fecha límite, incluidos sábados y domingos.

\n \n
GOMEZLEE MARKETING  |  Seguimiento de Impuestos GLM  |  Uso interno
\n
\n \n`.trim();\n\n const googleChatText = [\n `*${reminderLabel.toUpperCase()} · SEGUIMIENTO DE IMPUESTOS GLM*`,\n '',\n `Hola ${firstName},`,\n '',\n `*País:* ${row.country_name}`,\n `*Categoría:* ${categoryLabel}`,\n `*Fecha límite:* ${dueDate}`,\n `*Obligación:* ${row.description}`,\n '',\n 'Seguimiento automático: miércoles anterior y fecha límite, incluidos sábados y domingos.',\n '',\n `Abrir calendario: ${appUrl}`,\n ].join('\\n');\n\n return {\n json: {\n ...row,\n email_subject: subject,\n email_html: emailHtml,\n google_chat_text: googleChatText,\n },\n };\n});" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1760, 1632 ], "id": "22e34460-2700-4d7d-ba0a-c4d735120f19", "name": "Construir mensajes GLM" }, { "parameters": { "jsCode": "return $input.all().filter((item) => item.json.email_enabled === true && String(item.json.contact_email ?? '').trim());" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 2032, 1488 ], "id": "3841ce9e-3957-43e9-b280-34414e0b9bd9", "name": "Preparar correos" }, { "parameters": { "sendTo": "={{ $json.contact_email }}", "subject": "={{ $json.email_subject }}", "message": "={{ $json.email_html }}", "options": { "appendAttribution": false }, "emailType": "html" }, "type": "n8n-nodes-base.gmail", "typeVersion": 2.1, "position": [ 2288, 1488 ], "id": "a4e97d3e-7181-48b6-815a-8c2e97bdff85", "name": "Gmail - Enviar recordatorio", "webhookId": "16bfebbd-f4e3-4b1e-aba2-028fecf98da1", "credentials": { "gmailOAuth2": { "id": "UDcO1FLJqA453V2D", "name": "Gmail account 3" } } }, { "parameters": { "method": "POST", "url": "https://dbit.digitalcompass.agency/rest/v1/rpc/tax_record_notification", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "apikey", "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q" }, { "name": "Authorization", "value": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q" }, { "name": "Content-Type", "value": "application/json" } ] }, "sendBody": true, "specifyBody": "json", "jsonBody": "={{ { p_obligation_id: $('Preparar correos').item.json.obligation_id, p_contact_id: $('Preparar correos').item.json.contact_id, p_alert_date: $('Preparar correos').item.json.alert_date, p_channel: 'email', p_status: 'sent', p_provider_response: $json } }}", "options": {} }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.4, "position": [ 2544, 1488 ], "id": "5059ca4d-7766-415f-8f38-ed217ed382e8", "name": "Supabase - Registrar correo" }, { "parameters": { "jsCode": "return $input.all().filter((item) => item.json.google_chat_enabled === true && String(item.json.contact_email ?? '').trim());" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 2032, 1760 ], "id": "37385e9d-9d97-4622-99a0-c10fe09a73d0", "name": "Preparar Google Chat" }, { "parameters": { "method": "POST", "url": "https://chat.googleapis.com/v1/spaces:setup", "authentication": "predefinedCredentialType", "nodeCredentialType": "googleChatOAuth2Api", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "Content-Type", "value": "application/json" } ] }, "sendBody": true, "specifyBody": "json", "jsonBody": "={{ { space: { spaceType: 'DIRECT_MESSAGE', singleUserBotDm: false }, memberships: [ { member: { name: 'users/' + $json.contact_email, type: 'HUMAN' } } ] } }}", "options": { "timeout": 30000 } }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.4, "position": [ 2288, 1760 ], "id": "588061f1-c2b8-4747-82ff-7532ca60bf48", "name": "Google Chat - Abrir DM", "retryOnFail": true, "maxTries": 3, "waitBetweenTries": 1500, "onError": "continueRegularOutput", "credentials": { "googleChatOAuth2Api": { "id": "REEMPLAZAR_CREDENCIAL_GOOGLE_CHAT", "name": "Google Chat GLM" } } }, { "parameters": { "jsCode": "const source = $('Preparar Google Chat').item.json;\nconst spaceName = String($json.name ?? '').trim();\nreturn [{ json: { ...source, chat_space_name: spaceName, chat_setup_ok: Boolean(spaceName), google_chat_status: spaceName ? 'pending' : 'failed', google_chat_provider_response: $json } }];" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 2544, 1760 ], "id": "8c8a7a41-b131-4b27-b936-aa6a0896f70c", "name": "Evaluar DM Google Chat" }, { "parameters": { "jsCode": "return $input.all().filter((item) => item.json.chat_setup_ok === true);" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 2800, 1680 ], "id": "dd09b31d-435e-404f-823d-5e9cf1de9dc9", "name": "Preparar envío Google Chat" }, { "parameters": { "jsCode": "return $input.all().filter((item) => item.json.chat_setup_ok !== true);" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 2800, 1904 ], "id": "31bcae02-e1d8-4c66-b2c3-cf1b16f46d2d", "name": "Preparar fallo DM Google Chat" }, { "parameters": { "method": "POST", "url": "={{ 'https://chat.googleapis.com/v1/' + $json.chat_space_name + '/messages' }}", "authentication": "predefinedCredentialType", "nodeCredentialType": "googleChatOAuth2Api", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "Content-Type", "value": "application/json" } ] }, "sendBody": true, "specifyBody": "json", "jsonBody": "={{ { text: $json.google_chat_text, createMessageNotificationOptions: { notificationType: 'NOTIFICATION_TYPE_FORCE_NOTIFY' } } }}", "options": { "timeout": 30000 } }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.4, "position": [ 3056, 1680 ], "id": "ef1c2ada-440a-4f30-a66f-dc12ca9eb1e6", "name": "Google Chat - Enviar recordatorio", "retryOnFail": true, "maxTries": 3, "waitBetweenTries": 1500, "onError": "continueRegularOutput", "credentials": { "googleChatOAuth2Api": { "id": "REEMPLAZAR_CREDENCIAL_GOOGLE_CHAT", "name": "Google Chat GLM" } } }, { "parameters": { "jsCode": "const source = $('Preparar envío Google Chat').item.json;\nconst sent = Boolean($json.name) && !$json.error;\nreturn [{ json: { ...source, google_chat_status: sent ? 'sent' : 'failed', google_chat_provider_response: $json } }];" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 3312, 1680 ], "id": "130517e7-25f6-42c0-90ba-e1a319626a54", "name": "Evaluar envío Google Chat" }, { "parameters": { "method": "POST", "url": "https://dbit.digitalcompass.agency/rest/v1/rpc/tax_record_notification", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "apikey", "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q" }, { "name": "Authorization", "value": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q" }, { "name": "Content-Type", "value": "application/json" } ] }, "sendBody": true, "specifyBody": "json", "jsonBody": "={{ { p_obligation_id: $json.obligation_id, p_contact_id: $json.contact_id, p_alert_date: $json.alert_date, p_channel: 'google_chat', p_status: $json.google_chat_status, p_provider_response: $json.google_chat_provider_response } }}", "options": {} }, "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.4, "position": [ 3568, 1760 ], "id": "8bc5e6dd-3c21-41e8-bfdc-d44155618460", "name": "Supabase - Registrar Google Chat" } ], "pinData": {}, "connections": { "Diario 8:00 AM": { "main": [ [ { "node": "Preparar ejecución", "type": "main", "index": 0 } ] ] }, "Ejecutar manualmente": { "main": [ [ { "node": "Preparar ejecución", "type": "main", "index": 0 } ] ] }, "Webhook seguimiento-impuestos": { "main": [ [ { "node": "Validar ejecución por Webhook", "type": "main", "index": 0 } ] ] }, "Preparar ejecución": { "main": [ [ { "node": "Supabase - Recordatorios pendientes", "type": "main", "index": 0 } ] ] }, "Validar ejecución por Webhook": { "main": [ [ { "node": "Preparar ejecución", "type": "main", "index": 0 } ] ] }, "Supabase - Recordatorios pendientes": { "main": [ [ { "node": "Construir mensajes GLM", "type": "main", "index": 0 } ] ] }, "Construir mensajes GLM": { "main": [ [ { "node": "Preparar correos", "type": "main", "index": 0 }, { "node": "Preparar Google Chat", "type": "main", "index": 0 } ] ] }, "Preparar correos": { "main": [ [ { "node": "Gmail - Enviar recordatorio", "type": "main", "index": 0 } ] ] }, "Gmail - Enviar recordatorio": { "main": [ [ { "node": "Supabase - Registrar correo", "type": "main", "index": 0 } ] ] }, "Preparar Google Chat": { "main": [ [ { "node": "Google Chat - Abrir DM", "type": "main", "index": 0 } ] ] }, "Google Chat - Abrir DM": { "main": [ [ { "node": "Evaluar DM Google Chat", "type": "main", "index": 0 } ] ] }, "Evaluar DM Google Chat": { "main": [ [ { "node": "Preparar envío Google Chat", "type": "main", "index": 0 }, { "node": "Preparar fallo DM Google Chat", "type": "main", "index": 0 } ] ] }, "Preparar envío Google Chat": { "main": [ [ { "node": "Google Chat - Enviar recordatorio", "type": "main", "index": 0 } ] ] }, "Google Chat - Enviar recordatorio": { "main": [ [ { "node": "Evaluar envío Google Chat", "type": "main", "index": 0 } ] ] }, "Evaluar envío Google Chat": { "main": [ [ { "node": "Supabase - Registrar Google Chat", "type": "main", "index": 0 } ] ] }, "Preparar fallo DM Google Chat": { "main": [ [ { "node": "Supabase - Registrar Google Chat", "type": "main", "index": 0 } ] ] } }, "active": false, "settings": { "executionOrder": "v1", "binaryMode": "separate", "availableInMCP": true, "timeSavedMode": "fixed", "errorWorkflow": "puF4LUczoSz3hcek", "timezone": "America/Santo_Domingo", "callerPolicy": "workflowsFromSameOwner" }, "meta": { "templateCredsSetupCompleted": true, "instanceId": "b4b77b17af092830e794eef639ce2f6d7daccf7eddc075060b03b3b6545aac70" }, "tags": [] }