Repository: twentyhq/twenty
File: packages/twenty-server/src/engine/workspace-manager/workspace-cleaner/services/cleaner.workspace-service.ts (TypeScript)

Code before:
    try {
      const lastSubscription =
        await this.billingSubscriptionRepository.findOneOrFail({
          where: {
            workspaceId: workspace.id,
          },
          order: { updatedAt: 'DESC' },
        });

      if (
        lastSubscription.status !== SubscriptionStatus.Unpaid &&
        lastSubscription.status !== SubscriptionStatus.Canceled
      ) {
        throw new Error();
      }

      const daysSinceSubscriptionUnpaid = differenceInDays(
        new Date(),
        lastSubscription.currentPeriodStart,
      );

      return daysSinceSubscriptionUnpaid;
    } catch {
      throw new WorkspaceCleanerException(
        `No cancelled or unpaid billing subscription found for workspace ${workspace.id} ${workspace.displayName}`,
        WorkspaceCleanerExceptionCode.BILLING_SUBSCRIPTION_NOT_FOUND,
      );
    }
  }

  async checkIfAtLeastOneWorkspaceMemberWarned(
    workspaceMembers: WorkspaceMemberWorkspaceEntity[],
    workspaceId: string,
  ) {
    for (const workspaceMember of workspaceMembers) {
      const workspaceMemberWarned = await this.userVarsService.get({
        userId: workspaceMember.userId,
        workspaceId: workspaceId,
        key: USER_WORKSPACE_DELETION_WARNING_SENT_KEY,
      });

      if (workspaceMemberWarned) {
        return true;
      }
    }

    return false;
  }

  async sendWarningEmail(
    workspaceMember: WorkspaceMemberWorkspaceEntity,

Review comment (suggestion):
***Remark:*** we're swallowing this exception grain
```ts
      if (
        lastSubscription.status !== SubscriptionStatus.Unpaid &&
        lastSubscription.status !== SubscriptionStatus.Canceled
      ) {
        throw new Error();
      }
```

Code after:
    try {
      const lastSubscription =
        await this.billingSubscriptionRepository.findOneOrFail({
          where: {
            workspaceId: workspace.id,
          },
          order: { updatedAt: 'DESC' },
        });

      if (
        lastSubscription.status !== SubscriptionStatus.Unpaid &&
        lastSubscription.status !== SubscriptionStatus.Canceled
      ) {
        throw new Error(
          'No cancelled or unpaid billing subscription found for workspace',
        );
      }

      const daysSinceSubscriptionUnpaid = differenceInDays(
        new Date(),
        lastSubscription.currentPeriodStart,
      );

      return daysSinceSubscriptionUnpaid;
    } catch {
      throw new WorkspaceCleanerException(
        `No cancelled or unpaid billing subscription found for workspace ${workspace.id} ${workspace.displayName}`,
        WorkspaceCleanerExceptionCode.BILLING_SUBSCRIPTION_NOT_FOUND,
      );
    }
  }

  async checkIfAtLeastOneWorkspaceMemberWarned(
    workspaceMembers: WorkspaceMemberWorkspaceEntity[],
    workspaceId: string,
  ) {
    for (const workspaceMember of workspaceMembers) {
      const workspaceMemberWarned = await this.userVarsService.get({
        userId: workspaceMember.userId,
        workspaceId: workspaceId,
        key: USER_WORKSPACE_DELETION_WARNING_SENT_KEY,
      });

      if (workspaceMemberWarned) {
        return true;
      }
    }

    return false;
  }