From f810fb662f7cc446329ed9de9611ff6dd20fb725 Mon Sep 17 00:00:00 2001 From: x2Skyz Date: Mon, 17 Nov 2025 11:33:32 +0700 Subject: [PATCH] =?UTF-8?q?-=E0=B8=95=E0=B9=89=E0=B8=99=E0=B9=81=E0=B8=9A?= =?UTF-8?q?=E0=B8=9A=20=E0=B8=A3=E0=B8=B0=E0=B8=9A=E0=B8=9A=20Interface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- @template/src/interfaces/Interface.js | 51 +++++++++++ .../src/interfaces/table/bdgmstInterface.js | 86 +++++++++++++++++++ .../src/interfaces/Interface.js | 51 +++++++++++ .../src/interfaces/table/bdgmstInterface.js | 86 +++++++++++++++++++ 4 files changed, 274 insertions(+) create mode 100644 @template/src/interfaces/Interface.js create mode 100644 @template/src/interfaces/table/bdgmstInterface.js create mode 100644 exthernal-accountingwep-api/src/interfaces/Interface.js create mode 100644 exthernal-accountingwep-api/src/interfaces/table/bdgmstInterface.js diff --git a/@template/src/interfaces/Interface.js b/@template/src/interfaces/Interface.js new file mode 100644 index 0000000..21238b6 --- /dev/null +++ b/@template/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/@template/src/interfaces/table/bdgmstInterface.js b/@template/src/interfaces/table/bdgmstInterface.js new file mode 100644 index 0000000..c1abb87 --- /dev/null +++ b/@template/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-accountingwep-api/src/interfaces/Interface.js b/exthernal-accountingwep-api/src/interfaces/Interface.js new file mode 100644 index 0000000..21238b6 --- /dev/null +++ b/exthernal-accountingwep-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-accountingwep-api/src/interfaces/table/bdgmstInterface.js b/exthernal-accountingwep-api/src/interfaces/table/bdgmstInterface.js new file mode 100644 index 0000000..c1abb87 --- /dev/null +++ b/exthernal-accountingwep-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) + } +}