File size: 10,068 Bytes
d236183
 
 
 
 
e02b818
d236183
 
 
 
 
 
 
 
 
 
 
f4c7416
 
 
 
e02b818
 
 
 
d236183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e02b818
d236183
 
 
3b2d340
d236183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4c7416
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e02b818
f4c7416
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e02b818
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package controllers

import (
	"encoding/json"
	"net/http"
	"strconv"

	"dinacom-11.0-backend/models/dto"
	"dinacom-11.0-backend/services"
	"dinacom-11.0-backend/utils"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

type ReportController interface {
	CreateReport(ctx *gin.Context)
	GetReports(ctx *gin.Context)
	AssignWorker(ctx *gin.Context)
	GetAssignedReports(ctx *gin.Context)
	FinishReport(ctx *gin.Context)
	GetUserReports(ctx *gin.Context)
	GetWorkerAssignedReports(ctx *gin.Context)
	GetWorkerHistory(ctx *gin.Context)
	VerifyReport(ctx *gin.Context)
}

type reportController struct {
	reportService services.ReportService
}

func NewReportController(reportService services.ReportService) ReportController {
	return &reportController{reportService: reportService}
}

// @Summary Create Report
// @Description Submit a new report with image and location data
// @Tags Report
// @Accept multipart/form-data
// @Produce json
// @Param files formData file true "Image file (JPG, PNG, JPEG, max 32MB)"
// @Param json formData string true "JSON data" default({"longitude": 106.816666, "latitude": -6.200000, "road_name": "Jalan Sudirman", "description": "Lubang besar di tengah jalan"})
// @Security BearerAuth
// @Success 200 {object} dto.ReportResponse
// @Failure 400 {object} map[string]string
// @Router /api/user/report [post]
func (c *reportController) CreateReport(ctx *gin.Context) {
	userIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}
	userID := userIDVal.(uuid.UUID)

	file, header, err := ctx.Request.FormFile("files")
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "Image file is required")
		return
	}
	defer file.Close()

	jsonData := ctx.PostForm("json")
	if jsonData == "" {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "JSON data is required")
		return
	}

	var req dto.ReportRequest
	if err := json.Unmarshal([]byte(jsonData), &req); err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "Invalid JSON format")
		return
	}

	response, err := c.reportService.CreateReport(userID, file, header, req)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Report created successfully", response)
}

// @Summary Get Reports
// @Description Get all completed reports with non-good destruct class
// @Tags Report
// @Produce json
// @Success 200 {array} dto.ReportLocationResponse
// @Failure 500 {object} map[string]string
// @Router /api/get_report [get]
func (c *reportController) GetReports(ctx *gin.Context) {
	reports, err := c.reportService.GetReports()
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Reports retrieved", reports)
}

// @Summary Assign Worker to Report
// @Description Admin assigns a worker to a report
// @Tags Admin
// @Accept json
// @Produce json
// @Param request body dto.AssignWorkerRequest true "Assign Worker Request"
// @Security BearerAuth
// @Success 200 {object} map[string]string
// @Failure 400 {object} map[string]string
// @Router /api/admin/report/assign [patch]
func (c *reportController) AssignWorker(ctx *gin.Context) {
	var req dto.AssignWorkerRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	message, err := c.reportService.AssignWorker(req)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, message, nil)
}

// @Summary Get Assigned Workers
// @Description Get all workers with assigned reports
// @Tags Admin
// @Produce json
// @Security BearerAuth
// @Success 200 {array} dto.AssignedWorkerResponse
// @Failure 500 {object} map[string]string
// @Router /api/admin/report/assign [get]
func (c *reportController) GetAssignedReports(ctx *gin.Context) {
	reports, err := c.reportService.GetAssignedReports()
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Assigned reports retrieved", reports)
}

