Function Lotoperation_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        :   Lotoperation_API

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

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

#pragma precomp SRP_PreCompiler

Declare function OI_Wizard_Services, Lot_Operation_Services, Database_Services, Lot_Services, Clean_Services
Declare function Met_Test_Services
Declare subroutine  Lot_Services, Met_Test_Services, Wafer_Counter_Services

$insert APP_INSERTS
$insert API_SETUP
$insert HTTP_INSERTS
$insert OI_WIZARD_EQUATES
$insert LOT_OPERATION_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 lotoperation.HEAD
API lotoperation.GET

HTTP_Resource_Services('LoremIpsum')

end api


API lotoperation.addoperation.POST

    ErrorMessage = ''
    ResponseCode = ''
    ResponseMessage = ''
    Body = ''
    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
        UserId = Xlate('OI_WIZARD', OIWizardID, OI_WIZARD.EMPLOYEE_ID$, 'X')
        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
            LotId      = SRP_JSON(objBody, 'GetValue', 'NewLotOperationData.LotId')
            OperationId   = SRP_JSON(objBody, 'GetValue', 'NewLotOperationData.OperationId')
            Sequence = SRP_JSON(objBody, 'GetValue', 'NewLotOperationData.Sequence')
            SRP_JSON(objBody, 'Release')
        end
        If RowExists('LOT', LotId) then
            If RowExists('OPERATION', OperationId) then
                If Sequence NE '' then
                    NewOperationId = Lot_Operation_Services('AddOperationToLot', LotId, OperationId, Sequence, UserId)
                    If Error_Services('NoError') then
                        LotJsonString = Lot_Services('ConvertLotRecordToJson', LotId, '', UserId)
                        HTTP_Services('SetResponseBody', LotJsonString, False$, 'application/hal+json')
                        ResponseCode = 200
                    end else
                        ErrorMessage = Error_Services('GetMessage')
                        ResponseCode = 500
                    end
                end else
                    ErrorMessage = 'Invalid Sequence.'
                    ResponseCode = 500
                end
            end else
                ErrorMessage = 'Invalid Operation.'
                ResponseCode = 500
            end
        end else
            ErrorMessage = 'Invalid Lot.'
            ResponseCode = 500
        end
    end else
        ErrorMessage = 'Invalid session. Reauthentication required.'
        ResponseCode = 401
    end
    HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
    HTTP_Services('SetResponseStatus', ResponseCode, ErrorMessage)
    
end api

API lotoperation.removeoperation.POST
    
    ErrorMessage = ''
    ResponseCode = ''
    ResponseMessage = ''
    Body = ''
    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
        UserId = Xlate('OI_WIZARD', OIWizardID, OI_WIZARD.EMPLOYEE_ID$, 'X')
        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
            LotOperationId   = SRP_JSON(objBody, 'GetValue', 'LotOperationIdToRemove')
            SRP_JSON(objBody, 'Release')
        end

            If RowExists('LOT_OPERATION', LotOperationId) then
                LotId = Database_Services('ReadDataColumn', 'LOT_OPERATION', LotOperationId, LOT_OPERATION_LOT_ID$, True$, 0, False$)
                Removed = Lot_Operation_Services('RemoveLotOperation', LotOperationId, UserId)
                If Error_Services('NoError') then
                    LotJsonString = Lot_Services('ConvertLotRecordToJson', LotId, '', UserId)
                    HTTP_Services('SetResponseBody', LotJsonString, False$, 'application/hal+json')
                    ResponseCode = 200
                end else
                    ErrorMessage = Error_Services('GetMessage')
                    ResponseCode = 500
                end
            end else
                ErrorMessage = 'Invalid Operation.'
                ResponseCode = 500
            end
    end else
        ErrorMessage = 'Invalid session. Reauthentication required.'
        ResponseCode = 401
    end
    HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
    HTTP_Services('SetResponseStatus', ResponseCode, ErrorMessage)

end api

API lotoperation.startlotoperation.POST
    
    ErrorMessage = ''
    ResponseCode = ''
    ResponseMessage = ''
    Body = ''
    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
        UserId = Xlate('OI_WIZARD', OIWizardID, OI_WIZARD.EMPLOYEE_ID$, 'X')
        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
            LotOperationId      = SRP_JSON(objBody, 'GetValue', 'lotOperationId')
            SRP_JSON(objBody, 'Release')
        end
        
        LotProcessStarted = Lot_Operation_Services('StartLotOperation', LotOperationId, UserId)
        If Error_Services('NoError') then
            LotOperationJson = Lot_Operation_Services('ConvertRecordToJson', LotOperationId)    
            HTTP_Services('SetResponseBody', LotOperationJson,False$, 'application/hal+json')
            ResponseCode = 200 
        end else
            ErrorMessage = 'Error while moving lot in. ' : Error_Services('GetMessage')
            ResponseCode = 500
        end
    end else
        ErrorMessage = 'Invalid session. Reauthentication required.'
        ResponseCode = 401
    end
    HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
    HTTP_Services('SetResponseStatus', ResponseCode, ErrorMessage)
    
end api


