Skip to content

Instantly share code, notes, and snippets.

@jacobjuul
Created October 5, 2016 11:55
Show Gist options
  • Save jacobjuul/9b8b6c3319b5273174a1da2255d15607 to your computer and use it in GitHub Desktop.
Save jacobjuul/9b8b6c3319b5273174a1da2255d15607 to your computer and use it in GitHub Desktop.
const userNotificationSettings = userId => {
const targetUser = Meteor.users.findOne({ _id: userId });
return notificationType => channel => {
if (typeof userId !== 'string' ||
typeof notificationType !== 'string' ||
typeof channel !== 'string') {
throw new Meteor.Error('All args must be of type string');
}
if (['web', 'sms', 'email'].indexOf(channel) === -1) {
throw new Meteor.Error('channel should be "web, SMS or email"');
}
const notificationChannel = _get(
targetUser,
`settings.notifications.disabled.${channel}`,
[]
);
// if the notification is disabled return false
if (notificationChannel.indexOf(notificationType) === -1) {
return false;
} else {
return true;
}
};
};
export function userJoinedActivity(activity, user) {
const coach = Meteor.users.findOne({ _id: activity.coachId });
const coachNotificationDisabledForChannel =
userNotificationSettings(activity.coachId)(types.USER_JOINED_ACTIVITY_COACH);
const userNotificationDisabledForChannel =
userNotificationSettings(user._id)(types.USER_JOINED_ACTIVITY_USER);
// Create notification for coach
if (!userNotificationDisabledForChannel('SMS')) {
smsHelper.sendText(
`${user.profile.firstName} har tilmeldt sig ${activity.title}`,
coach.profile.phoneNumber,
);
}
if (!coachNotificationDisabledForChannel('web')) {
// Send notification to coach
Notification.insert({
createdBy: user._id,
icon: user.profile.picture,
targetId: activity.coachId,
data: activity,
type: types.USER_JOINED_ACTIVITY_COACH,
message: `${user.profile.firstName} har tilmeldt sig ${activity.title}`,
activityId: activity._id
});
}
if (!userNotificationDisabledForChannel('web')) {
// send notification to user
Notification.insert({
createdBy: activity.coachId,
icon: coach.profile.picture,
targetId: user._id,
type: types.USER_JOINED_ACTIVITY_USER,
data: activity,
message: `Du er nu tilmeldt ${activity.title}`,
activityId: activity._id
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment