forked from ttc/micro-service-api
-- แก้ไขตรวจสอบ ระบบ interface ให้ใช้งาน ได้ สมบูรแบบ
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import jwt from 'jsonwebtoken'
|
import jwt from 'jsonwebtoken'
|
||||||
import { BdgmstInterface } from './table/bdgmstInterface.js'
|
import { BdgmstInterface } from './table/bdgmstInterface.js'
|
||||||
|
import { sendError } from '../utils/response.js'
|
||||||
|
|
||||||
// import { ActmstInterface } from './actmstInterface.js'
|
// import { ActmstInterface } from './actmstInterface.js'
|
||||||
|
|
||||||
// -------------------------------
|
// -------------------------------
|
||||||
@@ -25,7 +27,7 @@ export class Interface {
|
|||||||
// ------------------------------
|
// ------------------------------
|
||||||
const handler = this.map[tableName.toLowerCase()]
|
const handler = this.map[tableName.toLowerCase()]
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
return new Error(`Interface not found for table: ${tableName}`)
|
return new sendError(`Interface not found for table: ${tableName}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------
|
// ------------------------------
|
||||||
@@ -33,18 +35,18 @@ export class Interface {
|
|||||||
// ------------------------------
|
// ------------------------------
|
||||||
const token = req.headers.authorization?.split(' ')[1]
|
const token = req.headers.authorization?.split(' ')[1]
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return new Error('Missing token in request header')
|
return new sendError('ไม่พบการยืนยันตัวตน' ,'Missing token in request header')
|
||||||
}
|
}
|
||||||
|
|
||||||
let decoded
|
let decoded
|
||||||
try {
|
try {
|
||||||
decoded = jwt.verify(token, process.env.JWT_SECRET)
|
decoded = jwt.verify(token, process.env.JWT_SECRET)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return new Error('Invalid token: ' + err.message)
|
return new sendError('Invalid token: ' + err.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
const schema = decoded.organization // ⭐ ได้ schema ที่ต้องการ
|
const schema = decoded.organization // ⭐ ได้ schema ที่ต้องการ
|
||||||
if (!schema) return new Error("Token missing 'organization' field")
|
if (!schema) return new sendError("Token missing 'organization' field")
|
||||||
|
|
||||||
// ------------------------------
|
// ------------------------------
|
||||||
// ✔ 3) ส่งงานไปยัง interface ของ table นั้น ๆ
|
// ✔ 3) ส่งงานไปยัง interface ของ table นั้น ๆ
|
||||||
|
|||||||
@@ -10,8 +10,7 @@ export class BdgmstInterface {
|
|||||||
|
|
||||||
async saveInterface(database, data) {
|
async saveInterface(database, data) {
|
||||||
const method = data.methods.toLowerCase()
|
const method = data.methods.toLowerCase()
|
||||||
const payload = { ...data }
|
const { methods, ...payload } = data
|
||||||
delete payload.method
|
|
||||||
|
|
||||||
if (method === 'put') return this.update(database, payload)
|
if (method === 'put') return this.update(database, payload)
|
||||||
if (method === 'post') return this.insert(database, payload)
|
if (method === 'post') return this.insert(database, payload)
|
||||||
@@ -20,6 +19,7 @@ export class BdgmstInterface {
|
|||||||
throw new Error(`Unknown method: ${method}`)
|
throw new Error(`Unknown method: ${method}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async insert(database, payload) {
|
async insert(database, payload) {
|
||||||
const cols = Object.keys(payload)
|
const cols = Object.keys(payload)
|
||||||
const vals = Object.values(payload)
|
const vals = Object.values(payload)
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export class BudgetAddService {
|
|||||||
bdgnam
|
bdgnam
|
||||||
FROM ${database}.bdgmst
|
FROM ${database}.bdgmst
|
||||||
WHERE bdgnam = $1
|
WHERE bdgnam = $1
|
||||||
s `
|
`
|
||||||
const params = [name]
|
const params = [name]
|
||||||
const result = await this.generalService.executeQueryParam(database, sql, params);
|
const result = await this.generalService.executeQueryParam(database, sql, params);
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user