routes/choreo.jsjavascript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.choreoRouter = void 0;
const express_1 = require("express");
const zod_1 = require("zod");
const ChoreoService_1 = __importDefault(require("../services/ChoreoService"));
const errors_1 = require("../utils/errors.js");
const validateMiddleware_1 = require("../middlewares/validateMiddleware.js");
const zodSchemas_1 = require("../utils/zodSchemas.js");
const { default: AuthService } = require("../services/AuthService");
const createChoreoSchema = zod_1.z.object({
    name: zod_1.z.string().min(1),
    counts: zod_1.z.number().int().min(0),
    matType: zod_1.z.string().min(1),
    seasonTeamId: zod_1.z.uuid(),
    participants: zod_1.z
        .array(zod_1.z.object({ id: zod_1.z.uuid(), color: zodSchemas_1.colorHex.optional() }))
        .optional()
        .default([]),
    ownerId: zod_1.z.uuid().optional(),
});
const updateChoreoSchema = createChoreoSchema.partial();
const choreoParticipationParams = zod_1.z.object({
    id: zod_1.z.uuid(),
    participationId: zod_1.z.uuid(),
});
const choreoParticipantParams = zod_1.z.object({
    id: zod_1.z.uuid(),
    participantId: zod_1.z.uuid(),
});
const addParticipantSchema = zod_1.z.object({
    MemberId: zod_1.z.uuid(),
    color: zodSchemas_1.colorHex.optional(),
});
const replaceParticipantSchema = zod_1.z.object({
    memberToRemoveId: zod_1.z.uuid(),
    memberToAddId: zod_1.z.uuid(),
});
const changeColorSchema = zod_1.z.object({
    color: zodSchemas_1.colorHex,
});
/**
 * @swagger
 * tags:
 * - name: Choreos
 */
/** @ignore */
const router = (0, express_1.Router)();
exports.choreoRouter = router;
/**
 * @openapi
 * /choreo/{id}:
 *  get:
 *    description: Get a list of choreos or a specific choreo
 *    tags:
 *    - Choreos
 *    security:
 *      - userAuthentication: []
 *    parameters:
 *    - name: id
 *      in: path
 *      required: false
 *      schema:
 *        type: string
 *        format: uuid
 *    responses:
 *      200:
 *        description: Either a list of choreos if id was not defined or a specific choreo otherwise
 *        content:
 *          application/json:
 *            schema:
 *              oneOf:
 *              - $ref: '#/components/schemas/Choreo'
 *              - type: array
 *                items:
 *                  $ref: '#/components/schemas/Choreo'
 *      401:
 *        $ref: '#/components/responses/UnauthorizedError'
 *      404:
 *        description: Choreo was not found
 *        content:
 *          application/json:
 *            schema:
 *              type: string
 *              example: Not found
 */
router.get("/{:id}", AuthService.authenticateUser(), (0, validateMiddleware_1.validate)(zodSchemas_1.uuidParamsOptional, "params"), (req, res, next) => {
    if (req.params.id)
        ChoreoService_1.default.findById(req.params.id, req.actingUserId)
            .then((foundChoreo) => {
            if (!foundChoreo)
                throw new errors_1.NotFoundError();
            else
                res.send(foundChoreo);
            return next();
        })
            .catch((e) => next(e));
    else {
        ChoreoService_1.default.getAll(req.ownerIds, req.actingUserId)
            .then((choreoList) => {
            res.send(choreoList);
            return next();
        })
            .catch((e) => next(e));
    }
});
/**
 * @openapi
 * /choreo/:
 *   post:
 *     description: Create a new choreo
 *     tags:
 *       - Choreos
 *     security:
 *       - userAuthentication: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - counts
 *               - matType
 *               - seasonTeamId
 *             properties:
 *               name:
 *                 type: string
 *               counts:
 *                 type: integer
 *               matType:
 *                 type: string
 *               seasonTeamId:
 *                 type: string
 *               participants:
 *                 type: array
 *                 items:
 *                   type: string
 *               ownerId:
 *                 type: string | null
 *                 description: Owner ID. If null/undefined, falls back to actingUserId
 *     responses:
 *       200:
 *         description: Choreo created successfully
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Choreo'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 */
router.post("/", AuthService.authenticateUser(), (0, validateMiddleware_1.validate)(createChoreoSchema), (req, res, next) => {
    const { name, counts, matType, seasonTeamId, participants, ownerId } = req.body;
    return ChoreoService_1.default.create(name, counts, matType, seasonTeamId, participants, ownerId || req.actingUserId, req.actingUserId)
        .then((choreo) => {
        res.send(choreo);
        return next();
    })
        .catch((e) => next(e));
});
/**
 * @openapi
 * /choreo/{id}:
 *   put:
 *     description: Update a choreo by ID
 *     tags:
 *       - Choreos
 *     security:
 *       - userAuthentication: []
 *     parameters:
 *       - name: id
 *         in: path
 *         required: true
 *         schema:
 *           type: string
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/Choreo'
 *     responses:
 *       200:
 *         description: Choreo updated successfully
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Choreo'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       404:
 *         description: Choreo not found
 */
