All files / src seeder.ts

0% Statements 0/20
100% Branches 0/0
0% Functions 0/2
0% Lines 0/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                                                                                                                                                                                                                                                           
import { DataSource } from 'typeorm';
import * as bcrypt from 'bcryptjs';
import * as dotenv from 'dotenv';
import { User } from './entities/user.entity';
import { Barang } from './entities/barang.entity';
import ormconfig from '../ormconfig';
 
dotenv.config();
 
async function seed() {
  const dataSource = await ormconfig.initialize();
 
  // Hapus data lama (urutan: detail_permintaan → permintaan → barang → users)
  // Reset all tables and identity (auto increment)
  await dataSource.query(`
    TRUNCATE TABLE "detail_permintaan", "permintaan", "barang", "users" RESTART IDENTITY CASCADE
  `);
 
  // User Seeder
  const userRepo = dataSource.getRepository(User);
 
  // Admin
  await userRepo.save(
    userRepo.create({
      nama: 'Admin SIAP',
      username: 'admin',
      password: await bcrypt.hash('admin123', 10),
      role: 'admin',
      unit_kerja: 'Kepala Kantor',
      status_aktif: true,
      foto: 'https://ui-avatars.com/api/?name=Admin+SIAP',
    }),
  );
 
  // Pegawai
  const pegawaiList = [
    {
      nama: 'Budi Santoso',
      username: 'budi',
      password: await bcrypt.hash('budi123', 10),
      role: 'pegawai' as const,
      unit_kerja: 'Statistik Produksi',
      status_aktif: true,
      foto: 'https://ui-avatars.com/api/?name=Budi+Santoso',
    },
    {
      nama: 'Siti Aminah',
      username: 'siti',
      password: await bcrypt.hash('siti123', 10),
      role: 'pegawai' as const,
      unit_kerja: 'Statistik Sosial',
      status_aktif: true,
      foto: 'https://ui-avatars.com/api/?name=Siti+Aminah',
    },
    {
      nama: 'Rudi Hartono',
      username: 'rudi',
      password: await bcrypt.hash('rudi123', 10),
      role: 'pegawai' as const,
      unit_kerja: 'Bagian Umum',
      status_aktif: true,
      foto: 'https://ui-avatars.com/api/?name=Rudi+Hartono',
    },
  ];
  await userRepo.save(userRepo.create(pegawaiList));
 
  // Barang Seeder
  const barangRepo = dataSource.getRepository(Barang);
  const barangList = [
    {
      kode_barang: 'BRG001',
      nama_barang: 'Kertas A4 80gsm',
      deskripsi: 'Kertas HVS ukuran A4, 80gsm, putih',
      satuan: 'rim',
      stok: 50,
      ambang_batas_kritis: 10,
      status_aktif: true,
    },
    {
      kode_barang: 'BRG002',
      nama_barang: 'Spidol Whiteboard',
      deskripsi: 'Spidol untuk papan tulis putih',
      satuan: 'pcs',
      stok: 20,
      ambang_batas_kritis: 5,
      status_aktif: true,
    },
    {
      kode_barang: 'BRG003',
      nama_barang: 'Toner Printer HP',
      deskripsi: 'Toner printer HP LaserJet',
      satuan: 'pcs',
      stok: 5,
      ambang_batas_kritis: 2,
      status_aktif: true,
    },
    {
      kode_barang: 'BRG004',
      nama_barang: 'Map Folder Plastik',
      deskripsi: 'Map plastik untuk dokumen',
      satuan: 'box',
      stok: 15,
      ambang_batas_kritis: 3,
      status_aktif: true,
    },
    {
      kode_barang: 'KRITIS01',
      nama_barang: 'Pulpen Biru',
      deskripsi: 'Pulpen tinta biru',
      satuan: 'box',
      stok: 1,
      ambang_batas_kritis: 5,
      status_aktif: true,
    },
  ];
  await barangRepo.save(barangRepo.create(barangList));
 
  await dataSource.destroy();
  console.log('Seeding selesai');
}
 
seed().catch((err) => {
  console.error('Seeding gagal:', err);
  process.exit(1);
});