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

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?

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;} )

Sort array based on startDate

I'm trying to sort an array and not having much luck. If I remove the helper I print out the all the qualifications in the order they went into the database. I would like to display them chronologically based on their startDate.
Path: dbExample
"profile": {
"CV": {
"education": [
{
"qualification": "Arts Degree",
"startDate": "2009-01-01T00:00:00.000Z",
"endDate": "2013-12-01T00:00:00.000Z"
},
{
"qualification": "Science Degree",
"startDate": "2007-01-01T00:00:00.000Z",
"endDate": "2008-12-01T00:00:00.000Z"
}
]
}
}
Path: education.html
<template name="education">
{{#each educationHistory}}
<div class="box">
<p class="title">{{qualification}}</p>
<p class="dates">{{startDate}} - {{endDate}}</p>
</div>
{{/each}}
</template>
Path: education.js
Template.education.helpers({
educationHistory: function () {
return Meteor.users.find({}, {sort: {"startDate": 1}});
}
});
Path: Schema.js
Schema.Education = new SimpleSchema({
qualification: {
type: String,
optional: true
},
startDate: {
type: Date,
optional: true
},
endDate: {
type: Date,
optional: true
}
});
Schema.CV = new SimpleSchema({
education: {
type: [Schema.Education],
optional: true
}
});
This is a little hard to follow because I don't know the context of the template or which user's education history to use. Here are a couple of solution ideas:
solution 1
Template.education.helpers({
educationHistory: function () {
// replace Meteor.user() with Meteor.users.findOne(someId) or something
const { education } = Meteor.user().profile.CV;
return _.sortBy(education, e => e.startDate);
},
});
This returns an array of education objects, sorted by startDate.
solution 2
If the template already has an educationHistory without the helper (based on your comments below), then you can replace the educationHistory helper with this:
Template.education.helpers({
sortByStartDate: function (education) {
return _.sortBy(education, e => e.startDate);
},
});
Then in your template:
<template name="education">
{{#each sortByStartDate educationHistory}}
<div class="box">
<p class="title">{{qualification}}</p>
<p class="dates">{{startDate}} - {{endDate}}</p>
</div>
{{/each}}
</template>

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

Ember-data loading async and order results according with property

I'm learning about ember/ember-data and would like to fetch some data from server and order it.
My json data is something like
{
'ninjas': [
{ id: '1', name: 'Ninja 1', age: 23},
{ id: '2', name: 'Ninja 2', age: 27},
{ id: '3', name: 'Ninja 3', age: 22}
],
'clans': [
{ id: '1566', title: 'Foot Clan', ninja_ids: ['1', '2']},
{ id: '8941', title: 'Hand Clan', ninja_ids: ['3']}
]
}
The templates are
<script type="text/x-handlebars" data-template-name="clans">
<div>Clans</div>
<ul>
{{#each controller}}
<li>
{{#link-to 'clan' this}}{{title}}{{/link-to}}
</li>
{{/each}}
</ul>
</script>
<script type="text/x-handlebars" data-template-name="clan">
<div>{{title}}</div>
{{#each fetchedNinjas}}
<ul>
<li>{{fetchedNinjas}}</li>
</ul>
{{/each}}
</script>
Here is the basic App script:
var App = Ember.Application.create();
App.AplicationStore = DS.Store.extend({
revision: 12,
adapter: DS.RESTAdapter.create({})
});
App.Router.map(function() {
this.resource('ninjas');
this.resource('clans');
this.resource('clan', {path: 'clans/:clan_id'});
});
The ninja script is here:
App.Ninja = DS.Model.extend({
name: DS.attr('string'),
age: DS.attr('number'),
clans: DS.hasMany('clan')
});
App.NinjasRoute = Ember.Route.extend({
model: function (params, transition, queryParams) {
return this.store.find('ninja');
}
});
Here is the Clan Model, ClansRoute, ClanRoute
App.Clan = DS.Model.extend({
title: DS.attr('string'),
ninjas: DS.hasMany('ninja', {async: true})
});
App.ClansRoute = Ember.Route.extend({
model: function (params, transition, queryParams) {
return this.store.find('clan');
}
});
App.ClanRoute = Ember.Route.extend({
model: function (params, transition, queryParams) {
return this.store.find('clan', params.clan_id);
}
});
I think that I should get the related ninja data on ClanController and then order it, but I don't know how to proceed.
App.ClanController = Ember.ObjectController.extend({
fetchedNinjas: function () {
//should I use a promise here?
}.property('ninjas')
});

Create a Meteor User in Fixtures with specific ID?

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'});

Resources