Function Gettestwaferlotzpl_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        :   Gettestwaferlotzpl_API

    Description :   API logic for the Gettestwaferlotzpl 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 Gettestwaferlotzpl[.ID.[<Property>]]
                                    - HTTPMethod can be any valid HTTP method, e.g., GET, POST, PUT, DELETE, etc.
                                Examples:
                                    - Gettestwaferlotzpl.POST
                                    - Gettestwaferlotzpl.ID.PUT
                                    - Gettestwaferlotzpl.ID.firstName.GET
        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)
        10/21/24    xxx     Original programmer.

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

#pragma precomp SRP_PreCompiler

Declare function Labeling_Services, OI_Wizard_services

$insert APP_INSERTS
$insert API_SETUP
$insert HTTP_INSERTS
$Insert LOT_EQUATES
$Insert OI_WIZARD_EQUATES

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 gettestwaferlotzpl.HEAD
API gettestwaferlotzpl.GET
//LotId, Username
    OIWizardID = ''
    Cookies    = HTTP_Services('GetHTTPCookie')
    For each Cookie in Cookies using ';'
        Key = Field(Cookie, '=', 1)
        If Key EQ 'sessionID' then
            OIWizardID = Field(Cookie, '=', 2)
        end
    Next Cookie
    
    ValidSession = OI_Wizard_Services('ValidateSession', OIWizardID)

    If ValidSession then
        // Check if payload has the required information to create a react mode change record
        Body = HTTP_Services('GetHTTPGetString')
        If Body NE '' then
            // The POST string will have been encoded so use percent (URL) decoding.
            ReactModeJSON = HTTP_Services('DecodePercentString', Body)
            ParseResponse = SRP_JSON(hReactModeJSON, 'PARSE', ReactModeJSON)
            If (ParseResponse EQ '') then
                LotId     = SRP_JSON(hReactModeJSON, 'GetValue', 'LotId')
                Username  = Xlate('OI_WIZARD', OIWizardID, OI_WIZARD.EMPLOYEE_ID$, 'X')
                SRP_JSON(hReactModeJSON, 'Release')
                //Reactor_Services('CreateReactModeChange', UserID, ReactNo, Mode, ModeSubCat, ModeText)
                ZPLString = Labeling_Services('GetTestWaferLotZPL', LotId, Username) 
                If Error_Services('NoError') And ZPLString NE '' then
                    // Maybe return the corresponding reactor object with the new current mode info.
                    HTTP_Services('SetResponseStatus', 201, ZPLString)
                end else
                    HTTP_Services('SetResponseStatus', 400, Error_Services('GetMessage'))
                end
            end else
                // Error parsing JSON
                HTTP_Services('SetResponseStatus', 400, 'Unable to parse the JSON data from the request.')
            end
        end else
            // No JSON payload sent with request
            If SRP_JSON(hTemplateJSON, 'New', 'Object', 'Template') then
                SRP_JSON(hTemplateJSON, 'SetValue', 'userID', '')
                SRP_JSON(hTemplateJSON, 'SetValue', 'reactNo', '')
                SRP_JSON(hTemplateJSON, 'SetValue', 'mode', '')
                SRP_JSON(hTemplateJSON, 'SetValue', 'modeSubCat', '')
                SRP_JSON(hTemplateJSON, 'SetValue', 'modeText', '')
                JSONTemplate = SRP_JSON(hTemplateJSON, 'Stringify', 'Styled')
                SRP_JSON(hTemplateJSON, 'Release')
                HTTP_Services('SetResponseBody', JSONTemplate, False$, 'application/hal+json')
            end
            HTTP_Services('SetResponseStatus', 400, 'JSON object is missing in the body of the request.')
        end
    end else
        HTTP_Services('SetResponseStatus', 401, 'Invalid session. Reauthentication required.')
    end

end api


API gettestwaferlotzpl.ID.HEAD
API gettestwaferlotzpl.ID.GET
    OIWizardID = ''
    Cookies    = HTTP_Services('GetHTTPCookie')
    For each Cookie in Cookies using ';'
        Key = Field(Cookie, '=', 1)
        If Key EQ 'sessionID' then
            OIWizardID = Field(Cookie, '=', 2)
        end
    Next Cookie
    
    ValidSession = OI_Wizard_Services('ValidateSession', OIWizardID)

    If ValidSession then
        // Check if payload has the required information to create a react mode change record
        LotId = EndpointSegment
        If LotId NE '' then
            // The POST string will have been encoded so use percent (URL) decoding.
                Username  = Xlate('OI_WIZARD', OIWizardID, OI_WIZARD.EMPLOYEE_ID$, 'X')
                //Reactor_Services('CreateReactModeChange', UserID, ReactNo, Mode, ModeSubCat, ModeText)
                ZPLString = Labeling_Services('GetTestWaferLotZPL', LotId, Username) 
                If Error_Services('NoError') And ZPLString NE '' then
                    // Maybe return the corresponding reactor object with the new current mode info.
                    HTTP_Services('SetResponseStatus', 201, ZPLString)
                end else
                    HTTP_Services('SetResponseStatus', 400, Error_Services('GetMessage'))
                end
        end else
            // No JSON payload sent with request
            If SRP_JSON(hTemplateJSON, 'New', 'Object', 'Template') then
                SRP_JSON(hTemplateJSON, 'SetValue', 'userID', '')
                SRP_JSON(hTemplateJSON, 'SetValue', 'reactNo', '')
                SRP_JSON(hTemplateJSON, 'SetValue', 'mode', '')
                SRP_JSON(hTemplateJSON, 'SetValue', 'modeSubCat', '')
                SRP_JSON(hTemplateJSON, 'SetValue', 'modeText', '')
                JSONTemplate = SRP_JSON(hTemplateJSON, 'Stringify', 'Styled')
                SRP_JSON(hTemplateJSON, 'Release')
                HTTP_Services('SetResponseBody', JSONTemplate, False$, 'application/hal+json')
            end
            HTTP_Services('SetResponseStatus', 400, 'JSON object is missing in the body of the request.')
        end
    end else
        HTTP_Services('SetResponseStatus', 401, 'Invalid session. Reauthentication required.')
    end

end api
