feat: add service management module with CRUD operations

This commit is contained in:
nguyenvanbao
2026-02-03 16:20:07 +07:00
parent d1b931d547
commit 9dc02974a4
21 changed files with 3798 additions and 208 deletions

View File

@@ -1,37 +1,68 @@
/**
* Thêm BACKEND_URL vào đường dẫn hình ảnh
* @param {Object} data - Dữ liệu cần xử lý
* @param {Object} data - Dữ liệu cần xử lý
* @returns {Object} - Dữ liệu đã được xử lý với đường dẫn hình ảnh đầy đủ
*/
function addBaseUrlToImages(data, baseUrl) {
// baseUrl can be passed explicitly (e.g., from req), otherwise fall back to env
const BACKEND_URL = baseUrl || process.env.BACKEND_URL || '';
const BACKEND_URL = baseUrl || process.env.BACKEND_URL || "";
// Tạo bản sao sâu để tránh thay đổi dữ liệu gốc
const processedData = JSON.parse(JSON.stringify(data));
// Hàm đệ quy để xử lý tất cả các URL hình ảnh trong đối tượng
const processObject = (obj) => {
if (!obj || typeof obj !== 'object') return;
Object.keys(obj).forEach(key => {
if (!obj || typeof obj !== "object") return;
Object.keys(obj).forEach((key) => {
// Kiểm tra nếu thuộc tính chứa đường dẫn hình ảnh bắt đầu bằng /uploads/
if (typeof obj[key] === 'string' && obj[key].startsWith('/uploads/')) {
if (typeof obj[key] === "string" && obj[key].startsWith("/uploads/")) {
// Thêm BACKEND_URL nếu đường dẫn chưa có http
if (!obj[key].startsWith('http')) {
if (!obj[key].startsWith("http")) {
obj[key] = `${BACKEND_URL}${obj[key]}`;
}
} else if (typeof obj[key] === 'object') {
} else if (typeof obj[key] === "object") {
// Đệ quy xử lý các đối tượng và mảng lồng nhau
processObject(obj[key]);
}
});
};
processObject(processedData);
return processedData;
}
/**
* Tạo full URL cho ảnh từ đường dẫn tương đối - dùng cho EJS templates
* @param {string} imagePath - Đường dẫn ảnh
* @param {string} backendUrl - Backend URL (optional, sẽ lấy từ env nếu không có)
* @returns {string} - Full URL của ảnh
*/
function getFullImageUrl(imagePath, backendUrl = null) {
if (!imagePath) return "";
// Nếu đã là full URL thì return luôn
if (imagePath.startsWith("http")) {
return imagePath;
}
// Lấy backend URL
const baseUrl = (
backendUrl ||
process.env.BACKEND_URL ||
"http://localhost:3001"
).replace(/\/$/, "");
// Xử lý đường dẫn
let imgSrc = imagePath;
if (!imgSrc.startsWith("/")) {
imgSrc = "/" + imgSrc;
}
return baseUrl + imgSrc;
}
module.exports = {
addBaseUrlToImages
};
addBaseUrlToImages,
getFullImageUrl,
};