anything-llm/server/endpoints/invite.js
Timothy Carambat 91f5f94200
[FEATURE] Enable the ability to have multi user instances (#158)
* multi user wip

* WIP MUM features

* invitation mgmt

* suspend or unsuspend users

* workspace mangement

* manage chats

* manage chats

* add Support for admin system settings for users to delete workspaces and limit chats per user

* fix issue ith system var
update app to lazy load invite page

* cleanup and bug fixes

* wrong method

* update readme

* update readme

* update readme

* bump version to 0.1.0
2023-07-25 10:37:04 -07:00

64 lines
1.7 KiB
JavaScript

const { Invite } = require("../models/invite");
const { User } = require("../models/user");
const { reqBody } = require("../utils/http");
function inviteEndpoints(app) {
if (!app) return;
app.get("/invite/:code", async (request, response) => {
try {
const { code } = request.params;
const invite = await Invite.get(`code = '${code}'`);
if (!invite) {
response.status(200).json({ invite: null, error: "Invite not found." });
return;
}
if (invite.status !== "pending") {
response
.status(200)
.json({ invite: null, error: "Invite is no longer valid." });
return;
}
response
.status(200)
.json({ invite: { code, status: invite.status }, error: null });
} catch (e) {
console.error(e);
response.sendStatus(500).end();
}
});
app.post("/invite/:code", async (request, response) => {
try {
const { code } = request.params;
const userParams = reqBody(request);
const invite = await Invite.get(`code = '${code}'`);
if (!invite || invite.status !== "pending") {
response
.status(200)
.json({ success: false, error: "Invite not found or is invalid." });
return;
}
const { user, error } = await User.create(userParams);
if (!user) {
console.error("Accepting invite:", error);
response
.status(200)
.json({ success: false, error: "Could not create user." });
return;
}
await Invite.markClaimed(invite.id, user);
response.status(200).json({ success: true, error: null });
} catch (e) {
console.error(e);
response.sendStatus(500).end();
}
});
}
module.exports = { inviteEndpoints };