From babaa5b32e0c544aad0998eddd4677080707fc62 Mon Sep 17 00:00:00 2001 From: Vihang Patil Date: Sat, 30 Nov 2019 14:38:45 +0100 Subject: [PATCH] Updated prime-admin with Arrow Fx --- .../prime/admin/actions/CustomerActions.kt | 53 ++++++++----------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/tools/prime-admin/src/main/kotlin/org/ostelco/tools/prime/admin/actions/CustomerActions.kt b/tools/prime-admin/src/main/kotlin/org/ostelco/tools/prime/admin/actions/CustomerActions.kt index 1c8635d3a..606e99243 100644 --- a/tools/prime-admin/src/main/kotlin/org/ostelco/tools/prime/admin/actions/CustomerActions.kt +++ b/tools/prime-admin/src/main/kotlin/org/ostelco/tools/prime/admin/actions/CustomerActions.kt @@ -1,12 +1,10 @@ package org.ostelco.tools.prime.admin.actions import arrow.core.Either -import arrow.core.fix +import arrow.core.extensions.fx import arrow.core.flatMap import arrow.core.left import arrow.core.right -import arrow.effects.IO -import arrow.instances.either.monad.monad import org.apache.commons.codec.digest.DigestUtils import org.ostelco.prime.dsl.withId import org.ostelco.prime.dsl.writeTransaction @@ -33,32 +31,27 @@ fun createCustomer(email: String, nickname: String): Either = analyticsId = UUID.randomUUID().toString(), referralId = UUID.randomUUID().toString())) -fun approveRegionForCustomer(email: String, regionCode: String): Either = IO { - Either.monad().binding { +fun approveRegionForCustomer(email: String, regionCode: String): Either = Either.fx { + val customerId = adminStore + .getCustomer(identity = emailAsIdentity(email = email)) + .bind() + .id + + adminStore.approveRegionForCustomer( + customerId = customerId, + regionCode = regionCode + ).bind() +} + +fun addCustomerToSegment(email: String, segmentId: String): Either = writeTransaction { + Either.fx { val customerId = adminStore .getCustomer(identity = emailAsIdentity(email = email)) .bind() .id - - adminStore.approveRegionForCustomer( - customerId = customerId, - regionCode = regionCode - ).bind() - - }.fix() -}.unsafeRunSync() - -fun addCustomerToSegment(email: String, segmentId: String): Either = writeTransaction { - IO { - Either.monad().binding { - val customerId = adminStore - .getCustomer(identity = emailAsIdentity(email = email)) - .bind() - .id - fact { (Customer withId customerId) belongsToSegment (org.ostelco.prime.storage.graph.model.Segment withId segmentId) }.bind() - }.fix() - }.unsafeRunSync() + fact { (Customer withId customerId) belongsToSegment (org.ostelco.prime.storage.graph.model.Segment withId segmentId) }.bind() + } } fun deleteCustomer(email: String) = adminStore @@ -129,8 +122,8 @@ private fun emailAsIdentity(email: String) = Identity( provider = "email" ) -fun identifyCustomer(idDigests: Set) = IO { - Either.monad().binding { +fun identifyCustomer(idDigests: Set) { + Either.fx { adminStore // get all Identity values .getAllIdentities() @@ -147,9 +140,7 @@ fun identifyCustomer(idDigests: Set) = IO { { println("Found Customer ID: ${it.id} for digest: $idDigest") } ) } - }.fix() + }.mapLeft { + println(it.message) } - .unsafeRunSync() - .mapLeft { - println(it.message) - } \ No newline at end of file +} \ No newline at end of file