import { IMemberInvite , MemberRoleEnum , MemberStatusEnum } from '@notifire/shared' ; import { MemberEntity } from './member.entity' ; import { BaseRepository } from '../base-repository' ; import { Member } from './member.schema' ; import { OrganizationEntity } from '../organization' ; interface IAddMemberData { _userId ? : string ; roles : MemberRoleEnum [ ] ; invite ? : IMemberInvite ; memberStatus : MemberStatusEnum ; } export class MemberRepository extends BaseRepository < MemberEntity > { constructor ( ) { super ( Member , MemberEntity ) ; } async removeMemberById ( organizationId , memberId ) { return Member . remove ( { _id : memberId , _organizationId : organizationId , } ) ; } async updateMemberRoles ( organizationId , memberId , roles ) { return this . update ( { _id : memberId , _organizationId : organizationId , } , { roles , } ) ; } async getOrganizationMembers ( organizationId ) { const members = await Member . find ( { _organizationId : organizationId , } ) . populate ( '_userId' , 'firstName lastName email _id profilePicture createdAt' ) ; if ( ! members ) return [ ] ; const membersEntity = this . mapEntities ( members ) ; return [ ... membersEntity . map ( ( member ) => { return { ... member , _userId : member . _userId ? member . _userId . _id : null , user : member . _userId , } ; } ) , ] ; } async getOrganizationAdmins ( organizationId ) { const members = await Member . find ( { _organizationId : organizationId , } ) . populate ( '_userId' , 'firstName lastName email _id' ) ; if ( ! members ) return [ ] ; const membersEntity = this . mapEntities ( members ) ; return [ ... membersEntity . filter ( ( i ) => i . roles . includes ( MemberRoleEnum . ADMIN ) ) . map ( ( member ) => { return { ... member , _userId : member . _userId ? ( member . _userId as any ) . _id : null , user : member . _userId , } ; } ) , ] ; } async findUserActiveMembers ( userId ) { return await this . find ( { _userId : userId , memberStatus : MemberStatusEnum . ACTIVE , } ) ; } async convertInvitedUserToMember ( token , data ) { await this . update ( { 'invite.token' : token , } , { memberStatus : data . memberStatus , _userId : data . _userId , 'invite.answerDate' : data . answerDate , } ) ; } async findByInviteToken ( token ) { return await this . findOne ( { 'invite.token' : token , } ) ; } async findInviteeByEmail ( organizationId , email ) { const foundMember = await this . findOne ( { _organizationId : organizationId , 'invite.email' : email , } ) ; if ( ! foundMember ) return null ; return foundMember ; } async addMember ( organizationId , member ) { await this . create ( { _userId : member . _userId , roles : member . roles , invite : member . invite , memberStatus : member . memberStatus , _organizationId : organizationId , } ) ; } async isMemberOfOrganization ( organizationId , userId ) { return ! ! ( await this . findOne ( { _organizationId : organizationId , _userId : userId , } , '_id' ) ) ; } async findMemberByUserId ( organizationId , userId ) { const member = await this . findOne ( { _organizationId : organizationId , _userId : userId , } ) ; if ( ! member ) return null ; return this . mapEntity ( member ) as any ; } async findMemberById ( organizationId , memberId ) { const member = await this . findOne ( { _organizationId : organizationId , _id : memberId , } ) ; if ( ! member ) return null ; return this . mapEntity ( member ) as any ; } }