Skip to content

Commit 46bcb98

Browse files
committed
feat(api): Access Attemp tLogs
1 parent d4c79be commit 46bcb98

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

server/src/controllers/logs.controller.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,36 @@ export async function getUserLogs(req: Request, res: Response, next: NextFunctio
6262
} catch (err) {
6363
next(err);
6464
}
65+
};
66+
67+
/**
68+
* สร้างบันทึกเหตุการณ์การเข้าถึงกล้อง (Camera Access Log)
69+
*
70+
* ฟังก์ชันนี้ทำหน้าที่รับข้อมูลจากคำขอ (request) ได้แก่ user_id, status
71+
* และรหัสกล้องจาก params (cam_id) จากนั้นส่งต่อไปยัง LogService
72+
* เพื่อบันทึกเหตุการณ์ว่า ผู้ใช้เข้าถึงกล้องสำเร็จหรือเพียงพยายามเข้าถึง
73+
*
74+
* การตอบกลับ (response):
75+
* - หากบันทึกสำเร็จ จะส่งสถานะ 201 พร้อมข้อความและข้อมูลที่บันทึก
76+
* - หากเกิดข้อผิดพลาด จะส่งต่อไปยัง middleware จัดการ error
77+
*
78+
* @param {Request} req - อ็อบเจ็กต์คำขอจาก Express
79+
* @param {Response} res - อ็อบเจ็กต์ตอบกลับจาก Express
80+
* @param {NextFunction} next - ฟังก์ชันส่งต่อข้อผิดพลาดให้ middleware ถัดไป
81+
* @returns {Promise<Response>} ส่งผลลัพธ์การสร้าง log กลับไปยัง client
82+
* @throws {Error} หากเกิดข้อผิดพลาดจาก Service หรือฐานข้อมูล
83+
*
84+
* @author Wanasart
85+
* @lastModified 2025-12-07
86+
*/
87+
export async function createAccessAttemptLogs(req: Request, res: Response, next: NextFunction) {
88+
try {
89+
const camera_id = Number(req.params.cam_id);
90+
const { user_id, status } = req.body;
91+
92+
const log = await LogService.insertAccessAttemptLogs(user_id, camera_id, status);
93+
return res.status(201).json({ message: 'Created successfully', data: log });
94+
} catch (err) {
95+
next(err);
96+
}
6597
};

server/src/routes/logs.routes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
* ## Logs
77
* - GET /camera → ดึงรายการบันทึกกล้อง (Camera Logs)
8+
* - POST /camera/:cam_id → สร้างบันทึกการเข้าถึงกล้อง
89
* - GET /alert → ดึงรายการบันทึกแจ้งเตือน (Alert Logs)
910
* - GET /user → ดึงประวัติการทำงานของผู้ใช้ (User Activity Logs)
1011
*
@@ -16,7 +17,7 @@
1617
*
1718
* @author Wanasart
1819
* @created 2025-10-31
19-
* @lastModified 2025-11-26
20+
* @lastModified 2025-12-07
2021
*/
2122
import { Router } from 'express';
2223
import * as ctrl from '../controllers/logs.controller';
@@ -28,4 +29,6 @@ router.get('/camera', ctrl.getCameraLogs);
2829
router.get('/alert', ctrl.getAlertLogs);
2930
router.get('/user', ctrl.getUserLogs);
3031

32+
router.post('/camera/:cam_id', ctrl.createAccessAttemptLogs);
33+
3134
export default router;

server/src/services/logs.service.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,52 @@ export async function getUserLogs(){
8888
`);
8989

9090
return rows.map(Mapping.mapUserLogsToSaveResponse);
91+
}
92+
93+
/**
94+
* บันทึกเหตุการณ์การเข้าถึงกล้อง (Camera Access Logs)
95+
* โดยบันทึกว่า "ผู้ใช้พยายามเข้าถึง" หรือ "เข้าถึงสำเร็จ"
96+
* ตามค่าที่ส่งเข้ามาในตัวแปร status
97+
*
98+
* - หาก status = true → บันทึกเป็น ACCESS_GRANTED
99+
* - หาก status = false → บันทึกเป็น ACCESS_ATTEMPT
100+
*
101+
* ข้อมูลจะถูกบันทึกลงในตาราง camera_logs พร้อม timestamp ปัจจุบัน
102+
*
103+
* @param {number} user_id รหัสผู้ใช้งานที่พยายามเข้าถึง
104+
* @param {number} camera_id รหัสกล้องที่ถูกเข้าถึง
105+
* @param {boolean} status สถานะการเข้าถึง (true = สำเร็จ, false = พยายาม)
106+
*
107+
* @returns {Promise<void>} ไม่มีค่าที่ส่งกลับ (เป็นการบันทึกข้อมูลอย่างเดียว)
108+
* @throws {Error} หากเกิดข้อผิดพลาดระหว่างการบันทึกลงฐานข้อมูล
109+
*
110+
* @author Wanasart
111+
* @lastModified 2025-12-07
112+
*/
113+
export async function insertAccessAttemptLogs(
114+
user_id: number,
115+
camera_id: number,
116+
status: boolean
117+
){
118+
119+
let action = 'UNKNOW';
120+
if(status){
121+
action = 'ACCESS_GRANTED';
122+
} else {
123+
action = 'ACCESS_ATTEMPT';
124+
}
125+
126+
await pool.query(`
127+
INSERT INTO camera_logs(
128+
clg_usr_id,
129+
clg_cam_id,
130+
clg_action,
131+
clg_created_at
132+
)
133+
VALUES($1, $2, $3, CURRENT_TIMESTAMP);
134+
`, [
135+
user_id,
136+
camera_id,
137+
action,
138+
]);
91139
}

0 commit comments

Comments
 (0)