import POSTTokensParamsSchema from "../schemas/tokens/PUT/params.json";
import DELETETokenParamsSchema from "../schemas/tokens/DELETE/params.json";
const tokensPlugin = (fastify, opts, done) => {
    fastify.addHook("onRequest", fastify.basicAuth);
    const baseSchema = {
        tags: ["Admin"],
        security: [
            {
                Admin: [],
            },
        ],
    };
    fastify.put("/:id", {
        schema: {
            ...baseSchema,
            params: POSTTokensParamsSchema,
            summary: "Issue new token",
        },
        async handler(request, reply) {
            var _a;
            const { id } = request.params;
            const token = await fastify.tokens.create(id);
            (_a = fastify.telemetrist) === null || _a === void 0 ? void 0 : _a.dispatch("token created");
            reply.status(201).send(token);
        },
    });
    fastify.delete("/:id", {
        schema: {
            ...baseSchema,
            params: DELETETokenParamsSchema,
            summary: "Revoke token",
        },
        async handler(request, reply) {
            var _a;
            const tokenId = request.params.id;
            const success = await fastify.tokens.revoke(tokenId);
            (_a = fastify.telemetrist) === null || _a === void 0 ? void 0 : _a.dispatch("token revoked");
            if (success) {
                await fastify.jobs.emptyToken(tokenId);
                reply.status(204);
            }
            else {
                reply.status(404).send("Not Found");
            }
        },
    });
    done();
};
export default tokensPlugin;
//# sourceMappingURL=tokens.js.map