Skip to content

Commit

Permalink
Merge pull request #3 from docspell/set-name
Browse files Browse the repository at this point in the history
Set the additional info as item name
  • Loading branch information
eikek authored Jun 14, 2022
2 parents 9db1139 + f15fd70 commit e421327
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
13 changes: 8 additions & 5 deletions modules/addon/src/main/scala/docspell/swissqr/addon/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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("*")))
Expand All @@ -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)
)
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
}

0 comments on commit e421327

Please sign in to comment.