Function Holds_API(@API)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from SRP Computer Solutions, Inc.

    Name        :   Holds_API

    Description :   API logic for the Holds resource.

    Notes       :   All web APIs should include the API_SETUP insert. This will provide several useful variables:

                        HTTPMethod              - The HTTP Method (Verb) submitted by the client (e.g., GET, POST, etc.)
                        APIURL                  - The URL for the API entry point (e.g., api.mysite.com/v1).
                        FullEndpointURL         - The URL submitted by the client, including query params.
                        FullEndpointURLNoQuery  - The URL submitted by the client, excluding query params.
                        EndpointSegment         - The URL endpoint segment.
                        ParentURL               - The URL path preceeding the current endpoint.
                        CurrentAPI              - The name of this stored procedure.

    Parameters  :
        API             [in] -- Web API to process. Format is [APIPattern].[HTTPMethod]:
                                    - APIPattern must follow this structure Materials[.ID.[<Property>]]
                                    - HTTPMethod can be any valid HTTP method, e.g., GET, POST, PUT, DELETE, etc.
                                Examples:
                                    - Holds.OnHold.POST
                                    - Holds.OffHold.POST

        Response       [out] -- Response to be sent back to the Controller (HTTP_MCP) or requesting procedure. Web API
                                services do not rely upon anything being returned in the response. This is what the
                                various services like SetResponseBody and SetResponseStatus services are for. A response
                                value is only helpful if the developers want to use it for debug purposes.

    History     :   (Date, Initials, Notes)
    08/06/24	djm     Original programmer.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler

$insert APP_INSERTS
$insert API_SETUP
$insert HTTP_INSERTS

Declare function RDS_Services, WM_Out_Services, Oi_Wizard_Services, Memberof, Hold_Services, SRP_Stopwatch
Declare subroutine Hold_Services, SRP_Stopwatch

GoToAPI else
    // The specific resource endpoint doesn't have a API handler yet.
    HTTP_Services('SetResponseStatus', 204, 'This is a valid endpoint but a web API handler has not yet been created.')
end

Return Response OR ''


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Endpoint Handlers
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

API Holds.OnHold.POST
*	SRP_Stopwatch('Reset')
*	SRP_Stopwatch('Start', 'API Start')
	HoldData   = ''
	OIWizardID = ''
	Cookies    = HTTP_Services('GetHTTPCookie')
	For each Cookie in Cookies using ';'
		Key = Trim(Field(Cookie, '=', 1))
		If Key EQ 'sessionID' then
			OIWizardID = Field(Cookie, '=', 2)
		end
		If Key EQ 'userID' then
			CurrUser = Field(Cookie, '=', 2)
		end
	Next Cookie
		
	ValidSession = OI_Wizard_Services('ValidateSession', OIWizardID)
	If ValidSession then
		StatusCode  = ''
		Body        = HTTP_Services('GetHTTPPostString', True$)
		// The POST string will have been encoded so use percent (URL) decoding.
		DecodedJSON = HTTP_Services('DecodePercentString', Body)
		If SRP_JSON(objBody, 'Parse', Body) EQ '' then
			Key      = SRP_JSON(objBody, 'GetValue', 'key')
			Reason   = SRP_JSON(objBody, 'GetValue', 'reason')
			Extended = SRP_JSON(objBody, 'GetValue', 'extendedFlag')
			SRP_JSON(objBody, 'Release')
		end
		If Key NE '' AND Reason NE '' AND Extended NE '' then
			GoSub GetEntityType
			ReactorType = Xlate('WO_MAT', WOMatKey, 'REACTOR_TYPE_NO_CONV', 'X', '')
			If ReactorType NE 'EPP' AND ReactorType NE '' then
				If HoldEntity NE 'Invalid' then
					HoldData<1> = CurrUser
					HoldData<2> = Reason
					HoldData<3> = Extended
					HoldType    = 'HOLD'
*					SRP_Stopwatch('Start', 'Service Start')
					Hold_Services("OnHold", WOMatKey, HoldEntity, HoldEntityID, HoldType, HoldData, CurrUser)
*					SRP_Stopwatch('Stop', 'Service Start')
					If Error_Services('NoError') then
						HTTP_Services('SetResponseStatus', 200, 'Lot successfully placed on hold.')
					end else
						ErrorText = Error_Services('GetMessage')
						HTTP_Services('SetResponseStatus', 400, ErrorText)
					end
				end else
					HTTP_Services('SetResponseStatus', 400, 'Error. The record type could not be determined.')
				end
			end else
				HTTP_Services('SetResponseStatus', 501, 'EpiPro lot holds are not currently supported by OIWizard. The OpenInsight user interface must be used in order to proceed..')
			end
		end else
			HTTP_Services('SetResponseStatus', 400, 'The Key, Reason, or Extended property is missing.')
		end
	End else
		HTTP_Services('SetResponseStatus', 401, 'User must be signed in to access this resource.')
	end
