You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.4 KiB
88 lines
2.4 KiB
import { Router, Request } from "express"; |
|
import { GetUserMiddleware } from "../middlewares/user"; |
|
import RequestError, { HttpStatusCode } from "../../helper/request_error"; |
|
import promiseMiddleware from "../../helper/promiseMiddleware"; |
|
import Client from "../../models/client"; |
|
import User from "../../models/user"; |
|
import verify, { Types } from "../middlewares/verify"; |
|
import { randomBytes } from "crypto"; |
|
|
|
|
|
const ClientRouter: Router = Router(); |
|
ClientRouter.use(GetUserMiddleware(true, true), (req: Request, res, next) => { |
|
if (!req.isAdmin) res.sendStatus(HttpStatusCode.FORBIDDEN) |
|
else next() |
|
}); |
|
ClientRouter.route("/") |
|
.get(promiseMiddleware(async (req, res) => { |
|
let clients = await Client.find({}); |
|
//ToDo check if user is required! |
|
res.json(clients); |
|
})) |
|
.delete(promiseMiddleware(async (req, res) => { |
|
let { id } = req.query; |
|
await Client.delete(id); |
|
res.json({ success: true }); |
|
})) |
|
.post(verify({ |
|
internal: { |
|
type: Types.BOOLEAN, |
|
optional: true |
|
}, |
|
name: { |
|
type: Types.STRING |
|
}, |
|
redirect_url: { |
|
type: Types.STRING |
|
}, |
|
website: { |
|
type: Types.STRING |
|
}, |
|
logo: { |
|
type: Types.STRING, |
|
optional: true |
|
} |
|
}, true), promiseMiddleware(async (req, res) => { |
|
req.body.client_secret = randomBytes(32).toString("hex"); |
|
let client = Client.new(req.body); |
|
client.maintainer = req.user._id; |
|
await Client.save(client) |
|
res.json(client); |
|
})) |
|
.put(verify({ |
|
id: { |
|
type: Types.STRING, |
|
query: true |
|
}, |
|
internal: { |
|
type: Types.BOOLEAN, |
|
optional: true |
|
}, |
|
name: { |
|
type: Types.STRING, |
|
optional: true |
|
}, |
|
redirect_url: { |
|
type: Types.STRING, |
|
optional: true |
|
}, |
|
website: { |
|
type: Types.STRING, |
|
optional: true |
|
}, |
|
logo: { |
|
type: Types.STRING, |
|
optional: true |
|
} |
|
}, true), promiseMiddleware(async (req, res) => { |
|
let { id } = req.query; |
|
let client = await Client.findById(id); |
|
if (!client) throw new RequestError(req.__("Client not found"), HttpStatusCode.BAD_REQUEST); |
|
for (let key in req.body) { |
|
client[key] = req.body[key]; |
|
} |
|
await Client.save(client); |
|
res.json(client); |
|
})) |
|
|
|
export default ClientRouter; |