def delete_stock(stock_id: str) -> StockResponseIdModel: # fmt: off stock = (Stock.queryNotSoftDeleted().filter_by( id=dehumanize(stock_id)).join(Offer, VenueSQLEntity).first_or_404()) # fmt: on offerer_id = stock.offer.venue.managingOffererId ensure_current_user_has_rights(RightsType.editor, offerer_id) offers_api.delete_stock(stock) return StockResponseIdModel.from_orm(stock)
def upsert_stocks( offer_id: int, stock_data_list: List[Union[StockCreationBodyModel, StockEditionBodyModel]] ) -> List[Stock]: stocks = [] edited_stocks = [] edited_stocks_previous_beginnings = {} offer = offer_queries.get_offer_by_id(offer_id) for stock_data in stock_data_list: if isinstance(stock_data, StockEditionBodyModel): stock = Stock.queryNotSoftDeleted().filter_by( id=stock_data.id).first_or_404() if stock.offerId != offer_id: errors = ApiErrors() errors.add_error( "global", "Vous n'avez pas les droits d'accès suffisant pour accéder à cette information." ) errors.status_code = 403 raise errors edited_stocks_previous_beginnings[ stock.id] = stock.beginningDatetime edited_stock = _edit_stock( stock, price=stock_data.price, quantity=stock_data.quantity, beginning=stock_data.beginning_datetime, booking_limit_datetime=stock_data.booking_limit_datetime, ) edited_stocks.append(edited_stock) stocks.append(edited_stock) else: created_stock = _create_stock( offer=offer, price=stock_data.price, quantity=stock_data.quantity, beginning=stock_data.beginning_datetime, booking_limit_datetime=stock_data.booking_limit_datetime, ) stocks.append(created_stock) repository.save(*stocks) for stock in edited_stocks: previous_beginning = edited_stocks_previous_beginnings[stock.id] if stock.beginningDatetime != previous_beginning: _notify_beneficiaries_upon_stock_edit(stock) if feature_queries.is_active(FeatureToggle.SYNCHRONIZE_ALGOLIA): redis.add_offer_id(client=app.redis_client, offer_id=offer.id) return stocks
def test_queryNotSoftDeleted_should_not_return_soft_deleted(app): # Given offerer = create_offerer() venue = create_venue(offerer) stock = create_stock_with_event_offer(offerer, venue) stock.isSoftDeleted = True repository.save(stock) # When result = Stock.queryNotSoftDeleted().all() # Then assert not result
def delete_stock(stock_id: str) -> StockIdResponseModel: # fmt: off stock = ( Stock.queryNotSoftDeleted() .filter_by(id=dehumanize(stock_id)) .join(Offer, Venue) .first_or_404() ) # fmt: on offerer_id = stock.offer.venue.managingOffererId check_user_has_access_to_offerer(current_user, offerer_id) offers_api.delete_stock(stock) return StockIdResponseModel.from_orm(stock)
def edit_stock(stock_id: str, body: StockEditionBodyModel) -> StockResponseIdModel: stock = Stock.queryNotSoftDeleted().filter_by( id=dehumanize(stock_id)).join(Offer, VenueSQLEntity).first_or_404() offerer_id = stock.offer.venue.managingOffererId ensure_current_user_has_rights(RightsType.editor, offerer_id) stock = offers_api.edit_stock( stock, price=body.price, quantity=body.quantity, beginning=body.beginning_datetime, booking_limit_datetime=body.booking_limit_datetime, ) return StockResponseIdModel.from_orm(stock)