*	SRP_Stopwatch('Stop', 'API Start')
*	Test = SRP_Stopwatch('GetAll')
*	debug
end api


API Holds.OffHold.POST
	
	HoldData   = ''
	Allowed    = FALSE$
	OIWizardID = ''
		Cookies    = HTTP_Services('GetHTTPCookie')
		For each Cookie in Cookies using ';'
			Key = Trim(Field(Cookie, '=', 1))
			If Key EQ 'sessionID' then
				OIWizardID = Field(Cookie, '=', 2)
			end
			If Key EQ 'userID' then
				CurrUser = Field(Cookie, '=', 2)
			end
		Next Cookie
		
		ValidSession = OI_Wizard_Services('ValidateSession', OIWizardID)
		If ValidSession then
			If MemberOf(CurrUser, 'ENG_TECH') OR MemberOf(CurrUser, 'LEAD') OR MemberOf(CurrUser, 'SUPERVISOR') then Allowed = True$
			If Allowed NE FALSE$ then
				StatusCode = ''
				Body    = HTTP_Services('GetHTTPPostString', True$)
				// The POST string will have been encoded so use percent (URL) decoding.
				DecodedJSON = HTTP_Services('DecodePercentString', Body)
				If SRP_JSON(objBody, 'Parse', Body) EQ '' then
					Key       = SRP_JSON(objBody, 'GetValue', 'key')
					Reason = SRP_JSON(objBody, 'GetValue', 'reason')
					SRP_JSON(objBody, 'Release')
				end
				If Key NE '' AND Reason NE ''then
					GoSub GetEntityType
					ReactorType = Xlate('WO_MAT', WOMatKey, 'REACTOR_TYPE_NO_CONV', 'X', '')
					If ReactorType NE 'EPP' AND ReactorType NE '' then
						If HoldEntity NE 'Invalid' then
							HoldData<1> = CurrUser
							HoldData<2> = Reason
							HoldData<3> = False$
							HoldType    = 'HOLD'
							Hold_Services("OffHold", WOMatKey, HoldEntity, HoldEntityID, HoldType, HoldData, CurrUser)
							If Error_Services('NoError') then
								HTTP_Services('SetResponseStatus', 200, 'Lot successfully taken off hold.')
							end else
								ErrorText = Error_Services('GetMessage')
								HTTP_Services('SetResponseStatus', 400, ErrorText)
							end
						end else
							HTTP_Services('SetResponseStatus', 400, 'Error. The record type could not be determined.')
						end
					end else
						HTTP_Services('SetResponseStatus', 501, 'EpiPro lot holds are not currently supported by OIWizard. The OpenInsight user interface must be used in order to proceed..')
					end
				end else
					HTTP_Services('SetResponseStatus', 400, 'The Key or Reason property is missing.')
				end
			End Else
				HTTP_Services('SetResponseStatus', 403, 'User is not permitted to access this resource.')
			end
		End else
			HTTP_Services('SetResponseStatus', 401, 'User must be signed in to access this resource.')
		end
	
end api


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


GetEntityType:
	
	 WOMatKey     = ''
	 HoldEntity   = ''
	 HoldEntityID = Key
	 Begin Case
        Case RowExists('RDS', HoldEntityID)
            EntityType = 'RDS'
            WOMatKey   = Xlate(EntityType, HoldEntityID, 'WO_MAT_KEY', 'X', '')
			If RowExists('WO_MAT', WOMatKey) then
				HoldEntity = EntityType
			end else
				HoldEntity = 'Invalid'
			end
        Case RowExists('WM_IN', HoldEntityID)
			EntityType = 'WM_IN'
            WOMatKey   = Xlate(EntityType, HoldEntityID, 'WO_MAT_KEY', 'X', '')
             If RowExists('WO_MAT', WOMatKey) then
            	HoldEntity = EntityType
            end else
            	HoldEntity = 'Invalid'
            end
        Case RowExists('WM_OUT', HoldEntityID)
        	EntityType = 'WM_OUT'
            WOMatKey   = Xlate(EntityType, HoldEntityID, 'WO_MAT_KEY', 'X', '')
            If RowExists('WO_MAT', WOMatKey) then
            	HoldEntity = EntityType
            end else
            	HoldEntity = 'Invalid'
            end
         Case RowExists('WO_MAT', HoldEntityID)
			WOMatKey   = HoldEntityID
			EntityType = 'WO_MAT'
			 If RowExists('WO_MAT', WOMatKey) then
            	HoldEntity = EntityType
            end else
            	HoldEntity = 'Invalid'
            end
        Case Otherwise$
            HoldEntity = 'Invalid'
            
    End Case
	
return


