Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE MATERIALIZED VIEW responsible_view AS SELECT distinct ("document" -> 'responsible' ->> 'responsibleId') as responsibleId FROM articledata where ("document" -> 'responsible' ->> 'responsibleId') IS NOT NULL;
CREATE UNIQUE INDEX responsibleId_idx ON responsible_view(responsibleId);
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class DraftController(using
migrateOutdatedGreps,
addNotes,
deleteCurrentRevision,
getResponsibles,
)

/** Does a scroll with [[ArticleSearchService]] If no scrollId is specified execute the function @orFunction in the
Expand Down Expand Up @@ -672,4 +673,19 @@ class DraftController(using
.serverLogicPure { _ => articleId =>
writeService.deleteCurrentRevision(articleId).handleErrorsOrOk
}

def getResponsibles: ServerEndpoint[Any, Eff] = endpoint
.get
.in("responsibles" / "list")
.summary("Get list of responsibles for drafts")
.description("Get list of responsibles for drafts")
.out(jsonBody[Seq[String]])
.errorOut(errorOutputsFor(401, 403))
.requirePermission(DRAFT_API_WRITE)
.serverLogicPure { _ => _ =>
readService.getAllResponsibles match {
case Success(resp) => Right(resp)
case Failure(ex) => errorHandling.returnLeftError(ex)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,12 @@ class DraftRepository(using draftErrorHelpers: DraftErrorHelpers, clock: Clock)
sq.map(rs => rs.long("count")).runSingle().map(_.exists(_ > 0))
}

def refreshResponsibleView(using session: DBSession): Try[Unit] = {
tsql"refresh materialized view responsible_view".update().map(_ => ())
}

def getAllResponsibles(using session: DBSession): Try[Seq[String]] = {
tsql"""select responsibleId from responsible_view""".map(rs => rs.string("responsibleId")).runList()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,12 @@ class ReadService(using

Success(ArticleRevisionHistoryDTO(articles, canDeleteCurrentRevision))
}

def getAllResponsibles: Try[Seq[String]] = dbUtility.readOnly { implicit session =>
draftRepository.getAllResponsibles match {
case Failure(exception) => Failure(exception)
case Success(value) => Success(value)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class WriteService(using
statusWasUpdated: Boolean,
updatedApiArticle: api.UpdatedArticleDTO,
shouldNotAutoUpdateStatus: Boolean,
): Draft = {
)(using DBSession): Draft = {
val isAutomaticResponsibleChange = updatedApiArticle.responsibleId match {
case UpdateWith(_) => false
case _ => true
Expand All @@ -353,7 +353,16 @@ class WriteService(using
draft.copy(started = true)
} else {
val responsibleIdWasUpdated = hasResponsibleBeenUpdated(draft, oldDraft)

if (responsibleIdWasUpdated) {
draftRepository
.refreshResponsibleView
.recover { case ex =>
logger.error(
s"Failed to refresh responsible view after responsible change for article ${draft.id.getOrElse("unknown")}",
ex,
)
}: Unit
}
val shouldReset = statusWasUpdated && !isAutomaticStatusChange || responsibleIdWasUpdated
draft.copy(started = !shouldReset)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,7 @@ class WriteServiceTest extends UnitSuite with TestEnvironment {
.copy(revision = 1, title = Some("updated title"), language = Some("nb"), responsibleId = UpdateWith("heiho"))
when(draftRepository.slugExists(any, any)(using any)).thenReturn(Success(false))
when(draftRepository.withId(eqTo(existing.id.get))(using any)).thenReturn(Success(Some(existing)))
when(draftRepository.refreshResponsibleView(using any)).thenReturn(Success(()))
val result = service.updateArticle(existing.id.get, updatedArticle, TestData.userWithWriteAccess).get

result.started should be(false)
Expand Down