Create a Meteor User in Fixtures with specific ID? - meteor

I need to create a fixtures file and after a reset my Meteor.user needs to have a very specific ID because I need to preserve its relationships to other pieces of data, which use the user ID.
fixtures:
if ( Meteor.users.find().count() === 0 ) {
Accounts.createUser({
_id: "LHrhapueRmyJkajZ2", // This does not work, but I need it to be this.
username: 'admin',
email: 'admin#none.com',
password: '123456',
profile: {
first_name: 'John',
last_name: 'Doe',
company: 'ABC',
}
});
};
UPDATE:
I figured out another way:
if ( Meteor.users.find().count() === 0 ) {
var userId = Accounts.createUser({
username: 'admin',
email: 'none#none.com',
password: '123456',
profile: {
first_name: 'John',
last_name: 'Doe',
company: 'ABC',
}
});
};
Then in the rest of my fixtures file I can use the userId variable to assign any "relationships" I want.

You can have one global variable, when assigned one var without variable, this is global.
if ( Meteor.users.find().count() === 0 ) {
userId = Accounts.createUser({
username: 'admin',
email: 'none#none.com',
password: '123456',
profile: {
first_name: 'John',
last_name: 'Doe',
company: 'ABC',
}
});
};
On the other hand, you can have one Session.set('user',value);
if ( Meteor.users.find().count() === 0 ) {
userId = Accounts.createUser({
username: 'admin',
email: 'none#none.com',
password: '123456',
profile: {
first_name: 'John',
last_name: 'Doe',
company: 'ABC',
}
});
Session.set('user',userId);
};
When you want to get the value:
Session.get('user');

if you want create one register with _id default you can do the of the following way
In your programn
Collection.insert({_id:'id-default'});
If you want on Mongo Db, you open console and run Meteor, later open other console and execute in the folder the your project
meteor mongo
For insert
db.collection.insert({_id:'id-default'});

Related

How do I create a custom connect stripe account and pass account id to payment intent method using firebase functions?

