Пример #1
0
    def create_new_message(self, chat_room_id, message, author_id):
        with session_scope() as session:
            chat_room = session.query(ChatRoom).get(chat_room_id)
            if chat_room is None:
                raise ResourceNotFoundException("Chat room not found")
            if ChatRoomService.is_disbanded(chat_room):
                raise ResourceNotFoundException("Chat room is disbanded")

            if (session.query(UserChatRoomAssociation).filter_by(
                    user_id=author_id,
                    chat_room_id=chat_room_id).count() == 0):
                raise ResourceNotOwnedException(
                    "User is not in this chat room")

            first_chat = (session.query(Chat).filter_by(
                chat_room_id=chat_room_id).count() == 0)

            message = Chat(
                chat_room_id=str(chat_room_id),
                message=message,
                author_id=str(author_id),
            )
            session.add(message)
            session.flush()
            chat_room.updated_at = message.created_at

            if first_chat:
                other_party_id = ChatRoomService._get_other_party_id(
                    chat_room_id=str(chat_room.id), user_id=author_id)
                other_party_email = session.query(User).get(
                    other_party_id).email
                self.email_service.send_email(emails=[other_party_email],
                                              template="new_chat_message")

            return {"type": "chat", **message.asdict()}
Пример #2
0
    def get_other_party_details(self, chat_room_id, user_id):
        with session_scope() as session:
            chat_room = session.query(ChatRoom).get(chat_room_id).asdict()

        if not chat_room["is_revealed"]:
            raise ResourceNotOwnedException("Other party has not revealed.")

        if chat_room["seller_id"] == user_id:
            other_party_user_id = chat_room["buyer_id"]
        elif chat_room["buyer_id"] == user_id:
            other_party_user_id = chat_room["seller_id"]
        else:
            raise ResourceNotOwnedException("Wrong user.")

        with session_scope() as session:
            user = session.query(User).get(other_party_user_id).asdict()
            return {k: user[k] for k in ["email", "full_name"]}
Пример #3
0
 def get_order_by_id(self, id, user_id):
     with session_scope() as session:
         order = session.query(BuyOrder).get(id)
         if order is None:
             raise ResourceNotFoundException()
         if order.user_id != user_id:
             raise ResourceNotOwnedException()
         return order.asdict()
Пример #4
0
    def delete_order(self, id, subject_id):
        with session_scope() as session:
            buy_order = session.query(BuyOrder).get(id)
            if buy_order is None:
                raise ResourceNotFoundException()
            if buy_order.user_id != subject_id:
                raise ResourceNotOwnedException("You need to own this order.")

            session.delete(buy_order)
        return {}
Пример #5
0
    def _check_deal_status(session, chat_room_id, user_id):
        chat_room = session.query(ChatRoom).get(chat_room_id)
        if chat_room is None:
            raise ResourceNotFoundException("Chat room not found")
        if chat_room.is_deal_closed:
            raise InvalidRequestException("Deal is closed")

        if (session.query(UserChatRoomAssociation).filter_by(
                user_id=user_id, chat_room_id=chat_room_id).count() == 0):
            raise ResourceNotOwnedException("User is not in this chat room")
Пример #6
0
    def delete_order(self, id, subject_id):
        with session_scope() as session:
            sell_order = session.query(SellOrder).get(id)
            if sell_order is None:
                raise ResourceNotFoundException()
            if sell_order.user_id != subject_id:
                raise ResourceNotOwnedException("You need to own this order.")

            session.query(SellOrder).filter_by(id=id).delete()
        return {}
Пример #7
0
    async def decorated_function(request, *args, **kwargs):
        PREFIX = "Bearer "
        header = request.headers.get("Authorization")
        if header is None or not header.startswith(PREFIX):
            raise InvalidAuthorizationTokenException(
                "Invalid Authorization Bearer")
        token = header[len(PREFIX):]
        linkedin_user = request.app.linkedin_login.get_linkedin_user(
            token=token)
        user = request.app.user_service.get_user_by_linkedin_id(
            provider_user_id=linkedin_user.get("provider_user_id"))
        if user is None:
            raise ResourceNotOwnedException("User not found")

        response = await f(request, user, *args, **kwargs)
        return response
Пример #8
0
    def edit_order(self, id, subject_id, new_number_of_shares=None, new_price=None):
        with session_scope() as session:
            buy_order = session.query(BuyOrder).get(id)
            if buy_order is None:
                raise ResourceNotFoundException()
            if buy_order.user_id != subject_id:
                raise ResourceNotOwnedException("You need to own this order.")

            if new_number_of_shares is not None:
                buy_order.number_of_shares = new_number_of_shares
            if new_price is not None:
                buy_order.price = new_price

            session.commit()

            user = session.query(User).get(buy_order.user_id)
            self.email_service.send_email(
                emails=[user.email], template="edit_buy_order"
            )

            return buy_order.asdict()
Пример #9
0
 def _verify_user(chat_room, user_id, user_type):
     if (user_type == "buyer" and chat_room.buyer_id != user_id) or (
         user_type == "seller" and chat_room.seller_id != user_id
     ):
         raise ResourceNotOwnedException("Wrong user")