2019-01-22 17:29:44 +03:00
|
|
|
module.exports = function ({
|
2019-02-07 12:41:39 +03:00
|
|
|
subscriptions,
|
2019-01-22 17:29:44 +03:00
|
|
|
createMember,
|
|
|
|
updateMember,
|
|
|
|
getMember,
|
2019-01-30 14:36:09 +03:00
|
|
|
listMembers,
|
2019-04-13 08:08:56 +03:00
|
|
|
deleteMember,
|
2019-01-22 17:29:44 +03:00
|
|
|
validateMember,
|
|
|
|
sendEmail,
|
|
|
|
encodeToken,
|
|
|
|
decodeToken
|
|
|
|
}) {
|
|
|
|
function requestPasswordReset({email}) {
|
2019-01-30 14:36:09 +03:00
|
|
|
return getMember({email}, {require: true}).then((member) => {
|
2019-01-22 17:29:44 +03:00
|
|
|
return encodeToken({
|
|
|
|
sub: member.id
|
|
|
|
}).then((token) => {
|
|
|
|
return sendEmail(member, {token});
|
|
|
|
});
|
|
|
|
}, (/*err*/) => {
|
|
|
|
// Ignore user not found err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetPassword({token, password}) {
|
|
|
|
return decodeToken(token).then(({sub}) => {
|
|
|
|
return updateMember({id: sub}, {password});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-07 12:41:39 +03:00
|
|
|
function get(...args) {
|
|
|
|
return getMember(...args).then((member) => {
|
|
|
|
return subscriptions.getAdapters().then((adapters) => {
|
|
|
|
return Promise.all(adapters.map((adapter) => {
|
|
|
|
return subscriptions.getSubscription(member, {
|
|
|
|
adapter
|
|
|
|
}).then((subscription) => {
|
|
|
|
return Object.assign(subscription, {adapter});
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}).then((subscriptions) => {
|
|
|
|
return Object.assign({}, member, {
|
|
|
|
subscriptions: subscriptions.filter(sub => sub.status === 'active')
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-13 08:08:56 +03:00
|
|
|
function destroy(...args) {
|
|
|
|
return getMember(...args).then((member) => {
|
|
|
|
if (!member) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return subscriptions.getAdapters().then((adapters) => {
|
|
|
|
return Promise.all(adapters.map((adapter) => {
|
|
|
|
return subscriptions.removeCustomer(member, {
|
|
|
|
adapter
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}).then(() => {
|
|
|
|
return deleteMember(...args);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:29:44 +03:00
|
|
|
return {
|
|
|
|
requestPasswordReset,
|
|
|
|
resetPassword,
|
|
|
|
create: createMember,
|
|
|
|
validate: validateMember,
|
2019-02-07 12:41:39 +03:00
|
|
|
list: listMembers,
|
2019-04-13 08:08:56 +03:00
|
|
|
destroy,
|
2019-02-07 12:41:39 +03:00
|
|
|
get
|
2019-01-22 17:29:44 +03:00
|
|
|
};
|
|
|
|
};
|