RevenueCat / firestore-revenuecat-purchases

Apache License 2.0
24 stars 7 forks source link

Store dates in Firestore using Timestamp type instead of String #86

Open EArminjon opened 1 month ago

EArminjon commented 1 month ago

Observation : Actually RevenueCat store dates as ISO 8601 "string" inside Firestore. This is an issue.

Feature request: To let us availability to create some Firestores rule based on dates, dates should be of type "Timestamp". Timestamp object can be easily converted to Date() on any Frontend/Backend. So this is not an issue.

Example of current limitation: Bellow an exemple explaining why we need that :

service cloud.firestore {
  match /databases/{database}/documents {

    function hasPaidSubs() {
      let query = get(/databases/$(database)/documents/users/$(request.auth.uid)/purchaser-info/$(request.auth.uid));
      let subscription = query.data;

      return subscription.entitlements.pro != null && subscription.entitlements.pro.expires_date > request.time;
    }

     match /cars/{carID} {
      allow read: if true;
      allow create: if request.auth != null && request.auth.uid == request.resource.data.creator && hasPaidSubs()
      allow update: if request.auth != null && request.auth.uid == resource.data.creator
      allow delete: if request.auth != null && request.auth.uid == resource.data.creator;  
    }
  }   
} 

In this example, "expires_date" is of type "string" and equal "2024-08-26T07:43:35Z", the comparaison generated the following exception because it can't be compare with request.time which is of type Timestamp :

Unsupported operation error. Received: string > timestamp. Expected: string > string, bytes > bytes, int > int, float > float, duration > duration, timestamp > timestamp, constraint > any, any > constraint. for 'create' @ L32, cause=null}

If you agree with this suggestion, it will generate a Breaking Change.

Alternative:

Inside Firebase Firestore, the Monitoring Rule test space allow the comparaison between String and Timestamp. But this is a bug and that didn't work on PROD and on Firebase cli Emulator.

I started a conversation with the Firebase support to check this issue and to know if it's possible to allow String to Timestamp conversion or comparaison inside Firestore Rules. If they allow that, i will close this issue.

Maybe we can also upvote this Firebase suggestion : https://firebase.uservoice.com/forums/948424-general/suggestions/48895802-allow-string-to-timestamp-conversion-in-firestore

EArminjon commented 1 month ago

Hello @vicfergar, can you check this ? This is really blocker for our business :/ Would like to know if it will be considered or should we fork it and use our own extension.