Skip to content

Commit

Permalink
Merge pull request #608 from MAIF/enhancement/linked-documentation-pages
Browse files Browse the repository at this point in the history
Enhancement/linked documentation pages
  • Loading branch information
quentinovega authored Nov 2, 2023
2 parents a7922d9 + 133aeb9 commit d0f48ac
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 89 deletions.
108 changes: 67 additions & 41 deletions daikoku/app/controllers/ApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,29 @@ class ApiController(
s"@{user.name} has accessed documentation page for @{api.name} - @{api.id} - $pageId"
)
)(ctx) {

def verifyLink(api: Api, pageId: ApiDocumentationPageId): EitherT[Future, AppError, Boolean] = {
ctx.tenant.display match {
case TenantDisplay.Environment =>
for{
plans <- EitherT.liftF[Future, AppError, Seq[UsagePlan]](env.dataStore.usagePlanRepo.forTenant(ctx.tenant)
.find(Json.obj("_id" -> Json.obj("$in" -> JsArray(api.possibleUsagePlans.map(_.asJson))))))
pages <- EitherT.pure[Future, AppError](plans.flatMap(_.documentation.map(_.pages).getOrElse(Seq.empty)))
} yield pages.exists(_.id == pageId)
case TenantDisplay.Default =>
EitherT.liftF[Future, AppError, Boolean](env.dataStore.apiRepo.findAllVersions(ctx.tenant, api.id.value)
.map(apis => {
apis.filter(_.id != api.id).flatMap(api => api.documentation.docIds())
})
.map(_.exists(_ == pageId.value)))
}
}


(for {
api <- EitherT.fromOptionF[Future, AppError, Api](env.dataStore.apiRepo.forTenant(ctx.tenant.id).findByIdNotDeleted(apiId), AppError.ApiNotFound)
page <- EitherT.fromOptionF[Future, AppError, ApiDocumentationPage](env.dataStore.apiDocumentationPageRepo.forTenant(ctx.tenant.id).findByIdNotDeleted(pageId), AppError.PageNotFound)
// _ <- EitherT.fromOption[Future](api.documentation.docIds().find(s => s == page.id.value), AppError.PageNotFound)
isLinked <- verifyLink(api, page.id)
} yield {
ctx.setCtxValue("api.id", api.id.value)
ctx.setCtxValue("api.name", api.name)
Expand All @@ -525,10 +544,10 @@ class ApiController(
s"/api/apis/$apiId/pages/$pageId/content"
)
Ok(
page.asWebUiJson.as[JsObject] ++ Json.obj("contentUrl" -> url)
page.asWebUiJson.as[JsObject] ++ Json.obj("contentUrl" -> url) ++ Json.obj("linked" -> isLinked)
)
} else {
Ok(page.asWebUiJson)
Ok(page.asWebUiJson.as[JsObject] ++ Json.obj("linked" -> isLinked))
}
})
.leftMap(_.render())
Expand Down Expand Up @@ -2005,43 +2024,45 @@ class ApiController(
)(teamId, ctx) { _ =>
{
val pages = (ctx.request.body \ "pages").as[Seq[String]]
val linked = (ctx.request.body \ "linked").asOpt[Boolean]

(for {
fromPages <- env.dataStore.apiDocumentationPageRepo
documentationPages <- EitherT.liftF[Future, AppError, Seq[ApiDocumentationPage]](env.dataStore.apiDocumentationPageRepo
.forTenant(ctx.tenant.id)
.find(
Json.obj(
"_id" -> Json.obj(
"$in" -> pages
)
)
)
createdPages <- Future.sequence(fromPages.map(page => {
val generatedId = ApiDocumentationPageId(BSONObjectID.generate().stringify)
env.dataStore.apiDocumentationPageRepo
.forTenant(ctx.tenant.id)
.save(page.copy(id = generatedId))
.flatMap(_ =>
FastFuture.successful(
ApiDocumentationDetailPage(generatedId, page.title, Seq.empty)
))
}))
api <- env.dataStore.apiRepo.findByVersion(ctx.tenant,
apiId,
version)
} yield {
api match {
case None => FastFuture.successful(AppError.render(ApiNotFound))
case Some(api) =>
env.dataStore.apiRepo
.forTenant(ctx.tenant.id)
.save(
api.copy(documentation = api.documentation
.copy(pages = api.documentation.pages ++ createdPages))
)
.map(_ => Ok(Json.obj("done" -> true)))
}
}).flatten
))
createdPages <- EitherT.liftF[Future, AppError, Seq[ApiDocumentationDetailPage]](
linked match {
case Some(true) => FastFuture.successful(documentationPages.map(page => ApiDocumentationDetailPage(page.id, page.title, Seq.empty)))
case _ => Future.sequence(documentationPages.map(page => {
val generatedId = ApiDocumentationPageId(BSONObjectID.generate().stringify)
env.dataStore.apiDocumentationPageRepo
.forTenant(ctx.tenant.id)
.save(page.copy(id = generatedId))
.flatMap(_ =>
FastFuture.successful(
ApiDocumentationDetailPage(generatedId, page.title, Seq.empty)
))
}))
})
api <- EitherT.fromOptionF[Future, AppError, Api](env.dataStore.apiRepo.findByVersion(ctx.tenant,
apiId,
version), AppError.ApiNotFound)
done <- EitherT.liftF[Future, AppError, Boolean](env.dataStore.apiRepo
.forTenant(ctx.tenant.id)
.save(
api.copy(documentation = api.documentation
.copy(pages = api.documentation.pages ++ createdPages))
))

} yield Ok(Json.obj("done" -> done)))
.leftMap(_.render())
.merge
}
}
}
Expand All @@ -2054,6 +2075,7 @@ class ApiController(
)(teamId, ctx) { _ =>
{
val pages = (ctx.request.body \ "pages").as[Seq[String]]
val linked = (ctx.request.body \ "linked").asOpt[Boolean]

(for {
fromPages <- EitherT.liftF[Future, AppError, Seq[ApiDocumentationPage]](env.dataStore.apiDocumentationPageRepo
Expand All @@ -2065,16 +2087,20 @@ class ApiController(
)
)
))
createdPages <- EitherT.liftF[Future, AppError, Seq[ApiDocumentationDetailPage]](Future.sequence(fromPages.map(page => {
val generatedId = ApiDocumentationPageId(BSONObjectID.generate().stringify)
env.dataStore.apiDocumentationPageRepo
.forTenant(ctx.tenant.id)
.save(page.copy(id = generatedId))
.flatMap(_ =>
FastFuture.successful(
ApiDocumentationDetailPage(generatedId, page.title, Seq.empty)
))
})))
createdPages <- EitherT.liftF[Future, AppError, Seq[ApiDocumentationDetailPage]](
linked match {
case Some(true) => FastFuture.successful(fromPages.map(page => ApiDocumentationDetailPage(page.id, page.title, Seq.empty)))
case _ => Future.sequence(fromPages.map(page => {
val generatedId = ApiDocumentationPageId(BSONObjectID.generate().stringify)
env.dataStore.apiDocumentationPageRepo
.forTenant(ctx.tenant.id)
.save(page.copy(id = generatedId))
.flatMap(_ =>
FastFuture.successful(
ApiDocumentationDetailPage(generatedId, page.title, Seq.empty)
))
}))
})
plan <- EitherT.fromOptionF(env.dataStore.usagePlanRepo.forTenant(ctx.tenant).findByIdNotDeleted(planId), AppError.PlanNotFound)
_ <- EitherT.liftF[Future, AppError, Boolean](env.dataStore.usagePlanRepo
.forTenant(ctx.tenant.id)
Expand Down
Loading

0 comments on commit d0f48ac

Please sign in to comment.