From d086632560fbabf1828e4dc0eb8a59101f2fdf2e Mon Sep 17 00:00:00 2001 From: Benjamin Piouffle Date: Tue, 30 Apr 2024 09:42:52 +0200 Subject: [PATCH] enhancement(Collective): expire host application when withdrawing --- .../v2/mutation/HostApplicationMutations.ts | 2 +- server/models/Collective.ts | 30 ++++++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/server/graphql/v2/mutation/HostApplicationMutations.ts b/server/graphql/v2/mutation/HostApplicationMutations.ts index a29d5ed8842..bfeda300742 100644 --- a/server/graphql/v2/mutation/HostApplicationMutations.ts +++ b/server/graphql/v2/mutation/HostApplicationMutations.ts @@ -373,6 +373,7 @@ const rejectApplication = async (host, collective, req, reason: string) => { const { remoteUser } = req; // Reset host for collective & its children + await models.HostApplication.updatePendingApplications(host, collective, HostApplicationStatus.REJECTED); await collective.changeHost(null, remoteUser); // Notify collective admins @@ -395,7 +396,6 @@ const rejectApplication = async (host, collective, req, reason: string) => { // Purge cache and change the status of the application purgeCacheForCollective(collective.slug); - await models.HostApplication.updatePendingApplications(host, collective, HostApplicationStatus.REJECTED); return collective; }; diff --git a/server/models/Collective.ts b/server/models/Collective.ts index fe8d691396b..1b5f82aa310 100644 --- a/server/models/Collective.ts +++ b/server/models/Collective.ts @@ -2464,14 +2464,28 @@ class Collective extends Model< } // Deactivate/remote everything related to previous host - if (this.HostCollectiveId && this.approvedAt) { - // Pause or cancel all orders that cannot be transferred - const newOrderStatus = pauseContributions ? OrderStatuses.PAUSED : OrderStatuses.CANCELLED; - await Order.stopActiveSubscriptions(this.id, newOrderStatus, { messageForContributors, messageSource }); - - // Delete all virtual cards - const virtualCards = await VirtualCard.findAll({ where: { CollectiveId: this.id } }); - await Promise.all(virtualCards.map(virtualCard => virtualCard.delete())); + if (this.HostCollectiveId) { + if (this.approvedAt) { + // Pause or cancel all orders that cannot be transferred + const newOrderStatus = pauseContributions ? OrderStatuses.PAUSED : OrderStatuses.CANCELLED; + await Order.stopActiveSubscriptions(this.id, newOrderStatus, { messageForContributors, messageSource }); + + // Delete all virtual cards + const virtualCards = await VirtualCard.findAll({ where: { CollectiveId: this.id } }); + await Promise.all(virtualCards.map(virtualCard => virtualCard.delete())); + } else { + // Expire all pending host applications + await HostApplication.update( + { status: HostApplicationStatus.EXPIRED }, + { + where: { + HostCollectiveId: this.HostCollectiveId, + CollectiveId: this.id, + status: HostApplicationStatus.PENDING, + }, + }, + ); + } } // Prepare events and projects to receive a new host