import { forwardRef , Inject , Injectable , UnauthorizedException } from '@nestjs/common' ; import { MemberRepository , OrganizationRepository , UserRepository } from '@notifire/dal' ; import { SwitchOrganizationCommand } from './switch-organization.command' ; import { AuthService } from '../../services/auth.service' ; @ Injectable ( ) export class SwitchOrganization { constructor ( private organizationRepository , private userRepository , private memberRepository , @ Inject ( forwardRef ( ( ) => AuthService ) ) private authService ) { } async execute ( command ) { const isAuthenticated = await this . authService . isAuthenticatedForOrganization ( command . userId , command . newOrganizationId ) ; if ( ! isAuthenticated ) { throw new UnauthorizedException ( `Not authorized for organization ${ command . newOrganizationId }` ) ; } const member = await this . memberRepository . findMemberByUserId ( command . newOrganizationId , command . userId ) ; const user = await this . userRepository . findById ( command . userId ) ; const token = await this . authService . getSignedToken ( user , command . newOrganizationId , member ) ; return token ; } }