async queryCurrentSubscriptions()

in ClassyTaxiServer/src/play-billing/UserManager.ts [37:86]


  async queryCurrentSubscriptions(userId: string, sku?: string, packageName?: string): Promise<Array<SubscriptionPurchase>> {
    const purchaseList = new Array<SubscriptionPurchase>();

    try {
      // Create query to fetch possibly active subscriptions from Firestore
      let query = this.purchasesDbRef
        .where('formOfPayment', '==', GOOGLE_PLAY_FORM_OF_PAYMENT)
        .where('skuType', '==', SkuType.SUBS)
        .where('userId', '==', userId)
        .where('isMutable', '==', true)

      if (sku) {
        query = query.where('sku', '==', sku);
      }

      if (packageName) {
        query = query.where('packageName', '==', packageName);
      }

      // Do fetch possibly active subscription from Firestore
      const queryResult = await query.get();

      // Loop through these subscriptions and filter those that are indeed active
      for (const purchaseRecordSnapshot of queryResult.docs) {
        let purchase: SubscriptionPurchase = SubscriptionPurchaseImpl.fromFirestoreObject(purchaseRecordSnapshot.data())

        if (!purchase.isEntitlementActive() && !purchase.isAccountHold() && !purchase.isPaused()) {
          // If a subscription purchase record in Firestore indicates says that it has expired,
          // and we haven't confirmed that it's in Account Hold,
          // and we know that its status could have been changed since we last fetch its details,
          // then we should query Play Developer API to get its latest status
          console.log('Updating cached purchase record for token = ', purchase.purchaseToken);
          purchase = await this.purchaseManager.querySubscriptionPurchase(purchase.packageName, purchase.sku, purchase.purchaseToken);
        }

        // Add the updated purchase to list to returned to clients
        if (purchase.isEntitlementActive() || purchase.isAccountHold() || purchase.isPaused()) {
          purchaseList.push(purchase);
        }
      }

      return purchaseList;

    } catch (err) {
      console.error('Error querying purchase records from Firestore. \n', err.message);
      const libraryError = new Error(err.message);
      libraryError.name = PurchaseQueryError.OTHER_ERROR;
      throw libraryError;
    }
  }