I want to access a subcsription, to see if the status is active.
I have this:
const subscription = await stripe.subscriptions.retrieve({
email: 'contact#Inderatech.com',
});
res.send(subscription)
however, it doesn't let me do it like this. On the stripe docs, it says to do it like this:
const subscription = await stripe.subscriptions.retrieve(
'sub_icsb2'
);
but i won't have the subscription id unless i grab it using the customer email. So is this possible?
The API only supports retrieving an object by their id. So when you want to retrieve a Subscription you need its identifier, the sub_123.
It is not possible to retrieve a Subscription by another property today and it applies to all APIs. There are List APIs that you can use to filter specific objects. For example when you list subscriptions you can filter them for a specific Price via the price parameter.
Today, the subscription doesn't have an "email" property so there isn't a way to list subscriptions for a specific email. Usually what you want is to first find the customer(s) that have a specific email address via the email parameter and then you can list subscriptions for that specific customer via the customer parameter.
Related
I'm hoping to use Postman to generate and insert test data for an API project I'm working on. The resources in the API have foreign key constraints, so I want to be able to generate static guids for the resources, and share those IDs among all the other requests so that we can maintain the references and have valid foreign keys.
At the moment I'm generating data that I want to keep accessible to all the requests in the collection in a pre-request script at the collection level:
// set the user ID for our test user
var userId = pm.variables.replaceIn("{{$guid}}");
pm.collectionVariables.set("userId", userId);
// then generate a product ID
var productId = pm.variables.replaceIn("{{$guid}}");
pm.collectionVariables.set("productId", productId);
// create a few vendors
var vendorId1 = pm.variables.replaceIn("{{$guid}}");
pm.collectionVariables.set("vendorId1", vendorId1);
var vendorId2 = pm.variables.replaceIn("{{$guid}}");
pm.collectionVariables.set("vendorId1", vendorId1);
// etc
My thought was that I'd use the userId in all subsequent POSTs so there'd be a real user to associate a new object with, then use the vendor IDs with the product inserts, etc.
However, the pre-request script runs before each request-- I think it's re-creating all these values rather than keeping them static, so my plan to maintain integrity isn't working.
Is there a better strategy? For example, can I capture the ID of the inserted object from the request response and pass that variable on to the next request in the workflow so that it can use that ID for the associated resources?
You could test if the userId is already set, and only if it isn't set your variables, something like:
if (!pm.collectionVariables.get("userId")) {
// set the user ID for our test user
var userId = pm.variables.replaceIn("{{$guid}}");
pm.collectionVariables.set("userId", userId);
// set your other variables...
}
I have a small realtime firebase database that's set up like this:
database
-messages
--XXXXXXXXXXXX
---id : "XXX-XXX"
---content : "Hello world!"
It's a very simple message system, the id field is basically a combination of users id from my mysql database. I'm trying to return all messages that match one of the ids, either sender or receiver. But I can't do it, seems like firebase only support exacts querys. Could you give me some guidanse?
Here's the code I'm working with
firebase.database().ref("messages").orderByChild("id").equalTo(userId).on("value", function(snapshot)
I'm looking for something like ".contains(userId)"
Firebase supports exact matches (with equalTo) and so-called prefix queries where the value starts with a certain value (by combining startAt and endAt). It does not support querying for values that end with a certain value.
I recommend keeping a mapping from each user IDs to their messages nodes, somewhere separately in their database.
So say that you have:
messages: {
"XXXXXXXXXXXX": {
id : "YYY-ZZZ",
content : "Hello world!"
}
}
You also have the following mappings:
userMessages: {
"YYY": {
"XXXXXXXXXXXX": true
},
"ZZZ": {
"XXXXXXXXXXXX": true
}
}
Now with this information you can look up the messages for each user based on their ID.
For more on the modeling of this type of data, I recommend:
Best way to manage Chat channels in Firebase
Many to Many relationship in Firebase
this artcle on NoSQL data modeling
This is the thing I want to accomplish: I'm building a web shop. The web shop has a React Front-end. The front-end fetches 5 collections from Firestore and displays all the items from the collection array on the shop page. A user selects an item on the shop page. I send the item fields such as (price, name, quantity, id) to my express server and the server makes a checkout session of the item fields. The user goes to a Stripe checkout form and is sent back to my front-end by Stripe when the payment is complete. I listen for that event on my server and when then want to update the quantity field of the item in Firestore.
But how do I query Firestore for this item? Is there a way to query Firestore with only this id field (or name field)? Some something like:
db
.collection('collections')
.where('id', '===', 1)
Or do I need to save the document id (of the collection) as a field inside the item map and also send that to Stripe? Or is there a better way to do this? I can't find anything online about this.
Here is a screenshot of Firestore.
Please forgive my beginner question. I'm still learning React, Firestore and Node.js.
First be sure you are sticking to the Firestore terminology correctly. There are collections and there are documents.
Collections you access via a path such as:
collRef = db.collection("products")
collRef = db.collection("products").where("quanity_on_hand", ">", "0")
collRef = db.collection("products").doc("12345").collection("purchase_history")
The latter instance can also be accessed via collRef = db.collection("products/12345/purchase_history").
In all the above cases you will get back a CollectionReference.
Documents you access such as:
docRef = db.collection("products").doc("12345")
docRef = db.doc("products/12345")
This returns you a DocumentReference for the document whose ID is "12345" in the collection "products".
So for your code example above, you want to use docRef = db.doc("collections/1") to get back the DocumentReference for the item you are after. (Or, alternatively, you could use: docRef = db.collection("collections").doc("1")
If you stick with the code that you have above, you'd get back a CollectionReference then you'd need to fetch the data with .get(), then extract the resulting documents (that will just be a single document), then work with that. Oh...and you will need to put an "id" field into all of your documents because the document's ID value (the "name" of the document) is not part of the document by default so if you want to use .where("id", "==", "1"), then you need to add an "id" field to your document and populate it correctly.
If you go with docRef = db.doc("collections/1"), you are querying for the document directly and will get back a reference to just that one. No need for extra fields, nor extracting a single document from a result set.
I am trying to make the friend request functionality work, I am through with the sending and receiving requests part but I am stuck as to how to update the friends data collection for a specific user as the user accepts a friend request.
I want this to happen :
Friends (collection)
userID (document)
list of friends (their uid's)
.
.
.
.
And the list of friends gets appended with a new uid if the user accepts a new request.
Here is a screenshot of my db:
You need to first focus on how you are going to structure your data. If you want to keep 'Friends' collection to store the information about the friendship of a user, you need to first figure out what kind of queries you will be issuing on that collection.
Primarily you will need:
1. Is user A friends with User B and vice versa.
2. List of all users A is friends with.
Your current structure solves both the cases, but try to see if you can improve/optimize it for other use cases. I would leave that to you but would leave you with a hint. You are modeling a "has many" relation through a table, an efficient way would be to store a "friendship" information (user1, user2)
Answer to the original question starts here
You need to add a new key/value pair in the Friends collection for a given key.
let newRelation = {};
newRelation[friendUID] = {date: new Date().getTime()} // store whatever info you need to store here.
firestore.collection("friends").doc(uid).update(newRelation) // Udpdate the user document to add friendship
You also need to update the "friendship" for the friend, just reverse the keys for that.
I have been taking a Node.js course on Udemy and would like to apply some of the knowledge I have gained to create a simple web application. I would like to have a user register, which leads him to an admin panel, this part I already have.
This user (requester) can then refer users (invitees) to this website using a unique link. For example he would click a button to generate a unique hyperlink (my idea for this link was to be the http://websiteurl/userid of the requester who is sending the link).
The requester can then send this link through email to their friends to invite them to the website, once they click the link they are taken to a sign up form and when they fill in the form they are linked (added to an array under the original user).
How would I go about setting up this "session", as in make sure that the form that the invitees fill out is linked to the original requester? How can those forms be generated dynamically on the requester's hyperlink?
I'm not looking for the exact code to do this, but rather validation if my idea for the url is a good approach or if there are other approaches I should consider.
Thanks in advance!
Well, this would require you changing the schema for your database. A user will need to have a property like:
{
referred: []
}
The referred array should contain ids or some sort of reference to a user's referred users.
On the "/:userid" route, the form should submit to the route where a new user is created and have a parameter with the user ID. In this case, I am using query parameters.
So if a person were to visit /foo, they would get a form that would post to a URL like /new?userid=foo.
In that route, you can do something like this (assuming Express):
app.post("/new", (req, res) => {
const userID = req.query.userid;
const newUser = // create user normally
if(userID) {
// `userID` referred this user
const referrer = getUser(userID);
referrer.referred.push(newUser);
save(referrer);
}
});
The getUser function should returning the current user, and you should modify the referred property with the new user. This code was merely an outline and you should update the user in your database.
In a nutshell, a form should post to /new?userid=foo, and when creating a new user, if this parameter is present, you can update the database entry for the user id in the parameter with the id of the new user.