diff --git a/app/org/maproulette/framework/controller/TaskBundleController.scala b/app/org/maproulette/framework/controller/TaskBundleController.scala index addbcfd4..effd945a 100644 --- a/app/org/maproulette/framework/controller/TaskBundleController.scala +++ b/app/org/maproulette/framework/controller/TaskBundleController.scala @@ -297,21 +297,6 @@ class TaskBundleController @Inject() ( } } - /** - * Adds tasks to a bundle. - * - * @param id The id for the bundle - * @param taskIds List of task ids to remove - * @return Task Bundle - */ - def bundleTasks(id: Long, taskIds: List[Long]): Action[AnyContent] = Action.async { - implicit request => - this.sessionManager.authenticatedRequest { implicit user => - this.serviceManager.taskBundle.bundleTasks(user, id, taskIds) - Ok(Json.toJson(this.serviceManager.taskBundle.getTaskBundle(user, id))) - } - } - /** * Delete bundle. * diff --git a/app/org/maproulette/framework/mixins/SearchParametersMixin.scala b/app/org/maproulette/framework/mixins/SearchParametersMixin.scala index bd8559bd..5cf7a16d 100644 --- a/app/org/maproulette/framework/mixins/SearchParametersMixin.scala +++ b/app/org/maproulette/framework/mixins/SearchParametersMixin.scala @@ -315,7 +315,7 @@ trait SearchParametersMixin { case Some(bid) => FilterGroup( List( - CustomParameter(s"CAST(${Task.TABLE}.${Task.FIELD_BUNDLE_ID} AS TEXT) LIKE '${bid}%'") + CustomParameter(s"${Task.TABLE}.${Task.FIELD_BUNDLE_ID} = $bid") ) ) case _ => FilterGroup(List()) diff --git a/app/org/maproulette/framework/repository/TaskBundleRepository.scala b/app/org/maproulette/framework/repository/TaskBundleRepository.scala index a2e8d1b3..53552f14 100644 --- a/app/org/maproulette/framework/repository/TaskBundleRepository.scala +++ b/app/org/maproulette/framework/repository/TaskBundleRepository.scala @@ -82,10 +82,13 @@ class TaskBundleRepository @Inject() ( "inList" -> taskIds ) .executeUpdate() - - primaryId.foreach { id => - SQL"""UPDATE tasks SET is_bundle_primary = true, bundle_id = $bundleId WHERE id = $id""" - .executeUpdate() + primaryId match { + case Some(id) => + val sqlQuery = s"""UPDATE tasks SET is_bundle_primary = true WHERE id = $id""" + SQL(sqlQuery).executeUpdate() + case None => + // Handle the case where primaryId is None + println("primaryId is not defined") } val parameters = lockedTasks.map(task => Seq[NamedParameter]("taskId" -> task.id)) @@ -198,6 +201,14 @@ class TaskBundleRepository @Inject() ( ) .executeUpdate() + // Retrieve the status of the primary task + val primaryTaskStatus = SQL( + """SELECT status FROM tasks WHERE id = {primaryTaskId}""" + ).on( + "primaryTaskId" -> primaryTaskId + ) + .as(SqlParser.scalar[Int].single) + val lockedTasks = this.withListLocking(user, Some(TaskType())) { () => this.taskDAL.retrieveListById(-1, 0)(tasksToAdd) } @@ -207,33 +218,29 @@ class TaskBundleRepository @Inject() ( } catch { case e: Exception => this.logger.warn(e.getMessage) } + // Update the status of all tasks in the bundle to match the status of the primary task + SQL( + """UPDATE tasks + SET status = {primaryTaskStatus} + WHERE bundle_id = {bundleId} + """ + ).on( + "primaryTaskStatus" -> primaryTaskStatus, + "bundleId" -> bundleId + ) + .executeUpdate() + this.cacheManager.withOptionCaching { () => Some( - task.copy(bundleId = Some(bundleId), isBundlePrimary = Some(primaryTaskId == task.id)) + task.copy( + bundleId = Some(bundleId), + status = Some(primaryTaskStatus), + isBundlePrimary = Some(primaryTaskId == task.id) + ) ) } } } - - // Retrieve the status of the primary task - val primaryTaskStatus = SQL( - """SELECT status FROM tasks WHERE id = {primaryTaskId}""" - ).on( - "primaryTaskId" -> primaryTaskId - ) - .as(SqlParser.scalar[Int].single) - - // Update the status of all tasks in the bundle to match the status of the primary task - SQL( - """UPDATE tasks - SET status = {primaryTaskStatus} - WHERE bundle_id = {bundleId} - """ - ).on( - "primaryTaskStatus" -> primaryTaskStatus, - "bundleId" -> bundleId - ) - .executeUpdate() } } @@ -275,25 +282,32 @@ class TaskBundleRepository @Inject() ( case Some(_) => SQL(s"""DELETE FROM task_bundles WHERE bundle_id = $bundleId AND task_id = ${task.id}""").executeUpdate() - + this.cacheManager.withOptionCaching { () => + Some( + task.copy(bundleId = None, status = Some(STATUS_CREATED)) + ) + } if (!preventTaskIdUnlocks.contains(task.id)) { try { this.unlockItem(user, task) } catch { case e: Exception => this.logger.warn(e.getMessage) } - } else { - SQL( - """UPDATE tasks + } + // This is in order to pass the filters so the task is displayed as "available" in task searching and maps. + SQL(s"DELETE FROM task_review tr WHERE tr.task_id = ${task.id}").executeUpdate() + + SQL( + """UPDATE tasks SET status = {status} WHERE id = {taskId} """ - ).on( - "taskId" -> task.id, - "status" -> STATUS_CREATED - ) - .executeUpdate() - } + ).on( + "taskId" -> task.id, + "status" -> STATUS_CREATED + ) + .executeUpdate() + case None => // do nothing } } @@ -301,59 +315,6 @@ class TaskBundleRepository @Inject() ( } } - /** - * Adds tasks to a bundle. - * - * @param bundleId The id of the bundle - */ - def bundleTasks(user: User, bundleId: Long, taskIds: List[Long]): Unit = { - this.withMRConnection { implicit c => - // Update tasks to set their bundle_id to the provided bundleId - SQL(s"""UPDATE tasks SET bundle_id = {bundleId} - WHERE bundle_id IS NULL - AND id IN ({inList})""") - .on( - "bundleId" -> bundleId, - "inList" -> taskIds - ) - .executeUpdate() - - // Construct the SQL query to insert task-bundle associations - val sqlQuery = - s"""INSERT INTO task_bundles (bundle_id, task_id) VALUES ($bundleId, {taskId})""" - - // Construct parameters for the BatchSql operation - val parameters = taskIds.map(taskId => Seq[NamedParameter]("taskId" -> taskId)) - - // Execute the BatchSql operation to insert task-bundle associations - BatchSql(sqlQuery, parameters.head, parameters.tail: _*).execute() - - // Retrieve the tasks associated with the given bundleId - val tasks = this.retrieveTasks( - Query.simple( - List( - BaseParameter("bundle_id", bundleId, table = Some("tb")) - ) - ) - ) - // Lock each of the new tasks to indicate they are part of the bundle - for (task <- tasks) { - try { - this.lockItem(user, task) - } catch { - case e: Exception => this.logger.warn(e.getMessage) - } - this.cacheManager.withOptionCaching { () => - Some( - task.copy( - bundleId = Some(bundleId) - ) - ) - } - } - } - } - /** * Deletes a task bundle. * diff --git a/app/org/maproulette/framework/service/TaskBundleService.scala b/app/org/maproulette/framework/service/TaskBundleService.scala index 73986020..a5d746f2 100644 --- a/app/org/maproulette/framework/service/TaskBundleService.scala +++ b/app/org/maproulette/framework/service/TaskBundleService.scala @@ -106,18 +106,6 @@ class TaskBundleService @Inject() ( this.getTaskBundle(user, bundleId) } - /** - * Removes tasks from a bundle. - * - * @param bundleId The id of the bundle - */ - def bundleTasks(user: User, bundleId: Long, taskIds: List[Long])(): TaskBundle = { - val bundle = this.getTaskBundle(user, bundleId) - - this.repository.bundleTasks(user, bundleId, taskIds) - this.getTaskBundle(user, bundleId) - } - /** * Removes tasks from a bundle. * diff --git a/conf/v2_route/bundle.api b/conf/v2_route/bundle.api index 807cb2d4..e6841b55 100644 --- a/conf/v2_route/bundle.api +++ b/conf/v2_route/bundle.api @@ -116,29 +116,3 @@ DELETE /taskBundle/:id @org.maproulette.framework.c # required: true ### POST /taskBundle/:id/unbundle @org.maproulette.framework.controller.TaskBundleController.unbundleTasks(id:Long, taskIds:List[Long], preventTaskIdUnlocks:List[Long]) -### -# tags: [ Bundle ] -# summary: Bundles extra tasks to Task Bundle -# description: Adds a list of tasks to a bundle of tasks -# responses: -# '200': -# description: The task bundle with the new increased set of tasks -# content: -# application/json: -# schema: -# $ref: '#/components/schemas/TaskBundle' -# '401': -# description: The user is not authorized to make this request -# '404': -# description: No Task Bundle with provided ID found -# parameters: -# - name: id -# in: path -# description: The id of the Task Bundle -# required: true -# - name: taskIds -# in: query -# description: The list of task ids to add from to bundle -# required: true -### -POST /taskBundle/:id/bundle @org.maproulette.framework.controller.TaskBundleController.bundleTasks(id:Long, taskIds:List[Long])