Get all records that have an item in an array with certain email - azure

I am trying to write a CosmosDB query to return all records in a table that have an item in the Users array that has an email of a certain value. Here is roughly what a full record looks like.
{
"year": "2022",
"id": "d992b109-d94a-4d75-9ebb-9ef4e5e851ae",
"partitionKey": "2022",
"LeagueName": "Hipster Kitties",
"Users": [
{
"Id": "eb71e719-4bc1-4f32-9767-e5dc20b2e63f",
"UserName": "fake#gmail.com",
"NormalizedUserName": "fake#gmail.com",
"Email": "fake#gmail.com",
"NormalizedEmail": "fake#gmail.com",
"EmailConfirmed": false,
"PhoneNumber": null,
"PhoneNumberConfirmed": false,
"TwoFactorEnabled": false,
"LockoutEnd": null,
"LockoutEnabled": true,
"AccessFailedCount": 0
},
{
"Id": "eb71e719-4bc1-4f32-9767-e5dc20sdfsdf",
"UserName": "fake2#gmail.com",
"NormalizedUserName": "fake2#gmail.com",
"Email": "fake2#gmail.com",
"NormalizedEmail": "fake2#gmail.com",
"EmailConfirmed": false,
"PhoneNumber": null,
"PhoneNumberConfirmed": false,
"TwoFactorEnabled": false,
"LockoutEnd": null,
"LockoutEnabled": true,
"AccessFailedCount": 0
}
],
"LeagueCreator": {
"Id": "eb71e719-4bc1-4f32-9767-e5dc20b2e63f",
"UserName": "fake#gmail.com",
"NormalizedUserName": "fake#gmail.com",
"Email": "fake#gmail.com",
"NormalizedEmail": "fake#gmail.com",
"EmailConfirmed": false,
"PhoneNumber": null,
"PhoneNumberConfirmed": false,
"TwoFactorEnabled": false,
"LockoutEnd": null,
"LockoutEnabled": true,
"AccessFailedCount": 0
},
"LeagueAdmins": [
{
"Id": "eb71e719-4bc1-4f32-9767-e5dc20b2e63f",
"UserName": "fake#gmail.com",
"NormalizedUserName": "fake#gmail.com",
"Email": "fake#gmail.com",
"NormalizedEmail": "fake#gmail.com",
"EmailConfirmed": false,
"PhoneNumber": null,
"PhoneNumberConfirmed": false,
"TwoFactorEnabled": false,
"LockoutEnd": null,
"LockoutEnabled": true,
"AccessFailedCount": 0
}
],
"IsPublic": false,
"Seasons": [],
"Type": 1,
"Settings": {
"TotalPicks": 6,
"KeyPicks": 1,
"KeyPickBonus": 1,
"WeekStartingMoney": 0,
"MinimumGamesToPick": 0
},
"_attachments": "attachments/",
"_ts": 1665116599
}
Here is the query I have so far which will get me the id's, but I can't get the full records.
SELECT c.id
FROM c
JOIN t in c.Users
WHERE t.Email = "eric.triebe#gmail.com"
If I execute the following query, I get a SQL syntax error.
SELECT *
FROM c
WHERE c.id IN (SELECT d.id
FROM d
JOIN t in d.Users
WHERE t.Email = "fake#gmail.com")
Message: {"errors":[{"severity":"Error","location":{"start":33,"end":39},"code":"SC1001","message":"Syntax error, incorrect syntax near 'SELECT'."}]} ActivityId: , Microsoft.Azure.Documents.Common/2.14.0
Worst case, I could get back these Id's and then run another query which would get all records with those Id's, but want to see if I can run it in one query instead of two.

You can't just SELECT *. You can specify individual properties though. For example:
SELECT c.id, c.LeagueName, t.UserName, t.Email
FROM c
JOIN t in c.Users
WHERE t.Email = "fake#gmail.com"
This would return something like:
[
{
"id": "d992b109-d94a-4d75-9ebb-9ef4e5e851ae",
"LeagueName": "Hipster Kitties",
"UserName": "fake#gmail.com",
"Email": "fake#gmail.com"
}
]

