Skip to content

Commit

Permalink
LDS-3702 : fix barcode not in prefetched_product. some renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-lcdp committed Jun 14, 2024
1 parent 59f3497 commit bc445a2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
27 changes: 15 additions & 12 deletions business/services/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from business.utils import rgetattr, execution_time


def __prefetch(type, keys_from_file, get_from_api, get_keys_from_api_object):
def __prefetch(prefetch_type, keys_from_file, get_from_api, get_keys_from_api_object):
packets = [list(keys_from_file)[i:i + CHUNK_SIZE] for i in range(0, len(keys_from_file), CHUNK_SIZE)]

array_from_api = list(map(get_from_api, packets))
Expand All @@ -38,7 +38,7 @@ def __prefetch(type, keys_from_file, get_from_api, get_keys_from_api_object):
for key in keys:
map_of_objects[key] = obj

logging.info(f"Map of {type} has {len(map_of_objects)} element(s)")
logging.info(f"Map of {prefetch_type} has {len(map_of_objects)} element(s)")

return map_of_objects

Expand Down Expand Up @@ -67,7 +67,8 @@ def create_sale_offer_from_excel_lines(lines):
# [owner_id, [product_id]]
owner_id_barcodes = {}
for line in lines:
if line.sale_offer.product.principal_barcode:
if (line.sale_offer.product.principal_barcode and
line.sale_offer.product.principal_barcode in prefetched_products):
owner_id_barcodes.setdefault(line.sale_offer.owner_id, []).append(
prefetched_products[line.sale_offer.product.principal_barcode].id)

Expand All @@ -83,13 +84,15 @@ def create_sale_offer_from_excel_lines(lines):
product_ids if (owner_id, product_id) not in prefetched_sale_offers]

if len(product_ids_not_found):
map_sale_offers_rest = __prefetch('sale_offers others status by (owner_id, product_id)',
product_ids_not_found,
lambda x: __get_latest_sale_offers(x, owner_id, ['WAITING_FOR_PRODUCT',
'ASKING_FOR_INVOICE',
'HOLIDAY', 'DISABLED']),
lambda x: [(owner_id, x.product.id)])
prefetched_sale_offers.update(map_sale_offers_rest)
prefetched_sale_offers_others_status = __prefetch('sale_offers others status by (owner_id, product_id)',
product_ids_not_found,
lambda x: __get_latest_sale_offers(x, owner_id,
['WAITING_FOR_PRODUCT',
'ASKING_FOR_INVOICE',
'HOLIDAY',
'DISABLED']),
lambda x: [(owner_id, x.product.id)])
prefetched_sale_offers.update(prefetched_sale_offers_others_status)
else:
logging.info("Update policy is PRODUCT_REFERENCE, we will get the sale offers by reference")

Expand Down Expand Up @@ -223,14 +226,14 @@ def excel_to_dict(obj_class, excel_path, excel_mapper, sheet_name, header_row,
return results


def __create_sale_offer_from_excel_line(map_sale_offers, prefetched_products, excel_line):
def __create_sale_offer_from_excel_line(prefetched_sale_offers, prefetched_products, excel_line):
sale_offer = None
error = None
try:
product = update_or_create_product(prefetched_products, excel_line.sale_offer.product,
excel_line.can_create_product_from_scratch())
change_product_status(product=product, new_status=excel_line.sale_offer.product.status)
sale_offer = create_or_edit_sale_offer(map_sale_offers, excel_line.sale_offer, product,
sale_offer = create_or_edit_sale_offer(prefetched_sale_offers, excel_line.sale_offer, product,
excel_line.can_create_sale_offer())
except SaleOfferApiException as sale_offer_api_err:
logging.error('An API error occur in sale offer api', sale_offer_api_err)
Expand Down
2 changes: 1 addition & 1 deletion business/services/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __get_product_by_barcode(prefetched_products, barcode):
if barcode in prefetched_products:
return prefetched_products[barcode]

logging.info(f'Product {barcode} not found in map, search in API')
logging.info(f'Product {barcode} not found in cache, search in API')

api = get_search_product_api()

Expand Down
26 changes: 13 additions & 13 deletions business/services/sale_offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ def __create_sale_offer_from_scratch(sale_offer, product):
return __create_sale_offer(sale_offer, product.id)


def __find_existing_sale_offer(map_sale_offers, sale_offer, product):
def __find_existing_sale_offer(prefetched_sale_offers, sale_offer, product):
logging.info(f'product {sale_offer.product.principal_barcode} : Try to find existing sale offer')
existing_sale_offer = None
if sale_offer.update_policy == UpdatePolicy.PRODUCT_BARCODE.value:
existing_sale_offer = __find_sale_offer_for_version(
map_sale_offers,
prefetched_sale_offers,
sale_offer.owner_id,
product.id
)
elif sale_offer.update_policy == UpdatePolicy.SALE_OFFER_REFERENCE.value and sale_offer.reference:
existing_sale_offer = __get_sale_offer(
map_sale_offers,
prefetched_sale_offers,
sale_offer.reference
)
return existing_sale_offer
Expand All @@ -48,11 +48,11 @@ def __clone_existing_sale_offer(existing_sale_offer, sale_offer):
return __clone_sale_offer(existing_sale_offer, sale_offer)


def create_or_edit_sale_offer(map_sale_offers, sale_offer, product, can_create_sale_offer):
def create_or_edit_sale_offer(prefetched_sale_offers, sale_offer, product, can_create_sale_offer):
new_sale_offer = None

if sale_offer:
existing_sale_offer = __find_existing_sale_offer(map_sale_offers, sale_offer, product)
existing_sale_offer = __find_existing_sale_offer(prefetched_sale_offers, sale_offer, product)

if existing_sale_offer:
if not existing_sale_offer.status.value == 'DISABLED':
Expand All @@ -70,11 +70,11 @@ def create_or_edit_sale_offer(map_sale_offers, sale_offer, product, can_create_s
raise CannotCreateSaleOffer()


def __find_sale_offer_for_version(map_sale_offers, owner_id, product_id):
if (owner_id, product_id) in map_sale_offers:
return map_sale_offers[(owner_id, product_id)]
def __find_sale_offer_for_version(prefetched_sale_offers, owner_id, product_id):
if (owner_id, product_id) in prefetched_sale_offers:
return prefetched_sale_offers[(owner_id, product_id)]

logging.info(f'Sale offer by (owner_id, product_id) {(owner_id, product_id)} not found in map, search in API')
logging.info(f'Sale offer by (owner_id, product_id) {(owner_id, product_id)} not found in cache, search in API')

return __find_sale_offer_for_status(product_id, owner_id, ['ENABLED']) or \
__find_sale_offer_for_status(product_id, owner_id, ['WAITING_FOR_PRODUCT', 'ASKING_FOR_INVOICE',
Expand Down Expand Up @@ -133,11 +133,11 @@ def __get_sale_offers(references):
return sale_offers.records if sale_offers else []


def __get_sale_offer(map_sale_offers, reference):
if reference in map_sale_offers:
return map_sale_offers[reference]
def __get_sale_offer(prefetched_sale_offers, reference):
if reference in prefetched_sale_offers:
return prefetched_sale_offers[reference]

logging.info(f'Sale offer {reference} not found in map, search in API')
logging.info(f'Sale offer {reference} not found in cache, search in API')

api = get_search_sale_offer_api()
sale_offer = api.get_sale_offer(
Expand Down

0 comments on commit bc445a2

Please sign in to comment.