Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate date fields from string to Firestore Timestamp #87

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ node_modules/

# macOS system file
.DS_store

# IDE personal folder
.idea
2 changes: 1 addition & 1 deletion POSTINSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ getDoc(doc(db, "${param:REVENUECAT_CUSTOMERS_COLLECTION}", getAuth().currentUser
.then((snapshot) => {
if (snapshot.exists()) {
snapshot.subscriptions
.filter(subscription => new Date(subscription.expires_date) >= new Date())
.filter(subscription => subscription.expires_date.toDate() >= new Date())
.forEach(subscription => console.log(JSON.stringify(subscription)));
}
});
Expand Down
27 changes: 25 additions & 2 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ const getCustomersCollection = ({
);
};

const isIso8601Date = (str: string): boolean => moment(str, moment.ISO_8601, true).isValid();


const convertDatesToTimestamps = (obj: any): any => {
if (typeof obj === 'string' && isIso8601Date(obj)) {
return admin.firestore.Timestamp.fromDate(moment(obj).toDate());
}

if (Array.isArray(obj)) {
return obj.map(convertDatesToTimestamps);
}

if (obj !== null && typeof obj === 'object') {
return Object.entries(obj).reduce((acc, [key, value]) => {
acc[key] = convertDatesToTimestamps(value);
return acc;
}, {} as any);
}

return obj;
};

const writeToCollection = async ({
firestore,
customersCollectionConfig,
Expand All @@ -63,10 +85,11 @@ const writeToCollection = async ({
userId,
});

const payloadToWrite = {
// Recursively convert all date strings in the customerPayload to Timestamps
const payloadToWrite = convertDatesToTimestamps({
...customerPayload,
aliases,
};
});

await customersCollection.doc(userId).set(payloadToWrite, { merge: true });
await customersCollection.doc(userId).update(payloadToWrite);
Expand Down
8 changes: 5 additions & 3 deletions functions/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {firestore} from "firebase-admin";

type EventType =
| "INITIAL_PURCHASE"
| "RENEWAL"
Expand All @@ -12,10 +14,10 @@ type EventType =
| "EXPIRATION";

interface Entitlement {
expires_date: string;
purchase_date: string;
expires_date: firestore.Timestamp;
purchase_date: firestore.Timestamp;
product_identifier: string;
grace_period_expires_date: string;
grace_period_expires_date: firestore.Timestamp;
}

export interface CustomerInfo {
Expand Down