mirror of
https://github.com/detleph/server.git
synced 2026-09-08 16:06:19 +02:00
Add function to create new organisations
+ Extract common functions from the admin controller
This commit is contained in:
@@ -2,7 +2,7 @@ import prisma from "../lib/prisma";
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { AdminLevel } from "@prisma/client";
|
import { AdminLevel } from "@prisma/client";
|
||||||
import argon2 from "argon2";
|
import argon2 from "argon2";
|
||||||
import { DataType, generateInvalidBodyError } from "./common";
|
import { AUTH_ERROR, createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common";
|
||||||
import { authClient } from "../lib/redis";
|
import { authClient } from "../lib/redis";
|
||||||
|
|
||||||
export const regenerateRevision = async (pid: string) => {
|
export const regenerateRevision = async (pid: string) => {
|
||||||
@@ -16,28 +16,6 @@ export const regenerateRevision = async (pid: string) => {
|
|||||||
await authClient.set(pid, revision.toISOString());
|
await authClient.set(pid, revision.toISOString());
|
||||||
};
|
};
|
||||||
|
|
||||||
const AUTH_ERROR = {
|
|
||||||
type: "failure",
|
|
||||||
payload: {
|
|
||||||
message: "The server was not able to validate your credentials; Please try again later",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const createInsufficientPermissionsError = (required: AdminLevel = "ELEVATED") => ({
|
|
||||||
type: "error",
|
|
||||||
payload: {
|
|
||||||
message: "You do not have sufficient permissions to use this feature",
|
|
||||||
required_level: required,
|
|
||||||
},
|
|
||||||
_links: [
|
|
||||||
{
|
|
||||||
rel: "authentication",
|
|
||||||
href: "/api/authentication",
|
|
||||||
type: "POST",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
// requires: auth(elevated)
|
// requires: auth(elevated)
|
||||||
export const getAllAdmins = async (req: Request, res: Response) => {
|
export const getAllAdmins = async (req: Request, res: Response) => {
|
||||||
if (!req.auth?.isAuthenticated) {
|
if (!req.auth?.isAuthenticated) {
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { AdminLevel } from "@prisma/client";
|
||||||
|
|
||||||
export enum DataType {
|
export enum DataType {
|
||||||
STRING = "string",
|
STRING = "string",
|
||||||
NUMBER = "number",
|
NUMBER = "number",
|
||||||
@@ -20,3 +22,25 @@ export function generateInvalidBodyError(body: Body) {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const AUTH_ERROR = {
|
||||||
|
type: "failure",
|
||||||
|
payload: {
|
||||||
|
message: "The server was not able to validate your credentials; Please try again later",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createInsufficientPermissionsError = (required: AdminLevel = "ELEVATED") => ({
|
||||||
|
type: "error",
|
||||||
|
payload: {
|
||||||
|
message: "You do not have sufficient permissions to use this feature",
|
||||||
|
required_level: required,
|
||||||
|
},
|
||||||
|
_links: [
|
||||||
|
{
|
||||||
|
rel: "authentication",
|
||||||
|
href: "/api/authentication",
|
||||||
|
type: "POST",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
|
import { PrismaClientUnknownRequestError } from "@prisma/client/runtime";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import prisma from "../lib/prisma";
|
import prisma from "../lib/prisma";
|
||||||
import { DataType, generateInvalidBodyError } from "./common";
|
import { AUTH_ERROR, createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common";
|
||||||
|
|
||||||
export const getAllOrganisations = async (req: Request, res: Response) => {
|
export const getAllOrganisations = async (req: Request, res: Response) => {
|
||||||
const organisations = await prisma.organisation.findMany({
|
const organisations = await prisma.organisation.findMany({
|
||||||
@@ -49,3 +50,62 @@ export const getOrganisation = async (req: Request<GetOrganisationQueryParams>,
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface CreateOrganisationQueryParams {
|
||||||
|
eventPid: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CreateOrganisationBody {
|
||||||
|
name?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// at: POST /api/events/:eventPid/organisations
|
||||||
|
// requires: auth(ELEVATED)
|
||||||
|
export const createOrganisation = async (
|
||||||
|
req: Request<CreateOrganisationQueryParams, {}, CreateOrganisationBody>,
|
||||||
|
res: Response
|
||||||
|
) => {
|
||||||
|
if (!req.auth?.isAuthenticated) {
|
||||||
|
return res.status(500).json(AUTH_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.auth.permission_level !== "ELEVATED") {
|
||||||
|
return res.status(403).json(createInsufficientPermissionsError());
|
||||||
|
}
|
||||||
|
|
||||||
|
const { name } = req.body || {};
|
||||||
|
const { eventPid } = req.params;
|
||||||
|
|
||||||
|
if (typeof name !== "string") {
|
||||||
|
return res.status(400).json(generateInvalidBodyError({ name: DataType.STRING, eventId: DataType.UUID }));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if event exists
|
||||||
|
|
||||||
|
try {
|
||||||
|
const event = await prisma.event.findUnique({ where: { pid: eventPid } });
|
||||||
|
|
||||||
|
if (!event) {
|
||||||
|
return res.status(404).json({
|
||||||
|
type: "error",
|
||||||
|
payload: {
|
||||||
|
message: `The event with the ID ${eventPid} could not be found`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// REVIEW: Check for valid UUID
|
||||||
|
if (e instanceof PrismaClientUnknownRequestError) {
|
||||||
|
return res.status(400).send({
|
||||||
|
type: "error",
|
||||||
|
payload: {
|
||||||
|
message: "Unknown error occured. This could be to malformed IDs",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const organisation = await prisma.organisation.create({ data: { name, event: { connect: { pid: eventPid } } } });
|
||||||
|
|
||||||
|
res.status(201).json({ type: "success", payload: { organisation } });
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user