add: filter schedule data harian/mingguan/bulanan
This commit is contained in:
@@ -31,11 +31,11 @@ const poolPromise = new sql.ConnectionPool(config)
|
||||
async function checkConnection() {
|
||||
try {
|
||||
const pool = await poolPromise;
|
||||
await pool.request().query('SELECT 1 AS isConnected');
|
||||
console.log('🔍 SQL Server terkoneksi dengan baik');
|
||||
await pool.request().query("SELECT 1 AS isConnected");
|
||||
console.log("🔍 SQL Server terkoneksi dengan baik");
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('⚠️ Gagal cek koneksi SQL Server:', error);
|
||||
console.error("⚠️ Gagal cek koneksi SQL Server:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -58,13 +58,16 @@ async function query(text, params = []) {
|
||||
return request.query(sqlText);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validasi tanggal
|
||||
*/
|
||||
function isValidDate(dateStr) {
|
||||
const d = new Date(dateStr);
|
||||
return !isNaN(d.getTime()); // true kalau valid
|
||||
return !isNaN(d.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Build filter query
|
||||
* Build filter query (AND)
|
||||
*/
|
||||
function buildFilterQuery(filterQuery = [], fixedParams = []) {
|
||||
let whereConditions = [];
|
||||
@@ -76,7 +79,9 @@ function buildFilterQuery(filterQuery = [], fixedParams = []) {
|
||||
switch (f.type) {
|
||||
case "string":
|
||||
queryParams.push(`%${f.param}%`);
|
||||
whereConditions.push(`${f.column} LIKE $${queryParams.length} COLLATE SQL_Latin1_General_CP1_CI_AS`);
|
||||
whereConditions.push(
|
||||
`${f.column} LIKE $${queryParams.length} COLLATE SQL_Latin1_General_CP1_CI_AS`
|
||||
);
|
||||
break;
|
||||
|
||||
case "number":
|
||||
@@ -89,10 +94,9 @@ function buildFilterQuery(filterQuery = [], fixedParams = []) {
|
||||
whereConditions.push(`${f.column} = $${queryParams.length}`);
|
||||
break;
|
||||
|
||||
case 'between':
|
||||
case "between":
|
||||
if (Array.isArray(f.param) && f.param.length === 2) {
|
||||
const from = f.param[0];
|
||||
const to = f.param[1];
|
||||
const [from, to] = f.param;
|
||||
if (isValidDate(from) && isValidDate(to)) {
|
||||
queryParams.push(from);
|
||||
queryParams.push(to);
|
||||
@@ -112,7 +116,7 @@ function buildFilterQuery(filterQuery = [], fixedParams = []) {
|
||||
* Build OR ILIKE (SQL Server pakai LIKE + COLLATE)
|
||||
*/
|
||||
function buildStringOrIlike(columnParam, criteria, fixedParams = []) {
|
||||
if (!criteria) return { whereClause: "", whereParam: fixedParams };
|
||||
if (!criteria) return { whereOrConditions: "", whereParamOr: fixedParams };
|
||||
|
||||
let orStringConditions = [];
|
||||
let queryParams = [...fixedParams];
|
||||
@@ -120,7 +124,9 @@ function buildStringOrIlike(columnParam, criteria, fixedParams = []) {
|
||||
columnParam.forEach((column) => {
|
||||
if (!column) return;
|
||||
queryParams.push(`%${criteria}%`);
|
||||
orStringConditions.push(`${column} LIKE $${queryParams.length} COLLATE SQL_Latin1_General_CP1_CI_AS`);
|
||||
orStringConditions.push(
|
||||
`${column} LIKE $${queryParams.length} COLLATE SQL_Latin1_General_CP1_CI_AS`
|
||||
);
|
||||
});
|
||||
|
||||
const whereClause = orStringConditions.length
|
||||
@@ -130,12 +136,60 @@ function buildStringOrIlike(columnParam, criteria, fixedParams = []) {
|
||||
return { whereOrConditions: whereClause, whereParamOr: queryParams };
|
||||
}
|
||||
|
||||
/**
|
||||
* Build Date Filter (harian / mingguan / bulanan)
|
||||
*/
|
||||
function buildDateFilter(column, type, dateValue, fixedParams = []) {
|
||||
let whereCondition = "";
|
||||
let queryParams = [...fixedParams];
|
||||
|
||||
if (!dateValue && type !== "monthly") {
|
||||
return { whereDateCondition: "", whereDateParams: queryParams };
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case "daily": {
|
||||
queryParams.push(dateValue);
|
||||
whereCondition = `CAST(${column} AS DATE) = $${queryParams.length}`;
|
||||
break;
|
||||
}
|
||||
|
||||
case "weekly": {
|
||||
const startDate = new Date(dateValue);
|
||||
if (!isNaN(startDate.getTime())) {
|
||||
const endDate = new Date(startDate);
|
||||
endDate.setDate(startDate.getDate() + 6);
|
||||
|
||||
queryParams.push(startDate.toISOString().split("T")[0]);
|
||||
queryParams.push(endDate.toISOString().split("T")[0]);
|
||||
|
||||
whereCondition = `CAST(${column} AS DATE) BETWEEN $${queryParams.length - 1} AND $${queryParams.length}`;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case "monthly": {
|
||||
const [year, month] = dateValue.split("-");
|
||||
if (year && month) {
|
||||
queryParams.push(parseInt(year), parseInt(month));
|
||||
whereCondition = `YEAR(${column}) = $${queryParams.length - 1} AND MONTH(${column}) = $${queryParams.length}`;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
whereCondition = "";
|
||||
}
|
||||
|
||||
return { whereDateCondition: whereCondition, whereDateParams: queryParams };
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Build dynamic UPDATE
|
||||
*/
|
||||
function buildDynamicUpdate(table, data, where) {
|
||||
|
||||
data.updated_by = data.userId
|
||||
data.updated_by = data.userId;
|
||||
delete data.userId;
|
||||
|
||||
const setParts = [];
|
||||
@@ -153,7 +207,6 @@ function buildDynamicUpdate(table, data, where) {
|
||||
throw new Error("Tidak ada kolom untuk diupdate");
|
||||
}
|
||||
|
||||
// updated_at otomatis pakai CURRENT_TIMESTAMP
|
||||
setParts.push(`updated_at = CURRENT_TIMESTAMP`);
|
||||
|
||||
const whereParts = [];
|
||||
@@ -175,9 +228,8 @@ function buildDynamicUpdate(table, data, where) {
|
||||
* Build dynamic INSERT
|
||||
*/
|
||||
function buildDynamicInsert(table, data) {
|
||||
|
||||
data.created_by = data.userId
|
||||
data.updated_by = data.userId
|
||||
data.created_by = data.userId;
|
||||
data.updated_by = data.userId;
|
||||
delete data.userId;
|
||||
|
||||
const columns = [];
|
||||
@@ -197,7 +249,6 @@ function buildDynamicInsert(table, data) {
|
||||
throw new Error("Tidak ada kolom untuk diinsert");
|
||||
}
|
||||
|
||||
// created_at & updated_at otomatis
|
||||
columns.push("created_at", "updated_at");
|
||||
placeholders.push("CURRENT_TIMESTAMP", "CURRENT_TIMESTAMP");
|
||||
|
||||
@@ -238,6 +289,7 @@ module.exports = {
|
||||
checkConnection,
|
||||
query,
|
||||
buildFilterQuery,
|
||||
buildDateFilter,
|
||||
buildStringOrIlike,
|
||||
buildDynamicInsert,
|
||||
buildDynamicUpdate,
|
||||
|
||||
Reference in New Issue
Block a user