router.put("/:id", AuthService.authenticateUser(), (0, validateMiddleware_1.validate)(zodSchemas_1.uuidParams, "params"), (0, validateMiddleware_1.validate)(updateChoreoSchema), (req, res, next) => {
    return ChoreoService_1.default.update(req.params.id, req.body, req.actingUserId)
        .then((choreo) => {
        res.send(choreo);
        return next();
    })
        .catch((e) => next(e));
});
/**
 * @openapi
 * /choreo/{id}:
 *   delete:
 *     description: Delete a choreo by ID
 *     tags:
 *       - Choreos
 *     security:
 *       - userAuthentication: []
 *     parameters:
 *       - name: id
 *         in: path
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Choreo deleted successfully
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 *       404:
 *         description: Choreo not found
 */
router.delete("/:id", AuthService.authenticateUser(), (0, validateMiddleware_1.validate)(zodSchemas_1.uuidParams, "params"), (req, res, next) => {
    return ChoreoService_1.default.remove(req.params.id, req.actingUserId)
        .then(() => {
        res.send();
        return next();
    })
        .catch((e) => next(e));
});
/**
 * @openapi
 * /choreo/{id}/participants:
 *   post:
 *     description: Add a participant to a choreo
 *     tags:
 *       - Choreos
 *     security:
 *       - userAuthentication: []
 *     parameters:
 *       - name: id
 *         in: path
 *         required: true
 *         schema:
 *           type: string
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - MemberId
 *             properties:
 *               MemberId:
 *                 type: string
 *               color:
 *                 type: string
 *     responses:
 *       200:
 *         description: Participant added successfully
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 */
router.post("/:id/participants", AuthService.authenticateUser(), (0, validateMiddleware_1.validate)(zodSchemas_1.uuidParams, "params"), (0, validateMiddleware_1.validate)(addParticipantSchema), (req, res, next) => {
    const { MemberId, color } = req.body;
    return ChoreoService_1.default.addParticipant(req.params.id, MemberId, req.actingUserId, false, color)
        .then(() => {
        res.send();
        return next();
    })
        .catch((e) => next(e));
});
/**
 * @openapi
 * /choreo/{id}/participants/{participationId}:
 *   delete:
 *     description: Remove a participant from a choreo
 *     tags:
 *       - Choreos
 *     security:
 *       - userAuthentication: []
 *     parameters:
 *       - name: id
 *         in: path
 *         required: true
 *         schema:
 *           type: string
 *       - name: participationId
 *         in: path
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Participant removed successfully
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 */
router.delete("/:id/participants/:participationId", AuthService.authenticateUser(), (0, validateMiddleware_1.validate)(choreoParticipationParams, "params"), (req, res, next) => {
    const params = req.params;
    return ChoreoService_1.default.removeParticipant(params.id, params.participationId, req.actingUserId)
        .then(() => {
        res.send();
        return next();
    })
        .catch((e) => next(e));
});
/**
 * @openapi
 * /choreo/{id}/participants:
 *   patch:
 *     description: Replace a participant in a choreo
 *     tags:
 *       - Choreos
 *     security:
 *       - userAuthentication: []
 *     parameters:
 *       - name: id
 *         in: path
 *         required: true
 *         schema:
 *           type: string
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - memberToRemoveId
 *               - memberToAddId
 *             properties:
 *               memberToRemoveId:
 *                 type: string
 *               memberToAddId:
 *                 type: string
 *     responses:
 *       200:
 *         description: Participant replaced successfully
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Choreo'
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 */
router.patch("/:id/participants", AuthService.authenticateUser(), (0, validateMiddleware_1.validate)(zodSchemas_1.uuidParams, "params"), (0, validateMiddleware_1.validate)(replaceParticipantSchema), (req, res, next) => {
    const { memberToRemoveId, memberToAddId } = req.body;
    return ChoreoService_1.default.replaceParticipant(req.params.id, memberToAddId, memberToRemoveId, req.actingUserId)
        .then((choreo) => {
        res.send(choreo);
        return next();
    })
        .catch((e) => next(e));
});
/**
 * @openapi
 * /choreo/{id}/participants/{participantId}:
 *   patch:
 *     description: Change the color of a participant in a choreo
 *     tags:
 *       - Choreos
 *     security:
 *       - userAuthentication: []
 *     parameters:
 *       - name: id
 *         in: path
 *         required: true
 *         schema:
 *           type: string
 *       - name: participantId
 *         in: path
 *         required: true
 *         schema:
 *           type: string
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - color
 *             properties:
 *               color:
 *                 type: string
 *     responses:
 *       200:
 *         description: Color changed successfully
 *       401:
 *         $ref: '#/components/responses/UnauthorizedError'
 */
router.patch("/:id/participants/:participantId", AuthService.authenticateUser(), (0, validateMiddleware_1.validate)(choreoParticipantParams, "params"), (0, validateMiddleware_1.validate)(changeColorSchema), (req, res, next) => {
    const { color } = req.body;
    const params = req.params;
    return ChoreoService_1.default.changeParticipationColor(params.id, params.participantId, color, req.actingUserId)
        .then(() => {
        res.send();
        return next();
    })
        .catch((e) => next(e));
});