{
	"info": {
		"_postman_id": "fb9bf198-5204-48d0-b551-847502f22282",
		"name": "Checkr Staging Collection",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [
		{
			"name": "Create candidate",
			"event": [
				{
					"listen": "test",
					"script": {
						"exec": [
							"pm.test(\"Candidate POST response code \" + pm.response.code, function () {",
							"    postman.setNextRequest(null);",
							"    pm.expect(pm.response.code).to.be.oneOf([200,201,202]);",
							"    postman.setNextRequest();",
							"    var jsondata = pm.response.json(responseBody);",
							"    pm.variables.set(\"candidate_id\", jsondata.id);",
							"});",
							"",
							""
						],
						"type": "text/javascript"
					}
				},
				{
					"listen": "prerequest",
					"script": {
						"exec": [
							""
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "POST",
				"header": [
					{
						"key": "Content-Type",
						"type": "text",
						"value": "application/json"
					}
				],
				"body": {
					"mode": "formdata",
					"formdata": [
						{
							"key": "first_name",
							"value": "{{candidate_first_name}}",
							"type": "text"
						},
						{
							"key": "last_name",
							"value": "{{candidate_last_name}}",
							"type": "text"
						},
						{
							"key": "ssn",
							"value": "{{candidate_ssn}}",
							"type": "text"
						},
						{
							"key": "dob",
							"value": "{{candidate_dob}}",
							"type": "text"
						},
						{
							"key": "driver_license_number",
							"value": "{{candidate_drivers_license_number}}",
							"type": "text"
						},
						{
							"key": "driver_license_state",
							"value": "{{candidate_drivers_license_state}}",
							"type": "text"
						},
						{
							"key": "email",
							"value": "{{candidate_email}}",
							"type": "text"
						},
						{
							"key": "zipcode",
							"value": "{{candidate_zipcode}}",
							"type": "text"
						},
						{
							"key": "no_middle_name",
							"value": "TRUE",
							"type": "text",
							"disabled": true
						},
						{
							"key": "middle_name",
							"value": "{{candidate_middle_name}}",
							"type": "text"
						},
						{
							"key": "copy_requested",
							"value": "{{candidate_copy_requested}}",
							"type": "text"
						}
					]
				},
				"url": {
					"raw": "{{endpoint_url}}/v1/candidates/",
					"host": [
						"{{endpoint_url}}"
					],
					"path": [
						"v1",
						"candidates",
						""
					]
				}
			},
			"response": []
		},
		{
			"name": "Create Invitation",
			"event": [
				{
					"listen": "test",
					"script": {
						"exec": [
							"// Assertions",
							"pm.test(\"Invitation Post Response Code \" + pm.response.code, function () {",
							"    postman.setNextRequest(null);",
							"    pm.expect(pm.response.code).to.be.oneOf([200,201,202]);",
							"    postman.setNextRequest();",
							"    var jsondata = pm.response.json(responseBody);",
							"    pm.variables.set(\"invitation_id\", jsondata.id);",
							"    pm.variables.set(\"invitation_url\",jsondata.invitation_url);",
							"});"
						],
						"type": "text/javascript"
					}
				},
				{
					"listen": "prerequest",
					"script": {
						"exec": [
							"console.log(\"Step 1 - Your candidate ID is \" + pm.variables.get('candidate_id'));",
							"console.log(\"If you have a webhook endpoint, you should have recieved a candidate.created event\")",
							""
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "POST",
				"header": [],
				"body": {
					"mode": "formdata",
					"formdata": [
						{
							"key": "candidate_id",
							"value": "{{candidate_id}}",
							"type": "text"
						},
						{
							"key": "work_locations[][state]",
							"value": "{{work_location_state}}",
							"type": "text"
						},
						{
							"key": "work_locations[][city]",
							"value": "{{work_location_city}}",
							"type": "text"
						},
						{
							"key": "package",
							"value": "{{package}}",
							"type": "text"
						},
						{
							"key": "node",
							"value": "{{account_node}}",
							"type": "text",
							"disabled": true
						}
					]
				},
				"url": {
					"raw": "{{endpoint_url}}/v1/invitations",
					"host": [
						"{{endpoint_url}}"
					],
					"path": [
						"v1",
						"invitations"
					]
				}
			},
			"response": []
		},
		{
			"name": "Get Invitation Status",
			"event": [
				{
					"listen": "test",
					"script": {
						"exec": [
							"// Assertions",
							"pm.test(\"GET Invitation Response Code \" + pm.response.code, function () {",
							"    postman.setNextRequest(null);",
							"    pm.expect(pm.response.code).to.be.oneOf([200,201,202]);",
							"    postman.setNextRequest();",
							"});",
							"",
							"// Retry constants",
							"// by default, retry every minute for 10 minutes",
							"// after the create invitation, you should get an email ",
							"const retriesCountKey = \"checkr-invitation-status-count\"",
							"const retryDelayMillis = 60000",
							"const maxRetries = 10",
							"",
							"// Response constants",
							"const jsondata = pm.response.json();",
							"const inviteStatus = \"completed\";",
							"",
							"// Run the test",
							"if (jsondata.status !== inviteStatus) {",
							"    console.log(\"Found invitation status \" + jsondata.status + \" but expected \" + inviteStatus);",
							"    console.log(\"Retry request in \" + retryDelayMillis + \"ms\");",
							"    retryRequestWithDelay();",
							"    return;",
							"} else {",
							"    pm.variables.set(\"report_id\",jsondata.report_id);",
							"    };",
							"",
							"",
							"",
							"// Retry functions",
							"function retryRequestWithDelay() {",
							"",
							"    if (getRetriesCount() > maxRetries) {",
							"",
							"        pm.test(",
							"            \"maxRetries of \" + maxRetries + \" reached for \" + retriesCountKey + \". The invitation isn't complete and I'm stopping.\",",
							"            function () {",
							"                throw new Error();",
							"            });",
							"    } else {",
							"        incrementRetriesCount();",
							"        console.log(\"I've looped \"+ pm.variables.get(retriesCountKey));",
							"        setTimeout(() => {}, retryDelayMillis)",
							"        postman.setNextRequest(pm.info.requestId);",
							"    }",
							"}",
							"",
							"function getRetriesCount() {",
							"    return pm.variables.has(retriesCountKey) && pm.variables.get(retriesCountKey) || 0;",
							"}",
							"",
							"function incrementRetriesCount() {",
							"    pm.variables.set(retriesCountKey, getRetriesCount() + 1);",
							"}",
							"",
							""
						],
						"type": "text/javascript"
					}
				},
				{
					"listen": "prerequest",
					"script": {
						"exec": [
							"console.log(\"Step 2 - Your invitation ID Is \" + pm.variables.get('invitation_id') + \"and your invitation URL is below. Paste the URL into your browser and complete the invitation to move forward.\");",
							"console.log(pm.variables.get('invitation_url'));",
							"console.log(\"\");",
							"console.log(\"If you have a webhook endpoint, you should have recieved an invitation.created event\");",
							"console.log(\"\");",
							"console.log(\"The scripts will now loop for the next 10 minutes and poll for invitation status. Complete the invitation manually for a report to be created.\");",
							"console.log(\"\")"
						],
						"type": "text/javascript"
					}
				}
			],
			"protocolProfileBehavior": {
				"disableBodyPruning": true
			},
			"request": {
				"method": "GET",
				"header": [],
				"body": {
					"mode": "formdata",
					"formdata": []
				},
				"url": {
					"raw": "{{endpoint_url}}/v1/invitations/{{invitation_id}}",
					"host": [
						"{{endpoint_url}}"
					],
					"path": [
						"v1",
						"invitations",
						"{{invitation_id}}"
					]
				}
			},
			"response": []
		},
		{
			"name": "Get Report",
			"event": [
				{
					"listen": "test",
					"script": {
						"exec": [
							"pm.test(\"Report GET Response Code \" + pm.response.code, function () {",
							"    postman.setNextRequest(null);",
							"    pm.expect(pm.response.code).to.be.oneOf([200,201,202]);",
							"    postman.setNextRequest();",
							"    var jsondata = pm.response.json(responseBody);",
							"    pm.variables.set(\"report_result\", jsondata.result);",
							"});",
							"",
							"console.log(\"\");",
							"console.log(\"Step 4 - Report \" + pm.variables.get('report_id') + \"has a result value of \" + pm.variables.get('report_result') + \". If you entered mocked data into the invitation forms in the browser, this status should change soon. You can check status on the Checkr Dashboard.\" );",
							"console.log(\"If you have webhooks enabled, you will receive report.updated and report.completed webhooks in the near future.\") "
						],
						"type": "text/javascript"
					}
				},
				{
					"listen": "prerequest",
					"script": {
						"exec": [
							"console.log(\"\");",
							"console.log(\"Step 3 - Your report ID is \" + pm.variables.get('report_id') + \". You can view your report on the Checkr Dashboard by accessing the below URL in your browser:\");",
							"console.log(\"\");",
							"console.log(\"https://dashboard.checkrhq-staging.net/reports/\" + pm.variables.get('report_id'));",
							"console.log(\"\");",
							"console.log(\"If you have a webhook endpoint, you should have recieved an invitation.completed and a report.created event.\");"
						],
						"type": "text/javascript"
					}
				}
			],
			"protocolProfileBehavior": {
				"disableBodyPruning": true
			},
			"request": {
				"method": "GET",
				"header": [],
				"body": {
					"mode": "formdata",
					"formdata": []
				},
				"url": {
					"raw": "{{endpoint_url}}/v1/reports/{{report_id}}?include=candidate,ssn_trace,county_criminal_searches,motor_vehicle_report",
					"host": [
						"{{endpoint_url}}"
					],
					"path": [
						"v1",
						"reports",
						"{{report_id}}"
					],
					"query": [
						{
							"key": "include",
							"value": "candidate,ssn_trace,county_criminal_searches,motor_vehicle_report"
						}
					]
				}
			},
			"response": []
		}
	],
	"auth": {
		"type": "basic",
		"basic": [
			{
				"key": "username",
				"value": "{{api_key}}",
				"type": "string"
			}
		]
	},
	"event": [
		{
			"listen": "prerequest",
			"script": {
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		},
		{
			"listen": "test",
			"script": {
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		}
	],
	"variable": [
		{
			"key": "api_key",
			"value": ""
		},
		{
			"key": "endpoint_url",
			"value": "https://api.checkr-staging.com/"
		},
		{
			"key": "package",
			"value": "criminal_trace_protected_flow"
		},
		{
			"key": "work_location_city",
			"value": "Denver"
		},
		{
			"key": "work_location_state",
			"value": "CO"
		},
		{
			"key": "candidate_email",
			"value": ""
		},
		{
			"key": "candidate_copy_requested",
			"value": "TRUE"
		},
		{
			"key": "candidate_first_name",
			"value": "Liam"
		},
		{
			"key": "candidate_last_name",
			"value": "Nelson"
		},
		{
			"key": "candidate_middle_name",
			"value": "Jack"
		},
		{
			"key": "candidate_ssn",
			"value": "234-23-2344"
		},
		{
			"key": "candidate_dob",
			"value": "1975-12-12"
		},
		{
			"key": "candidate_zipcode",
			"value": "02124"
		},
		{
			"key": "candidate_drivers_license_number",
			"value": "X7657654"
		},
		{
			"key": "candidate_drivers_license_state",
			"value": "CA"
		},
		{
			"key": "account_node",
			"value": "",
			"disabled": true
		}
	]
}