ShipmentNotifier/ShipmentNotifier.py

54 lines
2.8 KiB
Python

from ShipmentNotifierLogger import log, cleanLogs
from ShipmentNotifierAmazonAPI import (
getInboundShipmentData,
getInboundShipmentPlans,
getInboundShipmentPlan,
getProductName
)
from ShipmentNotifierHelpers import (
isInboundShipmentPlanIDInSentNotifications,
updateSentNotifications,
sendDiscordNotification,
isInboundShipmentPlanWithinSpecifiedDelta
)
def parseInboundShipmentPlans():
log('\U0001F504 Getting inbound shipment plans...', 'info')
inboundShipmentPlans = getInboundShipmentPlans()
inboundShipmentPlanIDs = []
inboundShipmentPlanData = {}
for plan in inboundShipmentPlans:
if isInboundShipmentPlanWithinSpecifiedDelta(plan['createdAt']):
log('Adding inbound shipment plan to list: {}'.format(plan['inboundPlanId']), 'info')
inboundShipmentPlanIDs.append(plan['inboundPlanId'])
inboundShipmentPlanData.update({plan['inboundPlanId']: {'creationDate': plan['createdAt']}})
if inboundShipmentPlanIDs:
log('\U0001F440 Checking inbound shipment plans...', 'info')
log(f'Shipments to check: {len(inboundShipmentPlanIDs)}', 'info')
for inboundShipmentPlanID in inboundShipmentPlanIDs:
if isInboundShipmentPlanIDInSentNotifications(inboundShipmentPlanID):
log(f'Ignoring inbound shipment plan {inboundShipmentPlanID}, notification has already been sent', 'info')
elif not isInboundShipmentPlanIDInSentNotifications(inboundShipmentPlanID):
inboundShipmentPlan = getInboundShipmentPlan(inboundShipmentPlanID)
if inboundShipmentPlan['items']:
itemDict = {}
totalItemCount = 0
for item in inboundShipmentPlan['items']:
productName = getProductName(item['msku'])
itemDict.update({productName: item.get('quantity')})
totalItemCount += item['quantity']
itemDict.update({'Total unit count': totalItemCount})
inboundShipmentPlanData[inboundShipmentPlanID]['contents'] = itemDict
inboundShipmentData = getInboundShipmentData(inboundShipmentPlanID)
inboundShipmentPlanData[inboundShipmentPlanID]['destinations'] = inboundShipmentData[inboundShipmentPlanID]['destinations']
inboundShipmentPlanData[inboundShipmentPlanID]['shipmentIDs'] = inboundShipmentData[inboundShipmentPlanID]['shipmentIDs']
log(f'\U0001F514 Sending Discord notification for {inboundShipmentPlanID}...', 'info')
sendDiscordNotification(inboundShipmentPlanID, inboundShipmentPlanData[inboundShipmentPlanID])
updateSentNotifications(inboundShipmentPlanID)
if __name__ == '__main__':
cleanLogs()
parseInboundShipmentPlans()