2023-09-12 01:27:04 +02:00
|
|
|
const { escape } = require("sqlstring-sqlite");
|
2023-07-25 19:37:04 +02:00
|
|
|
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;
|
2023-09-12 01:27:04 +02:00
|
|
|
const invite = await Invite.get(`code = ${escape(code)}`);
|
2023-07-25 19:37:04 +02:00
|
|
|
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);
|
2023-09-12 01:27:04 +02:00
|
|
|
const invite = await Invite.get(`code = ${escape(code)}`);
|
2023-07-25 19:37:04 +02:00
|
|
|
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 };
|