Normalize recipient name

This commit is contained in:
Mikołaj Pich 2019-01-14 19:35:31 +01:00
parent 1bce1e6d42
commit d2921c7286
No known key found for this signature in database
GPG key ID: F62B26E36D4C4BAA
4 changed files with 44 additions and 16 deletions

View file

@ -31,9 +31,12 @@ class MessagesRepository(private val api: MessagesService) {
return getReportingUnits().map { it.first() }.flatMap { unit ->
// invalid unit id produced error
if (unit.id == 0) return@flatMap Single.just(emptyList<Recipient>())
api.getRecipients(unit.id, role).map {
recipients = if (null !== it.data) it.data else listOf()
it.data
api.getRecipients(unit.id, role).map { it.data }.map { list ->
list.ifEmpty { listOf() }.map {
it.copy(name = it.name.normalizeRecipient())
}.apply {
recipients = this
}
}
}
}
@ -53,11 +56,11 @@ class MessagesRepository(private val api: MessagesService) {
.flatMapObservable { Observable.fromIterable(it) }
.flatMap { message ->
getRecipients().flatMapObservable {
Observable.fromIterable(it.filter { recipient -> recipient.name == message.recipient }.ifEmpty {
listOf(Recipient("0", message.recipient ?: "unknown", 0, 0, 2, "unknown"))
Observable.fromIterable(it.filter { recipient -> recipient.name == message.recipient?.normalizeRecipient() }.ifEmpty {
listOf(Recipient("0", message.recipient?.normalizeRecipient() ?: "unknown", 0, 0, 2, "unknown"))
})
}.map {
message.copy(recipient = it.name.substringBeforeLast(" -"), messageId = message.id).apply {
message.copy(recipient = it.name, messageId = message.id).apply {
recipientId = it.loginId
}
}
@ -84,6 +87,10 @@ class MessagesRepository(private val api: MessagesService) {
)).map { it.data }
}
private fun String.normalizeRecipient(): String {
return this.substringBeforeLast(" -").substringBefore(" [")
}
private fun getDate(date: LocalDateTime?): String {
if (date == null) return ""
return date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))

View file

@ -37,7 +37,7 @@ class MessagesTest : BaseLocalTest() {
server.enqueue(MockResponse().setBody(MessagesTest::class.java.getResource("Adresaci.json").readText()))
server.start(3000)
assertEquals(2, api.getSentMessages(null, null).blockingGet().size)
assertEquals(3, api.getSentMessages(null, null).blockingGet().size)
}
@Test
@ -47,7 +47,7 @@ class MessagesTest : BaseLocalTest() {
// server.enqueue(MockResponse().setBody(MessagesTest::class.java.getResource("Adresaci.json").readText()))
server.start(3000)
assertEquals(2, api.getSentMessages(null, null).blockingGet().size)
assertEquals(3, api.getSentMessages(null, null).blockingGet().size)
}
@Test
@ -59,10 +59,23 @@ class MessagesTest : BaseLocalTest() {
val recipients = api.getSentMessages(null, null).blockingGet()
assertEquals("Czerwieńska - Kowalska Joanna [CJ]", recipients[1].recipient)
assertEquals("Czerwieńska - Kowalska Joanna", recipients[1].recipient)
assertEquals(95, recipients[1].recipientId)
}
@Test
fun getMessagesSent_recipientWithoutBracket() {
server.enqueue(MockResponse().setBody(MessagesTest::class.java.getResource("WiadomosciWyslane.json").readText()))
server.enqueue(MockResponse().setBody(MessagesTest::class.java.getResource("JednostkiUzytkownika.json").readText()))
server.enqueue(MockResponse().setBody(MessagesTest::class.java.getResource("Adresaci.json").readText()))
server.start(3000)
val recipients = api.getSentMessages(null, null).blockingGet()
assertEquals("Czerwieńska - Kowalska Joanna", recipients[1].recipient)
assertEquals(95, recipients[2].recipientId)
}
@Test
fun getMessagesSent_recipientWithDashInNameAndEmptyUnits() {
server.enqueue(MockResponse().setBody(MessagesTest::class.java.getResource("WiadomosciWyslane.json").readText()))
@ -72,7 +85,7 @@ class MessagesTest : BaseLocalTest() {
val recipients = api.getSentMessages(null, null).blockingGet()
assertEquals("Czerwieńska - Kowalska Joanna [CJ]", recipients[1].recipient)
assertEquals("Czerwieńska - Kowalska Joanna", recipients[1].recipient)
assertEquals(0, recipients[1].recipientId)
}

View file

@ -6,27 +6,27 @@
"Nazwa": "Tracz Janusz [TJ] - pracownik (Fake123456)",
"IdLogin": 18,
"IdJednostkaSprawozdawcza": 6,
"RolaEnum": null,
"Rola": 2,
"PushWiadomosc": null
"PushWiadomosc": null,
"Hash": "NTVhNTQwMDhhZDFiYTU4OWFhMjEwZDI2MjljMWRmNDE="
},
{
"Id": "94rPracownik",
"Nazwa": "Lech Wałęsa [Dd] - pracownik (Fake123456)",
"IdLogin": 94,
"IdJednostkaSprawozdawcza": 6,
"RolaEnum": null,
"Rola": 2,
"PushWiadomosc": null
"PushWiadomosc": null,
"Hash": "NTVhNTQwMDhhZDFiYTU4OWFhMjEwZDI2MjljMWRmNDE="
},
{
"Id": "95rPracownik",
"Nazwa": "Czerwieńska - Kowalska Joanna [CJ] - pracownik (Fake123456)",
"IdLogin": 95,
"IdJednostkaSprawozdawcza": 6,
"RolaEnum": null,
"Rola": 2,
"PushWiadomosc": null
"PushWiadomosc": null,
"Hash": "NTVhNTQwMDhhZDFiYTU4OWFhMjEwZDI2MjljMWRmNDE="
}
]
}

View file

@ -16,6 +16,14 @@
"Nieprzeczytane": "1",
"Przeczytane": "0",
"Id": 32799
},
{
"Data": "2018-06-11T09:38:35.000Z",
"Temat": "Wiadomość testowa",
"Adresaci": "Czerwieńska - Kowalska Joanna - pracownik (Fake123456)",
"Nieprzeczytane": "1",
"Przeczytane": "0",
"Id": 32800
}
]
}