Related

How to update a subscription (adding a pricing plan) by Stripe in Node.js?

I'm building a web app which sells several products, each product corresponds to a pricing plan. All the payment system is managed by Stripe.
It happens very often that a user has a subscription that contains Product A (i.e., one pricing plan), and then he wants to add Product B (i.e., another pricing plan) to the same subscription. I want to know how to achieve this by APIs of Stripe.
There is a webpage of Stripe to update a subscription (e.g., https://dashboard.stripe.com/test/subscriptions/sub_H4pQGW8nnc80vF/edit where sub_H4pQGW8nnc80vF is the subscription ID). Let's assume this customer already has 1 Verificator in the subscription, and then he wants to add 1 Pretty Formula to the same subscription. I do this update via the website. Here is the log:
Here is the full Request POST body:
{
"items": {
"0": {
"billing_thresholds": "",
"deleted": "false",
"id": "si_H4pQal4ZxGzLbW",
"quantity": "1",
"tax_rates": ""
},
"1": {
"billing_thresholds": "",
"plan": "plan_Gz6i9yPVIjrDPX",
"deleted": "false",
"quantity": "1"
}
},
"off_session": "true",
"prorate": "true",
"cancel_at": "",
"days_until_due": "30",
"default_tax_rates": "",
"collection_method": "send_invoice",
"billing_thresholds": "",
"enable_incomplete_payments": "false",
"invoice_settings": {
"description": "",
"send_hosted_payment_email": "true",
"supported_payment_methods": {
"ach_credit_transfer": "false",
"au_becs_debit": "false",
"bancontact": "false",
"card": "true",
"fpx": "false",
"giropay": "false",
"ideal": "false",
"jp_credit_transfer": "false",
"paper_check": "false",
"sepa_credit_transfer": "false",
"sofort": "false"
},
"custom_fields": "",
"footer": ""
},
"default_payment_method": "",
"default_source": ""
}
Here is the full Response body:
{
"id": "sub_H4pQGW8nnc80vF",
"object": "subscription",
"application_fee_percent": null,
"billing_cycle_anchor": 1586597833,
"billing_thresholds": null,
"cancel_at": null,
"cancel_at_period_end": false,
"canceled_at": null,
"collection_method": "send_invoice",
"created": 1586597833,
"current_period_end": 1589189833,
"current_period_start": 1586597833,
"customer": "5e575130651c5721d808d25b",
"customer_email": "sdtikply#gmail.com",
"customer_name": "Thomas Joseph",
"days_until_due": 30,
"default_payment_method": null,
"default_source": null,
"default_tax_rates": [
],
"discount": null,
"ended_at": null,
"invoice_settings": {
"send_hosted_payment_email": true,
"supported_payment_methods": {
"ach_credit_transfer": false,
"au_becs_debit": false,
"bancontact": false,
"card": true,
"fpx": false,
"giropay": false,
"ideal": false,
"jp_credit_transfer": false,
"paper_check": false,
"sepa_credit_transfer": false,
"sofort": false
}
},
"items": {
"object": "list",
"data": [
{
"id": "si_H4pQal4ZxGzLbW",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1586597833,
"metadata": {
},
"plan": {
"id": "plan_Ga6n9yMYCDnHCu",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 500,
"amount_decimal": "500",
"billing_scheme": "per_unit",
"created": 1579512574,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {
},
"name": "Verificator",
"nickname": "Verificator",
"owning_merchant": "acct_1CiOQBEV4K2GahYL",
"owning_merchant_info": "acct_1CiOQBEV4K2GahYL",
"product": "prod_Ga6mVdA8KXyZ8I",
"tiers": null,
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"quantity": 1,
"subscription": "sub_H4pQGW8nnc80vF",
"tax_rates": [
]
},
{
"id": "si_H82ES9BdIKZCNG",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1587337381,
"metadata": {
},
"plan": {
"id": "plan_Gz6i9yPVIjrDPX",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 500,
"amount_decimal": "500",
"billing_scheme": "per_unit",
"created": 1585278262,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {
},
"name": "Pretty Formula",
"nickname": "Pretty Formula",
"owning_merchant": "acct_1CiOQBEV4K2GahYL",
"owning_merchant_info": "acct_1CiOQBEV4K2GahYL",
"product": "prod_GxqkRFdI08DvyR",
"tiers": null,
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"quantity": 1,
"subscription": "sub_H4pQGW8nnc80vF",
"tax_rates": [
]
}
],
"has_more": false,
"total_count": 2,
"url": "/v1/subscription_items?subscription=sub_H4pQGW8nnc80vF"
},
"latest_invoice": "in_1GWfm1EV4K2GahYLlmtUEISo",
"livemode": false,
"metadata": {
},
"next_pending_invoice_item_invoice": null,
"owning_merchant": "acct_1CiOQBEV4K2GahYL",
"owning_merchant_info": "acct_1CiOQBEV4K2GahYL",
"pause_collection": null,
"pending_invoice_item_interval": null,
"pending_setup_intent": null,
"pending_update": null,
"plan": null,
"quantity": null,
"schedule": null,
"start_date": 1586597833,
"status": "active",
"tax_percent": null,
"trial_end": null,
"trial_start": null
}
So my question is, how can I code in my backend (Node.js) to achieve exactly the same thing?
You just need to use the Update Subscription API and provide the items portion just as you're seeing above:
"items": {
"0": {
"billing_thresholds": "",
"deleted": "false",
"id": "si_H4pQal4ZxGzLbW",
"quantity": "1",
"tax_rates": ""
},
"1": {
"billing_thresholds": "",
"plan": "plan_Gz6i9yPVIjrDPX",
"deleted": "false",
"quantity": "1"
}
},
The first item is the existing Subscription Item (si_), and the second one is the new one you want to add.

Why does Stripe checkout Webhooks send customer_email to null?

I try to get the customer email after a payment on Checkout Stripe new interface. The JSON posted by stripe Webhook always send customer_email with null value.
The stripe Checkout page ask for customer email so I don't understand why Stripe send back this value to null.
Though, customer value is not null.
{
"id": "evt_1FItv8Kj5elW7ZcvEuY6",
"object": "event",
"api_version": "2019-03-14",
"created": 1568539286,
"data": {
"object": {
"id": "cs_test_123123123",
"object": "checkout.session",
"billing_address_collection": null,
"cancel_url": "https://www.example.fr/canceled",
"client_reference_id": null,
"customer": "cus_FoWzBx2yusHfs9",
"customer_email": null,
"display_items": [
{
"amount": 1000,
"currency": "eur",
"quantity": 1,
"sku": {
"id": "sku_1234567",
"object": "sku",
"active": true,
"attributes": {
"name": "Product test"
},
"created": 1568538814,
"currency": "eur",
"image": null,
"inventory": {
"quantity": null,
"type": "infinite",
"value": null
},
"livemode": false,
"metadata": {
},
"package_dimensions": null,
"price": 1000,
"product": "prod_FoWr00dX3",
"updated": 1568538814
},
"type": "sku"
}
],
"livemode": false,
"locale": null,
"mode": "payment",
"payment_intent": "pi_1FItj5elW70Z2",
"payment_method_types": [
"card"
],
"setup_intent": null,
"submit_type": null,
"subscription": null,
"success_url": "https://www.example.fr/success"
}
},
"livemode": false,
"pending_webhooks": 1,
"request": {
"id": null,
"idempotency_key": null
},
"type": "checkout.session.completed"
}
The email the customer entered is actually on the Customer object that the CheckoutSession links to. [0] The customer_email field is something else(it's the field that your code might have set to prefill an email into the Session).
So retrieve the Customer object from the API (cus_FoWzBx2yusHfs9) and check the email field there; or retrieve the Session object and expand the Customer field.
[0] - https://stripe.com/docs/api/customers/object#customer_object-email

Query for Joining two collection in mongo from one array filed of primary collection

I have two collections, one is subsegments as below:
[
{
"_id": "5c8b2931ff2fa8129c8f666f",
"segments": [
"5c8b2915ff2fa8129c8f666d"
],
"isSchool": false,
"isActive": false,
"isDeleted": true,
"name": "X-IIT MAINS-1552623928407",
"bannerUrl": "",
"timestamp": 1552623921559,
"updatedAt": "2019-03-15T04:25:28.408Z",
"createdAt": "2019-03-15T04:25:21.559Z",
"__v": 0,
"iconUrl": null,
"updatedById": null
}
]
Other one is segments as below:
[
{
"_id": "5a11620dbed7fd0c58ef4ec7",
"createdAt": "2017-11-19T10:50:53.495Z",
"updatedAt": "2018-08-27T12:25:36.997Z",
"timestamp": 1511088653495,
"bannerUrl": "",
"iconUrl": "https://bloom-foundation-dev.s3.amazonaws.com/20171129T000000Z_CBSE_Logo-1.jpg",
"name": "CBSE",
"isDeleted": false,
"isActive": true,
"isSchool": true,
"__v": 0,
"updatedById": null
}
]
I want to query to select subsegments and join segments details for various segments ids in the segments array of subsegments

Getting duplicate records in select query for the Azure DocumentDB

I need to write a Select query for the below JSON data in Azure DatabaseDB.
{
"Result": [
{
"media": [
{
"url": "https://someurl.com",
"thumb_url": "https://someurl.com",
"id": "f545f874-a9b4-4573-a0b0-b2d50a7994e0",
"removed": false,
"size": 133454,
"length": 0,
"type": "IMG",
"avail": true,
"has_thumb": true,
"tagged_chi": [
{
"chi_id": "1069b9ef-1028-45f4-b9a1-a40e0d438f4e",
"tag_x": 262.048,
"tag_y": 157.472,
"tag_by": "d481a522-6e2f-4dc6-8aeb-bc87cf27287d",
"created": 1486723018,
"last_updated": 1486723018
},
{
"chi_id": "7102fc10-62e8-4d0a-9fcf-35645253fcef",
"tag_x": 231.648,
"tag_y": 146.528,
"tag_by": "d481a522-6e2f-4dc6-8aeb-bc87cf27287d",
"created": 1486723018,
"last_updated": 1486723018
}
],
"created": 1486723012,
"last_updated": 1486723017
}
],
"id": "23bcd070-0f64-4914-8bc1-d5e936552295",
"acc_id": "d481a522-6e2f-4dc6-8aeb-bc87cf27287d",
"chi_id": "7102fc10-62e8-4d0a-9fcf-35645253fcef",
"is_note": false,
"title": "",
"when": -2147483648,
"loc_id": null,
"col_id": null,
"comment": null,
"removed": false,
"created": -2147483648,
"last_updated": -2147483648,
"note_type": null,
"note_value": null
},
{
"media": [
{
"url": "https://someurl.com",
"thumb_url": "https://someurl.com",
"id": "7665b921-2790-496b-a70f-30afae43d8c6",
"removed": false,
"size": 6872977,
"length": 0,
"type": "IMG",
"avail": true,
"has_thumb": true,
"tagged_chi": [
{
"chi_id": "1069b9ef-1028-45f4-b9a1-a40e0d438f4e",
"tag_x": 2305.152,
"tag_y": 686.5653,
"tag_by": "d481a522-6e2f-4dc6-8aeb-bc87cf27287d",
"created": 1486976119,
"last_updated": 1486976119
},
{
"chi_id": "7102fc10-62e8-4d0a-9fcf-35645253fcef",
"tag_x": 1070.757,
"tag_y": 1038.741,
"tag_by": "d481a522-6e2f-4dc6-8aeb-bc87cf27287d",
"created": 1486976119,
"last_updated": 1486976119
}
],
"created": 1486976100,
"last_updated": 1486976118
}
],
"id": "58fa3c58-5508-4371-83f4-405332c636e1",
"acc_id": "d481a522-6e2f-4dc6-8aeb-bc87cf27287d",
"chi_id": "7102fc10-62e8-4d0a-9fcf-35645253fcef",
"is_note": false,
"title": "",
"when": -2147483648,
"loc_id": null,
"col_id": null,
"comment": null,
"removed": false,
"created": -2147483648,
"last_updated": -2147483648,
"note_type": null,
"note_value": null
}
],
"Continuation": null
}
I was trying something like below but it is not working for me. I want the data matched to Media => tagged_chil => id
Query suggested by, #peter-tirrell:
string.Format("select c.id, c.acc_id, c.chi_id, c.is_note, c.title, c.loc_id, c.media, t from c JOIN m IN c.media JOIN t IN m.tagged_chi where c.chi_id = '{0}' OR t.chi_id = '{0}'", childId)
Minor changes in #peter-tirrell's query:
string.Format("select c.id, c.acc_id, c.chi_id, c.is_note, c.title, c.loc_id, c.media, t from c JOIN m IN c.media JOIN t IN m.tagged_chi where c.chi_id = '{0}' OR ( t.chi_id != c.chi_id AND t.chi_id = '{0}')", childId)
I am getting duplicate records if the c.child and t.child both are having same values.
You could potentially use JOINs to flatten the structure which might help with querying, too. Something like:
select
c.id,
c.acc_id,
c.chi_id,
c.is_note,
c.title,
c.loc_id,
m,
t
from c JOIN m IN c.media
JOIN t IN m.tagged_chi
where c.chi_id = {0} OR t.id = {0}
Then you can select out whichever specific data fields you need.
Base on my experience, your query code will return null. Because ARRAY_CONTAINS it will return a Boolean indicating whether the array contains the specified value. That means your query code can be short as SELECT * FROM TimelineEvent t WHERE OR ARRAY_CONTAINS ( t.media, true) that will return null in your case.
Please have a try to use the following code:
SELECT * FROM TimelineEvent t WHERE ARRAY_CONTAINS ( t.media[0].tagged_chi, {
"id":"0af23202-07f9-40a0-90ba-d2e2f6679331"
})
We also could use UDFs to implement it with customized code, more detail about UDF,please refer to document.

Sequelize - Search using the associated table but exclude results from the associated table

Is it possible to search using the associated table but excluding the data in the associated table?
E.g.
db.Bill.findAll({
include: [{
model: self.db.Group,
where: {
id: groupId
},
include: [{
model: self.db.Account,
where: {username: username}
}]
}]
});
Currently, I get these results...
[
{
"id": 2,
"title": "Title",
"category": "OTHERS",
"currency": "php",
"recurring": false,
"date_from": null,
"date_to": null,
"createdAt": "2015-08-12T10:54:08.000Z",
"updatedAt": "2015-08-12T10:54:08.000Z",
"group_id": 2,
"Group": {
"id": 2,
"name": "Group",
"createdAt": "2015-08-12T10:54:07.000Z",
"updatedAt": "2015-08-12T10:54:07.000Z",
"Account": {
"username": "username"
}
}
}
]
I want to find all the bills under a group but I only want the results to be
[
{
"id": 2,
"title": "Title",
"category": "OTHERS",
"currency": "php",
"recurring": false,
"date_from": null,
"date_to": null,
"createdAt": "2015-08-12T10:54:08.000Z",
"updatedAt": "2015-08-12T10:54:08.000Z",
"group_id": 2
}
];

Resources