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

PLUGIN_TOOLS: Organization add tools & Get list of organization tools #512

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 55 additions & 35 deletions client/src/components/Apps/GoogleDrive/Modal/Form.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,65 @@
import React from 'react';
import React from "react";

import styles from "../GoogleDrive.module.css";

import axios from "axios";

class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = { value: '' };
constructor(props) {
super(props);
this.state = { value: "" };

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChange(event) {
this.setState({ value: event.target.value });
}
handleChange(event) {
this.setState({ value: event.target.value });
}

handleSubmit(event) {
alert('A comment was submitted: ' + this.state.value);
event.preventDefault();
}
handleSubmit(event) {
// alert("A comment was submitted: " + this.state.value);
event.preventDefault();
axios
.post("/tools/addtools", {
comment: this.state.value,
tool: "googledrive",
})
.then((resp) => {
alert("A comment was submitted: " + resp);
})
.catch((error) => {
alert("An error occured: " + error);
});
}

render() {
return (
<div>
<form onSubmit={this.handleSubmit} >
<label className={styles.name_style}>
Why you want to Add Google Drive to Zuri Plugin Tools
</label>
<textarea id="w3review" placeholder="Enter a comment.."
name="w3review" rows="4" cols="50"
className={styles.input_section}
value={this.state.value}
onChange={this.handleChange}></textarea>
<div>
<input type="submit" value="Submit" className={styles.submit_button} />
</div>
</form>
</div>
);
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label className={styles.name_style}>
Why you want to Add Google Drive to Zuri Plugin Tools
</label>
<textarea
id="w3review"
placeholder="Enter a comment.."
name="w3review"
rows="4"
cols="50"
className={styles.input_section}
value={this.state.value}
onChange={this.handleChange}
></textarea>
<div>
<input
type="submit"
value="Submit"
className={styles.submit_button}
/>
</div>
</form>
</div>
);
}
}

export default NameForm
export default NameForm;
7 changes: 6 additions & 1 deletion server/src/config/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ const express = require("express");
const morgan = require("morgan");
const compression = require("compression");
const cors = require("cors");
const corsOptions = {
origin: "*",
credentials: true, //access-control-allow-credentials:true
optionSuccessStatus: 200,
};

const { NotFoundError } = require("../lib/errors");
const errorMiddleware = require("../middlewares/error");
Expand All @@ -14,7 +19,7 @@ module.exports = (app) => {
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
app.use(cors(corsOptions));

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

Expand Down
3 changes: 1 addition & 2 deletions server/src/controllers/googledrive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class GoogleDriveController {
);
}
async getFilesList(req, res) {
console.log(REDIRECT_URI);
try {
const r = await drive.files.list();
const { data } = r;
Expand All @@ -41,7 +40,7 @@ class GoogleDriveController {
}
}
async getData(req, res) {
const resp = await fetch('https://jsonplaceholder.typicode.com/posts');
const resp = await fetch("https://jsonplaceholder.typicode.com/posts");
const json = await resp.json();
res.send(response("Data info returned successfully", json));
}
Expand Down
58 changes: 56 additions & 2 deletions server/src/controllers/tools.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,76 @@
const tools = require("../services/tools");
const response = require("../utils/response");
const axios = require("axios");
const fs = require("fs");

//store
const toolsData = [];
class ToolsController {
async getAllAvailableTools(req, res) {
const origin = req.headers.referer || req.headers.host;
const availableTools = await tools.getAll(req.query, origin);

res.send(response("Available Tools returned successfully", availableTools));
}

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

res.send(
response("Recommended Tools returned successfully", recommendedTools)
);
}
async getOrganizationTools(req, res) {
const { plugin_id, coll_name, org_id } = req.body;
axios
.get(
`https://api.zuri.chat/data/read/${plugin_id}/${coll_name}/${org_id}`
)
.then((resp) => {
res.send(resp.data);
})
.catch((err) => {
res.status(500).json({
statusCode: 500,
message: err.message,
});
});
}

async addTool(req, res) {
const options = {
headers: {
"content-type": "application/json",
"cache-control": "no-cache",
},
};
axios
.post(
"https://api.zuri.chat/v1/data/write",
{
plugin_id: req.body.plugin_id,
organization_id: req.body.organization_id,
collection_name: "organization_tools",
payload: {
plugin_id: req.body.plugin_id,
organization_id: req.body.organization_id,
message: req.body.message,
},
bulk_write: false,
object_id: "",
filter: {},
},
options
)
.then((resp) => {
res.send("This tool has been added successfully");
})
.catch((err) => {
res.status(500).json({
statusCode: 500,
message: err.message,
});
});
}
}

module.exports = new ToolsController();
2 changes: 2 additions & 0 deletions server/src/routes/externaltools.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const externaltools = require("../controllers/tools");
module.exports = function () {
router.get("/tools", externaltools.getAllAvailableTools);
router.get("/tools/recommended", externaltools.getRecommendedTools);
router.get("/tools/organizationtools", externaltools.getOrganizationTools);
router.post("/tools/addtools", externaltools.addTool);

return router;
};
3 changes: 1 addition & 2 deletions server/src/routes/googledrive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const googleDrvieController = require("../../controllers/googledrive/index");

module.exports = function () {
router.get("/googledrive/info", googleDrvieController.index);
router.get("/googledrive/data", googleDrvieController.getData)

router.get("/googledrive/data", googleDrvieController.getData);
router.get("/googledrive/get-files-list", googleDrvieController.getFilesList);
return router;
};