UNPKG

@tasolutions/express-core

Version:
65 lines (59 loc) 2.32 kB
const config = require('../config'); const { filterModels } = require('../core/crud/auth/permission'); const { Module } = require('../src/ui/models'); const actionList = require('../utils/actionList'); const modelList = require('../utils/modelList'); module.exports = (app) => { app.get('/info.json', (req, res) => { const responseStructure = { name: config.applicationName, route_name: config.routeInfo.name, base_url: config.routeInfo.baseUrl, api_info_endpoint_url: `${config.routeInfo.baseUrl}/info.json`, items: [] }; actionList.forEach(action => { if (action.path && !action.path.includes('undefined')) { responseStructure.items.push({ name: action.name, path: action.path, method: action.method ? action.method.toLowerCase() : 'NULL' }); } }); responseStructure.items.push({ name: "Scopes", path: "/v1/scopes", method: "get" }); res.json(responseStructure); }); app.get('/v1/scopes', async (req, res) => { const userId = req.headers['st-user-id']; const models = modelList; const modelsFiltered = await filterModels(userId, models); let scopes = []; modelsFiltered.forEach(m => { scopes.push({ name: m.name, key: m.model_name, path: `${config.apiGWInfo.prefix}${m.path}/schemas`, url: `${config.apiGWInfo.url}${m.path}/schemas` }) }); try { // Duyệt qua từng scope và lưu hoặc cập nhật vào cơ sở dữ liệu for (const scope of scopes) { await Module.findOneAndUpdate( { key: scope.key }, // Tìm kiếm theo key { $set: scope }, // Cập nhật thông tin sản phẩm { upsert: true, new: true } // Nếu không có thì tạo mới ); } console.log('Modules saved or updated successfully.'); } catch (error) { console.error("Error saving modules:", error); } res.json({ scopes }); }); };