import { Body , ClassSerializerInterceptor , Controller , Delete , Get , Param , Post , Put , UseGuards , UseInterceptors , } from '@nestjs/common' ; import { OrganizationEntity } from '@notifire/dal' ; import { IJwtPayload , MemberRoleEnum } from '@notifire/shared' ; import { AuthGuard } from '@nestjs/passport' ; import { Roles } from '../auth/framework/roles.decorator' ; import { UserSession } from '../shared/framework/user.decorator' ; import { CreateOrganizationDto } from './dtos/create-organization.dto' ; import { CreateOrganizationCommand } from './usecases/create-organization/create-organization.command' ; import { CreateOrganization } from './usecases/create-organization/create-organization.usecase' ; import { GetMyOrganizationCommand } from './usecases/get-my-organization/get-my-organization.command' ; import { GetMyOrganization } from './usecases/get-my-organization/get-my-organization.usecase' ; import { RemoveMember } from './usecases/membership/remove-member/remove-member.usecase' ; import { RemoveMemberCommand } from './usecases/membership/remove-member/remove-member.command' ; import { IGetMyOrganizationDto } from './dtos/get-my-organization.dto' ; import { JwtAuthGuard } from '../auth/framework/auth.guard' ; import { GetMembersCommand } from './usecases/membership/membership/get-members/get-members.command' ; import { GetMembers } from './usecases/membership/membership/get-members/get-members.usecase' ; import { ChangeMemberRoleCommand } from './usecases/membership/membership/change-member-role/change-member-role.command' ; import { ChangeMemberRole } from './usecases/membership/membership/change-member-role/change-member-role.usecase' ; @ Controller ( '/organizations' ) @ UseInterceptors ( ClassSerializerInterceptor ) @ UseGuards ( JwtAuthGuard ) export class OrganizationController { constructor ( private createOrganizationUsecase , private getMyOrganizationUsecase , private getMembers , private removeMemberUsecase , private changeMemberRoleUsecase ) { } @ Post ( '/' ) async createOrganization ( @ UserSession ( ) user , @ Body ( ) body ) { const command = CreateOrganizationCommand . create ( { userId : user . _id , logo : body . logo , name : body . name , } ) ; const organization = await this . createOrganizationUsecase . execute ( command ) ; return organization ; } @ Delete ( '/members/:memberId' ) @ Roles ( MemberRoleEnum . ADMIN ) async removeMember ( @ UserSession ( ) user , @ Param ( 'memberId' ) memberId ) { return await this . removeMemberUsecase . execute ( RemoveMemberCommand . create ( { userId : user . _id , organizationId : user . organizationId , memberId , } ) ) ; } @ Put ( '/members/:memberId/roles' ) @ Roles ( MemberRoleEnum . ADMIN ) async updateMemberRoles ( @ UserSession ( ) user , @ Param ( 'memberId' ) memberId , @ Body ( 'role' ) role ) { return await this . changeMemberRoleUsecase . execute ( ChangeMemberRoleCommand . create ( { memberId , role , userId : user . _id , organizationId : user . organizationId , } ) ) ; } @ Get ( '/members' ) @ Roles ( MemberRoleEnum . ADMIN ) async getMember ( @ UserSession ( ) user ) { return await this . getMembers . execute ( GetMembersCommand . create ( { userId : user . _id , organizationId : user . organizationId , } ) ) ; } @ Post ( '/members/invite' ) @ Roles ( MemberRoleEnum . ADMIN ) async inviteMember ( @ UserSession ( ) user ) { return await this . getMembers . execute ( GetMembersCommand . create ( { userId : user . _id , organizationId : user . organizationId , } ) ) ; } @ Get ( '/me' ) async getMyOrganization ( @ UserSession ( ) user ) { const command = GetMyOrganizationCommand . create ( { userId : user . _id , id : user . organizationId , } ) ; return await this . getMyOrganizationUsecase . execute ( command ) ; } }