// @Summary Finish Report by Worker
// @Description Worker uploads after image and marks report as finished
// @Tags Worker
// @Accept multipart/form-data
// @Produce json
// @Param files formData file true "After image file"
// @Param json formData string true "JSON data" default({"report_id": "uuid-here"})
// @Security BearerAuth
// @Success 200 {object} map[string]string
// @Failure 400 {object} map[string]string
// @Router /api/worker/report [patch]
func (c *reportController) FinishReport(ctx *gin.Context) {
	workerIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}
	workerID := workerIDVal.(uuid.UUID)

	file, header, err := ctx.Request.FormFile("files")
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "Image file is required")
		return
	}
	defer file.Close()

	jsonData := ctx.PostForm("json")
	if jsonData == "" {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "JSON data is required")
		return
	}

	var req dto.WorkerReportRequest
	if err := json.Unmarshal([]byte(jsonData), &req); err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, "Invalid JSON format")
		return
	}

	if err := c.reportService.FinishReport(workerID, file, header, req.ReportID); err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Report finished successfully", nil)
}

// @Summary Get User's Reports
// @Description Get all reports created by the logged-in user with pagination
// @Tags User
// @Produce json
// @Param page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10)
// @Security BearerAuth
// @Success 200 {object} dto.PaginatedReportsResponse
// @Failure 401 {object} map[string]string
// @Router /api/user/report/me [get]
func (c *reportController) GetUserReports(ctx *gin.Context) {
	userIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}
	userID := userIDVal.(uuid.UUID)

	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
	if page < 1 {
		page = 1
	}
	if limit < 1 || limit > 100 {
		limit = 10
	}

	response, err := c.reportService.GetUserReports(userID, page, limit)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "User reports retrieved", response)
}

// @Summary Get Worker's Assigned Reports
// @Description Get all reports assigned to the logged-in worker with pagination
// @Tags Worker
// @Produce json
// @Param page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10)
// @Security BearerAuth
// @Success 200 {object} dto.PaginatedReportsResponse
// @Failure 401 {object} map[string]string
// @Router /api/worker/report/assign/me [get]
func (c *reportController) GetWorkerAssignedReports(ctx *gin.Context) {
	workerIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}
	workerID := workerIDVal.(uuid.UUID)

	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
	if page < 1 {
		page = 1
	}
	if limit < 1 || limit > 100 {
		limit = 10
	}

	response, err := c.reportService.GetWorkerAssignedReports(workerID, page, limit)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Worker assigned reports retrieved", response)
}

// @Summary Get Worker's History
// @Description Get worker's completed reports with pagination and status filter
// @Tags Worker
// @Produce json
// @Param page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10)
// @Param verify_admin query bool false "True: finished, False: Finish by Worker" default(false)
// @Security BearerAuth
// @Success 200 {object} dto.PaginatedReportsResponse
// @Failure 401 {object} map[string]string
// @Router /api/worker/report/history/me [get]
func (c *reportController) GetWorkerHistory(ctx *gin.Context) {
	workerIDVal, exists := ctx.Get("user_id")
	if !exists {
		utils.SendErrorResponse(ctx, http.StatusUnauthorized, "Unauthorized")
		return
	}
	workerID := workerIDVal.(uuid.UUID)

	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
	verifyAdmin := ctx.DefaultQuery("verify_admin", "false") == "true"
	if page < 1 {
		page = 1
	}
	if limit < 1 || limit > 100 {
		limit = 10
	}

	response, err := c.reportService.GetWorkerHistory(workerID, verifyAdmin, page, limit)
	if err != nil {
		utils.SendErrorResponse(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Worker history retrieved", response)
}

// @Summary Verify Report by Admin
// @Description Admin verifies a report with status 'Finish by Worker' to 'finished'
// @Tags Admin
// @Accept json
// @Produce json
// @Param request body dto.VerifyReportRequest true "Verify Report Request"
// @Security BearerAuth
// @Success 200 {object} map[string]string
// @Failure 400 {object} map[string]string
// @Router /api/admin/report/verify [patch]
func (c *reportController) VerifyReport(ctx *gin.Context) {
	var req dto.VerifyReportRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	if err := c.reportService.VerifyReport(req.ReportID); err != nil {
		utils.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
		return
	}

	utils.SendSuccessResponse(ctx, "Report verified successfully", nil)
}