API lotoperation.completelotoperation.POST

    ErrorMessage = ''
    ResponseCode = ''
    ResponseMessage = ''
    Body = ''
    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
        UserId = Xlate('OI_WIZARD', OIWizardID, OI_WIZARD.EMPLOYEE_ID$, 'X')
        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
            LotOperationId      = SRP_JSON(objBody, 'GetValue', 'lotOperationId')
            SRP_JSON(objBody, 'Release')
        end
        
        LotProcessCompleted = Lot_Operation_Services('CompleteLotOperation', LotOperationId, UserId)
        If Error_Services('NoError') then
            LotOperationJson = Lot_Operation_Services('ConvertRecordToJson', LotOperationId)    
            HTTP_Services('SetResponseBody', LotOperationJson)
            ResponseCode = 200 
        end else
            ErrorMessage = Error_Services('GetMessage')
            ResponseCode = 500
        end
    end else
        ErrorMessage = 'Invalid session. Reauthentication required.'
        ResponseCode = 401
    end
    HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
    HTTP_Services('SetResponseStatus', ResponseCode, ErrorMessage)
    
end api


API lotoperation.ID.HEAD
API lotoperation.ID.GET
    ErrorMessage = ''
    ResponseCode = ''
    ResponseMessage = ''
    Body = ''
    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
        LotOperationId = EndpointSegment
        If RowExists('LOT_OPERATION', LotOperationId) then
            LotOperationJson = Lot_Operation_Services('ConvertRecordToJson', LotOperationId)
            HTTP_Services('SetResponseBody', LotOperationJson, False$, 'application/hal+json')
            ResponseCode = 200
        end else
            ErrorMessage = 'Lot Operation not found in database.'
        end
        
    end else
        ErrorMessage = 'Invalid session. Reauthentication required.'
        ResponseCode = 401
    end
    
    HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
    HTTP_Services('SetResponseStatus', ResponseCode, ErrorMessage)

end api


API lotoperation.associatemettest.POST
    
    ErrorMessage = ''
    ResponseCode = ''
    ResponseMessage = ''
    Body = ''
    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
        UserId = Xlate('OI_WIZARD', OIWizardID, OI_WIZARD.EMPLOYEE_ID$, 'X')
        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
            LotOperationId      = SRP_JSON(objBody, 'GetValue', 'LotOperationId')
            MetTestId   = SRP_JSON(objBody, 'GetValue', 'MetTestId')
            SRP_JSON(objBody, 'Release')
        end

        Met_Test_Services('AttachMetTestToLotOperation', MetTestId, LotOperationId, UserId)
        If Error_Services('NoError') then
            ResponseCode = 200
        end else
            ErrorMessage = Error_Services('GetMessage')
            ResponseCode = 500
        end

        
    end else
        ErrorMessage = 'Invalid session. Reauthentication required.'
        ResponseCode = 401
    end
    HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
    HTTP_Services('SetResponseStatus', ResponseCode, ErrorMessage)

end api


API lotoperation.removemettest.POST

        ErrorMessage = ''
    ResponseCode = ''
    ResponseMessage = ''
    Body = ''
    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
        UserId = Xlate('OI_WIZARD', OIWizardID, OI_WIZARD.EMPLOYEE_ID$, 'X')
        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
            LotOperationId      = SRP_JSON(objBody, 'GetValue', 'LotOperationId')
            MetTestId   = SRP_JSON(objBody, 'GetValue', 'MetTestId')
            SRP_JSON(objBody, 'Release')
        end

        Met_Test_Services('RemoveMetTestFromLotOperation', MetTestId, LotOperationId, UserId)
        If Error_Services('NoError') then
            HTTP_Services('SetResponseBody', LotJsonString, False$, 'application/hal+json')
            ResponseCode = 200
        end else
            ErrorMessage = Error_Services('GetMessage')
            ResponseCode = 500
        end

        
    end else
        ErrorMessage = 'Invalid session. Reauthentication required.'
        ResponseCode = 401
    end
    HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
    HTTP_Services('SetResponseStatus', ResponseCode, ErrorMessage)

end api


API lotoperation.associatewafercounter.POST

    ErrorMessage = ''
    ResponseCode = ''
    ResponseMessage = ''
    Body = ''
    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
        UserId = Xlate('OI_WIZARD', OIWizardID, OI_WIZARD.EMPLOYEE_ID$, 'X')
        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
            LotOperationId      = SRP_JSON(objBody, 'GetValue', 'LotOperationId')
            WaferCounterId   = SRP_JSON(objBody, 'GetValue', 'WaferCounterId')
            SRP_JSON(objBody, 'Release')
        end

        Wafer_Counter_Services('AssociateWaferCounter', LotOperationId, WaferCounterId, UserId)
        If Error_Services('NoError') then
            ResponseCode = 200
        end else
            ErrorMessage = Error_Services('GetMessage')
            ResponseCode = 500
        end

        
    end else
        ErrorMessage = 'Invalid session. Reauthentication required.'
        ResponseCode = 401
    end
    HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
    HTTP_Services('SetResponseStatus', ResponseCode, ErrorMessage)

end api
