{
	"info": {
		"_postman_id": "32-Completion-Certificate-Locking",
		"name": "Maintenance - Completion & Data Locking (US-013)",
		"description": "Collection for testing the Maintenance Visit handover process, including Completion Certificate submission and strict data locking enforcement.",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [
		{
			"name": "Case 1: Happy Path (Standard Workflow)",
			"item": [
				{
					"name": "1. GET Handover Data (Installed Parts Summary)",
					"event": [
						{
							"listen": "test",
							"script": {
								"exec": [
									"pm.test(\"Parts Summary Present\", function () {",
									"    var jsonData = pm.response.json();",
									"    pm.expect(jsonData.data.installed_parts).to.be.an('array');",
									"});"
								],
								"type": "text/javascript"
							}
						}
					],
					"request": {
						"method": "GET",
						"header": [],
						"url": {
							"raw": "{{baseUrl}}/api/maintenance/visits/{{visitId}}/completion-certificate",
							"host": [
								"{{baseUrl}}"
							],
							"path": [
								"api",
								"maintenance",
								"visits",
								"{{visitId}}",
								"completion-certificate"
							]
						},
						"description": "Fetches certificate info and the aggregated list of installed parts for client review."
					},
					"response": []
				},
				{
					"name": "2. Submit Completion Certificate",
					"event": [
						{
							"listen": "test",
							"script": {
								"exec": [
									"pm.test(\"Certificate Submitted & Status Updated\", function () {",
									"    pm.response.to.have.status(201);",
									"});"
								],
								"type": "text/javascript"
							}
						}
					],
					"request": {
						"method": "POST",
						"header": [],
						"body": {
							"mode": "formdata",
							"formdata": [
								{
									"key": "signed_by_name",
									"value": "Ibrahim Shikeh",
									"type": "text"
								},
								{
									"key": "client_notes",
									"value": "Technician was very professional. Work completed on time.",
									"type": "text"
								},
								{
									"key": "replaced_parts_acknowledged",
									"value": "true",
									"type": "text"
								},
								{
									"key": "signature_lat",
									"value": "24.7136",
									"type": "text"
								},
								{
									"key": "signature_lng",
									"value": "46.6753",
									"type": "text"
								},
								{
									"key": "document",
									"type": "file",
									"src": []
								}
							]
						},
						"url": {
							"raw": "{{baseUrl}}/api/maintenance/visits/{{visitId}}/completion-certificate",
							"host": [
								"{{baseUrl}}"
							],
							"path": [
								"api",
								"maintenance",
								"visits",
								"{{visitId}}",
								"completion-certificate"
							]
						},
						"description": "Stores the certificate, uploads the document, and triggers the visit lock. Transitions status to AWAITING_INVOICING."
					},
					"response": []
				},
				{
					"name": "3. Verify Visit Status",
					"event": [
						{
							"listen": "test",
							"script": {
								"exec": [
									"pm.test(\"Status is AWAITING_INVOICING\", function () {",
									"    var jsonData = pm.response.json();",
									"    pm.expect(jsonData.data.status).to.eql(\"awaiting_invoicing\");",
									"});"
								],
								"type": "text/javascript"
							}
						}
					],
					"request": {
						"method": "GET",
						"header": [],
						"url": {
							"raw": "{{baseUrl}}/api/maintenance/visits/{{visitId}}",
							"host": [
								"{{baseUrl}}"
							],
							"path": [
								"api",
								"maintenance",
								"visits",
								"{{visitId}}"
							]
						}
					},
					"response": []
				}
			]
		},
		{
			"name": "Case 2: Data Locking Enforcement",
			"item": [
				{
					"name": "Attempt to Update Part (Locked)",
					"event": [
						{
							"listen": "test",
							"script": {
								"exec": [
									"pm.test(\"Fails: Visit is locked\", function () {",
									"    pm.response.to.have.status(403);",
									"    pm.expect(pm.response.json().message).to.contain(\"locked\");",
									"});"
								],
								"type": "text/javascript"
							}
						}
					],
					"request": {
						"method": "PUT",
						"header": [
							{
								"key": "Content-Type",
								"value": "application/json"
							}
						],
						"body": {
							"mode": "raw",
							"raw": "{\n    \"quantity\": 20\n}"
						},
						"url": {
							"raw": "{{baseUrl}}/api/maintenance/visit-spare-parts/{{partId}}",
							"host": [
								"{{baseUrl}}"
							],
							"path": [
								"api",
								"maintenance",
								"visit-spare-parts",
								"{{partId}}"
							]
						},
						"description": "Attempting to change quantity of an installed part after certificate submission should fail."
					},
					"response": []
				},
				{
					"name": "Attempt to Delete Daily Log (Locked)",
					"event": [
						{
							"listen": "test",
							"script": {
								"exec": [
									"pm.test(\"Fails: Visit is locked\", function () {",
									"    pm.response.to.have.status(403);",
									"    pm.expect(pm.response.json().message).to.contain(\"locked\");",
									"});"
								],
								"type": "text/javascript"
							}
						}
					],
					"request": {
						"method": "DELETE",
						"header": [],
						"url": {
							"raw": "{{baseUrl}}/api/maintenance/visit-daily-logs/{{logId}}",
							"host": [
								"{{baseUrl}}"
							],
							"path": [
								"api",
								"maintenance",
								"visit-daily-logs",
								"{{logId}}"
							]
						},
						"description": "Attempting to delete a daily log after certificate submission should fail."
					},
					"response": []
				}
			]
		},
		{
			"name": "Case 3: Edge Cases & Constraints",
			"item": [
				{
					"name": "Invalid Status (Scheduled)",
					"event": [
						{
							"listen": "test",
							"script": {
								"exec": [
									"pm.test(\"Fails: Visit not completed\", function () {",
									"    pm.response.to.have.status(422);",
									"    pm.expect(pm.response.json().message).to.contain(\"completed\");",
									"});"
								],
								"type": "text/javascript"
							}
						}
					],
					"request": {
						"method": "POST",
						"header": [],
						"body": {
							"mode": "formdata",
							"formdata": [
								{
									"key": "signed_by_name",
									"value": "Test",
									"type": "text"
								},
								{
									"key": "replaced_parts_acknowledged",
									"value": "true",
									"type": "text"
								},
								{
									"key": "document",
									"type": "file",
									"src": []
								}
							]
						},
						"url": {
							"raw": "{{baseUrl}}/api/maintenance/visits/{{scheduledVisitId}}/completion-certificate",
							"host": [
								"{{baseUrl}}"
							],
							"path": [
								"api",
								"maintenance",
								"visits",
								"{{scheduledVisitId}}",
								"completion-certificate"
							]
						},
						"description": "Submit certificate for a visit that is still in SCHEDULED status."
					},
					"response": []
				},
				{
					"name": "Closing Guard (Missing Certificate)",
					"event": [
						{
							"listen": "test",
							"script": {
								"exec": [
									"pm.test(\"Fails: Certificate required to close\", function () {",
									"    pm.response.to.have.status(400);",
									"    pm.expect(pm.response.json().message).to.contain(\"certificate\");",
									"});"
								],
								"type": "text/javascript"
							}
						}
					],
					"request": {
						"method": "POST",
						"header": [],
						"url": {
							"raw": "{{baseUrl}}/api/maintenance/visits/{{visitIdWithoutCert}}/execution/close",
							"host": [
								"{{baseUrl}}"
							],
							"path": [
								"api",
								"maintenance",
								"visits",
								"{{visitIdWithoutCert}}",
								"execution",
								"close"
							]
						},
						"description": "Attempt to close a visit that has no submitted completion certificate."
					},
					"response": []
				}
			]
		}
	],
	"variable": [
		{
			"key": "baseUrl",
			"value": "https://api.example.com"
		},
		{
			"key": "visitId",
			"value": "123"
		},
		{
			"key": "partId",
			"value": "456"
		},
		{
			"key": "logId",
			"value": "789"
		},
		{
			"key": "scheduledVisitId",
			"value": "101"
		},
		{
			"key": "visitIdWithoutCert",
			"value": "102"
		}
	]
}
