"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.videoChannelSyncRouter = void 0;
const tslib_1 = require("tslib");
const express_1 = tslib_1.__importDefault(require("express"));
const audit_logger_1 = require("../../helpers/audit-logger");
const logger_1 = require("../../helpers/logger");
const middlewares_1 = require("../../middlewares");
const video_channel_sync_1 = require("../../models/video/video-channel-sync");
const videoChannelSyncRouter = express_1.default.Router();
exports.videoChannelSyncRouter = videoChannelSyncRouter;
const auditLogger = (0, audit_logger_1.auditLoggerFactory)('channel-syncs');
videoChannelSyncRouter.post('/', middlewares_1.authenticate, middlewares_1.ensureSyncIsEnabled, (0, middlewares_1.asyncMiddleware)(middlewares_1.videoChannelSyncValidator), middlewares_1.ensureCanManageChannelOrAccount, (0, middlewares_1.asyncRetryTransactionMiddleware)(createVideoChannelSync));
videoChannelSyncRouter.delete('/:id', middlewares_1.authenticate, (0, middlewares_1.asyncMiddleware)(middlewares_1.ensureSyncExists), middlewares_1.ensureCanManageChannelOrAccount, (0, middlewares_1.asyncRetryTransactionMiddleware)(removeVideoChannelSync));
function createVideoChannelSync(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const syncCreated = new video_channel_sync_1.VideoChannelSyncModel({
            externalChannelUrl: req.body.externalChannelUrl,
            videoChannelId: req.body.videoChannelId,
            state: 1
        });
        yield syncCreated.save();
        syncCreated.VideoChannel = res.locals.videoChannel;
        auditLogger.create((0, audit_logger_1.getAuditIdFromRes)(res), new audit_logger_1.VideoChannelSyncAuditView(syncCreated.toFormattedJSON()));
        logger_1.logger.info('Video synchronization for channel "%s" with external channel "%s" created.', syncCreated.VideoChannel.name, syncCreated.externalChannelUrl);
        return res.json({
            videoChannelSync: syncCreated.toFormattedJSON()
        });
    });
}
function removeVideoChannelSync(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const syncInstance = res.locals.videoChannelSync;
        yield syncInstance.destroy();
        auditLogger.delete((0, audit_logger_1.getAuditIdFromRes)(res), new audit_logger_1.VideoChannelSyncAuditView(syncInstance.toFormattedJSON()));
        logger_1.logger.info('Video synchronization for channel "%s" with external channel "%s" deleted.', syncInstance.VideoChannel.name, syncInstance.externalChannelUrl);
        return res.type('json').status(204).end();
    });
}
//# sourceMappingURL=video-channel-sync.js.map