+ pkg multer, archiver
+ ระบบ uploads ที่เือบสมบูร
This commit is contained in:
@@ -13,7 +13,7 @@ SMTP_USER=lalisakuty@gmail.com
|
|||||||
SMTP_PASS=lurl pckw qugk tzob
|
SMTP_PASS=lurl pckw qugk tzob
|
||||||
|
|
||||||
# REDIS
|
# REDIS
|
||||||
REDIS_HOST=localhost
|
REDIS_HOST=10.9.0.0
|
||||||
REDIS_PORT=6379
|
REDIS_PORT=6379
|
||||||
OTP_TTL_SECONDS=300
|
OTP_TTL_SECONDS=300
|
||||||
|
|
||||||
|
|||||||
@@ -30,20 +30,28 @@ export class projectAdd {
|
|||||||
let token = req.headers.authorization?.split(' ')[1];
|
let token = req.headers.authorization?.split(' ')[1];
|
||||||
const decoded = verifyToken(token);
|
const decoded = verifyToken(token);
|
||||||
|
|
||||||
let name = req.body.request.prjnam;
|
// ✅ รองรับทั้ง JSON { request: {...} } และ Form-Data (Flat body)
|
||||||
database = decoded.organization;
|
const requestData = req.body.request ? req.body.request : req.body;
|
||||||
|
|
||||||
|
let name = requestData.prjnam;
|
||||||
|
|
||||||
|
// Override Database จาก Token ตาม Pattern เดิม
|
||||||
|
database = decoded.organization || 'dbo';
|
||||||
|
|
||||||
aryResult = await this.projectAddService.getProjectAdd(database, name);
|
aryResult = await this.projectAddService.getProjectAdd(database, name);
|
||||||
latSeq = await this.projectAddService.getLatestProjectSeq(database);
|
latSeq = await this.projectAddService.getLatestProjectSeq(database);
|
||||||
// this.generalService.devhint(1, 'budgetSearch.js', 'Login success');
|
// this.generalService.devhint(1, 'budgetSearch.js', 'Login success');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
idx = 1;
|
idx = 1;
|
||||||
|
console.error(error); // เพิ่ม log error เพื่อ debug
|
||||||
} finally {
|
} finally {
|
||||||
if (idx === 1) return sendError('เกิดข้อผิดพลาดไม่คาดคิดเกิดขึ้น', 'Unexpected error');
|
if (idx === 1) return sendError('เกิดข้อผิดพลาดไม่คาดคิดเกิดขึ้น', 'Unexpected error');
|
||||||
// if (!aryResult) return sendError('ไม่พบการมีอยู่ของข้อมูล', 'Cannot Find Any Data');
|
// if (!aryResult) return sendError('ไม่พบการมีอยู่ของข้อมูล', 'Cannot Find Any Data');
|
||||||
|
|
||||||
if (aryResult == 0) {
|
if (aryResult == 0) {
|
||||||
let prommis = await this.makeArySave(req, latSeq[0].prjseq);
|
// ส่ง latSeq เข้าไป (ต้อง handle กรณี null ถ้า table ว่าง)
|
||||||
|
const currentSeq = (latSeq && latSeq[0] && latSeq[0].prjseq) ? latSeq[0].prjseq : 0;
|
||||||
|
let prommis = await this.makeArySave(req, currentSeq);
|
||||||
return prommis
|
return prommis
|
||||||
} else {
|
} else {
|
||||||
return sendError('คีย์หลักซ้ำในระบบ', 'Duplicate Primary Key');
|
return sendError('คีย์หลักซ้ำในระบบ', 'Duplicate Primary Key');
|
||||||
@@ -53,17 +61,38 @@ export class projectAdd {
|
|||||||
|
|
||||||
|
|
||||||
async makeArySave(req, latseq) {
|
async makeArySave(req, latseq) {
|
||||||
|
// Extract Data ให้รองรับทั้ง 2 แบบ
|
||||||
|
const requestData = req.body.request ? req.body.request : req.body;
|
||||||
|
|
||||||
|
// ✅ แก้ไข: Sanitise Input ป้องกัน Error numeric: ""
|
||||||
|
// ถ้าเป็นค่าว่าง ให้แปลงเป็น null หรือ 0.00 ตามประเภทข้อมูล
|
||||||
|
const prjwntbdg = (requestData.prjwntbdg && requestData.prjwntbdg !== '') ? requestData.prjwntbdg : '0.00';
|
||||||
|
const prjusrseq = (requestData.prjusrseq && requestData.prjusrseq !== '') ? requestData.prjusrseq : null;
|
||||||
|
|
||||||
let arysave = {
|
let arysave = {
|
||||||
methods: 'post',
|
methods: 'post',
|
||||||
prjseq: latseq+1,
|
prjseq: latseq + 1,
|
||||||
prjnam: req.body.request.prjnam,
|
prjnam: requestData.prjnam,
|
||||||
prjusrseq: req.body.request.prjusrseq,
|
prjusrseq: prjusrseq,
|
||||||
prjwntbdg: req.body.request.prjwntbdg,
|
prjwntbdg: prjwntbdg,
|
||||||
prjacpbdg: '0.00',
|
prjacpbdg: '0.00',
|
||||||
prjbdgcod: '',
|
prjbdgcod: '',
|
||||||
prjcomstt: req.body.request.prjcomstt,
|
prjcomstt: requestData.prjcomstt || 'UAC', // Default UAC ถ้ายิงมาแค่ชื่อกับงบ
|
||||||
prjacpdtm: req.body.request.prjacpdtm
|
prjacpdtm: requestData.prjacpdtm || null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// เพิ่ม Logic จัดการไฟล์
|
||||||
|
if (req.file) {
|
||||||
|
arysave.prjdoc = req.file.filename // บันทึกชื่อไฟล์ลง DB
|
||||||
|
}
|
||||||
|
|
||||||
|
// กรณี User Seq ไม่ได้ส่งมา (เป็น null) ให้ใช้จาก Token
|
||||||
|
if (!arysave.prjusrseq) {
|
||||||
|
const token = req.headers.authorization?.split(' ')[1];
|
||||||
|
const decoded = verifyToken(token);
|
||||||
|
if (decoded) arysave.prjusrseq = decoded.id;
|
||||||
|
}
|
||||||
|
|
||||||
return this.Interface.saveInterface('prjmst', arysave, req);
|
return this.Interface.saveInterface('prjmst', arysave, req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import Redis from 'ioredis';
|
import Redis from '../utils/redis.js';
|
||||||
import { GeneralService } from '../share/generalservice.js';
|
import { GeneralService } from '../share/generalservice.js';
|
||||||
// import { sendError } from './response.js';
|
// import { sendError } from './response.js';
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { projectAdd } from '../controllers/projectAddController.js'
|
|||||||
import { BudgetExpenseController } from '../controllers/budgetExpenseController.js'
|
import { BudgetExpenseController } from '../controllers/budgetExpenseController.js'
|
||||||
import { reportController } from '../controllers/reportController.js'
|
import { reportController } from '../controllers/reportController.js'
|
||||||
import { transactionSearch } from '../controllers/transactionSearchController.js'
|
import { transactionSearch } from '../controllers/transactionSearchController.js'
|
||||||
|
import { uploadMiddleware } from '../middlewares/uploadMiddleware.js' // ✅ Import แบบ Named Export
|
||||||
|
|
||||||
// import { authMiddleware } from '../middlewares/auth.js'
|
// import { authMiddleware } from '../middlewares/auth.js'
|
||||||
// import { sendResponse } from '../utils/response.js'
|
// import { sendResponse } from '../utils/response.js'
|
||||||
@@ -40,7 +41,8 @@ router.post('/projectsearch', async (req, res) => {
|
|||||||
if (result) return res.json(result)
|
if (result) return res.json(result)
|
||||||
})
|
})
|
||||||
|
|
||||||
router.post('/projectadd', async (req, res) => {
|
// ใช้ uploadMiddleware แทน upload.single (เพราะ Wrapper จัดการให้แล้ว)
|
||||||
|
router.post('/projectadd', uploadMiddleware, async (req, res) => {
|
||||||
const result = await controller_projectAdd_post.onNavigate(req, res)
|
const result = await controller_projectAdd_post.onNavigate(req, res)
|
||||||
if (result) return res.json(result)
|
if (result) return res.json(result)
|
||||||
})
|
})
|
||||||
@@ -60,4 +62,4 @@ router.post('/report', async (req, res) => {
|
|||||||
if (result) return res.json(result)
|
if (result) return res.json(result)
|
||||||
})
|
})
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
@@ -4,7 +4,19 @@ dotenv.config()
|
|||||||
|
|
||||||
const redis = new Redis({
|
const redis = new Redis({
|
||||||
host: process.env.REDIS_HOST,
|
host: process.env.REDIS_HOST,
|
||||||
port: process.env.REDIS_PORT
|
port: process.env.REDIS_PORT,
|
||||||
|
connectTimeout: 10000,
|
||||||
|
maxRetriesPerRequest: null
|
||||||
|
})
|
||||||
|
|
||||||
|
redis.on('error', (err) => {
|
||||||
|
// Log the error so you know it's happening, but don't crash
|
||||||
|
console.error('Redis connection error:', err.code);
|
||||||
|
})
|
||||||
|
|
||||||
|
// Optional: Log when connected successfully
|
||||||
|
redis.on('connect', () => {
|
||||||
|
console.log('Connected to Redis successfully');
|
||||||
})
|
})
|
||||||
|
|
||||||
export async function saveOtp(email, otp) {
|
export async function saveOtp(email, otp) {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
"author": "Nuttakit Pothong",
|
"author": "Nuttakit Pothong",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"archiver": "^7.0.1",
|
||||||
"bcrypt": "^6.0.0",
|
"bcrypt": "^6.0.0",
|
||||||
"connect-redis": "^9.0.0",
|
"connect-redis": "^9.0.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
@@ -28,6 +29,7 @@
|
|||||||
"express-session": "^1.18.2",
|
"express-session": "^1.18.2",
|
||||||
"ioredis": "^5.8.2",
|
"ioredis": "^5.8.2",
|
||||||
"jsonwebtoken": "^9.0.2",
|
"jsonwebtoken": "^9.0.2",
|
||||||
|
"multer": "^2.0.2",
|
||||||
"nodemailer": "^7.0.10",
|
"nodemailer": "^7.0.10",
|
||||||
"pg": "^8.16.3",
|
"pg": "^8.16.3",
|
||||||
"socket.io": "^4.8.1",
|
"socket.io": "^4.8.1",
|
||||||
|
|||||||
Reference in New Issue
Block a user