Source: RouterProject/projectSnapshot/controller.js

// @ts-check
/**
 * Snapshot controllers (contract envelope).
 * @import {ExpressRequestAuthorized, ExpressResponse} from '../../types.js'
 */

import { sendOk, sendErrorFrom } from '../../utils/apiEnvelope.js';
import * as service from './service.js';

/**
 * @swagger
 * /api/project/project/{projectUid}/snapshot/current:
 *   post:
 *     summary: Current time-exact project snapshot
 *     description: |
 *       Reads the authoritative MariaDB CURRENT_TIMESTAMP(6) first and resolves
 *       project, shares, links and metadata with exactly this value. The
 *       snapshot never contains user rights.
 *     tags:
 *       - Project Snapshots
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: projectUid
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Snapshot
 *         content:
 *           application/json:
 *             schema:
 *               $ref: './projectSnapshot.swagger.yaml#/components/schemas/SuccessResponse'
 *       403:
 *         description: Project not accessible
 *         content:
 *           application/json:
 *             schema:
 *               $ref: './projectSnapshot.swagger.yaml#/components/schemas/ErrorResponse'
 */
/**
 * POST /projects/:projectUid/snapshot/current
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const postSnapshotCurrent = async (req, res) => {
    try {
        const result = await service.snapshotCurrent(req, req.params.projectUid);
        sendOk(res, result.result);
    } catch (e) {
        sendErrorFrom(res, e);
    }
};

/**
 * @swagger
 * /api/project/project/{projectUid}/snapshot:
 *   get:
 *     summary: Historical project snapshot
 *     description: |
 *       Resolves the project state at the given RFC3339 point in time. A point
 *       older than the guaranteed 13-year temporal history returns
 *       `410 SNAPSHOT_EXPIRED`; a point where no project state existed returns
 *       `404 SNAPSHOT_NOT_FOUND`.
 *     tags:
 *       - Project Snapshots
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: projectUid
 *         required: true
 *         schema:
 *           type: string
 *       - in: query
 *         name: asOf
 *         required: true
 *         schema:
 *           type: string
 *           format: date-time
 *         description: RFC 3339 timestamp with microseconds (UTC)
 *     responses:
 *       200:
 *         description: Snapshot
 *         content:
 *           application/json:
 *             schema:
 *               $ref: './projectSnapshot.swagger.yaml#/components/schemas/SuccessResponse'
 *       400:
 *         description: Invalid asOf timestamp
 *         content:
 *           application/json:
 *             schema:
 *               $ref: './projectSnapshot.swagger.yaml#/components/schemas/ErrorResponse'
 *       404:
 *         description: No project state exists at this time
 *         content:
 *           application/json:
 *             schema:
 *               $ref: './projectSnapshot.swagger.yaml#/components/schemas/ErrorResponse'
 *       410:
 *         description: Snapshot older than the guaranteed temporal history
 *         content:
 *           application/json:
 *             schema:
 *               $ref: './projectSnapshot.swagger.yaml#/components/schemas/ErrorResponse'
 */
/**
 * GET /projects/:projectUid/snapshot?asOf=...
 * @param {ExpressRequestAuthorized} req
 * @param {ExpressResponse} res
 */
export const getSnapshotAsOf = async (req, res) => {
    try {
        const asOf = typeof req.query.asOf === 'string' ? req.query.asOf : '';
        const result = await service.snapshotAsOf(req, req.params.projectUid, asOf);
        sendOk(res, result.result);
    } catch (e) {
        sendErrorFrom(res, e);
    }
};