{
    "name": "Daily Absence Warnings (11 PM)",
    "nodes": [
        {
            "parameters": {
                "httpMethod": "POST",
                "path": "daily-warnings",
                "options": []
            },
            "id": "webhook-trigger-daily-warnings",
            "name": "Webhook Trigger",
            "type": "n8n-nodes-base.webhook",
            "typeVersion": 1,
            "position": [
                -1260,
                64
            ]
        },
        {
            "parameters": {
                "fieldToSplitOut": "body.candidates",
                "options": []
            },
            "id": "8ca5cf26-46e8-4013-913b-cbee48b1dbf7",
            "name": "Split Candidates",
            "type": "n8n-nodes-base.itemLists",
            "typeVersion": 3,
            "position": [
                -1080,
                64
            ]
        },
        {
            "parameters": {
                "operation": "copy",
                "fileId": {
                    "__rl": true,
                    "value": "19i5sUHlxJAAuLWX0SrBE1bDhUfb6DrsmGS4ZPxmPlbc",
                    "mode": "id"
                },
                "name": "={{ `${$json.student_name} - ${$json.warning_type} Warning - ${new Date().toISOString().split('T')[0]}` }}",
                "options": []
            },
            "id": "2f2b2009-f98e-426b-8e99-b91650ae53f6",
            "name": "Copy Warning Template",
            "type": "n8n-nodes-base.googleDrive",
            "typeVersion": 3,
            "position": [
                -920,
                64
            ],
            "executeOnce": false,
            "alwaysOutputData": false,
            "credentials": {
                "googleDriveOAuth2Api": {
                    "id": "gzM9fBeTzJafQgYL",
                    "name": "Google Drive account"
                }
            }
        },
        {
            "parameters": {
                "jsCode": "// نجمع كل عناصر هذه العقدة (نتيجة نسخ القالب، تحتوي فقط على id/name/mimeType الخاصة بالملف)\nconst copiedItems = $input.all();\n\n// نجيب العناصر الأصلية من عقدة \"Split Candidates\" قبل ما ينمسح الـ json بالكامل\nconst originalItems = $('Split Candidates').all();\n\nconst result = copiedItems.map((item, index) => {\n  const original = (originalItems[index] && originalItems[index].json) || {};\n\n  return {\n    json: {\n      // معرف ملف Google Docs المنسوخ - لازم يبقى موجود عشان عقدة Replace Document Placeholders والعقد اللي بعدها\n      id: item.json.id,\n\n      // كل الحقول الأصلية القادمة من الـ API، محفوظة كنص لضمان التوافق مع Google Docs replace\n      student_id: original.student_id ?? '',\n      group_id: original.group_id ?? '',\n      course_id: original.course_id ?? '',\n      subject_id: original.subject_id ?? '',\n      subject_name: String(original.subject_name ?? ''),\n      subjects_breakdown: String(original.subjects_breakdown ?? ''),\n      student_whatsapp: original.student_whatsapp ?? '',\n      guardian_phone: original.guardian_phone ?? '',\n      student_name: String(original.student_name ?? ''),\n      student_code: String(original.student_code ?? ''),\n      trigger_reason: String(original.trigger_reason ?? ''),\n      consecutive_days: String(original.consecutive_days ?? '0'),\n      total_absent_days: String(original.total_absent_days ?? '0'),\n      resulting_status: String(original.resulting_status ?? ''),\n      warning_type: String(original.warning_type ?? ''),\n      warning_type_ar: String(original.warning_type_ar ?? ''),\n      absence_percentage: String(original.absence_percentage ?? ''),\n      total_absences: String(original.total_absences ?? ''),\n      total_sessions: String(original.total_sessions ?? ''),\n      group_name: String(original.group_name ?? '')\n    },\n    // نربط كل عنصر ناتج بعنصره الأصلي المقابل بالترتيب، عشان أي عقدة تالية تستخدم\n    // $('Split Candidates').item.json... تجيب البيانات الصحيحة المطابقة لنفس الطالب\n    pairedItem: { item: index }\n  };\n});\n\nreturn result;"
            },
            "id": "831572f7-7f5c-4ddc-9cd7-07daa48ccf94",
            "name": "Prepare Fields",
            "type": "n8n-nodes-base.code",
            "typeVersion": 2,
            "position": [
                -760,
                64
            ]
        },
        {
            "parameters": {
                "operation": "update",
                "documentURL": "={{ `${$json.id}` }}",
                "actionsUi": {
                    "actionFields": [
                        {
                            "action": "replaceAll",
                            "text": "{{student_name}}",
                            "replaceText": "={{ $json.student_name }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{ student_name }}",
                            "replaceText": "={{ $json.student_name }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{warning_type}}",
                            "replaceText": "={{ $json.warning_type }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{ warning_type }}",
                            "replaceText": "={{ $json.warning_type }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{absence_percentage}}",
                            "replaceText": "={{ $json.absence_percentage }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{ absence_percentage }}",
                            "replaceText": "={{ $json.absence_percentage }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{total_absences}}",
                            "replaceText": "={{ $json.total_absences }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{ total_absences }}",
                            "replaceText": "={{ $json.total_absences }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{total_sessions}}",
                            "replaceText": "={{ $json.total_sessions }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{ total_sessions }}",
                            "replaceText": "={{ $json.total_sessions }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{group_name}}",
                            "replaceText": "={{ $json.group_name }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{ group_name }}",
                            "replaceText": "={{ $json.group_name }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{subjects_breakdown}}",
                            "replaceText": "={{ $json.subjects_breakdown }}"
                        },
                        {
                            "action": "replaceAll",
                            "text": "{{ subjects_breakdown }}",
                            "replaceText": "={{ $json.subjects_breakdown }}"
                        }
                    ]
                }
            },
            "id": "ad0a6de2-f209-442c-b0e3-f6fefddb1fd2",
            "name": "Replace Document Placeholders",
            "type": "n8n-nodes-base.googleDocs",
            "typeVersion": 2,
            "position": [
                -600,
                64
            ],
            "credentials": {
                "googleDocsOAuth2Api": {
                    "id": "TOJy6AZ1qb6Tqy0F",
                    "name": "Google Docs account"
                }
            }
        },
        {
            "parameters": {
                "operation": "share",
                "fileId": {
                    "__rl": true,
                    "value": "={{ $('Prepare Fields').item.json.id }}",
                    "mode": "id"
                },
                "permissionsUi": {
                    "permissionsValues": [
                        {
                            "role": "reader",
                            "type": "anyone"
                        }
                    ]
                }
            },
            "id": "1df440ae-75e8-4c3b-9ef6-ae4b75bf288e",
            "name": "Share Warning Doc",
            "type": "n8n-nodes-base.googleDrive",
            "typeVersion": 3,
            "position": [
                -460,
                64
            ],
            "credentials": {
                "googleDriveOAuth2Api": {
                    "id": "gzM9fBeTzJafQgYL",
                    "name": "Google Drive account"
                }
            }
        },
        {
            "parameters": {
                "method": "POST",
                "url": "https://albsaer.bzzix.com/api/automation/send-whatsapp",
                "authentication": "genericCredentialType",
                "genericAuthType": "httpHeaderAuth",
                "sendBody": true,
                "specifyBody": "json",
                "jsonBody": "={\n  \"phone\": \"{{ $('Prepare Fields').item.json.student_whatsapp }}\",\n  \"template\": \"student_warning_pdf\",\n  \"variables\": [\n    \"{{ $('Prepare Fields').item.json.student_name }}\",\n    \"{{ $('Prepare Fields').item.json.warning_type_ar }}\",\n    \"{{ $('Prepare Fields').item.json.absence_percentage }}\"\n  ],\n  \"document_url\": \"https://docs.google.com/document/d/{{ $('Prepare Fields').item.json.id }}/export?format=pdf\",\n  \"document_name\": \"إنذار غياب - {{ $('Prepare Fields').item.json.student_name }}.pdf\"\n}",
                "options": []
            },
            "id": "4ef23102-c1b6-4efc-ab5e-2ecda351ffe9",
            "name": "Send WhatsApp (Respond.io)",
            "type": "n8n-nodes-base.httpRequest",
            "typeVersion": 4.1,
            "position": [
                -320,
                64
            ],
            "credentials": {
                "httpHeaderAuth": {
                    "id": "LaravelTokenCredential",
                    "name": "Laravel API Token"
                }
            },
            "onError": "continue"
        },
        {
            "parameters": {
                "conditions": {
                    "options": {
                        "caseSensitive": true,
                        "leftValue": "",
                        "typeValidation": "strict",
                        "version": 1
                    },
                    "conditions": [
                        {
                            "id": "check-whatsapp-status",
                            "leftValue": "={{ $json.statusCode }}",
                            "rightValue": 300,
                            "operator": {
                                "type": "number",
                                "operation": "lt"
                            }
                        }
                    ],
                    "combinator": "and"
                },
                "options": []
            },
            "id": "79e37812-b78e-46ca-a4b8-db9d5d1b791f",
            "name": "Was WhatsApp Sent?",
            "type": "n8n-nodes-base.if",
            "typeVersion": 2,
            "position": [
                -160,
                64
            ]
        },
        {
            "parameters": {
                "method": "POST",
                "url": "https://albsaer.bzzix.com/api/automation/record-warning",
                "authentication": "genericCredentialType",
                "genericAuthType": "httpHeaderAuth",
                "sendBody": true,
                "bodyParameters": {
                    "parameters": [
                        {
                            "name": "student_id",
                            "value": "={{ $('Prepare Fields').item.json.student_id }}"
                        },
                        {
                            "name": "group_id",
                            "value": "={{ $('Prepare Fields').item.json.group_id }}"
                        },
                        {
                            "name": "course_id",
                            "value": "={{ $('Prepare Fields').item.json.course_id }}"
                        },
                        {
                            "name": "subject_id",
                            "value": "={{ $('Prepare Fields').item.json.subject_id }}"
                        },
                        {
                            "name": "warning_type",
                            "value": "={{ $('Prepare Fields').item.json.warning_type }}"
                        },
                        {
                            "name": "absence_percentage",
                            "value": "={{ $('Prepare Fields').item.json.absence_percentage }}"
                        },
                        {
                            "name": "total_absences",
                            "value": "={{ $('Prepare Fields').item.json.total_absences }}"
                        },
                        {
                            "name": "total_sessions",
                            "value": "={{ $('Prepare Fields').item.json.total_sessions }}"
                        },
                        {
                            "name": "pdf_path",
                            "value": "={{ $('Prepare Fields').item.json.id ?? '' }}"
                        },
                        {
                            "name": "trigger_reason",
                            "value": "={{ $('Prepare Fields').item.json.trigger_reason }}"
                        },
                        {
                            "name": "consecutive_days",
                            "value": "={{ $('Prepare Fields').item.json.consecutive_days }}"
                        },
                        {
                            "name": "total_absent_days",
                            "value": "={{ $('Prepare Fields').item.json.total_absent_days }}"
                        },
                        {
                            "name": "resulting_status",
                            "value": "={{ $('Prepare Fields').item.json.resulting_status }}"
                        }
                    ]
                },
                "options": []
            },
            "id": "c363d27d-2010-4e2b-8e92-e2e957d0a233",
            "name": "Record Warning in DB",
            "type": "n8n-nodes-base.httpRequest",
            "typeVersion": 4.1,
            "position": [
                0,
                0
            ]
        },
        {
            "parameters": {
                "options": []
            },
            "id": "d6ed562d-2ed4-4ca6-a789-304ea878449a",
            "name": "Log WhatsApp Failure",
            "type": "n8n-nodes-base.set",
            "typeVersion": 3.3,
            "position": [
                0,
                160
            ]
        },
        {
            "parameters": {
                "method": "POST",
                "url": "https://albsaer.bzzix.com/api/automation/send-whatsapp",
                "authentication": "genericCredentialType",
                "genericAuthType": "httpHeaderAuth",
                "sendBody": true,
                "specifyBody": "json",
                "jsonBody": "={\n  \"phone\": \"{{ $('Prepare Fields').item.json.guardian_phone }}\",\n  \"template\": \"parent_warning_pdf\",\n  \"variables\": [\n    \"{{ $('Prepare Fields').item.json.student_name }}\",\n    \"{{ $('Prepare Fields').item.json.warning_type_ar }}\",\n    \"{{ $('Prepare Fields').item.json.absence_percentage }}\"\n  ],\n  \"document_url\": \"https://docs.google.com/document/d/{{ $('Prepare Fields').item.json.id }}/export?format=pdf\",\n  \"document_name\": \"إنذار غياب - ولي أمر {{ $('Prepare Fields').item.json.student_name }}.pdf\"\n}",
                "options": []
            },
            "id": "guardian-whatsapp-node-id",
            "name": "Send WhatsApp to Guardian (Respond.io)",
            "type": "n8n-nodes-base.httpRequest",
            "typeVersion": 4.1,
            "position": [
                -320,
                220
            ],
            "credentials": {
                "httpHeaderAuth": {
                    "id": "LaravelTokenCredential",
                    "name": "Laravel API Token"
                }
            },
            "onError": "continue"
        }
    ],
    "pinData": [],
    "connections": {
        "Webhook Trigger": {
            "main": [
                [
                    {
                        "node": "Split Candidates",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Split Candidates": {
            "main": [
                [
                    {
                        "node": "Copy Warning Template",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Copy Warning Template": {
            "main": [
                [
                    {
                        "node": "Prepare Fields",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Prepare Fields": {
            "main": [
                [
                    {
                        "node": "Replace Document Placeholders",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Replace Document Placeholders": {
            "main": [
                [
                    {
                        "node": "Share Warning Doc",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Share Warning Doc": {
            "main": [
                [
                    {
                        "node": "Send WhatsApp (Respond.io)",
                        "type": "main",
                        "index": 0
                    },
                    {
                        "node": "Send WhatsApp to Guardian (Respond.io)",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Send WhatsApp (Respond.io)": {
            "main": [
                [
                    {
                        "node": "Was WhatsApp Sent?",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Was WhatsApp Sent?": {
            "main": [
                [
                    {
                        "node": "Record Warning in DB",
                        "type": "main",
                        "index": 0
                    }
                ],
                [
                    {
                        "node": "Log WhatsApp Failure",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        }
    },
    "active": false,
    "settings": {
        "executionOrder": "v1",
        "binaryMode": "separate"
    },
    "versionId": "219a6db7-5b84-4004-951b-913d475c2457",
    "meta": {
        "templateCredsSetupCompleted": true,
        "instanceId": "d988638097d4f2b3009aca10f6d842df82c82fdc4acdca301af78e311eddde7d"
    },
    "nodeGroups": [],
    "id": "jCndTQ3AqqsAEDyR",
    "tags": [
        {
            "updatedAt": "2026-07-02T16:23:36.607Z",
            "createdAt": "2026-07-02T16:23:36.607Z",
            "id": "81Joz5yDoVIq3BdN",
            "name": "daily"
        },
        {
            "updatedAt": "2026-07-02T16:23:36.570Z",
            "createdAt": "2026-07-02T16:23:36.570Z",
            "id": "Fa8vNuyiebtCI7vr",
            "name": "warnings"
        }
    ]
}