2022-06-14 20:08:58 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
|
|
|
|
2022-06-23 22:09:23 +00:00
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
2022-06-14 20:08:58 +00:00
|
|
|
import { defaultResponder } from "@calcom/lib/server";
|
|
|
|
|
|
|
|
import { schemaUserCreateBodyParams } from "@lib/validations/user";
|
|
|
|
|
2022-06-29 22:01:14 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /users:
|
|
|
|
* post:
|
|
|
|
* operationId: addUser
|
|
|
|
* summary: Creates a new user
|
2022-11-18 19:20:15 +00:00
|
|
|
* parameters:
|
|
|
|
* - in: query
|
|
|
|
* name: apiKey
|
|
|
|
* required: true
|
|
|
|
* schema:
|
|
|
|
* type: string
|
|
|
|
* description: Your API key
|
|
|
|
* requestBody:
|
|
|
|
* description: Create a new user
|
|
|
|
* required: true
|
|
|
|
* content:
|
|
|
|
* application/json:
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* required:
|
|
|
|
* - email
|
|
|
|
* - username
|
|
|
|
* properties:
|
|
|
|
* email:
|
|
|
|
* type: string
|
|
|
|
* format: email
|
|
|
|
* description: Email that belongs to the user being edited
|
|
|
|
* username:
|
|
|
|
* type: string
|
|
|
|
* description: Username for the user being created
|
|
|
|
* brandColor:
|
|
|
|
* description: The new user's brand color
|
|
|
|
* type: string
|
|
|
|
* darkBrandColor:
|
|
|
|
* description: The new user's brand color for dark mode
|
|
|
|
* type: string
|
|
|
|
* weekStart:
|
|
|
|
* description: Start of the week. Acceptable values are one of [SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]
|
|
|
|
* type: string
|
2022-11-21 05:19:55 +00:00
|
|
|
* timeZone:
|
2022-11-18 19:20:15 +00:00
|
|
|
* description: The new user's time zone
|
|
|
|
* type: string
|
|
|
|
* theme:
|
|
|
|
* description: Default theme for the new user. Acceptable values are one of [DARK, LIGHT]
|
|
|
|
* type: string
|
|
|
|
* timeFormat:
|
|
|
|
* description: The new user's time format. Acceptable values are one of [TWELVE, TWENTY_FOUR]
|
|
|
|
* type: string
|
|
|
|
* locale:
|
|
|
|
* description: The new user's locale. Acceptable values are one of [EN, FR, IT, RU, ES, DE, PT, RO, NL, PT_BR, ES_419, KO, JA, PL, AR, IW, ZH_CH, ZH_TW, CS, SR, SV, VI]
|
|
|
|
* type: string
|
|
|
|
* examples:
|
|
|
|
* user:
|
|
|
|
* summary: An example of USER
|
|
|
|
* value:
|
|
|
|
* email: email@example.com
|
|
|
|
* username: johndoe
|
|
|
|
* weekStart: MONDAY
|
|
|
|
* brandColor: #555555
|
|
|
|
* darkBrandColor: #111111
|
|
|
|
* timeZone: EUROPE/PARIS
|
|
|
|
* theme: LIGHT
|
|
|
|
* timeFormat: TWELVE
|
|
|
|
* locale: FR
|
2022-06-29 22:01:14 +00:00
|
|
|
* tags:
|
|
|
|
* - users
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, user created
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. user body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-06-14 20:08:58 +00:00
|
|
|
async function postHandler(req: NextApiRequest) {
|
2022-06-23 22:53:15 +00:00
|
|
|
const { prisma, isAdmin } = req;
|
2022-06-14 20:08:58 +00:00
|
|
|
// If user is not ADMIN, return unauthorized.
|
|
|
|
if (!isAdmin) throw new HttpError({ statusCode: 401, message: "You are not authorized" });
|
2022-11-17 18:35:06 +00:00
|
|
|
const data = await schemaUserCreateBodyParams.parseAsync(req.body);
|
2022-06-14 20:08:58 +00:00
|
|
|
const user = await prisma.user.create({ data });
|
|
|
|
req.statusCode = 201;
|
|
|
|
return { user };
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultResponder(postHandler);
|