From f15fd70e2e0516c665e5196cba0dd5f1ad8f11f0 Mon Sep 17 00:00:00 2001 From: eikek Date: Wed, 15 Jun 2022 00:10:24 +0200 Subject: [PATCH] Set the additional info as item name --- README.md | 6 +++++ .../scala/docspell/swissqr/addon/Config.scala | 13 +++++---- .../scala/docspell/swissqr/addon/Main.scala | 7 ++++- .../scala/docspell/swissqr/addon/Output.scala | 5 ++++ .../swissqr/addon/ConfigParseTest.scala | 27 +++++++++++++++++++ 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3f7c032..59726fd 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ add-tags: check-tags: - Invoice + +additional-as-name: true ``` The `field-currency` is a map that defines the name of a custom field @@ -58,6 +60,10 @@ of the defined tags are already present on an item. This can be useful if the tag classification works well and so you only apply this addon to documents classified as "Invoice", for example. +The flag `additional-as-name` controls whether to set the item name +from the _additional information_ provided in the qr code. Quite often +this line is some sense of title of that invoice. + Each section is optional. You need to define `field-currency` and `add-tags` if you want to add additional tags or set the amount to a custom field. diff --git a/modules/addon/src/main/scala/docspell/swissqr/addon/Config.scala b/modules/addon/src/main/scala/docspell/swissqr/addon/Config.scala index 19aaa1d..427a2c4 100644 --- a/modules/addon/src/main/scala/docspell/swissqr/addon/Config.scala +++ b/modules/addon/src/main/scala/docspell/swissqr/addon/Config.scala @@ -9,7 +9,8 @@ import io.circe.syntax.* case class Config( fieldCurrency: Option[Map[String, String]], addTags: Option[List[String]], - checkTags: Option[List[String]] + checkTags: Option[List[String]], + additionalAsName: Option[Boolean] ): def getFieldName(currencyCode: String): Option[String] = fieldCurrency.flatMap(m => m.get(currencyCode).orElse(m.get("*"))) @@ -34,12 +35,14 @@ object Config: .rethrow val default: Config = - Config(None, None, None) + Config(None, None, None, None) given jsonDecoder: Decoder[Config] = - Decoder.forProduct3("field-currency", "add-tags", "check-tags")(Config.apply) + Decoder.forProduct4("field-currency", "add-tags", "check-tags", "additional-as-name")( + Config.apply + ) given jsonEncoder: Encoder[Config] = - Encoder.forProduct3("field-currency", "add-tags", "check-tags")(cfg => - (cfg.fieldCurrency, cfg.addTags, cfg.checkTags) + Encoder.forProduct4("field-currency", "add-tags", "check-tags", "additional-as-name")( + cfg => (cfg.fieldCurrency, cfg.addTags, cfg.checkTags, cfg.additionalAsName) ) diff --git a/modules/addon/src/main/scala/docspell/swissqr/addon/Main.scala b/modules/addon/src/main/scala/docspell/swissqr/addon/Main.scala index aaa52cc..03898ee 100644 --- a/modules/addon/src/main/scala/docspell/swissqr/addon/Main.scala +++ b/modules/addon/src/main/scala/docspell/swissqr/addon/Main.scala @@ -89,7 +89,12 @@ object Main extends IOApp: val addNotes: Output.Action = Output.AddNotes(qrs.map(_.show).toList.mkString("\n\n"), "----") - Output(itemMetadata.id, addNotes :: setFields ::: addTags) + val setName: List[Output.Action] = + if (cfg.additionalAsName.exists(identity)) + qrs.head.additional.headOption.toList.map(name => Output.SetName(name)) + else Nil + + Output(itemMetadata.id, addNotes :: setFields ::: addTags ::: setName) /** Print to stderr. */ def errln(msg: String): IO[Unit] = diff --git a/modules/addon/src/main/scala/docspell/swissqr/addon/Output.scala b/modules/addon/src/main/scala/docspell/swissqr/addon/Output.scala index 56e2566..61714c9 100644 --- a/modules/addon/src/main/scala/docspell/swissqr/addon/Output.scala +++ b/modules/addon/src/main/scala/docspell/swissqr/addon/Output.scala @@ -25,6 +25,10 @@ object Output: object AddNotes: given jsonEncoder: Encoder[AddNotes] = deriveEncoder + case class SetName(name: String, action: String = "set-name") extends Action + object SetName: + given jsonEncoder: Encoder[SetName] = deriveEncoder + case class AddTags(tags: List[String], action: String = "add-tags") extends Action object AddTags: given jsonEncoder: Encoder[AddTags] = deriveEncoder @@ -35,6 +39,7 @@ object Output: case c: SetField => c.asJson case c: AddNotes => c.asJson case c: AddTags => c.asJson + case c: SetName => c.asJson } case class Command( diff --git a/modules/addon/src/test/scala/docspell/swissqr/addon/ConfigParseTest.scala b/modules/addon/src/test/scala/docspell/swissqr/addon/ConfigParseTest.scala index 887dc4b..54bbac5 100644 --- a/modules/addon/src/test/scala/docspell/swissqr/addon/ConfigParseTest.scala +++ b/modules/addon/src/test/scala/docspell/swissqr/addon/ConfigParseTest.scala @@ -22,8 +22,35 @@ class ConfigParseTest extends CatsEffectSuite: Config( Some(Map("CHF" -> "chf", "EUR" -> "eur")), Some(List("Invoice", "QR-Code")), + None, None ) ) } yield () } + + test("parse with missing pieces 2") { + val yamlString = + """field-currency: + | CHF: chf + | EUR: eur + | + |additional-as-name: true + | + |add-tags: + | - Invoice + | - QR-Code""".stripMargin + + for { + cfg <- Config.fromYaml[IO](yamlString) + _ = assertEquals( + cfg, + Config( + Some(Map("CHF" -> "chf", "EUR" -> "eur")), + Some(List("Invoice", "QR-Code")), + None, + Some(true) + ) + ) + } yield () + }