CREATE PROCEDURE dbo.ReceiveDeliveryReport @AMessageID VARCHAR(255), @ADateStamp DATETIME, @AStatusCode INTEGER, @AOperatorID VARCHAR(100), @AOperatorStatusCode VARCHAR(100), @ADateTimeOfSMSSend DATETIME, @ANumberOfSMSSend INTEGER AS
/*
  Created by Arne Munch, March 2020
  Receive a Delivery Report from the LINK Mobility REST SMS API

  Changes:

*/

SET NOCOUNT ON

DECLARE
@LogState INT,
@Text VARCHAR(2000),
@Message VARCHAR(2000)

IF @AStatusCode = 1001 OR @AStatusCode = 1007
BEGIN
  SET @LogState = 3 -- sslsSuccessfulDelivery
  SET @Message = CAST(@AStatusCode AS VARCHAR(4)) + '/Delivered'
  SET @Text = 'The message was delivered to recipient by ' + @AOperatorID + ' at ' + CONVERT(VARCHAR, @ADateTimeOfSMSSend, 120)
END
ELSE
BEGIN
  SET @LogState = -5 -- sslsErrorDelivery
  IF @AStatusCode = 1000 OR @AStatusCode = 1005 OR @AStatusCode = 1008 OR @AStatusCode = 1011 OR @AStatusCode = 1012
    SET @Message = CAST(@AStatusCode AS VARCHAR(4)) + '/Delayed'
  ELSE
    SET @Message = CAST(@AStatusCode AS VARCHAR(4)) + '/Not delivered'
  SET @Text = 'The message was not delivered to recipient - Operator Status Code: ' + @AOperatorStatusCode
END

EXEC PostDeliveryReportAndNotification @AMessageID, @ADateStamp, @LogState, @Message, @Text, @ANumberOfSMSSend
