forked from ttc/micro-service-api
budgetAdd: problematic req.headers & placeholder projectSearch*
Signed-off-by: supphakitd <67319010028@technictrang.ac.th>
This commit is contained in:
51
exthernal-ttc-api/src/interfaces/Interface.js
Normal file
51
exthernal-ttc-api/src/interfaces/Interface.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import jwt from 'jsonwebtoken'
|
||||
import { BdgmstInterface } from './table/bdgmstInterface.js'
|
||||
// import { ActmstInterface } from './actmstInterface.js'
|
||||
|
||||
export class Interface {
|
||||
|
||||
constructor() {
|
||||
this.map = {
|
||||
bdgmst: new BdgmstInterface(),
|
||||
// actmst: new ActmstInterface(),
|
||||
}
|
||||
}
|
||||
|
||||
// ===============================================================
|
||||
// 📌 saveInterface → แกะ token เอง และ route ไปยัง interface เฉพาะ table
|
||||
// ===============================================================
|
||||
async saveInterface(tableName, req, data) {
|
||||
|
||||
// ------------------------------
|
||||
// ✔ 1) จับ Interface ที่ตรงกับ table
|
||||
// ------------------------------
|
||||
const handler = this.map[tableName.toLowerCase()]
|
||||
if (!handler) {
|
||||
throw new Error(`Interface not found for table: ${tableName}`)
|
||||
}
|
||||
|
||||
// ------------------------------
|
||||
// ✔ 2) แกะ token → ดึง organization → schema
|
||||
// ------------------------------
|
||||
const token = req.headers.authorization?.split(' ')[1]
|
||||
if (!token) {
|
||||
throw new Error('Missing token in request header')
|
||||
}
|
||||
|
||||
let decoded
|
||||
try {
|
||||
decoded = jwt.verify(token, process.env.JWT_SECRET)
|
||||
} catch (err) {
|
||||
throw new Error('Invalid token: ' + err.message)
|
||||
}
|
||||
|
||||
const schema = decoded.organization // ⭐ ได้ schema ที่ต้องการ
|
||||
if (!schema) throw new Error("Token missing 'organization' field")
|
||||
|
||||
// ------------------------------
|
||||
// ✔ 3) ส่งงานไปยัง interface ของ table นั้น ๆ
|
||||
// ------------------------------
|
||||
return await handler.saveInterface(schema, data)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user