I am trying to collect and transfer payments using firebase cloud functions in my react native app. I'm using the stripe.accounts.create and stripe.paymentIntents.create functions as well as library axios. I'm really not too sure how to create the connect account and pass the account id created into the payment intent method. I get the following error in the firebase logs 'code: 'StripeInvalidRequestError: Can only apply an application_fee_amount when the PaymentIntent is attempting a direct payment (using an OAuth key or Stripe-Account header) or destination payment (using transfer_data[destination])'
when trying to run my code below. Can someone please assist? I don't think the connectAcc.id is null since I can see it in my stripe dashboard logs in the response body where the account is created:
Response body
{
"id": "acct_**********U5",
"object": "account",
"business_profile": {
"mcc": "5734",
"name": null,
"product_description": null,
"support_address": null,
"support_email": null,
"support_phone": null,
"support_url": null,
index.js file
const stripe = require('stripe')('**SK_LIVE**');
exports.payWithStripe = functions.https.onRequest((request, response) => {
const connectAcc = stripe.accounts.create({
type: 'custom',
email: 'name#gmail.com',
country: 'GB',
business_type: 'individual',
business_profile: {
mcc: '5734',
url: 'site.com',
},
individual: {
first_name: 'First',
last_name: 'Last',
dob : {
day: 1,
month: 10,
year: 1990
},
email: 'name#gmail.com',
phone: '+44xxxxxxx',
address: {
city: 'city',
country: 'GB',
line1: '1',
line2: 'Street Rd',
postal_code: 'XXX XXX'
}
},
tos_acceptance: {
date: Math.floor(Date.now() / 1000),
ip: request.connection.remoteAddress,
},
capabilities: {
card_payments: {requested: true},
transfers: {requested: true},
},
external_account: {
object: 'bank_account',
country: 'GB',
currency: 'gbp',
account_number: 'xxxxx',
routing_number: 'xxxxx',
accounter_holder_name: 'First Last',
account_holder_type: 'individual',
}
})
stripe.paymentIntents.create({
amount: request.body.amount,
currency: request.body.currency,
payment_method_types: ['card'],
payment_method: request.body.payment_method.id,
application_fee_amount: 20,
on_behalf_of: connectAcc.id,
transfer_data: {
destination: connectAcc.id,
},
confirm: true,
description: 'UniHome'
}
).then((charge) => {
response.send(charge);
})
.catch(err =>{
console.log(err);
})
});
Thanks.
connectAcc is not an account object — it's a Promise. That's what Stripe's SDK returns.
You'd have to resolve the Promise first, like:
let connectAcc = await stripe.accounts.create(...); let id = connectAcc.id or stripe.accounts.create(...).then(function(acct){let id = acct.id;} )

Firestore: Query documents by property of object

I've a collection with contacts with a structure like:
name: 'XPTO Company',
emails: {
susan#xpto.com: { name: 'Susan', text: 'manager' },
fred#xpto.com: { name: 'Fred', text: 'marketing' }
}
How do I retrieve documents with email 'susan#xpto.com'
Something like:
firebase.firestore().collection('contacts')
.where(new firebase.firestore.FieldPath('emails', email), '==', true).get()
.then(snap => {
})
You may add a property id that holds the email address to your email object. and replace '.' in email object key with '-', e.g. susan#xpto-com, so that your data structure looks like this:
name: 'XPTO Company',
emails: {
susan#xpto-com: { id: 'susan#xpto.com', name: 'Susan', text: 'manager' },
fred#xpto-com: { id: 'fred#xpto.com', name: 'Fred', text: 'marketing' }
Then you can retrieve documents with email 'susan#xpto.com' in this way:
firebase.firestore().collection('contacts')
.where('emails.susan#xpto-com.id', '==', 'susan#xpto.com').get()
.then(snap => {
})

Assign value from user profile to collection2 schema

How can I assign the values of the meteor profile to a collection2 schema? I need to assign the email address and name.
I thought it might be something like this but its not:
email: {
type: String,
regEx: SimpleSchema.RegEx.Email,
defaultValue: function() { return this.user().emails[0].address }
},
Any ideas?
This sorted it for the email address:
email: {
type: String,
regEx: SimpleSchema.RegEx.Email,
defaultValue: function(){ return Meteor.user().emails[0].address }
},
For the profile name:
firstName: {
type: String,
regEx: /^[a-zA-Z]{2,25}$/,
defaultValue: function(){ return Meteor.user().profile.name }
},

Ordering q.all promises

I'm trying to insert an array of users into a sequelize database using q.all() and the create() function like so:
var users = [{
provider: 'local',
role: 'user',
name: 'Test user 1',
email: 'test#example.com',
password: 'test',
gender: 'f'
}, {
provider: 'local',
role: 'admin',
name: 'Test user 2',
email: 'test2#example.com',
password: 'admin',
gender: 'm'
}];
var userIDs = [];
return q.all([
User.create(users[0]),
User.create(users[1])
])
.then(function(data) {
async.eachSeries(data, function(newUser, cb) {
userIDs.push(newUser.dataValues._id);
cb();
}, function() {
console.log('finished populating users');
deferred.resolve(userIDs);
});
})
.catch(function(err) {
console.log(err);
});
This works and I can access the userID's later on, but unfortunately the users returned in data are not always created in the same order. How can I make it asynchronous so that Test user 1 is always created first, then once that is finished, test user 2 is created?
Thanks for any help

How to count the number of similar ids in a collection?

I have a collection which I populate with seed that looks like this
if (Schools.find().count() === 0) {
school1Id = Schools.insert({
name: "High School For Boys",
housename: {
house1: 'Milton',
house2: 'Granton',
house3: 'Buxton',
house4: 'Barnard'
}
});
school2Id = Schools.insert({
name: "Pretoria Boys High",
housename: {
house1: 'House1',
house2: 'House2',
house3: 'House3',
house4: 'House4'
}
});
I have another collection that is seeded like this
if (Years.find().count() === 0) {
for (y = 1905; y < 2015; y++ ) {
Years.insert({
year: y
});
}
}
I have one further collection that looks like this
if (Meteor.users.find().count() === 0) {
var school1Id = Schools.findOne({name: "High School For Boys"})._id;
var school2Id = Schools.findOne({name: "Pretoria Boys High"})._id;
seed1UserId = Accounts.createUser({
username: 'lbob',
email: 'l#oo.com',
password: '123456',
profile: {
firstname: 'Lance',
lastname: 'Bones',
phone: '0000000000',
schoolName: school1Id,
firstschoolyear: Years.findOne({year: 1984})._id,
lastschoolyear: Years.findOne({year: 1988})._id,
matriculated: 1,
housename: Schools.findOne(school1Id).housename.house1,
country: 'South Africa',
cityofresidence: 'Cape Town',
emplindustry: 'IT',
joined: new Date() // current time
}
});
seed2UserId = Accounts.createUser({
username: 'david',
email: 'd#oo.com',
password: '123456',
profile: {
firstname: 'David',
lastname: 'McNulty',
phone: '0000000000',
schoolName: school1Id,
firstschoolyear: Years.findOne({year: 1988})._id,
lastschoolyear: Years.findOne({year: 1990})._id,
matriculated: 0,
housename: Schools.findOne(school1Id).housename.house1,
country: 'Scotlad',
cityofresidence: 'Glasgow',
emplindustry: 'Hospitality',
joined: new Date() // current time
}
});
seed3UserId = Accounts.createUser({
username: 'glenn',
email: 'g#oo.com',
password: '123456',
profile: {
firstname: 'Glenn-Douglas',
lastname: 'Jones',
phone: '0000000000',
schoolName: school1Id,
firstschoolyear: Years.findOne({year: 1981})._id,
lastschoolyear: Years.findOne({year: 1986})._id,
matriculated: 1,
housename: Schools.findOne(school1Id).housename.hous1,
country: 'South Africa',
cityofresidence: 'Cape Town',
emplindustry: 'Coaching',
joined: new Date() // current time
}
});
seed4UserId = Accounts.createUser({
username: 'martin',
email: 'm#oo.com',
password: '123456',
profile: {
firstname: 'Martin',
lastname: 'James',
phone: '0000000000',
schoolName: school2Id,
firstschoolyear: Years.findOne({year: 1983})._id,
lastschoolyear: Years.findOne({year: 1987})._id,
matriculated: 1,
housename: Schools.findOne(school2Id).housename.house2,
country: 'Austrailia',
cityofresidence: 'Melbourne',
emplindustry: 'Airline',
joined: new Date() // current time
}
});
}
The school template looks like this
<template name="schoolList">
<div class="title">Schools List</div>
<div class="list-group">
{{#each schools}}
{{> school}}
{{/each}}
</div>
<template>
<template name="school">
<a href="#" class="list-group-item school">
<!-- <span class="badge">44</span> -->
<span class="badge">{{totalalumnis}}</span>
{{name}}
</a>
<template>
My js file looks like this
Template.schoolList.helpers({
schools: function () {
return Schools.find({});
},
totalalumnis: function () {
// return Schools.find({}'_id');
//return Meteor.users.findOne()
return Schools.find({}).count();
}
});
I am trying to produce a count of the number of students per school and present it in the badge
As you can see from the js file I am struggling to understand how would be the best way to setup the query.
I am not sure if I have created my data incorrectly and I should rather store the students in the schools collection. My logic tells me that this would be easier to count and be more reactive, I have however decided to store the school id in the student with the logic that all information about a student is in their user document.
I also would like to on another page that lists the number of students per year for the school duplicate this type of search so that I could add that result to the year badge.
I would really appreciate the help.
EDIT: I have made some changes to the helper code that look like this
Template.schoolList.helpers({
schools: function () {
return Schools.find({});
},
totalalumnis: function () {
return AlumniList.find({schoolName: this._id}).count();
}
});
I have tested the query in the browser console and if I ad a valid schoolName id it returns the correct count of the number of students. This however does not get displayed in the template.
Can someone help?

Resources