From 7c55e786f31430fbc90ffe18af6a04c701cb2125 Mon Sep 17 00:00:00 2001 From: Antony Kurniawan Date: Sat, 11 Oct 2025 16:20:22 +0700 Subject: [PATCH] add: sub section db --- db/sub_section.db.js | 108 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 db/sub_section.db.js diff --git a/db/sub_section.db.js b/db/sub_section.db.js new file mode 100644 index 0000000..e4358d7 --- /dev/null +++ b/db/sub_section.db.js @@ -0,0 +1,108 @@ +const pool = require("../config"); + +// Get all sub sections +const getAllSubSectionsDb = async (searchParams = {}) => { + let queryParams = []; + + if (searchParams.limit) { + const page = Number(searchParams.page ?? 1) - 1; + queryParams = [Number(searchParams.limit ?? 10), page]; + } + + // OR condition (pencarian bebas) + const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike( + ["a.sub_section_code", "a.sub_section_name"], + searchParams.criteria, + queryParams + ); + + queryParams = whereParamOr ?? queryParams; + + // AND condition (filter spesifik) + const { whereConditions, whereParamAnd } = pool.buildFilterQuery( + [ + { column: "a.sub_section_code", param: searchParams.code, type: "string" }, + { column: "a.sub_section_name", param: searchParams.name, type: "string" }, + ], + queryParams + ); + + queryParams = whereParamAnd ?? queryParams; + + // Query utama + const queryText = ` + SELECT COUNT(*) OVER() AS total_data, a.* + FROM m_plant_sub_section a + WHERE a.deleted_at IS NULL + ${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""} + ${whereOrConditions ? whereOrConditions : ""} + ORDER BY a.sub_section_id ASC + ${searchParams.limit ? `OFFSET $2 ROWS FETCH NEXT $1 ROWS ONLY` : ""}; + `; + + const result = await pool.query(queryText, queryParams); + const total = + result?.recordset.length > 0 + ? parseInt(result.recordset[0].total_data, 10) + : 0; + + return { data: result.recordset, total }; +}; + +// Get sub section by ID +const getSubSectionByIdDb = async (id) => { + const queryText = ` + SELECT a.* + FROM m_plant_sub_section a + WHERE a.sub_section_id = $1 AND a.deleted_at IS NULL + `; + const result = await pool.query(queryText, [id]); + return result.recordset; +}; + +// Create new sub section +const createSubSectionDb = async (data) => { + // Generate kode otomatis + const newCode = await pool.generateKode("SUB", "m_plant_sub_section", "sub_section_code"); + + const store = { + ...data, + sub_section_code: newCode + }; + + const { query: queryText, values } = pool.buildDynamicInsert("m_plant_sub_section", store); + const result = await pool.query(queryText, values); + const insertedId = result.recordset[0]?.inserted_id; + + return insertedId ? await getSubSectionByIdDb(insertedId) : null; +}; + +// Update sub section +const updateSubSectionDb = async (id, data) => { + const store = { ...data }; + const whereData = { sub_section_id: id }; + + const { query: queryText, values } = pool.buildDynamicUpdate("m_plant_sub_section", store, whereData); + await pool.query(`${queryText} AND deleted_at IS NULL`, values); + + return getSubSectionByIdDb(id); +}; + +// Soft delete sub section +const deleteSubSectionDb = async (id, deletedBy) => { + const queryText = ` + UPDATE m_plant_sub_section + SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1 + WHERE sub_section_id = $2 AND deleted_at IS NULL + `; + await pool.query(queryText, [deletedBy, id]); + return true; +}; + +module.exports = { + getAllSubSectionsDb, + getSubSectionByIdDb, + createSubSectionDb, + updateSubSectionDb, + deleteSubSectionDb, +};