2022-07-12 23:14:36 +02:00
|
|
|
import { getAllWebsites, getUserWebsites } from 'queries';
|
2020-08-12 07:24:41 +02:00
|
|
|
import { useAuth } from 'lib/middleware';
|
2020-09-11 08:55:29 +02:00
|
|
|
import { ok, methodNotAllowed, unauthorized } from 'lib/response';
|
2020-08-12 07:24:41 +02:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
await useAuth(req, res);
|
|
|
|
|
2020-09-17 20:40:04 +02:00
|
|
|
const { user_id: current_user_id, is_admin } = req.auth;
|
2021-12-02 01:03:18 +01:00
|
|
|
const { user_id, include_all } = req.query;
|
2020-09-18 07:52:20 +02:00
|
|
|
const userId = +user_id;
|
2020-08-12 07:24:41 +02:00
|
|
|
|
|
|
|
if (req.method === 'GET') {
|
2020-09-18 09:34:22 +02:00
|
|
|
if (userId && userId !== current_user_id && !is_admin) {
|
2020-09-11 08:55:29 +02:00
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2021-12-02 01:03:18 +01:00
|
|
|
const websites =
|
|
|
|
is_admin && include_all
|
|
|
|
? await getAllWebsites()
|
|
|
|
: await getUserWebsites(userId || current_user_id);
|
2020-08-12 07:24:41 +02:00
|
|
|
|
|
|
|
return ok(res, websites);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|