orchidmap-front/backend/routes/route/patch.js
2019-03-21 17:58:38 +07:00

24 lines
809 B
JavaScript

const { User, Route } = require('../../models');
const { parseString } = require('../../utils/parse');
module.exports = async (req, res) => {
const { body, body: { id, token, address } } = req;
const owner = await User.findOne({ _id: id, token }).populate('routes');
if (!owner) return res.send({ success: false, reason: 'unauthorized' });
const title = parseString(body.title, 64);
const is_public = !!body.is_public;
const exists = await Route.findOne({ _id: address, is_deleted: false }).populate('owner', '_id');
if (!exists) return res.send({ success: false, mode: 'not_exists' });
if (exists && exists.owner._id !== id) return res.send({ success: false, mode: 'not_yours' });
await exists.set({ title, is_public }).save();
return res.send({ success: true, ...exists });
};