コード例 #1
0
ファイル: services.py プロジェクト: acquity3/api
    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
ファイル: services.py プロジェクト: indocomsoft/api
    def create_order(self, user_id, number_of_shares, price, security_id):
        with session_scope() as session:
            user = session.query(User).get(user_id)
            if user is None:
                raise ResourceNotFoundException()
            if user.asdict()["can_buy"] == "NO":
                raise UnauthorizedException("User cannot place buy orders.")

            buy_order_count = session.query(BuyOrder).filter_by(user_id=user_id).count()
            if buy_order_count >= self.config["ACQUITY_BUY_ORDER_PER_ROUND_LIMIT"]:
                raise UnauthorizedException("Limit of buy orders reached.")

            active_round = RoundService(self.config).get_active()

            buy_order = BuyOrder(
                user_id=user_id,
                number_of_shares=number_of_shares,
                price=price,
                security_id=security_id,
                round_id=(active_round and active_round["id"]),
            )

            session.add(buy_order)
            session.commit()

            self.email_service.send_email(
                emails=[user.email], template="create_buy_order"
            )

            return buy_order.asdict()
コード例 #3
0
ファイル: services.py プロジェクト: acquity3/api
    def create_new_offer(self, chat_room_id, author_id, price,
                         number_of_shares):
        with session_scope() as session:
            OfferService._check_deal_status(session=session,
                                            chat_room_id=chat_room_id,
                                            user_id=author_id)

            offers = session.query(Offer).filter_by(chat_room_id=chat_room_id,
                                                    offer_status="PENDING")
            if offers.count() > 0:
                raise InvalidRequestException("There are still pending offers")

            chat_room = session.query(ChatRoom).get(chat_room_id)
            if ChatRoomService.is_disbanded(chat_room):
                raise ResourceNotFoundException("Chat room is disbanded")

            offer = Offer(
                chat_room_id=str(chat_room_id),
                price=price,
                number_of_shares=number_of_shares,
                author_id=str(author_id),
            )
            session.add(offer)
            session.flush()
            chat_room.updated_at = offer.created_at

            offer_dict = offer.asdict()
            return OfferService._serialize_chat_offer(
                offer=offer_dict, is_deal_closed=chat_room.is_deal_closed)
コード例 #4
0
ファイル: services.py プロジェクト: indocomsoft/api
 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()
コード例 #5
0
ファイル: services.py プロジェクト: acquity3/api
 def get_user_by_linkedin_id(self, provider_user_id):
     with session_scope() as session:
         user = (session.query(User).filter_by(
             provider_user_id=provider_user_id).one_or_none())
         if user is None:
             raise ResourceNotFoundException()
         user_dict = user.asdict()
     return user_dict
コード例 #6
0
ファイル: services.py プロジェクト: indocomsoft/api
 def _check_deal_status(session, chat_room_id, user_id, user_type):
     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")
     OfferService._verify_user(
         chat_room=chat_room, user_id=user_id, user_type=user_type
     )
コード例 #7
0
ファイル: services.py プロジェクト: indocomsoft/api
    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 {}
コード例 #8
0
ファイル: services.py プロジェクト: acquity3/api
    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")
コード例 #9
0
ファイル: services.py プロジェクト: acquity3/api
    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 {}
コード例 #10
0
ファイル: services.py プロジェクト: acquity3/api
    def edit_market_price(self, id, subject_id, market_price):
        with session_scope() as session:
            security = session.query(Security).get(id)
            if security is None:
                raise ResourceNotFoundException()

            subject = session.query(User).get(subject_id)
            if not subject.is_committee:
                raise UnauthorizedException(
                    "You need to be a committee of this security.")

            security.market_price = market_price
            session.commit()
            return security.asdict()
コード例 #11
0
ファイル: services.py プロジェクト: indocomsoft/api
 def create_new_message(self, chat_room_id, message, author_id, user_type):
     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")
         ChatService._verify_user(
             chat_room=chat_room, user_id=author_id, user_type=user_type
         )
         message = Chat(
             chat_room_id=str(chat_room_id),
             message=message,
             author_id=str(author_id),
         )
         message = ChatService._get_current_message(session=session, message=message)
         ChatService._update_chatroom_datetime(
             session=session, chat_room=chat_room, message=message
         )
         return ChatService._serialize_chat_message(
             chat_room_id=chat_room_id, message=message
         )
コード例 #12
0
ファイル: services.py プロジェクト: indocomsoft/api
    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()
コード例 #13
0
ファイル: services.py プロジェクト: indocomsoft/api
    def get_conversation(self, user_id, chat_room_id, user_type):
        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")
            ChatService._verify_user(
                chat_room=chat_room, user_id=user_id, user_type=user_type
            )
            results = (
                session.query(ChatRoom, BuyOrder, SellOrder)
                .filter(ChatRoom.id == chat_room_id)
                .outerjoin(BuyOrder, ChatRoom.buyer_id == BuyOrder.user_id)
                .outerjoin(SellOrder, ChatRoom.seller_id == SellOrder.user_id)
                .one()
            )

            chat_room = results[0].asdict()
            buy_order = results[1].asdict()
            sell_order = results[2].asdict()

            offers = OfferService(self.config).get_chat_offers(
                user_id=user_id, chat_room_id=chat_room_id
            )
            messages = self.get_chat_messages(
                user_id=user_id, chat_room_id=chat_room_id
            )

            return {
                "chat_room_id": chat_room_id,
                "seller_price": sell_order.get("price"),
                "seller_number_of_shares": sell_order.get("number_of_shares"),
                "buyer_price": buy_order.get("price"),
                "buyer_number_of_shares": buy_order.get("number_of_shares"),
                "updated_at": datetime.timestamp(chat_room.get("updated_at")) * 1000,
                "is_deal_closed": chat_room.get("is_deal_closed"),
                "conversation": sorted(
                    messages + offers, key=lambda item: item["created_at"]
                ),
            }
コード例 #14
0
ファイル: services.py プロジェクト: acquity3/api
    def create_order(self, user_id, number_of_shares, price, security_id,
                     scheduler):
        with session_scope() as session:
            user = session.query(User).get(user_id)
            if user is None:
                raise ResourceNotFoundException()
            if not user.can_sell:
                raise UnauthorizedException("User cannot place sell orders.")

            sell_order_count = len(
                self.get_orders_by_user_in_current_round(user_id=user_id))
            if sell_order_count >= self.config[
                    "ACQUITY_SELL_ORDER_PER_ROUND_LIMIT"]:
                raise UnauthorizedException("Limit of sell orders reached.")

            sell_order = SellOrder(
                user_id=user_id,
                number_of_shares=number_of_shares,
                price=price,
                security_id=security_id,
            )

            active_round = RoundService(self.config).get_active()
            if active_round is None:
                session.add(sell_order)
                session.commit()
                if RoundService(self.config).should_round_start():
                    RoundService(
                        self.config).create_new_round_and_set_orders(scheduler)
            else:
                sell_order.round_id = active_round["id"]
                session.add(sell_order)

            session.commit()

            self.email_service.send_email(emails=[user.email],
                                          template="create_sell_order")

            return sell_order.asdict()