Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: filter available tools by category #513

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
Open
10 changes: 6 additions & 4 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"name": "ZC-plugin-tools-API",
"name": "zc-plugin-tools",
"version": "1.0.0",
"description": "This api serves all requests for the company online tools on Zuri Chat",
"main": "app.js",
"main": "server.js",
"scripts": {
"start": "node app.js --env production",
"build": "cd ../frontend && yarn install && yarn build",
"start": "node server.js --env production",
"build": "yarn build:frontend",
"build:frontend": "cd ../frontend && yarn install && yarn build && yarn build:rootConfig",
"build:rootConfig": "cd ../single_spa && yarn install && yarn build",
"eslint": "eslint \"./**/*.js\"",
"eslint:fix": "eslint --fix \"./**/*.js\"",
"dev": "nodemon --watch */*.* app.js",
Expand Down
1 change: 0 additions & 1 deletion backend/public/maintenance.svg

This file was deleted.

Empty file.
2 changes: 1 addition & 1 deletion backend/src/config/logger/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const logFormat = printf(({ level, message, label, timestamp, stack }) => {

const logger = createLogger({
format: combine(
label({ label: "ZC-plugin-tools-API" }),
label({ label: "zc_plugin_tools_api" }),
timestamp(),
errors({ stack: true }),
json()
Expand Down
2 changes: 1 addition & 1 deletion backend/src/config/logger/prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const logFormat = printf(({ level, message, label, timestamp, stack }) => {

const logger = createLogger({
format: combine(
label({ label: "ZC-plugin-tools-API" }),
label({ label: "zc_plugin_tools_api" }),
timestamp(),
errors({ stack: true }),
json()
Expand Down
38 changes: 33 additions & 5 deletions backend/src/config/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,49 @@ const staticEngine = require("./static-engine");
const router = express.Router();
const routes = require("../routes/index")(router);

const env = require("./env");
const isProduction = env.NODE_ENV == "production";

module.exports = (app) => {
app.use(cors());
app.use(compression());
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
app.use(express.urlencoded({ extended: false }));

app.use("/api", routes);

staticEngine(app);


app.use((req, res, next) => {

const allowedOrigins = [
'https://zuri.chat',
'https://www.zuri.chat',
'https://externaltools.zuri.chat',
'http://localhost:9000',
'http://localhost:8500'
];

const origin = req.headers.origin;
if (allowedOrigins.includes(origin)) {
res.header("Access-Control-Allow-Origin", origin); // restrict it to the required domain
}
next();
});

if (isProduction) {
app.set('trust proxy', 1); // Trust first proxy
} else {
app.use(morgan("dev"));
}

app.use('/api/v1', routes); // For mounting the root router on the specified path
app.use("/api/*",(req, res, next) => {
next(new NotFoundError());
});

staticEngine(app); // setup static pages


app.use(errorMiddleware);

return app;
Expand Down
22 changes: 15 additions & 7 deletions backend/src/config/static-engine.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const express = require("express");

const env = require("./env");
const isProduction = env.NODE_ENV == "production";

const path = require("path");

const frontendBase = path.join(
__dirname,
"..",
Expand All @@ -9,22 +13,26 @@ const frontendBase = path.join(
"frontend",
"build"
);
const rootConfigBase = path.join(
__dirname,
"..",
"..",
"..",
"single_spa",
"build"
);
const frontendIndex = path.join(frontendBase, "zuri-zuri-plugin-tools.js");
const rootConfigIndex = path.join(rootConfigBase, "");

const staticEngine = (app) => {
app.use(express.static(frontendBase));

// app.get("*", (req, res, next) => {
// res.sendFile(frontendIndex);
// });
app.use(express.static(rootConfigIndex));

app.get("/zuri-zuri-plugin-tools.js", (req, res) => {
res.sendFile(frontendIndex);
});

app.use(
express.static(path.join(__dirname, "..", "..", "..", "frontend", "build"))
);
// y
};

module.exports = staticEngine;
9 changes: 9 additions & 0 deletions backend/src/controllers/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ class ToolsController {
response("Recommended Tools returned successfully", recommendedTools)
);
}

async getToolsCategories(req, res) {
const origin = req.headers.referer || req.headers.host;
const toolsCategories = await tools.getToolsCategories();

res.send(
response("Tools Categories returned successfully", toolsCategories)
);
}
}

module.exports = new ToolsController();
1 change: 1 addition & 0 deletions backend/src/routes/externaltools.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const externaltools = require("../controllers/tools");
module.exports = function () {
router.get("/tools", externaltools.getAllAvailableTools);
router.get("/tools/recommended", externaltools.getRecommendedTools);
router.get("/tools/category-list", externaltools.getToolsCategories);

return router;
};
26 changes: 25 additions & 1 deletion backend/src/services/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ class ToolsService {
return tool;
});
if (query?.sortBy == "collections") tools = _.groupBy(tools, "collection");

if (query?.category) {
let selectedCategory = query.category;
tools = tools.filter((tool) => {
if (tool.categories.includes(selectedCategory)) {
return tool;
}
});
}
return tools;
}

Expand All @@ -38,6 +45,23 @@ class ToolsService {

return tools;
}

async getToolsCategories() {
let categories = [];

availableTools.forEach((tool) => {
if (categories.length < 1) {
categories = tool.categories;
}
tool.categories.forEach((category) => {
if (!categories.includes(category)) {
categories.push(category);
}
});
});

return categories;
}
}

module.exports = new ToolsService();