Skip to content

Commit 6a29384

Browse files
committed
feat: add operations router
1 parent 0a8ce56 commit 6a29384

File tree

4 files changed

+296
-1
lines changed

4 files changed

+296
-1
lines changed
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
11
package com.ctrlhub.core.governance.schemes.workorders.operations
22

3+
import com.ctrlhub.core.Api
4+
import com.ctrlhub.core.governance.schemes.workorders.WorkOrdersRouter
35
import com.ctrlhub.core.governance.schemes.workorders.operations.response.Operation
46
import com.ctrlhub.core.router.Router
57
import io.ktor.client.HttpClient
68

79
class OperationsRouter(httpClient: HttpClient) : Router(httpClient) {
10+
11+
/**
12+
* Retrieve a list of all operations
13+
*
14+
* @param organisationId String The organisation ID to retrieve all operations for
15+
* @param schemeId String The scheme ID to retrieve all operations for
16+
* @param workOrderId String The work order ID to retreive all operations for
17+
*
18+
* @return A list of all operations
19+
*/
820
suspend fun all(organisationId: String, schemeId: String, workOrderId: String): List<Operation> {
921
val endpoint = "/v3/orgs/$organisationId/governance/schemes/$schemeId/work-orders/$workOrderId/operations"
1022

1123
return fetchJsonApiResources(endpoint)
1224
}
1325

26+
/**
27+
* Retrieve a single operation
28+
*
29+
* @param organisationId String The organisation ID to retrieve all operations for
30+
* @param schemeId String The scheme ID to retrieve all operations for
31+
* @param workOrderId String The work order ID to retrieve all operations for
32+
* @param operationId String The operation ID to retrieve data for
33+
*
34+
* @return A list of all operations
35+
*/
1436
suspend fun one(organisationId: String, schemeId: String, workOrderId: String, operationId: String): Operation {
1537
val endpoint = "/v3/orgs/$organisationId/governance/schemes/$schemeId/work-orders/$workOrderId/operations/$operationId"
1638

1739
return fetchJsonApiResource(endpoint)
1840
}
19-
}
41+
}
42+
43+
val Api.operations: OperationsRouter
44+
get() = OperationsRouter(httpClient)
45+
46+
val WorkOrdersRouter.operations: OperationsRouter
47+
get() = OperationsRouter(httpClient)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.ctrlhub.core.governance.schemes.workorders.operations
2+
3+
import com.ctrlhub.core.configureForTest
4+
import com.ctrlhub.core.governance.schemes.workorders.operations.response.Operation
5+
import io.ktor.client.*
6+
import io.ktor.client.engine.mock.*
7+
import io.ktor.http.*
8+
import kotlinx.coroutines.runBlocking
9+
import org.junit.jupiter.api.Test
10+
import java.nio.file.Files
11+
import java.nio.file.Paths
12+
import kotlin.test.assertIs
13+
import kotlin.test.assertNotNull
14+
15+
class OperationsRouterTest {
16+
@Test
17+
fun `can retrieve all operations for work order`() {
18+
val jsonFilePath = Paths.get("src/test/resources/governance/schemes/workorders/operations/all-operations-response.json")
19+
val jsonContent = Files.readString(jsonFilePath)
20+
21+
val mockEngine = MockEngine { request ->
22+
respond(
23+
content = jsonContent,
24+
status = HttpStatusCode.OK,
25+
headers = headersOf(HttpHeaders.ContentType, "application/json")
26+
)
27+
}
28+
29+
val operationsRouter = OperationsRouter(httpClient = HttpClient(mockEngine).configureForTest())
30+
31+
runBlocking {
32+
val response = operationsRouter.all(
33+
organisationId = "123",
34+
schemeId = "abc",
35+
workOrderId = "def"
36+
)
37+
38+
assertIs<List<Operation>>(response)
39+
assertNotNull(response[0].id)
40+
}
41+
}
42+
43+
@Test
44+
fun `can retrieve a single operation`() {
45+
val jsonFilePath = Paths.get("src/test/resources/governance/schemes/workorders/operations/one-operation-response.json")
46+
val jsonContent = Files.readString(jsonFilePath)
47+
48+
val mockEngine = MockEngine { request ->
49+
respond(
50+
content = jsonContent,
51+
status = HttpStatusCode.OK,
52+
headers = headersOf(HttpHeaders.ContentType, "application/json")
53+
)
54+
}
55+
56+
val operationsRouter = OperationsRouter(httpClient = HttpClient(mockEngine).configureForTest())
57+
58+
runBlocking {
59+
val response = operationsRouter.one(
60+
organisationId = "123",
61+
schemeId = "abc",
62+
workOrderId = "def",
63+
operationId = "ghi"
64+
)
65+
66+
assertIs<Operation>(response)
67+
assertNotNull(response.id)
68+
}
69+
}
70+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"data": [
3+
{
4+
"id": "a3b0f0d6-34d1-4f87-9a6a-58cf8f92e57d",
5+
"type": "operations",
6+
"attributes": {
7+
"aborted": false,
8+
"cancelled": false,
9+
"code": "xxxxxx",
10+
"completed": false,
11+
"description": null,
12+
"end_date": null,
13+
"labels": [],
14+
"name": "xxxxxx",
15+
"start_date": null,
16+
"type": null,
17+
"uprns": [],
18+
"usrns": []
19+
},
20+
"relationships": {
21+
"assignees": {
22+
"data": [
23+
{
24+
"id": "e16e8b2a-8304-4297-aabf-7b03a57d8f93",
25+
"type": "users"
26+
}
27+
]
28+
},
29+
"properties": {
30+
"data": []
31+
},
32+
"streets": {
33+
"data": []
34+
},
35+
"template": {
36+
"data": null
37+
},
38+
"work_order": {
39+
"data": {
40+
"id": "3cd07051-74ff-4f2d-93f5-1ae8b2535d78",
41+
"type": "work-orders"
42+
}
43+
}
44+
},
45+
"meta": {
46+
"created_at": "2025-02-16T16:50:29Z",
47+
"updated_at": "2025-02-16T16:50:29Z"
48+
}
49+
},
50+
{
51+
"id": "0d5c5f4c-3a0d-4284-bbe4-c0d5898fe6e0",
52+
"type": "operations",
53+
"attributes": {
54+
"aborted": false,
55+
"cancelled": false,
56+
"code": "xxxxxx",
57+
"completed": false,
58+
"description": null,
59+
"end_date": null,
60+
"labels": [],
61+
"name": "xxxxxx",
62+
"start_date": null,
63+
"type": null,
64+
"uprns": [],
65+
"usrns": []
66+
},
67+
"relationships": {
68+
"assignees": {
69+
"data": []
70+
},
71+
"properties": {
72+
"data": []
73+
},
74+
"streets": {
75+
"data": []
76+
},
77+
"template": {
78+
"data": null
79+
},
80+
"work_order": {
81+
"data": {
82+
"id": "3cd07051-74ff-4f2d-93f5-1ae8b2535d78",
83+
"type": "work-orders"
84+
}
85+
}
86+
},
87+
"meta": {
88+
"created_at": "2025-02-16T16:51:00Z",
89+
"updated_at": "2025-02-16T16:51:00Z"
90+
}
91+
}
92+
],
93+
"meta": {
94+
"features": {
95+
"params": {
96+
"include": {
97+
"options": [
98+
"work_order",
99+
"properties",
100+
"streets",
101+
"assignees"
102+
]
103+
},
104+
"sort": {
105+
"default": "name",
106+
"options": [
107+
"name",
108+
"created_at"
109+
]
110+
},
111+
"filter": {
112+
"options": null
113+
}
114+
}
115+
},
116+
"count": 2,
117+
"pagination": {
118+
"current_page": 1,
119+
"counts": {
120+
"resources": 2,
121+
"pages": 1
122+
},
123+
"requested": {
124+
"offset": 0,
125+
"limit": 100
126+
},
127+
"offsets": {
128+
"previous": null,
129+
"next": null
130+
}
131+
}
132+
},
133+
"jsonapi": {
134+
"version": "1.0",
135+
"meta": {}
136+
},
137+
"links": {
138+
"self": "https://api.ctrl-hub.dev/v3/orgs/21c7d3e3-2e88-46c0-a3a0-d23cb8e819b0/governance/schemes/824bc8a0-e25f-44f2-9e9a-3e758ee2a99f/work-orders/3cd07051-74ff-4f2d-93f5-1ae8b2535d78/operations"
139+
}
140+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"data": {
3+
"id": "f1bbc6ae-ff6c-4b4b-85ba-0c3df64118aa",
4+
"type": "operations",
5+
"attributes": {
6+
"aborted": false,
7+
"cancelled": false,
8+
"code": "bc.1.1",
9+
"completed": false,
10+
"description": null,
11+
"end_date": null,
12+
"labels": [],
13+
"name": "bc.1.1",
14+
"start_date": null,
15+
"type": null,
16+
"uprns": [],
17+
"usrns": []
18+
},
19+
"relationships": {
20+
"assignees": {
21+
"data": [
22+
{
23+
"id": "36286b89-e120-48b5-89a3-c8bf92848711",
24+
"type": "users"
25+
}
26+
]
27+
},
28+
"properties": {
29+
"data": []
30+
},
31+
"streets": {
32+
"data": []
33+
},
34+
"template": {
35+
"data": null
36+
},
37+
"work_order": {
38+
"data": {
39+
"id": "0c3efcf8-2d8b-48bf-8a46-371fcff2de2c",
40+
"type": "work-orders"
41+
}
42+
}
43+
},
44+
"meta": {
45+
"created_at": "2025-02-16T16:50:29Z",
46+
"updated_at": "2025-02-16T16:50:29Z"
47+
}
48+
},
49+
"meta": {},
50+
"jsonapi": {
51+
"version": "1.0",
52+
"meta": {}
53+
},
54+
"links": {
55+
"self": "https://api.ctrl-hub.dev/v3/orgs/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/governance/schemes/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/work-orders/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/operations/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
56+
}
57+
}

0 commit comments

Comments
 (0)