From 73b4abfd68a6b8f24a4e35dac4754ea3c84d84ec Mon Sep 17 00:00:00 2001 From: mhmmdafif Date: Thu, 4 Dec 2025 14:30:57 +0700 Subject: [PATCH] repair: notification error sparepart --- db/notification_error_sparepart.db.js | 55 ++++++++----- .../notification_error_sparepart.service.js | 81 ++++++++----------- 2 files changed, 69 insertions(+), 67 deletions(-) diff --git a/db/notification_error_sparepart.db.js b/db/notification_error_sparepart.db.js index 17b9288..45af6dc 100644 --- a/db/notification_error_sparepart.db.js +++ b/db/notification_error_sparepart.db.js @@ -11,10 +11,10 @@ const getAllNotificationErrorSparepartDb = async (searchParams = {}) => { const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike( [ "a.brand_sparepart_id", - "a.contact_id", + "a.device_id", + "a.sparepart_id", "b.sparepart_name", - "d.contact_name", - "d.contact_type" + "d.device_name", ], searchParams.criteria, queryParams @@ -25,11 +25,10 @@ const getAllNotificationErrorSparepartDb = async (searchParams = {}) => { const { whereConditions, whereParamAnd } = pool.buildFilterQuery( [ { column: "a.brand_sparepart_id", param: searchParams.name, type: "int" }, - { column: "a.contact_id", param: searchParams.code, type: "int" }, + { column: "a.device_id", param: searchParams.code, type: "int" }, { column: "a.unit", param: searchParams.unit, type: "string" }, { column: "b.sparepart_name", param: searchParams.device, type: "string" }, - { column: "d.contact_name", param: searchParams.device, type: "string" }, - { column: "d.contact_type", param: searchParams.device, type: "string" }, + { column: "d.device_name", param: searchParams.device, type: "string" }, ], queryParams ); @@ -41,14 +40,23 @@ const getAllNotificationErrorSparepartDb = async (searchParams = {}) => { COUNT(*) OVER() AS total_data, a.*, b.sparepart_name, - b.brand_sparepart_description, - d.contact_name, - d.contact_type + b.sparepart_foto, + b.sparepart_stok, + b.sparepart_qty, + b.sparepart_description, + b.sparepart_model, + b.sparepart_merk, + b.sparepart_unit, + b.sparepart_item_type, + d.device_name + FROM notification_error_sparepart a - LEFT JOIN brand_sparepart b ON a.brand_sparepart_id = b.brand_sparepart_id + LEFT JOIN brand_sparepart c ON a.brand_sparepart_id = c.brand_sparepart_id - LEFT JOIN contact d on a.contact_id = d.contact_id + LEFT JOIN m_sparepart b ON c.sparepart_id = b.sparepart_id + + LEFT JOIN m_device d on c.device_id = d.device_id WHERE a.deleted_at IS NULL ${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""} @@ -69,15 +77,26 @@ const getAllNotificationErrorSparepartDb = async (searchParams = {}) => { const getNotificationErrorSparepartByIdDb = async (id) => { const queryText = ` - SELECT - a.*, + a.*, b.sparepart_name, - b.brand_sparepart_description, - d.contact_name, - d.contact_type + b.sparepart_foto, + b.sparepart_stok, + b.sparepart_qty, + b.sparepart_description, + b.sparepart_model, + b.sparepart_merk, + b.sparepart_unit, + b.sparepart_item_type, + d.device_name + FROM notification_error_sparepart a - LEFT JOIN brand_sparepart b ON a.brand_sparepart_id = b.brand_sparepart_id - LEFT JOIN contact d on a.contact_id = d.contact_id + + LEFT JOIN brand_sparepart c ON a.brand_sparepart_id = c.brand_sparepart_id + + LEFT JOIN m_sparepart b ON c.sparepart_id = b.sparepart_id + + LEFT JOIN m_device d on c.device_id = d.device_id + WHERE a.notification_error_sparepart_id = $1 AND a.deleted_at IS NULL `; diff --git a/services/notification_error_sparepart.service.js b/services/notification_error_sparepart.service.js index a5208c4..6f6620d 100644 --- a/services/notification_error_sparepart.service.js +++ b/services/notification_error_sparepart.service.js @@ -6,31 +6,23 @@ const { deleteNotificationErrorSparepartDb, } = require("../db/notification_error_sparepart.db"); -const { getContactByIdDb } = require("../db/contact.db"); const { ErrorHandler } = require("../helpers/error"); class NotificationErrorSparepartService { - static _checkAccess(contactType) { - if (contactType !== "gudang") { - throw new ErrorHandler( - 403, - "Akses ditolak. Hanya contact_type 'gudang' yang dapat getAll/create/update/delete." - ); + + static async getAll(param) { + try { + const results = await getAllNotificationErrorSparepartDb(param); + + results.data.map(element => { + }); + + return results + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); } } - static async getAll(param, contact_id) { - const contactResult = await getContactByIdDb(contact_id); - - if (!contactResult || contactResult.length < 1) - throw new ErrorHandler(404, "Contact tidak ditemukan"); - - const contact = contactResult[0]; - - this._checkAccess(contact.contact_type); - return await getAllNotificationErrorSparepartDb(param); - } - static async getById(id) { const result = await getNotificationErrorSparepartByIdDb(id); @@ -40,46 +32,37 @@ class NotificationErrorSparepartService { return result; } - static async create(data) { - const contactResult = await getContactByIdDb(data.contact_id); + static async create(data) { + try { + if (!data || typeof data !== 'object') data = {}; - if (!contactResult || contactResult.length < 1) - throw new ErrorHandler(404, "Contact tidak ditemukan"); + const result = await createNotificationErrorSparepartDb(data); - const contact = contactResult[0]; - - this._checkAccess(contact.contact_type); - - return await createNotificationErrorSparepartDb(data); + return result; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } } - static async update(id, data) { - const contactResult = await getContactByIdDb(data.contact_id); +static async update(id, data) { + try { + if (!data || typeof data !== 'object') data = {}; - if (!contactResult || contactResult.length < 1) - throw new ErrorHandler(404, "Contact tidak ditemukan"); + const dataExist = await getNotificationErrorSparepartByIdDb(id); - const contact = contactResult[0]; + if (dataExist.length < 1) { + throw new ErrorHandler(404, 'Roles not found'); + } - this._checkAccess(contact.contact_type); + const result = await updateNotificationErrorSparepartDb(id, data); - const exist = await getNotificationErrorSparepartByIdDb(id); - - if (exist.length < 1) - throw new ErrorHandler(404, "Notification Error Sparepart not found"); - - return await updateNotificationErrorSparepartDb(id, data); + return result; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } } - static async delete(id, contact_id) { - const contactResult = await getContactByIdDb(contact_id); - - if (!contactResult || contactResult.length < 1) - throw new ErrorHandler(404, "Contact tidak ditemukan"); - - const contact = contactResult[0]; - - this._checkAccess(contact.contact_type); + static async delete(id) { const exist = await getNotificationErrorSparepartByIdDb(id);