{
	"info": {
		"_postman_id": "920b2980-9710-4f1a-a6e7-470009f46b18",
		"name": "Update on legacy subscription_id",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
		"_exporter_id": "3432402"
	},
	"item": [
		{
			"name": "Search by legacy_id and update",
			"item": [
				{
					"name": "Search Subscriptions by legacy_id",
					"event": [
						{
							"listen": "test",
							"script": {
								"exec": [
									"if (pm.response.json().summary.hits >= 1) {",
									"    // Extract the first uuid from the accounts object in the response",
									"    pm.test(\"Extract the first uuid from the response object\", function () {",
									"        var result = pm.response.json();",
									"                console.log(result);",
									"",
									"        pm.expect(result.hits[0].uuid).to.exist;",
									"        pm.environment.set(\"subscriptionUuid\", result.hits[0].uuid);",
									"    });",
									"    console.log(pm.environment.get(\"subscriptionUuid\"));",
									"}",
									"else {",
									"        console.log(\"No exact match by source_uuid. Hits found: \" + pm.response.json().summary.hits)",
									"        let currentValue = pm.environment.get(\"subscription_update_failure_log\");",
									"        pm.environment.set(\"subscription_update_failure_log\", currentValue + pm.environment.get(\"subscriptionUuid\") + ',' + 'Failure to retrieve subsciription for chartiy' + \"\\n\");",
									"}"
								],
								"type": "text/javascript",
								"packages": {}
							}
						},
						{
							"listen": "prerequest",
							"script": {
								"exec": [
									""
								],
								"type": "text/javascript",
								"packages": {}
							}
						}
					],
					"request": {
						"method": "POST",
						"header": [],
						"body": {
							"mode": "raw",
							"raw": "{\n  \"query\": {\n    \"$and\": [\n      {\n        \"$term\": {\n          \"status\": \"active\"\n        }\n      },\n      {\n        \"$term\": {\n          \"custom_parameters.source_identifier\": \"{{legacy_subscription_id}}\"\n        }\n      }\n    ]\n  },\n  \"size\": 10000,\n  \"from\": 0,\n  \"includes\": [\"uuid\",\"amount\",\"custom_parameters\",\"payment_method\"],\n  \"excludes\": [],\n  \"aggs\": {\n    \"status\": {\n      \"field\": \"status\",\n      \"type\": \"terms\"\n    }\n  },\n  \"sort\": {\n    \"created\": \"DESC\"\n  }\n}",
							"options": {
								"raw": {
									"language": "json"
								}
							}
						},
						"url": {
							"raw": "{{base_url}}/search/subscriptions",
							"host": [
								"{{base_url}}"
							],
							"path": [
								"search",
								"subscriptions"
							]
						}
					},
					"response": []
				},
				{
					"name": "Update Subscription",
					"event": [
						{
							"listen": "test",
							"script": {
								"exec": [
									"setTimeout(function() {",
									"    pm.environment.unset(\"custom_parameters\");",
									"}, 5000);",
									""
								],
								"type": "text/javascript",
								"packages": {}
							}
						},
						{
							"listen": "prerequest",
							"script": {
								"exec": [
									"    let custom_parameters = {};",
									"",
									"    if (pm.iterationData.get(\"my_field\") !== 'NULL') {",
									"        custom_parameters.my_field1 = pm.iterationData.get(\"my_field1\").toString();",
									"        custom_parameters.my_field2 = pm.iterationData.get(\"my_field2\").toString();",
									"    }",
									"    pm.environment.set(\"custom_parameters\",JSON.stringify(custom_parameters));",
									"",
									""
								],
								"type": "text/javascript",
								"packages": {}
							}
						}
					],
					"request": {
						"method": "PATCH",
						"header": [],
						"body": {
							"mode": "raw",
							"raw": "{\n \"custom_parameters\": {{custom_parameters}}\n}",
							"options": {
								"raw": {
									"language": "json"
								}
							}
						},
						"url": {
							"raw": "{{base_url}}/subscriptions/{{subscriptionUuid}}",
							"host": [
								"{{base_url}}"
							],
							"path": [
								"subscriptions",
								"{{subscriptionUuid}}"
							]
						}
					},
					"response": []
				}
			]
		}
	],
	"auth": {
		"type": "oauth2",
		"oauth2": [
			{
				"key": "clientSecret",
				"value": "{{client_secret}}",
				"type": "string"
			},
			{
				"key": "clientId",
				"value": "{{client_id}}",
				"type": "string"
			},
			{
				"key": "accessTokenUrl",
				"value": "https://api.raisenow.io/oauth2/token",
				"type": "string"
			},
			{
				"key": "tokenName",
				"value": "NDCS-Token",
				"type": "string"
			},
			{
				"key": "challengeAlgorithm",
				"value": "S256",
				"type": "string"
			},
			{
				"key": "useBrowser",
				"value": true,
				"type": "boolean"
			},
			{
				"key": "grant_type",
				"value": "client_credentials",
				"type": "string"
			},
			{
				"key": "addTokenTo",
				"value": "header",
				"type": "string"
			},
			{
				"key": "client_authentication",
				"value": "body",
				"type": "string"
			}
		]
	},
	"event": [
		{
			"listen": "prerequest",
			"script": {
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		},
		{
			"listen": "test",
			"script": {
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		}
	],
	"variable": [
		{
			"key": "epp_api_key",
			"value": "pro-ptvc2h"
		},
		{
			"key": "mpos_transactions_id",
			"value": ""
		},
		{
			"key": "refno",
			"value": ""
		},
		{
			"key": "timestamp",
			"value": ""
		},
		{
			"key": "refno_merchant_id",
			"value": "sal-3rt189"
		},
		{
			"key": "refno_organisation",
			"value": "acme"
		},
		{
			"key": "terminal_id",
			"value": "1234"
		},
		{
			"key": "space_id",
			"value": "1234"
		},
		{
			"key": "campaign_id",
			"value": "7011j000000RgzmAAC"
		},
		{
			"key": "account_uuid",
			"value": "0c3ba797-90ee-45e1-a4e2-0b5c19ac1009"
		},
		{
			"key": "epms_organization",
			"value": "5a98f9da-9be1-43eb-a071-53d47faf83f3"
		},
		{
			"key": "direct_debite_payment_profile",
			"value": "ch_dd"
		},
		{
			"key": "crm_fundraising_team_id",
			"value": "a1R1j000000EtE1EAK"
		},
		{
			"key": "crm_fundraiser_id",
			"value": "0031j00000tdga9AAA"
		},
		{
			"key": "epm_client_id",
			"value": "{{epms_client_id}}"
		},
		{
			"key": "epms_client_secret",
			"value": "epms_client_secret"
		}
	]
}