Spaces:
Sleeping
Sleeping
| import { Request, Response, NextFunction } from 'express'; | |
| import { PenggunaService } from '../services/pengguna.service'; | |
| import { sukses } from '../utils/response'; | |
| import { AuthenticatedRequest } from '../middlewares/auth.middleware'; | |
| export class PenggunaController { | |
| private static formatUser(user: any) { | |
| const { password_hash, ...rest } = user; | |
| return rest; | |
| } | |
| static async list(req: AuthenticatedRequest, res: Response, next: NextFunction) { | |
| try { | |
| const skip = parseInt(req.query.skip as string) || 0; | |
| const limit = parseInt(req.query.limit as string) || 10; | |
| const aktif = req.query.aktif !== undefined ? parseInt(req.query.aktif as string) : undefined; | |
| const users = await PenggunaService.getUsers(skip, limit, aktif); | |
| const data = users.map((u: any) => PenggunaController.formatUser(u)); | |
| return sukses(res, data, 'Daftar pengguna berhasil dimuat'); | |
| } catch (error) { | |
| next(error); | |
| } | |
| } | |
| static async get(req: AuthenticatedRequest, res: Response, next: NextFunction) { | |
| try { | |
| const userId = parseInt(req.params.id); | |
| const user = await PenggunaService.getUserById(userId); | |
| const data = PenggunaController.formatUser(user); | |
| return sukses(res, data, 'Detail pengguna berhasil dimuat'); | |
| } catch (error) { | |
| next(error); | |
| } | |
| } | |
| static async create(req: AuthenticatedRequest, res: Response, next: NextFunction) { | |
| try { | |
| const actorId = req.user.id; | |
| const ip = (req.ip || req.socket.remoteAddress || 'unknown').replace('::ffff:', ''); | |
| const userAgent = (req.headers['user-agent'] || 'unknown') as string; | |
| const user = await PenggunaService.create(req.body, actorId, ip, userAgent); | |
| const data = PenggunaController.formatUser(user); | |
| return sukses(res, data, 'Pengguna berhasil dibuat', null, 200); | |
| } catch (error) { | |
| next(error); | |
| } | |
| } | |
| static async update(req: AuthenticatedRequest, res: Response, next: NextFunction) { | |
| try { | |
| const userId = parseInt(req.params.id); | |
| const actorId = req.user.id; | |
| const ip = (req.ip || req.socket.remoteAddress || 'unknown').replace('::ffff:', ''); | |
| const userAgent = (req.headers['user-agent'] || 'unknown') as string; | |
| const user = await PenggunaService.update(userId, req.body, actorId, ip, userAgent); | |
| const data = PenggunaController.formatUser(user); | |
| return sukses(res, data, 'Pengguna berhasil diperbarui'); | |
| } catch (error) { | |
| next(error); | |
| } | |
| } | |
| static async delete(req: AuthenticatedRequest, res: Response, next: NextFunction) { | |
| try { | |
| const userId = parseInt(req.params.id); | |
| const actorId = req.user.id; | |
| const ip = (req.ip || req.socket.remoteAddress || 'unknown').replace('::ffff:', ''); | |
| const userAgent = (req.headers['user-agent'] || 'unknown') as string; | |
| await PenggunaService.delete(userId, actorId, ip, userAgent); | |
| return sukses(res, null, 'Pengguna berhasil dinonaktifkan'); | |
| } catch (error) { | |
| next(error); | |
| } | |
| } | |
| } | |