thanks @robfallows for your many interventions. I have read a couple of your posts on async/await and though it still seems a bit above my head, I have some spaghetti code that sometime runs into race conditions and I believe this async/await will help resolve it.
this answer right here explained how I can setup my server methods ( I am thinking only converting the methods called on the client and involved in this call back from h**)
so on the client I have the following: how do I rewrite it to take advantage of async/await, to be more predictable and cleaner after converting the server side methods as you have described here?
AutoForm.hooks({
addOrganizationForm:{
onSubmit: function (insertDoc, updateDoc, currentDoc) {
this.event.preventDefault();
var self = this;
// TODO: create a record of this in orders before continuing, NB approve instantly
// 1. setup orders object
// 2. create order and on sucess
// 3. create organization
let insertOrdersArgs = {
orderType: 'registerOrganization',
orderDetails: insertDoc,
orderedByEntityType: Meteor.user().currentType ? Meteor.user().currentType : Meteor.user().initialType, // for first run there is no currentType
orderStatus: "Approved", // for now, regeistering a organization should be aproved immediately, may change in future
pending: false,
approved: true,
approvedAt: new Date(),
// approvedBy: Meteor.userId() not necessary, handled in db
};
Meteor.call('orders.insert', insertOrdersArgs, function(error, result) {
if (error) {
// if error regestirng the order
Bert.alert({
title: error.error,
message: error.reason,
type: 'danger',
style: 'growl-top-right',
icon: 'fa-frown-o',
hideDelay: 5000,
});
return false;
}
// else {
if(result){ // if order is created,
// actually create the organization now
Meteor.call('organizations.insert', insertDoc, function(error, result) {
if (error) {
Bert.alert({
title: error.error,
message: error.reason,
type: 'danger',
style: 'growl-top-right',
icon: 'fa-frown-o',
hideDelay: 5000,
});
}
if(result){ // if organization is created,
// assign Creator role of this lincesor (group) to this user
// Roles.addUsersToRoles(joesUserId, ['manage-team','schedule-game'], 'manchester-united.com')
// Roles.addUsersToRoles(Meteor.userId(), ['Creator','Assignee'], licensorId);
let updateRoleArgs = {
group: result, // saving organization._id as group name
roles: ['Creator'] // roles: ['Creator','Assignee']
};
// update user,
Meteor.call('users.assignUserRoles', updateRoleArgs , function(error, result){
if(error){
Bert.alert({
title: error.error,
message: error.reason,
type: 'danger',
style: 'growl-top-right',
icon: 'fa-frown-o',
hideDelay: 5000,
});
}
else {
// if(result){ // if role is assigned, doenst retunr a valid result
// update user's currentType, associatedOrganizations and currentOrganization,
// 1. update user's currentType
// 2. update user's associatedOrganizations with new associatedOrganization (organization) id
// 3. set user's currentOrganization
// which will reactively update the reactiveVars currentUserType, currentOrganization
let userUpdateArgs = {
_id: Meteor.userId(),
modifier: {
// for cases where currentType not set to assignee, eg user later decided to setup a organization, long after first run stuff
$set:{currentType: "Creator", currentOrganization: updateRoleArgs.group},
$push: {'associatedOrganizations': updateRoleArgs.group} // for when using a simple array to store ids
}
};
// update,
Meteor.call('users.update', userUpdateArgs , function(error, result){
if(error){
Bert.alert({
title: error.error,
message: error.reason,
type: 'danger',
style: 'growl-top-right',
icon: 'fa-frown-o',
hideDelay: 5000,
});
}
if(result){
Meteor.call('subscriptions.insert', {
subscriptionType: "Follower",
subscriber: Meteor.userId(),
subscriberType: "User",
subscribedTo: Meteor.user().currentOrganization,
subscribedToType: "Organisation"
} , function(error, result) {
if(error){
console.log(error)
}
console.log(result);
});
// Modal.hide();
FlowRouter.go('/dashboard');
}
});
}
});
self.done();
}
});
}
});
return false;
},