From 61eb1b5533fb727f7cbaeb602118ca32d094f878 Mon Sep 17 00:00:00 2001 From: kraktus Date: Wed, 17 Nov 2021 17:43:31 +0100 Subject: [PATCH] Fix patron gift when the receiver is already patron. Previously nothing was changed for the receiver. --- modules/plan/src/main/PlanApi.scala | 37 ++++++++++++++--------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/modules/plan/src/main/PlanApi.scala b/modules/plan/src/main/PlanApi.scala index 6ea1f74425..152a94aa69 100644 --- a/modules/plan/src/main/PlanApi.scala +++ b/modules/plan/src/main/PlanApi.scala @@ -282,25 +282,24 @@ final class PlanApi( .void >> setDbUserPlanOnCharge(user, levelUp = false) def gift(from: User, to: User, money: Money): Funit = - !to.isPatron ?? { - for { - isLifetime <- pricingApi isLifetime money - _ <- patronColl.update - .one( - $id(to.id), - $set( - "lastLevelUp" -> DateTime.now, - "lifetime" -> isLifetime, - "free" -> Patron.Free(DateTime.now, by = from.id.some), - "expiresAt" -> (!isLifetime option DateTime.now.plusMonths(1)) - ), - upsert = true - ) - newTo = to.mapPlan(_.incMonths) - _ <- setDbUserPlan(newTo) - } yield { - notifier.onGift(from, newTo, isLifetime) - } + for { + toPatronOpt <- userPatron(to) + isLifetime <- fuccess(~toPatronOpt.map(_.isLifetime)) >>| (pricingApi isLifetime money) + _ <- patronColl.update + .one( + $id(to.id), + $set( + "lastLevelUp" -> DateTime.now, + "lifetime" -> isLifetime, + "free" -> Patron.Free(DateTime.now, by = from.id.some), + "expiresAt" -> (!isLifetime option DateTime.now.plusMonths(1)) + ), + upsert = true + ) + newTo = to.mapPlan(p => if (~toPatronOpt.map(_.canLevelUp)) p.incMonths else p.enable) + _ <- setDbUserPlan(newTo) + } yield { + notifier.onGift(from, newTo, isLifetime) } def recentGiftFrom(from: User): Fu[Option[Patron]] =