From 8b23ff1684c7343a95bdc7c16351579a012ba15d Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Sun, 26 Jan 2020 01:36:43 -0500 Subject: [PATCH] cannot delete account with unpaid bills --- .../src/main/java/bubble/dao/account/AccountDAO.java | 9 ++++++++- bubble-server/src/main/java/bubble/dao/bill/BillDAO.java | 4 ++++ .../en_US/server/post_auth/ResourceMessages.properties | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bubble-server/src/main/java/bubble/dao/account/AccountDAO.java b/bubble-server/src/main/java/bubble/dao/account/AccountDAO.java index eef7f72a..2d75c819 100644 --- a/bubble-server/src/main/java/bubble/dao/account/AccountDAO.java +++ b/bubble-server/src/main/java/bubble/dao/account/AccountDAO.java @@ -3,6 +3,7 @@ package bubble.dao.account; import bubble.cloud.CloudServiceDriver; import bubble.dao.account.message.AccountMessageDAO; import bubble.dao.app.*; +import bubble.dao.bill.BillDAO; import bubble.dao.cloud.AnsibleRoleDAO; import bubble.dao.cloud.BubbleDomainDAO; import bubble.dao.cloud.BubbleFootprintDAO; @@ -10,6 +11,7 @@ import bubble.dao.cloud.CloudServiceDAO; import bubble.dao.device.DeviceDAO; import bubble.model.account.*; import bubble.model.app.*; +import bubble.model.bill.Bill; import bubble.model.cloud.*; import bubble.server.BubbleConfiguration; import bubble.service.boot.SelfNodeService; @@ -56,6 +58,7 @@ public class AccountDAO extends AbstractCRUDDAO implements SqlViewSearc @Autowired private AccountMessageDAO messageDAO; @Autowired private DeviceDAO deviceDAO; @Autowired private SelfNodeService selfNodeService; + @Autowired private BillDAO billDAO; public Account newAccount(Request req, AccountRegistration request, Account parent) { return create(new Account(request) @@ -238,7 +241,11 @@ public class AccountDAO extends AbstractCRUDDAO implements SqlViewSearc throw invalidEx("err.delete.invalid", "cannot delete account ("+account.getUuid()+") that owns current network ("+configuration.getThisNetwork().getUuid()+")", account.getUuid()); } - // todo: cannot delete an account that has unpaid bills + // cannot delete account with unpaid bills + final List unpaid = billDAO.findUnpaidByAccount(uuid); + if (!unpaid.isEmpty()) { + throw invalidEx("err.delete.unpaidBills", "cannot delete account ("+account.getUuid()+") with "+unpaid.size()+" unpaid bills", account.getUuid()); + } final AccountPolicy policy = policyDAO.findSingleByAccount(uuid); diff --git a/bubble-server/src/main/java/bubble/dao/bill/BillDAO.java b/bubble-server/src/main/java/bubble/dao/bill/BillDAO.java index f2f21789..453b15e4 100644 --- a/bubble-server/src/main/java/bubble/dao/bill/BillDAO.java +++ b/bubble-server/src/main/java/bubble/dao/bill/BillDAO.java @@ -28,6 +28,10 @@ public class BillDAO extends AccountOwnedEntityDAO { return findByFields("accountPlan", accountPlanUuid, "status", BillStatus.unpaid); } + public List findUnpaidByAccount(String accountUuid) { + return findByFields("account", accountUuid, "status", BillStatus.unpaid); + } + public Bill createFirstBill(BubblePlan plan, AccountPlan accountPlan) { return create(newBill(plan, accountPlan, accountPlan.getCtime())); } diff --git a/bubble-server/src/main/resources/message_templates/en_US/server/post_auth/ResourceMessages.properties b/bubble-server/src/main/resources/message_templates/en_US/server/post_auth/ResourceMessages.properties index f0cdebce..130c323e 100644 --- a/bubble-server/src/main/resources/message_templates/en_US/server/post_auth/ResourceMessages.properties +++ b/bubble-server/src/main/resources/message_templates/en_US/server/post_auth/ResourceMessages.properties @@ -474,6 +474,7 @@ err.delegatedPayment.notDelegated=Error locating payment service err.delegatedPayment.delegateNotFound=Error locating payment service err.delete.cannotDeleteSelf=You cannot delete yourself (you've still got a chance to win) err.delete.invalid=You cannot delete the account that owns the current network +err.delete.unpaidBills=Cannot delete account with unpaid bills err.deletionPolicy.required=Deletion policy is required err.description.length=Description is too long err.deviceDir.notFound=Device status directory not found