-api เส้น register

-เครื่องมือต่างๆ (ไม่สมบูร)
This commit is contained in:
x2Skyz
2025-11-21 12:09:43 +07:00
parent 4cb135d251
commit f8344e7afc
13 changed files with 561 additions and 139 deletions

View File

@@ -0,0 +1,70 @@
import jwt from 'jsonwebtoken'
import { BdgmstInterface } from './table/bdgmstInterface.js'
import { ActmstInterface } from './table/actmstInterface.js'
import { UsrmstInterface } from './table/usrmstInterface.js'
import { sendError } from '../utils/response.js'
// -------------------------------
// GLOBAL FILE
// -----------------------------
export class Interface {
constructor() {
this.map = {
bdgmst: new BdgmstInterface(),
actmst: new ActmstInterface(),
usrmst: new UsrmstInterface(),
}
}
// ===============================================================
// saveInterface → แกะ token เอง และ route ไปยัง interface เฉพาะ table
// ===============================================================
async saveInterface(tableName, data, req) {
// ------------------------------
// 1) จับ Interface ที่ตรงกับ table
// ------------------------------
const handler = this.map[tableName.toLowerCase()]
if (!handler) {
return new sendError(`Interface not found for table: ${tableName}`)
}
let schema;
// ------------------------------
// 2) ตรวจสอบเงื่อนไข (Exception for usrmst)
// ------------------------------
if (tableName.toLowerCase() === 'usrmst') {
// กรณี usrmst (เช่น Register/Login) ไม่บังคับ Token
// เราต้องกำหนด schema เอง เพราะไม่มี token ให้แกะ
schema = 'nuttakit'
} else {
// ------------------------------
// 3) ตารางอื่น ๆ บังคับ Token ตามปกติ
// ------------------------------
const token = req.headers.authorization?.split(' ')[1]
if (!token) {
return new sendError('ไม่พบการยืนยันตัวตน' ,'Missing token in request header')
}
let decoded
try {
decoded = jwt.verify(token, process.env.JWT_SECRET)
} catch (err) {
return new sendError('Invalid token: ' + err.message)
}
schema = decoded.organization
}
if (!schema) return new sendError("Token missing 'organization' field or Schema undefined")
// ------------------------------
// ✔ 4) ส่งงานไปยัง interface ของ table นั้น ๆ
// ------------------------------
return await handler.saveInterface(schema, data)
}
}

View File

@@ -0,0 +1,86 @@
import { GeneralService } from '../../share/generalservice.js'
export class ActmstInterface {
constructor() {
this.general = new GeneralService()
this.table = 'actmst'
this.pk = ['actseq', 'actnum'] // ⭐ PK ตาม CREATE TABLE
}
async saveInterface(database, data) {
const method = data.methods.toLowerCase()
const { methods, ...payload } = data
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)
}
}

View File

@@ -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)
}
}

View File

@@ -0,0 +1,86 @@
import { GeneralService } from '../../share/generalservice.js'
export class UsrmstInterface {
constructor() {
this.general = new GeneralService()
this.table = 'usrmst'
this.pk = ['usrseq'] // ⭐ PK ตาม CREATE TABLE
}
async saveInterface(database, data) {
const method = data.methods.toLowerCase()
const { methods, ...payload } = data
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)
}
}