-ต้นแบบ โครงสร้าง ไฟล์ API เส้น /api/ttc

This commit is contained in:
2025-11-17 09:03:36 +07:00
parent eefbb8e5dd
commit 9f9c9aa80d
25 changed files with 1019 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
import { verifyToken } from '../utils/token.js'
import { sendError } from '../utils/response.js'
export function authMiddleware(req, res, next) {
const authHeader = req.headers['authorization']
const token = authHeader && authHeader.split(' ')[1]
if (!token) return sendError('ไม่พบ Token', 'Missing token', 401)
const decoded = verifyToken(token)
if (!decoded) return sendError('Token ไม่ถูกต้อง', 'Invalid token', 403)
req.user = decoded
next()
}

View File

@@ -0,0 +1,20 @@
import { formatSuccessResponse } from '../utils/response.js'
export function globalResponseHandler(req, res, next) {
const oldJson = res.json.bind(res)
res.json = (data) => {
if (!data) return oldJson(formatSuccessResponse(null))
// ถ้า code ไม่ใช่ 200 → ตั้ง HTTP status ให้ตรงกับ code
if (data?.code && String(data.code) !== '200') {
res.status(Number(data.code) || 400)
return oldJson(data)
}
res.status(200)
return oldJson(formatSuccessResponse(data))
}
next()
}

View File

@@ -0,0 +1,24 @@
import { sendError } from '../utils/response.js'
/**
* ✅ Middleware สำหรับตรวจสอบความถูกต้องของ JSON body
* ป้องกัน body-parser crash (SyntaxError)
*/
export function validateJsonFormat(err, req, res, next) {
if (err instanceof SyntaxError && 'body' in err) {
console.error('[Invalid JSON Format]', err.message)
return sendError('รูปแบบ บอร์ดี้ ไม่ถูกต้อง', 'Invalid Body format')
}
next()
}
// /**
// * ✅ ตรวจสอบ body/query/params ว่ามีค่า organization หรือไม่
// */
// export function validateRequest(req, res, next) {
// const { organization } = req.body || {}
// if (!organization) {
// return sendResponse(res, 400, 'ไม่พบค่า organization', 'Missing organization')
// }
// next()
// }

View File

@@ -0,0 +1,37 @@
import Redis from 'ioredis';
import { GeneralService } from '../share/generalservice.js';
// import { sendError } from './response.js';
export async function verifyEmailHandler(req, res) {
const redis = new Redis();
const generalService = new GeneralService();
try {
const { email, token } = req.query;
const schema = req.body?.organization || 'nuttakit'; // 🧩 ใช้ schema ตาม org
const storedData = await redis.get(`verify:${email}`);
if (!storedData) {
return res.status(400).send('ลิงก์หมดอายุหรือไม่ถูกต้อง');
}
const { fname, lname, hashedPwd, token: storedToken } = JSON.parse(storedData);
if (token !== storedToken) {
return res.status(400).send('Token ไม่ถูกต้อง');
}
let sql = `
INSERT INTO ${schema}.usrmst (usrnam, usrthinam, usrthilstnam, usrpwd, usrrol)
VALUES ($1, $2, $3, $4, 'U')
`;
let param = [email, fname, lname, hashedPwd];
await generalService.executeQueryParam(sql, param);
await redis.del(`verify:${email}`);
res.send(`<h2>✅ ยืนยันอีเมลสำเร็จ บัญชีของคุณถูกสร้างแล้ว (${schema})</h2>`);
} catch (error) {
console.error('❌ [Verify Email Error]', error);
res.status(500).send('เกิดข้อผิดพลาดในระบบ');
}
}