| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import Constants from "../Models/Constants" | 
					
						
							| 
									
										
										
										
											2022-07-13 08:03:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class Maproulette { | 
					
						
							| 
									
										
										
										
											2023-02-14 00:09:04 +01:00
										 |  |  |     public static readonly STATUS_OPEN = 0 | 
					
						
							|  |  |  |     public static readonly STATUS_FIXED = 1 | 
					
						
							|  |  |  |     public static readonly STATUS_FALSE_POSITIVE = 2 | 
					
						
							|  |  |  |     public static readonly STATUS_SKIPPED = 3 | 
					
						
							|  |  |  |     public static readonly STATUS_DELETED = 4 | 
					
						
							|  |  |  |     public static readonly STATUS_ALREADY_FIXED = 5 | 
					
						
							|  |  |  |     public static readonly STATUS_TOO_HARD = 6 | 
					
						
							|  |  |  |     public static readonly STATUS_DISABLED = 9 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static readonly STATUS_MEANING = { | 
					
						
							|  |  |  |         0: "Open", | 
					
						
							|  |  |  |         1: "Fixed", | 
					
						
							|  |  |  |         2: "False positive", | 
					
						
							|  |  |  |         3: "Skipped", | 
					
						
							|  |  |  |         4: "Deleted", | 
					
						
							|  |  |  |         5: "Already fixed", | 
					
						
							|  |  |  |         6: "Too hard", | 
					
						
							|  |  |  |         9: "Disabled", | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |      * The API endpoint to use | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     endpoint: string | 
					
						
							| 
									
										
										
										
											2022-07-13 08:03:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * The API key to use for all requests | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private apiKey: string | 
					
						
							| 
									
										
										
										
											2022-07-13 08:03:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Creates a new Maproulette instance | 
					
						
							|  |  |  |      * @param endpoint The API endpoint to use | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     constructor(endpoint: string = "https://maproulette.org/api/v2") { | 
					
						
							|  |  |  |         this.endpoint = endpoint | 
					
						
							|  |  |  |         this.apiKey = Constants.MaprouletteApiKey | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-13 08:03:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-02-14 00:09:04 +01:00
										 |  |  |      * Close a task; might throw an error | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Also see:https://maproulette.org/docs/swagger-ui/index.html?url=/assets/swagger.json&docExpansion=none#/Task/setTaskStatus
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |      * @param taskId The task to close | 
					
						
							| 
									
										
										
										
											2023-02-14 00:09:04 +01:00
										 |  |  |      * @param status A number indicating the status. Use MapRoulette.STATUS_* | 
					
						
							|  |  |  |      * @param options Additional settings to pass. Refer to the API-docs for more information | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-02-14 00:09:04 +01:00
										 |  |  |     async closeTask( | 
					
						
							|  |  |  |         taskId: number, | 
					
						
							|  |  |  |         status = Maproulette.STATUS_FIXED, | 
					
						
							|  |  |  |         options?: { | 
					
						
							|  |  |  |             comment?: string | 
					
						
							|  |  |  |             tags?: string | 
					
						
							|  |  |  |             requestReview?: boolean | 
					
						
							|  |  |  |             completionResponses?: Record<string, string> | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ): Promise<void> { | 
					
						
							|  |  |  |         const response = await fetch(`${this.endpoint}/task/${taskId}/${status}`, { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             method: "PUT", | 
					
						
							|  |  |  |             headers: { | 
					
						
							|  |  |  |                 "Content-Type": "application/json", | 
					
						
							|  |  |  |                 apiKey: this.apiKey, | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2023-02-14 00:09:04 +01:00
										 |  |  |             body: options !== undefined ? JSON.stringify(options) : undefined, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2023-02-14 00:09:04 +01:00
										 |  |  |         if (response.status !== 204) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             console.log(`Failed to close task: ${response.status}`) | 
					
						
							| 
									
										
										
										
											2023-02-14 00:09:04 +01:00
										 |  |  |             throw `Failed to close task: ${response.status}` | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-07-13 08:03:09 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } |