{
  "name": "Workflow-Pengajuan",
  "nodes": [
    {
      "parameters": {
        "formTitle": "Formulir Pengajuan Cuti",
        "formDescription": "Silahkan isi detail cuti Anda. Pastikan tanggal sesuai.",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Email Karyawan",
              "fieldName": "Email Karyawan ",
              "placeholder": "Contoh: annisa123@gmail.com",
              "requiredField": true
            },
            {
              "fieldLabel": "Jenis Cuti",
              "fieldType": "dropdown",
              "fieldName": "Jenis Cuti",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Cuti Tahunan"
                  },
                  {
                    "option": "Cuti Sakit"
                  },
                  {
                    "option": "Cuti Tanpa Bayaran"
                  }
                ]
              },
              "requiredField": true
            },
            {
              "fieldLabel": "Tanggal Mulai",
              "fieldType": "date",
              "fieldName": "Tanggal Mulai",
              "requiredField": true
            },
            {
              "fieldLabel": "Tanggal Selesai",
              "fieldType": "date",
              "fieldName": "Tanggal Selesai",
              "requiredField": true
            },
            {
              "fieldLabel": "Alasan/Keterangan",
              "fieldName": "Alasan/Keterangan",
              "requiredField": true
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 2.5,
      "position": [
        0,
        0
      ],
      "id": "5cb100d4-76ee-4acf-9fdd-e3e29ec449f2",
      "name": "Formulir pengajuan cuti"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "53457812-724e-44df-b530-41fd9353e5cb",
              "leftValue": "={{ $('Validasi Cuti').item.json.isApprovedSystem }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        832,
        0
      ],
      "id": "6eebe815-bbb0-4e99-b750-02c874b43f24",
      "name": "If"
    },
    {
      "parameters": {
        "sendTo": "={{ $('Formulir pengajuan cuti').item.json['Email Karyawan '] }}",
        "subject": "Pengajuan Cuti Ditolak - Saldo Tidak Cukup",
        "message": "=Halo ,{{ $('Validasi Cuti').item.json.Nama_Lengkap }}  Pengajuan cuti Anda untuk tanggal {{ $('Validasi Cuti').item.json['Tanggal Mulai'] }} s/d {{ $('Validasi Cuti').item.json['Tanggal Selesai'] }} DITOLAK secara otomatis oleh sistem.  \n\nAlasan: Sisa saldo cuti Anda ({{ $json.Saldo_Cuti }} hari) tidak mencukupi untuk pengajuan {{ $('Validasi Cuti').item.json.durationRequested }} hari.  \n\nSalam, HR Bot",
        "options": {}
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        1040,
        96
      ],
      "id": "47234702-685c-43ea-a644-94b29cfe00a6",
      "name": "Email cuti tidak cukup",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "sendTo": "={{ $json.Email_Karyawan }}",
        "subject": "=Permohonan  {{ $('Validasi Cuti').item.json['Jenis Cuti'] }}",
        "message": "=```html\n<p>Halo Manager,</p>\n\n<p>Ada pengajuan cuti baru yang membutuhkan persetujuan Anda:</p>\n\n<ul>\n  <li><strong>Nama:</strong> {{ $('Validasi Cuti').item.json.Nama_Lengkap }}</li>\n  <li><strong>Email:</strong> {{ $('Validasi Cuti').item.json.Email }}</li>\n  <li><strong>Tanggal:</strong> {{ $('Validasi Cuti').item.json['Tanggal Mulai'] }} s.d {{ $('Validasi Cuti').item.json['Tanggal Selesai'] }}</li>\n  <li><strong>Total Durasi:</strong> {{ $('Validasi Cuti').item.json.durationRequested }} Hari</li>\n  <li><strong>Alasan:</strong> {{ $('Validasi Cuti').item.json['Alasan/Keterangan'] }}</li>\n</ul>\n\n<p>Silakan klik keputusan Anda:</p>\n\n<p>\n  <a href=\"https://n8n-ethckhqlooro.jkt3.sumopod.my.id/webhook-test/approval-handler?status=approved&email={{ $('Validasi Cuti').item.json.Email }}&days={{ $('Validasi Cuti').item.json.durationRequested }}&id={{ $('Validasi Cuti').item.json.requestId }}\"\n     style=\"background-color: #4CAF50; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; font-weight: bold; margin-right: 15px; display: inline-block;\">\n     \u2705 SETUJUI\n  </a>\n\n  <a href=\"https://n8n-ethckhqlooro.jkt3.sumopod.my.id/webhook-test/approval-handler?status=rejected&email={{ $('Validasi Cuti').item.json.Email }}&days={{ $('Validasi Cuti').item.json.durationRequested }}&id={{ $('Validasi Cuti').item.json.requestId }}\"\n     style=\"background-color: #f44336; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; font-weight: bold; display: inline-block;\">\n     \u2716\ufe0f TOLAK\n  </a>\n</p>\n```\n",
        "options": {}
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        1040,
        -96
      ],
      "id": "d53db8b7-747c-427c-9748-90c9577078c1",
      "name": "Email ke manager",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// 1. Data dari Google Sheet\nconst employeeData = items[0].json;\n\n// 2. Data dari Form\nconst formData = $('Formulir pengajuan cuti').first().json;\n\n// 3. Tanggal\nconst start = DateTime.fromISO(formData[\"Tanggal Mulai\"]);\nconst end = DateTime.fromISO(formData[\"Tanggal Selesai\"]);\n\n// 4. Hitung durasi\nconst durationInDays = end.diff(start, 'days').days + 1;\n\n// 5. Validasi saldo\nconst currentBalance = parseInt(employeeData[\"Saldo_Cuti\"]);\nconst hasEnoughBalance = currentBalance >= durationInDays;\n\n// 6. Generate ID\nconst uniqueId = 'REQ-' + Date.now();\n\n// 7. Return data\nreturn [\n  {\n    json: {\n      ...employeeData,\n      ...formData,\n\n      \"Email Karyawan\": employeeData.Email,\n\n      durationRequested: durationInDays,\n      isApprovedSystem: hasEnoughBalance,\n      message: hasEnoughBalance ? \"Saldo Cukup\" : \"Saldo Tidak Cukup\",\n      requestId: uniqueId\n    }\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        416,
        0
      ],
      "id": "9b01612e-cfeb-4aab-b31d-de2a31976872",
      "name": "Validasi Cuti"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1RiI-7VD4dRdSTy9rfeuWRWMNLo2_T6d_kZReSHDfjXw",
          "mode": "list",
          "cachedResultName": "Cuti Karyawan",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1RiI-7VD4dRdSTy9rfeuWRWMNLo2_T6d_kZReSHDfjXw/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "DB_Karyawan",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1RiI-7VD4dRdSTy9rfeuWRWMNLo2_T6d_kZReSHDfjXw/edit#gid=0"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        208,
        0
      ],
      "id": "4a227146-d47d-4612-a90d-5b583bc59bb6",
      "name": "Baca Sheet DB_Karyawan",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "1RiI-7VD4dRdSTy9rfeuWRWMNLo2_T6d_kZReSHDfjXw",
          "mode": "list",
          "cachedResultName": "Cuti Karyawan",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1RiI-7VD4dRdSTy9rfeuWRWMNLo2_T6d_kZReSHDfjXw/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": 1732935521,
          "mode": "list",
          "cachedResultName": "Log Pengajuan",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1RiI-7VD4dRdSTy9rfeuWRWMNLo2_T6d_kZReSHDfjXw/edit#gid=1732935521"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "ID_Request": "={{ $json.requestId }}",
            "Timestamp": "={{ $json.submittedAt }}",
            "Email_Karyawan": "={{ $json['Email Karyawan '] }}",
            "Jenis_Cuti": "={{ $json['Jenis Cuti'] }}",
            "Tanggal_Mulai": "={{ $json['Tanggal Mulai'] }}",
            "Tanggal_Selesai": "={{ $json['Tanggal Selesai'] }}",
            "Durasi": "={{ $json.durationRequested }}",
            "Alasan": "={{ $json['Alasan/Keterangan'] }}",
            "Status": "Pending"
          },
          "matchingColumns": [
            "ID_Request"
          ],
          "schema": [
            {
              "id": "ID_Request",
              "displayName": "ID_Request",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Timestamp",
              "displayName": "Timestamp",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Email_Karyawan",
              "displayName": "Email_Karyawan",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Jenis_Cuti",
              "displayName": "Jenis_Cuti",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Tanggal_Mulai",
              "displayName": "Tanggal_Mulai",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Tanggal_Selesai",
              "displayName": "Tanggal_Selesai",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Durasi",
              "displayName": "Durasi",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Alasan",
              "displayName": "Alasan",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        624,
        0
      ],
      "id": "2dce6459-d356-4a84-8dc8-a13a8389c7b9",
      "name": "Update Sheet Log",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Formulir pengajuan cuti": {
      "main": [
        [
          {
            "node": "Baca Sheet DB_Karyawan",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Email ke manager",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Email cuti tidak cukup",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validasi Cuti": {
      "main": [
        [
          {
            "node": "Update Sheet Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Baca Sheet DB_Karyawan": {
      "main": [
        [
          {
            "node": "Validasi Cuti",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Sheet Log": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate"
  },
  "versionId": "7f6cb8b0-e653-4054-8d58-83c84a96e131",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "Lrn1IA5D08zhmLC1",
  "tags": []
}