"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.AuthorizationService = void 0; const common_1 = require("@nestjs/common"); const prisma_service_1 = require("../prisma/prisma.service"); const platform_admin_1 = require("./platform-admin"); let AuthorizationService = class AuthorizationService { prisma; constructor(prisma) { this.prisma = prisma; } async checkWorkspaceAccess(workspaceId, userId, allowedRoles) { const membership = await this.prisma.workspaceMember.findUnique({ where: { userId_workspaceId: { userId, workspaceId } }, }); if (!membership) { throw new common_1.ForbiddenException("You do not have access to this workspace"); } const role = membership.role; if (allowedRoles && !allowedRoles.includes(role)) { throw new common_1.ForbiddenException("You do not have permission to perform this action"); } } async checkPodcastAccess(podcastId, userId) { const podcast = await this.prisma.podcast.findUnique({ where: { id: podcastId }, select: { workspaceId: true }, }); if (!podcast) { throw new common_1.NotFoundException(`Podcast ${podcastId} not found`); } await this.checkWorkspaceAccess(podcast.workspaceId, userId); return { workspaceId: podcast.workspaceId }; } async checkEpisodeAccess(episodeId, userId) { const episode = await this.prisma.episode.findUnique({ where: { id: episodeId }, select: { podcastId: true, podcast: { select: { workspaceId: true } } }, }); if (!episode) { throw new common_1.NotFoundException(`Episode ${episodeId} not found`); } await this.checkWorkspaceAccess(episode.podcast.workspaceId, userId); return { podcastId: episode.podcastId, workspaceId: episode.podcast.workspaceId }; } async checkSequenceAccess(sequenceId, userId) { const sequence = await this.prisma.outreachSequence.findUnique({ where: { id: sequenceId }, select: { guest: { select: { podcast: { select: { workspaceId: true } } } } }, }); if (!sequence) { throw new common_1.NotFoundException(`Sequence ${sequenceId} not found`); } await this.checkWorkspaceAccess(sequence.guest.podcast.workspaceId, userId); } async checkRecordingAccess(episodeId, userId) { await this.checkEpisodeAccess(episodeId, userId); } async checkClipAccess(clipId, userId) { const clip = await this.prisma.clip.findUnique({ where: { id: clipId }, select: { episode: { select: { podcast: { select: { workspaceId: true } } } } }, }); if (!clip) { throw new common_1.NotFoundException(`Clip ${clipId} not found`); } await this.checkWorkspaceAccess(clip.episode.podcast.workspaceId, userId); } async checkAdminAccess(userId) { await (0, platform_admin_1.assertPlatformAdmin)(this.prisma, userId); } async checkAssetRequestAccess(requestId, userId) { const request = await this.prisma.assetRequest.findUnique({ where: { id: requestId }, select: { episode: { select: { podcast: { select: { workspaceId: true } } } } }, }); if (!request) { throw new common_1.NotFoundException(`Asset request ${requestId} not found`); } await this.checkWorkspaceAccess(request.episode.podcast.workspaceId, userId); } async checkAssetSubmissionAccess(submissionId, userId) { const submission = await this.prisma.assetSubmission.findUnique({ where: { id: submissionId }, select: { request: { select: { episode: { select: { podcast: { select: { workspaceId: true } } } } } } }, }); if (!submission) { throw new common_1.NotFoundException(`Asset submission ${submissionId} not found`); } await this.checkWorkspaceAccess(submission.request.episode.podcast.workspaceId, userId); } async checkBookingAccess(bookingId, userId) { const booking = await this.prisma.booking.findUnique({ where: { id: bookingId }, select: { episode: { select: { podcast: { select: { workspaceId: true } } } } }, }); if (!booking) { throw new common_1.NotFoundException(`Booking ${bookingId} not found`); } await this.checkWorkspaceAccess(booking.episode.podcast.workspaceId, userId); } }; exports.AuthorizationService = AuthorizationService; exports.AuthorizationService = AuthorizationService = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [prisma_service_1.PrismaService]) ], AuthorizationService); //# sourceMappingURL=authorization.service.js.map