-web-service

This commit is contained in:
2025-11-11 15:11:56 +07:00
parent 9ad26fa5ef
commit fcf59ce5db
21 changed files with 537 additions and 253 deletions

View File

@@ -1,11 +1,9 @@
import { connection } from '../config/db.js'
import dotenv from 'dotenv'
import { sendError } from '../utils/response.js'
dotenv.config()
export class GeneralService {
// constructor() {
// this.devhint = this.devhint.bind(this)
// }
devhint(level, location, message, extra = null) {
const isEnabled = process.env.DEVHINT === 'true'
const currentLevel = parseInt(process.env.DEVHINT_LEVEL || '1', 10)
@@ -14,12 +12,38 @@ export class GeneralService {
const timestamp = new Date().toISOString()
const prefix = `🧩 [DEVHINT:${location}]`
const formatted = `${prefix}${message} (${timestamp})`
if (extra) console.log(formatted, '\n', extra)
else console.log(formatted)
// 🔹 highlight jumpout
if (message.includes('Jumpout')) {
console.log('\x1b[31m%s\x1b[0m', formatted) // แดง = jumpout
} else if (message.includes('Error')) {
console.log('\x1b[33m%s\x1b[0m', formatted) // เหลือง = error
} else {
console.log(formatted)
}
if (extra) console.log(extra)
}
// ===================================================
// ✅ executeQueryConditions()
// ✅ executeQueryParam() — เจ๊งจริง แล้ว controller catch ได้จริง
// ===================================================
async executeQueryParam(database, sql, params = []) {
const formattedSQL = sql.replace(/\${database}/g, database)
try {
this.devhint(2, 'executeQueryParam', `📤 Executing Query`, `sql = ${formattedSQL}`)
const result = await connection.query(formattedSQL, params)
this.devhint(2, 'executeQueryParam', `✅ Query Success (${result.rowCount} rows)`)
return result.rows
} catch (err) {
this.devhint(1, 'executeQueryParam', `❌ SQL Error`, err.message)
console.error('🧨 SQL Error:', err.message)
throw new Error(`SQL_EXECUTION_FAILED::${err.message}`) // ✅ “เจ๊ง” แล้วโยนขึ้น controller จริง
}
}
// ===================================================
// ✅ executeQueryConditions() — เหมือนกัน
// ===================================================
async executeQueryConditions(database, baseQuery, conditions = {}) {
this.devhint(2, 'GeneralService', 'executeQueryConditions() start')
@@ -30,7 +54,6 @@ export class GeneralService {
for (const [key, value] of Object.entries(conditions)) {
if (value === undefined || value === null || value === '') continue
const match = String(value).match(/^(ILIKE|LIKE)\s+(.+)$/i)
if (match) {
const operator = match[1].toUpperCase()
@@ -48,39 +71,22 @@ export class GeneralService {
if (whereClauses.length > 0) finalQuery += ' AND ' + whereClauses.join(' AND ')
const formattedSQL = finalQuery.replace(/\${database}/g, database)
this.devhint(2, 'executeQueryConditions', `📤 Executing Query`, {
database,
sql: formattedSQL,
params,
})
const result = await connection.query(formattedSQL, params)
this.devhint(2, 'executeQueryConditions', `✅ Query Success (${result.rowCount} rows)`)
return result.rows
}
// ===================================================
// ✅ executeQueryParam()
// ===================================================
// ===================================================
async executeQueryParam(database, sql, params = []) {
const formattedSQL = sql.replace(/\${database}/g, database)
this.devhint(2, 'executeQueryParam', `📤 Executing Query`, `sql = ${formattedSQL}`)
try {
const result = await connection.query(formattedSQL, params)
this.devhint(2, 'executeQueryParam', `✅ Query Success (${result.rowCount} rows)`)
return result.rows
} catch (err) {
this.devhint(2, 'executeQueryParam', `❌ Query Failed`, err.message)
console.error('SQL Error:', err)
throw err // < ส่งต่อ error เพื่อ controller จะจับได้
try {
this.devhint(2, 'executeQueryConditions', `📤 Executing Query`, {
database,
sql: formattedSQL,
params
})
const result = await connection.query(formattedSQL, params)
this.devhint(2, 'executeQueryConditions', `✅ Query Success (${result.rowCount} rows)`)
return result.rows
} catch (err) {
this.devhint(1, 'executeQueryConditions', `❌ SQL Error`, err.message)
console.error('🧨 SQL Error:', err.message)
throw new Error(`SQL_EXECUTION_FAILED::${err.message}`) // ✅ “เจ๊งจริง” ส่งถึง controller แน่นอน
}
}
}
}
// ===================================================
// Export สำหรับ controller หรืออื่นๆ เรียกใช้ได้ด้วย
// ===================================================