147 lines
5.4 KiB
JavaScript
147 lines
5.4 KiB
JavaScript
const express = require('express')
|
|
const router = express.Router();
|
|
|
|
const Campaign = require("../models/Campaign");
|
|
const User = require('../models/User');
|
|
const serviceLinks = require('../services/service-links');
|
|
|
|
// Every service links to a campaign through the same sign-up link
|
|
// (routes/service-link.js); a campaign only drops one of them again.
|
|
async function unlinkService(campaign, service) {
|
|
if (service === 'foundry') {
|
|
campaign.foundryWorldId = undefined;
|
|
campaign.foundryWorldTitle = undefined;
|
|
campaign.foundrySystem = undefined;
|
|
campaign.set('foundrySyncTokenHash', undefined);
|
|
} else if (service === 'discord') {
|
|
// The Discord account the bot answers to stays: the server is simply
|
|
// left without a campaign until somebody picks one again.
|
|
campaign.discordGuildId = undefined;
|
|
}
|
|
|
|
await campaign.save();
|
|
// A link a service still holds becomes invalid, and it finds that out the
|
|
// next time it asks where its link stands.
|
|
await serviceLinks.revokeServiceLink(service, campaign._id);
|
|
}
|
|
|
|
router.post('/create', async (req, res) => {
|
|
try {
|
|
const { name, description, color, settings } = req.body;
|
|
const newCampaign = new Campaign({
|
|
name,
|
|
description,
|
|
color,
|
|
settings,
|
|
createdBy: req.user.id
|
|
});
|
|
await newCampaign.save();
|
|
res.json({ status: "ok", campaign: newCampaign });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: "error", msg: "errors.internal" });
|
|
}
|
|
});
|
|
|
|
router.post('/update', async (req, res) => {
|
|
try {
|
|
const { id, name, color, settings } = req.body;
|
|
if (!id) return res.json({ status: "error", msg: "errors.missing-data" });
|
|
|
|
const campaign = await Campaign.findOne({ _id: id, createdBy: req.user.id });
|
|
if (!campaign) return res.json({ status: "error", msg: "errors.notfound" });
|
|
|
|
if (name !== undefined) {
|
|
if (typeof name !== 'string' || !name.trim()) {
|
|
return res.json({ status: "error", msg: "errors.missing-data" });
|
|
}
|
|
campaign.name = name.trim();
|
|
}
|
|
|
|
if (color !== undefined) {
|
|
if (typeof color !== 'string') return res.json({ status: "error", msg: "errors.missing-data" });
|
|
campaign.color = color;
|
|
}
|
|
|
|
if (settings !== undefined) {
|
|
if (!settings || typeof settings !== 'object' || Array.isArray(settings)) {
|
|
return res.json({ status: "error", msg: "errors.missing-data" });
|
|
}
|
|
campaign.settings = settings;
|
|
}
|
|
|
|
await campaign.save();
|
|
res.json({ status: "ok", campaign });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: "error", msg: "errors.internal" });
|
|
}
|
|
});
|
|
|
|
// Which services a campaign has connected, and the account each of them was
|
|
// authorized with.
|
|
router.get('/services', async (req, res) => {
|
|
try {
|
|
const campaign = await Campaign.findOne({ _id: req.query.campaignId, createdBy: req.user.id });
|
|
if (!campaign) return res.json({ status: 'error', msg: 'errors.notfound' });
|
|
|
|
const links = await serviceLinks.listLinkedServices(campaign._id);
|
|
const userIds = [...new Set(links.map((link) => String(link.dragonrollUser ?? '')).filter(Boolean))];
|
|
const users = userIds.length
|
|
? await User.find({ _id: { $in: userIds } }).select('username name').lean()
|
|
: [];
|
|
const names = new Map(users.map((user) => [String(user._id), user.username || user.name]));
|
|
|
|
res.json({
|
|
status: 'ok',
|
|
services: links.map((link) => ({
|
|
...link,
|
|
dragonrollUserName: names.get(String(link.dragonrollUser ?? '')) || null
|
|
}))
|
|
});
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
router.post('/service-unlink', async (req, res) => {
|
|
try {
|
|
const service = String(req.body.service || '').trim();
|
|
if (!serviceLinks.isService(service)) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
|
|
|
const campaign = await Campaign.findOne({ _id: req.body.campaignId, createdBy: req.user.id });
|
|
if (!campaign) return res.json({ status: 'error', msg: 'errors.notfound' });
|
|
|
|
await unlinkService(campaign, service);
|
|
res.json({ status: 'ok', campaign, services: await serviceLinks.listLinkedServices(campaign._id) });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.json({ status: 'error', msg: 'errors.internal' });
|
|
}
|
|
});
|
|
|
|
router.get('/list', async (req, res) => {
|
|
try {
|
|
const campaigns = await Campaign.find({ createdBy: req.user.id });
|
|
res.json({ status: "ok", campaigns });
|
|
} catch (err) {
|
|
res.json({ status: "error", msg: "errors.internal", err });
|
|
}
|
|
});
|
|
|
|
router.get('/retrieve/:id', async (req, res) => {
|
|
try {
|
|
if (!req.user?.id) return res.status(401).json({ status: "error", msg: "errors.unauthorized" });
|
|
|
|
const campaign = await Campaign.findOne({ _id: req.params.id, createdBy: req.user.id });
|
|
if (!campaign) return res.json({ status: "error", msg: "errors.not-found" });
|
|
|
|
res.json({ status: "ok", campaign });
|
|
} catch (err) {
|
|
res.json({ status: "error", msg: "errors.internal", err });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|