diff --git a/exthernal-ttc-api/src/controllers/budgetAddController.js b/exthernal-ttc-api/src/controllers/budgetAddController.js index 3d740c0..dda67da 100644 --- a/exthernal-ttc-api/src/controllers/budgetAddController.js +++ b/exthernal-ttc-api/src/controllers/budgetAddController.js @@ -1,4 +1,4 @@ -import { AccountingSearchService } from '../services/accountingSearchService.js' +import { BudgetAddService } from '../services/budgetAddService.js' import { sendError } from '../utils/response.js' // import { OftenError } from '../utils/oftenError.js' import { GeneralService } from '../share/generalservice.js'; @@ -7,36 +7,33 @@ import { verifyToken, generateToken } from '../utils/token.js' import { Interface } from '../interfaces/Interface.js'; -export class accountingSearch { +export class budgetAdd { constructor() { this.generalService = new GeneralService(); this.Interface = new Interface(); - this.accountingSearchService = new AccountingSearchService(); + this.budgetAddService = new BudgetAddService(); } async onNavigate(req, res) { - this.generalService.devhint(1, 'accountingSearch.js', 'onNavigate() start'); + this.generalService.devhint(1, 'budgetAdd.js', 'onNavigate() start'); let organization = req.body.organization; - const prommis = await this.onAccountingSearch(req, res, organization); + const prommis = await this.onBudgetAdd(req, res, organization); return prommis; } - async onAccountingSearch(req, res, database) { + async onBudgetAdd(req, res, database) { let idx = -1 let aryResult = [] try { - // let username = req.body.request.username; - // let password = req.body.request.password; let token = req.body.request.token; const decoded = verifyToken(token); - let id = decoded.id - let username = decoded.name + let id = decoded.bdgseq database = decoded.organization - aryResult = await this.accountingSearchService.getAccountingSearch(database, id, username); // เช็คกับ db กลาง ส่ง jwttoken ออกมา - // this.generalService.devhint(1, 'accountingSearch.js', 'Login success'); + aryResult = await this.budgetAddService.getBudgetAdd(database, id); // เช็คกับ db กลาง ส่ง jwttoken ออกมา + // this.generalService.devhint(1, 'budgetSearch.js', 'Login success'); } catch (error) { idx = 1; } finally { @@ -54,8 +51,8 @@ export class accountingSearch { let arysave = { methods: 'post', bdgseq: req.body.request.bdgseq, - bdgttl: req.body.request.bdgseq + bdgttl: req.body.request.bdgttl } - return this.Interface.saveInterface('bdgmst', arysave); + return this.Interface.saveInterface('bdgmst', req, arysave); } } diff --git a/exthernal-ttc-api/src/controllers/projectSearchService.js b/exthernal-ttc-api/src/controllers/projectSearchService.js new file mode 100644 index 0000000..e69de29 diff --git a/exthernal-ttc-api/src/interfaces/Interface.js b/exthernal-ttc-api/src/interfaces/Interface.js new file mode 100644 index 0000000..21238b6 --- /dev/null +++ b/exthernal-ttc-api/src/interfaces/Interface.js @@ -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) + } + +} diff --git a/exthernal-ttc-api/src/interfaces/table/bdgmstInterface.js b/exthernal-ttc-api/src/interfaces/table/bdgmstInterface.js new file mode 100644 index 0000000..c1abb87 --- /dev/null +++ b/exthernal-ttc-api/src/interfaces/table/bdgmstInterface.js @@ -0,0 +1,86 @@ +import { GeneralService } from '../../share/generalservice.js' + +export class BdgmstInterface { + + constructor() { + this.general = new GeneralService() + this.table = 'bdgmst' + this.pk = ['bdgseq'] + } + + async saveInterface(database, data) { + const method = data.method.toLowerCase() + const payload = { ...data } + delete payload.method + + if (method === 'put') return this.update(database, payload) + if (method === 'post') return this.insert(database, payload) + if (method === 'delete') return this.remove(database, payload) + + throw new Error(`Unknown method: ${method}`) + } + + async insert(database, payload) { + const cols = Object.keys(payload) + const vals = Object.values(payload) + const placeholders = cols.map((_, i) => `$${i + 1}`).join(', ') + + const sql = ` + INSERT INTO ${database}.${this.table} (${cols.join(', ')}) + VALUES (${placeholders}) + RETURNING * + ` + return await this.general.executeQueryParam(database, sql, vals) + } + + async update(database, payload) { + const where = {} + const update = {} + + for (const col in payload) { + if (this.pk.includes(col)) where[col] = payload[col] + else update[col] = payload[col] + } + + const setCols = Object.keys(update) + .map((col, i) => `${col} = $${i + 1}`) + .join(', ') + + const whereCols = Object.keys(where) + .map((col, i) => `${col} = $${i + 1 + Object.keys(update).length}`) + .join(' AND ') + + const params = [...Object.values(update), ...Object.values(where)] + + const sql = ` + UPDATE ${database}.${this.table} + SET ${setCols} + WHERE ${whereCols} + RETURNING * + ` + + return await this.general.executeQueryParam(database, sql, params) + } + + async remove(database, payload) { + const where = {} + this.pk.forEach(pk => { + if (!payload[pk]) throw new Error(`Missing PK: ${pk}`) + where[pk] = payload[pk] + }) + + const whereCols = Object.keys(where) + .map((col, i) => `${col} = $${i + 1}`) + .join(' AND ') + + const params = Object.values(where) + + const sql = ` + DELETE FROM ${database}.${this.table} + WHERE ${whereCols} + RETURNING * + ` + + return await this.general.executeQueryParam(database, sql, params) + } +} diff --git a/exthernal-ttc-api/src/routes/route.js b/exthernal-ttc-api/src/routes/route.js index 5ea01e4..76b32d4 100644 --- a/exthernal-ttc-api/src/routes/route.js +++ b/exthernal-ttc-api/src/routes/route.js @@ -1,23 +1,26 @@ import express from 'express' +// import { budgetSetup } from '../controllers/budgetSetupController.js' import { budgetSearch } from '../controllers/budgetSearchController.js' +import { budgetAdd } from '../controllers/budgetAddController.js' // import { authMiddleware } from '../middlewares/auth.js' // import { sendResponse } from '../utils/response.js' const router = express.Router() const controller_budgetSearch_get = new budgetSearch() +const controller_budgetAdd_put = new budgetAdd() -router.post('/budgetSetup', async (req, res) => { - const result = await controller_budgetSetup_post.onNavigate(req, res) - if (result) return res.json(result) -}) - -// router.put('/budgetadd', async (req, res) => { +// router.post('/budgetSetup', async (req, res) => { // const result = await controller_budgetSetup_post.onNavigate(req, res) // if (result) return res.json(result) // }) -router.get('/budgetsearch', async (req, res) => { +router.post('/budgetadd', async (req, res) => { + const result = await controller_budgetAdd_put.onNavigate(req, res) + if (result) return res.json(result) +}) + +router.post('/budgetsearch', async (req, res) => { const result = await controller_budgetSearch_get.onNavigate(req, res) if (result) return res.json(result) }) diff --git a/exthernal-ttc-api/src/services/budgetAddService.js b/exthernal-ttc-api/src/services/budgetAddService.js new file mode 100644 index 0000000..6ffbdd4 --- /dev/null +++ b/exthernal-ttc-api/src/services/budgetAddService.js @@ -0,0 +1,21 @@ +import { GeneralService } from '../share/generalservice.js' + +export class BudgetAddService { + + constructor() { + this.generalService = new GeneralService() + } + + async getBudgetAdd(database, id) { + const sql = ` + SELECT + bdgseq, + bdgttl + FROM ${database}.bdgmst + WHERE bdgseq = $1 + ` + const params = [id] + const result = await this.generalService.executeQueryParam(database, sql, params); + return result + } +} \ No newline at end of file diff --git a/exthernal-ttc-api/src/services/projectSearchService.js b/exthernal-ttc-api/src/services/projectSearchService.js new file mode 100644 index 0000000..e69de29