Skip to content

Commit a6ea150

Browse files
committed
Refactor: Moved all functions to be per-file for scaleability and easier debugging
1 parent b6936f2 commit a6ea150

8 files changed

Lines changed: 661 additions & 619 deletions

File tree

main.js

Lines changed: 11 additions & 619 deletions
Large diffs are not rendered by default.

services/add-data.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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;

services/delete-data.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
const inquirer = require("inquirer");
2+
const Employee = require("../models/Employee");
3+
const EmployeeLog = require("../models/EmployeeLog");
4+
const DeletedEmployee = require("../models/DeletedEmployee");
5+
6+
7+
// HAPUS DATA KARYAWAN ============================================================================
8+
async function delete_data() {
9+
console.log("========== HAPUS DATA KARYAWAN ==========");
10+
11+
try {
12+
const { search_by } = await inquirer.prompt([
13+
{
14+
type: "list",
15+
name: "search_by",
16+
message: "Cari data berdasarkan:",
17+
choices: ["ID", "Nama"],
18+
},
19+
]);
20+
21+
let results = [];
22+
23+
if (search_by === "ID") {
24+
const { search_id } = await inquirer.prompt([
25+
{
26+
type: "input",
27+
name: "search_id",
28+
message: "Masukkan ID karyawan:",
29+
validate: (val) => (val.trim() ? true : "ID tidak boleh kosong!"),
30+
},
31+
]);
32+
33+
results = await Employee.find({
34+
ID: search_id.trim().toUpperCase(),
35+
});
36+
37+
} else {
38+
const { search_name } = await inquirer.prompt([
39+
{
40+
type: "input",
41+
name: "search_name",
42+
message: "Masukkan nama karyawan :",
43+
validate: (val) => (val.trim() ? true : "Nama tidak boleh kosong!"),
44+
},
45+
]);
46+
47+
results = await Employee.find({
48+
NAMA: { $regex: search_name.trim(), $options: "i" },
49+
});
50+
}
51+
52+
if (results.length === 0) {
53+
console.log("Data karyawan tidak ditemukan.");
54+
return;
55+
}
56+
57+
// PILIH DATA JIKA LEBIH DARI SATU HASIL ----------------------------
58+
let target;
59+
if (results.length > 1) {
60+
const { pilih } = await inquirer.prompt([
61+
{
62+
type: "list",
63+
name: "pilih",
64+
message: "Pilih data yang ingin dihapus:",
65+
choices: results.map(
66+
(k) => `${k.ID} | ${k.NAMA} | ${k.JABATAN} | ${k.TELP}`,
67+
),
68+
},
69+
]);
70+
71+
target = results.find((k) => k.ID === pilih);
72+
} else {
73+
target = results[0];
74+
}
75+
// ------------------------------------------------------------------
76+
77+
console.table([
78+
{
79+
ID: target.ID,
80+
NAMA: target.NAMA,
81+
JABATAN: target.JABATAN,
82+
TELP: target.TELP,
83+
},
84+
]);
85+
86+
// KONFIRMASI HAPUS -------------------------------------------------
87+
const { konfirmasi } = await inquirer.prompt([
88+
{
89+
type: "confirm",
90+
name: "konfirmasi",
91+
message: "Apakah anda yakin ingin menghapus data ini?",
92+
},
93+
]);
94+
95+
if (!konfirmasi) {
96+
console.log("Penghapusan dibatalkan.");
97+
return;
98+
}
99+
100+
// SIMPAN KE LOG ------------------------------------------
101+
try {
102+
await DeletedEmployee.create({
103+
ID: target.ID,
104+
NAMA: target.NAMA,
105+
JABATAN: target.JABATAN,
106+
TELP: target.TELP,
107+
});
108+
109+
await EmployeeLog.create({
110+
action: "DELETE",
111+
data_before: target,
112+
data_after: null,
113+
});
114+
115+
await Employee.deleteOne({ ID: target.ID });
116+
117+
await Employee.deleteOne({ ID: target.ID });
118+
119+
console.log("Data berhasil dihapus & disimpan ke log.");
120+
} catch (err) {
121+
console.error("Gagal proses delete + log:", err.message);
122+
}
123+
124+
console.log("Data karyawan berhasil dihapus.");
125+
} catch (err) {
126+
console.error("Terjadi kesalahan saat menghapus data:", err.message);
127+
}
128+
// --------------------------------------------------------------------
129+
}
130+
// ================================================================================================
131+
132+
module.exports = delete_data;

0 commit comments

Comments
 (0)