Function WebAccounts_Services(@Service, @Params)
/***********************************************************************************************************************

    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        :   WebAccounts_Services

    Description :   Handler program for all WebAccounts services.

    Notes       :   

    Parameters  :
        Service         [in] -- Name of the service being requested
        Param1-10   [in/out] -- Additional request parameter holders
        Response       [out] -- Response to be sent back to the Controller (MCP) or requesting procedure

    Metadata    :

    History     :   (Date, Initials, Notes)
        10/13/18    dmb     [SRPFW-254] Original programmer.
        10/13/18    dmb     [SRPFW-254] Add GetWebAccounts, SetWebAccounts, and ConvertMVWebAccountsToJSON serives.
        10/22/18    dmb     [SRPFW-254] Add ConvertJSONWebAccountsToMV service.
        01/18/20    dmb     [SRPFW-296] Update the ConvertJSONWebAccountsToMV service by replacing
                            Utility_DotNet('TIMEZONE') with the SRP_DateTime service (SRP Utilities 2.1) to avoid
                            localization problems and potential 502 Bad Gateway errors.

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

#pragma precomp SRP_PreCompiler

$insert LOGICAL
$insert SERVICE_SETUP
$insert WEB_ACCOUNTS_EQUATES

Equ SecondsPerHour$ to 60 * 60              ; // 60 minutes * 60 seconds = 3600
Equ SecondsPerDay$  to 24 * SecondsPerHour$ ; // 24 hours * 60 minutes * 60 seconds = 86400

Declare function    WebAccounts_Services, Memory_Services, Database_Services, SRP_JSON, RTI_CreateGUID, SRP_DateTime
Declare subroutine  WebAccounts_Services, Memory_Services, Database_Services, SRP_JSON

GoToService else
    Error_Services('Add', Service : ' is not a valid service request within the ' : ServiceModule : ' module.')
end

Return Response OR ''


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Service Parameter Options
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Options BOOLEAN         = True$, False$


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Services
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//----------------------------------------------------------------------------------------------------------------------
// GetWebAccounts
//
// Returns the database row from the WebAccounts table for the indicated Account ID. The default format is MultiValue.
//----------------------------------------------------------------------------------------------------------------------
Service GetWebAccounts(AccountID, ReturnJSON)

    WebAccountsRow  = ''

    If AccountID NE '' then
        WebAccountsRow  = Database_Services('ReadDataRow', 'WEB_ACCOUNTS', AccountID)
        If ReturnJSON EQ True$ then
            WebAccountsRow  = WebAccounts_Services('ConvertMVWebAccountsToJSON', AccountID, WebAccountsRow)
        end
    end else
        Error_Services('Add', 'AccountID argument was missing in the ' : Service : ' service.')
    end

    Response    = WebAccountsRow

end service


//----------------------------------------------------------------------------------------------------------------------
// SetWebAccounts
//
// Updates the WebAccounts database row for the indicated Account ID.
//----------------------------------------------------------------------------------------------------------------------
Service SetWebAccounts(AccountID, WebAccountsRow)

    If (AccountID NE '') AND (WebAccountsRow NE '') then
        Database_Services('WriteDataRow', 'WEB_ACCOUNTS', AccountID, WebAccountsRow)
    end else
        Error_Services('Add', 'AccountID or WebAccountsRow argument was missing in the ' : Service : ' service.')
    end

end service


//----------------------------------------------------------------------------------------------------------------------
// ConvertMVWebAccountsToJSON
//
// Converts a MultiValue formatted WebAccounts row into a serialized JSON object and returns the result. If the
// mvWebAccounts argument is empty, the service will attempt to get it from the WebAccounts table. If the itemURL
// argument is not empty, HAL+JSON properties will be added to the JSON object.
//----------------------------------------------------------------------------------------------------------------------
Service ConvertMVWebAccountsToJSON(AccountID, mvWebAccounts, itemURL)

    jsonWebAccounts   = ''

    If AccountID NE '' then

        If mvWebAccounts EQ '' then mvWebAccounts = Database_Services('ReadDataRow', 'WEB_ACCOUNTS', AccountID)
        If Error_Services('NoError') then
            @DICT   = Database_Services('GetTableHandle', 'DICT.WEB_ACCOUNTS')
            @ID     = AccountID
            @RECORD = mvWebAccounts

            // WebAccounts object.
            If SRP_JSON(objJSONWebAccounts, 'New', 'Object') then
                SRP_JSON(objJSONWebAccounts, 'SetValue', 'id', @ID, 'String')
                SRP_JSON(objJSONWebAccounts, 'SetValue', 'name', {NAME}, 'String')
                If SRP_JSON(objPassword, 'New', 'Object') then
                    SRP_JSON(objPassword, 'SetValue', 'value', {CURRENT_PASSWORD}, 'String')
                    SRP_JSON(objPassword, 'SetValue', 'created', {CURRENT_PASSWORD_CREATED}, 'String')
                    SRP_JSON(objPassword, 'SetValue', 'expires', {CURRENT_PASSWORD_EXPIRES}, 'String')
                    SRP_JSON(objJSONWebAccounts, 'Set', 'password', objPassword)
                    SRP_JSON(objPassword, 'Release')
                end

                jsonWebAccounts    = SRP_JSON(objJSONWebAccounts, 'Stringify', 'Styled')
*                Swap \0D0A\ with @FM in jsonWebAccounts
                SRP_JSON(objJSONWebAccounts, 'Release')
            end else
                Error_Services('Add', 'Unable to create JSON representation in the ' : Service : ' service.')
            end
        end
    end else
        Error_Services('Add', 'AccountID argument was missing in the ' : Service : ' service.')
    end
 
    Response    = jsonWebAccounts

end service


//----------------------------------------------------------------------------------------------------------------------
// ConvertJSONWebAccountsToMV
//
// Converts a serialized JSON WebAccounts object into a MultiValue formatted WebAccounts row and returns the result.
//----------------------------------------------------------------------------------------------------------------------
Service ConvertJSONWebAccountsToMV(jsonWebAccounts)

    mvWebAccounts      = ''

    If jsonWebAccounts NE '' then
        If SRP_JSON(objJSONWebAccounts, 'Parse', jsonWebAccounts) EQ '' then
            AccountID       = SRP_JSON(objJSONWebAccounts, 'GetValue', 'id')
            mvWebAccounts   = WebAccounts_Services('GetWebAccounts', AccountID, False$)
            If Error_Services('NoError') then
                mvWebAccounts<WEB_ACCOUNTS.NAME$>                           = SRP_JSON(objJSONWebAccounts, 'GetValue', 'name')
                mvWebAccounts<WEB_ACCOUNTS.CURRENT_PASSWORD$>               = SRP_JSON(objJSONWebAccounts, 'GetValue', 'password.value')
                CreateDateTime                                              = SRP_JSON(objJSONWebAccounts, 'GetValue', 'password.created')
                TMZ                                                         = Oconv(SRP_DateTime('Format', SRP_DateTime('Now', True$), "DDD, DD MMM YYYY hh:mm:ss 'GMT'")[-1, 'B '], 'MD2') ; // Get the TimeZone modifier.
                CreateDate                                                  = Iconv(Field(CreateDateTime, ' ', 2, 3), 'D')
                CreateTime                                                  = Iconv(Field(CreateDateTime, ' ', 5, 1), 'MT')
                thisSeconds                                                 = CreateDate * SecondsPerDay$ + CreateTime
                thisSeconds                                                += TMZ * SecondsPerHour$
                CreateDate                                                  = Int(thisSeconds / SecondsPerDay$)
                CreateTime                                                  = Mod(thisSeconds, SecondsPerDay$)
                mvWebAccounts<WEB_ACCOUNTS.CURRENT_PASSWORD_CREATE_DATE$>   = CreateDate
                mvWebAccounts<WEB_ACCOUNTS.CURRENT_PASSWORD_CREATE_TIME$>   = CreateTime
                ExpireDateTime                                              = SRP_JSON(objJSONWebAccounts, 'GetValue', 'password.expires')
                ExpireDate                                                  = Iconv(Field(ExpireDateTime, ' ', 2, 3), 'D')
                ExpireTime                                                  = Iconv(Field(ExpireDateTime, ' ', 5, 1), 'MT')
                thisSeconds                                                 = ExpireDate * SecondsPerDay$ + ExpireTime
                thisSeconds                                                += TMZ * SecondsPerHour$
                ExpireDate                                                  = Int(thisSeconds / SecondsPerDay$)
                ExpireTime                                                  = Mod(thisSeconds, SecondsPerDay$)
                mvWebAccounts<WEB_ACCOUNTS.CURRENT_PASSWORD_EXPIRE_DATE$>   = ExpireDate
                mvWebAccounts<WEB_ACCOUNTS.CURRENT_PASSWORD_EXPIRE_TIME$>   = ExpireTime
                SRP_JSON(objJSONWebAccounts, 'Release')
            end
        end else
            Error_Services('Add', 'Error parsing jsonWebAccounts in the ' : Service : ' service.')
        end
    end else
        Error_Services('Add', 'jsonWebAccounts argument was missing in the ' : Service : ' service.')
    end

    Response    = mvWebAccounts

end service


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

//----------------------------------------------------------------------------------------------------------------------
// GoSubLabel
//
//----------------------------------------------------------------------------------------------------------------------
GoSubLabel:

return
