async def list(self, organization_id: OrganizationID, greeter: UserID) -> List[Invitation]: async with self.dbh.pool.acquire() as conn: rows = await conn.fetch( *_q_list_invitations( organization_id=organization_id.str, greeter_user_id=greeter.str ) ) invitations_with_claimer_online = self._claimers_ready[organization_id] invitations = [] for ( token_uuid, type, greeter, greeter_human_handle_email, greeter_human_handle_label, claimer_email, created_on, deleted_on, deleted_reason, ) in rows: token = InvitationToken(token_uuid) greeter_human_handle = None if greeter_human_handle_email: greeter_human_handle = HumanHandle( email=greeter_human_handle_email, label=greeter_human_handle_label ) if deleted_on: status = InvitationStatus.DELETED elif token in invitations_with_claimer_online: status = InvitationStatus.READY else: status = InvitationStatus.IDLE invitation: Invitation if type == InvitationType.USER.value: invitation = UserInvitation( greeter_user_id=UserID(greeter), greeter_human_handle=greeter_human_handle, claimer_email=claimer_email, token=token, created_on=created_on, status=status, ) else: # Device invitation = DeviceInvitation( greeter_user_id=UserID(greeter), greeter_human_handle=greeter_human_handle, token=token, created_on=created_on, status=status, ) invitations.append(invitation) return invitations
async def _new( self, organization_id: OrganizationID, greeter_user_id: UserID, created_on: Optional[DateTime], claimer_email: Optional[str] = None, ) -> Invitation: org = self._organizations[organization_id] for invitation in org.invitations.values(): if ( invitation.greeter_user_id == greeter_user_id and getattr(invitation, "claimer_email", None) == claimer_email and invitation.token not in org.deleted_invitations ): # An invitation already exists for what the user has asked for return invitation else: # Must create a new invitation created_on = created_on or pendulum_now() greeter_human_handle = self._user_component._get_user( organization_id, greeter_user_id ).human_handle if claimer_email: invitation = UserInvitation( greeter_user_id=greeter_user_id, greeter_human_handle=greeter_human_handle, claimer_email=claimer_email, created_on=created_on, ) else: # Device invitation = DeviceInvitation( greeter_user_id=greeter_user_id, greeter_human_handle=greeter_human_handle, created_on=created_on, ) org.invitations[invitation.token] = invitation await self._send_event( BackendEvent.INVITE_STATUS_CHANGED, organization_id=organization_id, greeter=invitation.greeter_user_id, token=invitation.token, status=invitation.status, ) return invitation
async def info(self, organization_id: OrganizationID, token: InvitationToken) -> Invitation: async with self.dbh.pool.acquire() as conn: row = await conn.fetchrow( *_q_info_invitation(organization_id=organization_id.str, token=token.uuid) ) if not row: raise InvitationNotFoundError(token) ( type, greeter, greeter_human_handle_email, greeter_human_handle_label, claimer_email, created_on, deleted_on, deleted_reason, ) = row if deleted_on: raise InvitationAlreadyDeletedError(token) greeter_human_handle = None if greeter_human_handle_email: greeter_human_handle = HumanHandle( email=greeter_human_handle_email, label=greeter_human_handle_label ) if type == InvitationType.USER.value: return UserInvitation( greeter_user_id=UserID(greeter), greeter_human_handle=greeter_human_handle, claimer_email=claimer_email, token=token, created_on=created_on, status=InvitationStatus.READY, ) else: # Device return DeviceInvitation( greeter_user_id=UserID(greeter), greeter_human_handle=greeter_human_handle, token=token, created_on=created_on, status=InvitationStatus.READY, )
async def new_for_device( self, organization_id: OrganizationID, greeter_user_id: UserID, created_on: Optional[DateTime] = None, ) -> DeviceInvitation: """ Raise: Nothing """ created_on = created_on or pendulum_now() async with self.dbh.pool.acquire() as conn, conn.transaction(): token = await _do_new_user_invitation( conn, organization_id=organization_id, greeter_user_id=greeter_user_id, claimer_email=None, created_on=created_on, ) return DeviceInvitation( greeter_user_id=greeter_user_id, greeter_human_handle=None, token=token, created_on=created_on, )