"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthSessionEventListener = void 0;
const common_1 = require("@nestjs/common");
const event_emitter_1 = require("@nestjs/event-emitter");
const auth_constants_1 = require("../../auth.constants");
const user_updated_event_1 = require("../../user/events/user-updated.event");
const user_deleted_event_1 = require("../../user/events/user-deleted.event");
const session_manager_service_1 = require("../../session/services/session-manager.service");
const debug_logger_service_1 = require("../../core/services/debug-logger.service");
let AuthSessionEventListener = class AuthSessionEventListener {
    sessionManager;
    debugLogger;
    constructor(sessionManager, debugLogger) {
        this.sessionManager = sessionManager;
        this.debugLogger = debugLogger;
    }
    async handleUserUpdated(event) {
        const { user, updatedFields } = event.payload;
        if (updatedFields.includes('isActive') && user.isActive === false) {
            this.debugLogger.info('User deactivated. Revoking all sessions.', 'AuthSessionEventListener', { userId: user.id });
            await this.sessionManager.revokeAllUserSessions(user.id);
        }
    }
    async handleUserDeleted(event) {
        const { user } = event.payload;
        this.debugLogger.info('User deleted. Revoking all sessions.', 'AuthSessionEventListener', { userId: user.id });
        await this.sessionManager.revokeAllUserSessions(user.id);
    }
};
exports.AuthSessionEventListener = AuthSessionEventListener;
__decorate([
    (0, event_emitter_1.OnEvent)(auth_constants_1.NestAuthEvents.USER_UPDATED),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [user_updated_event_1.UserUpdatedEvent]),
    __metadata("design:returntype", Promise)
], AuthSessionEventListener.prototype, "handleUserUpdated", null);
__decorate([
    (0, event_emitter_1.OnEvent)(auth_constants_1.NestAuthEvents.USER_DELETED),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [user_deleted_event_1.UserDeletedEvent]),
    __metadata("design:returntype", Promise)
], AuthSessionEventListener.prototype, "handleUserDeleted", null);
exports.AuthSessionEventListener = AuthSessionEventListener = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [session_manager_service_1.SessionManagerService,
        debug_logger_service_1.DebugLoggerService])
], AuthSessionEventListener);
//# sourceMappingURL=auth-session-event-listener.service.js.map