<?xml version="1.0" encoding="utf-8"?>
<ConnectionManager Name="WorkflowMax" RuntimeVersion="" Help="http://www.cozyroc.com/ssis/workflowmax-connection" xmlns="http://www.cozyroc.com/schema/rcm-config-1.0.xsd">
	<Service EndPoint="https://api.workflowmax2.com" />

	<Authentication Name="OAuth 2">
		<Documentation>https://workflowmaxbybluerock.zendesk.com/hc/en-us/articles/28754786654233-API-authentication</Documentation>

		<User>
			<Parameter Name="client_id">
				<Documentation>Required. Specify client ID.</Documentation>
			</Parameter>
			<Parameter Name="client_secret">
				<Documentation>Required. Specify client secret.</Documentation>
			</Parameter>
			<Parameter Name="account_id">
				<Documentation>Required. Specify organization id.</Documentation>
			</Parameter>
			<Parameter Name="scope" Default="openid email profile workflowmax offline_access">
				<Documentation>Required. Specify scopes for the token.</Documentation>
			</Parameter>
			<Parameter Name="redirect_uri">
				<Documentation>Required. Specify redirect_uri.</Documentation>
			</Parameter>
		</User>

		<Test Url="/category.api/list">
			<Parameters>
				<Parameter Name="account_id" Value="{{=connection.token.account_id}}" Type="HttpHeader" />
			</Parameters>
		</Test>

		<Token Url="https://oauth.workflowmax2.com/oauth/authorize" Result="{{=response.access_token}}">
			<Parameters>
				<Parameter Name="response_type" Value="code" />
				<Parameter Name="client_id" Value="{{=token.client_id}}" />
				<Parameter Name="scope" Value="{{=token.scope}}" />
				<Parameter Name="redirect_uri" Value="{{=token.redirect_uri}}" />
				<Parameter Name="state" Value="{{=Math.random()}}" />
				<Parameter Name="prompt" Value="consent" />
			</Parameters>

			<Authorized Id="{{=token.redirect_uri}}" Result="{{=
				connection.execute({
					url: 'https://oauth.workflowmax2.com/oauth/token',
					method: 'POST',
					parameters: {
						grant_type: 'authorization_code',
						code: response.code,
						client_id: token.client_id,
						client_secret: token.client_secret,
						scope: token.scope,
						redirect_uri: token.redirect_uri
				} })
			}}" />

			<Refresh Url="https://oauth.workflowmax2.com/oauth/token" Method="POST" Result="{{=response.refresh_token}}" Expiration="{{=Date.now() + (response.expires_in - 300) * 1000}}">
				<Parameters>
					<Parameter Name="grant_type" Value="refresh_token" />
					<Parameter Name="client_id" Value="{{=token.client_id}}" />
					<Parameter Name="client_secret" Value="{{=token.client_secret}}" />
					<Parameter Name="refresh_token" Value="{{=token.Refresh}}" />
				</Parameters>
			</Refresh>
		</Token>

		<Parameters>
			<Parameter Name="authorization" Value="Bearer {{=token.Access}}" Type="HttpHeader" />
			<Parameter Name="account_id" Value="{{=connection.token.account_id}}" Type="HttpHeader" />
		</Parameters>
	</Authentication>

	<Resources>
		<Template>
			<Field Name="ShortText" DataType="DT_WSTR" Length="255" />
			<Field Name="LongText" DataType="DT_WSTR" Length="1000" />
			<Field Name="DateTime" DataType="DT_DBTIMESTAMP" />
			<Field Name="Date" DataType="DT_DBDATE" />

			<Resource Name="Base">
				<Read>
					<Parameters>
						<Parameter Name="page" Value="{{=parameters.iterator}}" />
						<Parameter Name="pagesize" Value="{{=parameters.batchSize}}" />
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
					</Parameters>

					<Iterator>
						<Next Value="{{=parseInt(parameters.iterator || 0) + 1}}" />
					</Iterator>
				</Read>

				<Create Method="POST">
					<Parameters>
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Create>

				<Update Method="PUT">
					<Parameters>
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Update>

				<Delete Method="DELETE" />
			</Resource>
		</Template>
	</Resources>
	<Script>
		<Module Name="Main">
			<![CDATA[
require('underscore');

]]>
		</Module>
	</Script>
</ConnectionManager>