|
| 1 | +const inquirer = require("inquirer"); |
| 2 | +const Employee = require("../models/Employee"); |
| 3 | +const EmployeeLog = require("../models/EmployeeLog"); |
| 4 | + |
| 5 | + |
| 6 | +// TAMBAH DATA BARU =============================================================================== |
| 7 | +async function tambah_data() { |
| 8 | + console.log("========== TAMBAH DATA BARU =========="); |
| 9 | + |
| 10 | + try { |
| 11 | + const { count: count_raw } = await inquirer.prompt([ |
| 12 | + { |
| 13 | + type: "input", |
| 14 | + name: "count", |
| 15 | + message: |
| 16 | + "Masukkan jumlah data yang akan diinput. [MASUKKAN 0 UNTUK BATAL] : ", |
| 17 | + validate: (val) => { |
| 18 | + const n = parseInt(String(val).trim(), 10); |
| 19 | + if (Number.isNaN(n) || n < 0) { |
| 20 | + return "Masukkan angka >= 0"; |
| 21 | + } |
| 22 | + return true; |
| 23 | + }, |
| 24 | + }, |
| 25 | + ]); |
| 26 | + |
| 27 | + const count = parseInt(String(count_raw).trim(), 10); |
| 28 | + |
| 29 | + // BATAL JIKA INPUT = 0 ---------------- |
| 30 | + if (count === 0) { |
| 31 | + console.log("Input data dibatalkan."); |
| 32 | + return; |
| 33 | + } |
| 34 | + // ------------------------------------- |
| 35 | + |
| 36 | + let new_data = []; |
| 37 | + |
| 38 | + for (let i = 0; i < count; i++) { |
| 39 | + console.log(`\nInput data karyawan ke-${i + 1}:`); |
| 40 | + |
| 41 | + const { ID, NAMA, JABATAN, TELP } = await inquirer.prompt([ |
| 42 | + { |
| 43 | + type: "input", |
| 44 | + name: "ID", |
| 45 | + message: "Masukkan ID karyawan :", |
| 46 | + validate: async (val) => { |
| 47 | + const input = val.trim().toUpperCase(); |
| 48 | + |
| 49 | + if (!input) { |
| 50 | + return "ID tidak boleh kosong!"; |
| 51 | + } |
| 52 | + |
| 53 | + if (!/^[A-Za-z0-9]+$/.test(val)) { |
| 54 | + return "ID hanya boleh huruf & angka!"; |
| 55 | + } |
| 56 | + |
| 57 | + const existsDB = await Employee.findOne({ ID: input }); |
| 58 | + if (existsDB) { |
| 59 | + return "ID sudah digunakan di database!"; |
| 60 | + } |
| 61 | + |
| 62 | + const existsLocal = new_data.some((k) => k.ID === input); |
| 63 | + if (existsLocal) { |
| 64 | + return "ID sudah digunakan di input ini!"; |
| 65 | + } |
| 66 | + |
| 67 | + return true; |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + type: "input", |
| 72 | + name: "NAMA", |
| 73 | + message: "Masukkan nama karyawan :", |
| 74 | + validate: (val) => { |
| 75 | + return val.trim() ? true : "Nama tidak boleh kosong!"; |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + type: "input", |
| 80 | + name: "JABATAN", |
| 81 | + message: "Masukkan jabatan karyawan :", |
| 82 | + validate: (val) => { |
| 83 | + return val.trim() ? true : "Jabatan tidak boleh kosong!"; |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + type: "input", |
| 88 | + name: "TELP", |
| 89 | + message: "Masukkan no telp karyawan :", |
| 90 | + validate: (val) => { |
| 91 | + if (!val.trim()) { |
| 92 | + return "Nomor telepon tidak boleh kosong!"; |
| 93 | + } |
| 94 | + if (!/^[0-9]+$/.test(val)) { |
| 95 | + return "Nomor telepon hanya boleh angka!"; |
| 96 | + } |
| 97 | + return true; |
| 98 | + }, |
| 99 | + }, |
| 100 | + ]); |
| 101 | + |
| 102 | + new_data.push({ |
| 103 | + ID: ID.trim().toUpperCase(), |
| 104 | + NAMA: NAMA.trim(), |
| 105 | + JABATAN: JABATAN.trim(), |
| 106 | + TELP: TELP.trim(), |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + // KONFIRMASI SIMPAN --------------------------------------------------------- |
| 111 | + const { save_confirm } = await inquirer.prompt([ |
| 112 | + { |
| 113 | + type: "confirm", |
| 114 | + name: "save_confirm", |
| 115 | + message: `\nApakah anda yakin ingin menyimpan ${new_data.length} data ini?`, |
| 116 | + }, |
| 117 | + ]); |
| 118 | + |
| 119 | + if (!save_confirm) { |
| 120 | + console.log("Data batal disimpan."); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + try { |
| 125 | + await Employee.insertMany(new_data); |
| 126 | + console.log(`${new_data.length} data berhasil disimpan ke database.`); |
| 127 | + } catch (err) { |
| 128 | + console.error("Gagal menyimpan ke database:", err.message); |
| 129 | + } |
| 130 | + |
| 131 | + await Employee.insertMany(new_data); |
| 132 | + |
| 133 | + for (const item of new_data) { |
| 134 | + await EmployeeLog.create({ |
| 135 | + action: "CREATE", |
| 136 | + data_before: null, |
| 137 | + data_after: item, |
| 138 | + }); |
| 139 | + } |
| 140 | + } catch (err) { |
| 141 | + console.error("Terjadi kesalahan saat menambahkan data:", err.message); |
| 142 | + } |
| 143 | + // ----------------------------------------------------------------------------- |
| 144 | +} |
| 145 | +// ================================================================================================ |
| 146 | + |
| 147 | +module.exports = tambah_data; |
0 commit comments