﻿@startuml
title tradein response and event handler
participant OFTradeinHandler
participant EMS
participant EER
participant OMS
participant order.update.status.int.queue
participant StateMachineService
participant µS

OFTradeinHandler-->EMS:[[./ce.dsg.od.of.tradeindispositionhandler.svg Tradein response]]
OFTradeinHandler-->EMS:[[./ce.dsg.od.of.tradeineventshandler.svg Tradein Event]]
note left: TMOBILE.ICLOUD.ORDERMANAGEMENT.TRADEIN.RESPONSE.V4
EMS->OMS:Tradein Event
OMS->µS: invoke µS responsehandler
alt success response µS
	alt successful trade in quote update in QR
		autonumber 1	
		µS->OMS:Save RMA Number and Expiration date on tradein child order
		µS->OMS:Tradein Complete
	else trade in failed Event
		autonumber 1
		µS->OMS:Tradein failed Event
		µS->OMS:Update error code description on tradein child order
	end
	opt event assurant recieved device
		autonumber 7
		OFTradeinHandler ->EMS:ASSURANT_RECEIVED_DEVICE
		EMS->OMS:ASSURANT_RECEIVED_DEVICE
		OMS->OMS:ASSURANT_RECEIVED_DEVICE status update in OMS
	end
	opt event bill credit posted
		autonumber 7	
		OFTradeinHandler ->EMS:BILL_CREDIT_POSTED
		EMS->OMS:BILL_CREDIT_POSTED
		OMS->OMS:BILL_CREDIT_POSTED status update in OMS
	end
	opt event RMA Expired (this follows SAP shipped event)
		autonumber 7	
		OFTradeinHandler->EMS:RMAExpired
		EMS->OMS:RMAExpired
		OMS->OMS:RMAExpired status update in OMS
	end
	OMS-> EMS: Publish status update to API (Completed, Assurant Recieved device,bill credit posted,RMA Expired)
	note left: order.update.status.out.queue
	OMS-> EMS: Message to Internal queue for State Machine
	note left:buildplan.process.queue \n Order status changes to Interim/<substatus>, Completed, Cancelled will be evaluated
	OMS -> StateMachineService: Trigger Next Dependent Child orders
	note left: buildplan.process.queue
else failure response µS
	alt circuit breaker is on
		OMS->OMS:OMS consumes event using BAU flow
	else
		OMS->OMS:OMS generates reprocessible exception
	end
end
@enduml