From d0c4c8b24f61ae10832d0807b989eb0183a87e06 Mon Sep 17 00:00:00 2001 From: Alexandre Bourdin Date: Wed, 6 Apr 2022 14:33:08 +0200 Subject: [PATCH] Merge pull request #6278 from overleaf/ab-login-registration-cleanup [web] Login/registration cleanup GitOrigin-RevId: 02bb2fa04a504f42b7594def3cec03ab883c64f5 --- .../src/Features/Referal/ReferalAllocator.js | 90 +++++++++++-------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/services/web/app/src/Features/Referal/ReferalAllocator.js b/services/web/app/src/Features/Referal/ReferalAllocator.js index d67e147a93..7e45e98360 100644 --- a/services/web/app/src/Features/Referal/ReferalAllocator.js +++ b/services/web/app/src/Features/Referal/ReferalAllocator.js @@ -1,51 +1,63 @@ const OError = require('@overleaf/o-error') const { User } = require('../../models/User') const FeaturesUpdater = require('../Subscription/FeaturesUpdater') +const { promisify } = require('../../util/promises') -module.exports = { - allocate(referalId, newUserId, referalSource, referalMedium, callback) { - if (callback == null) { - callback = function () {} +function allocate( + referalId, + newUserId, + referalSource, + referalMedium, + callback +) { + if (callback == null) { + callback = function () {} + } + if (referalId == null) { + return callback(null) + } + + const query = { referal_id: referalId } + return User.findOne(query, { _id: 1 }, function (error, user) { + if (error != null) { + return callback(error) } - if (referalId == null) { + if (user == null || user._id == null) { return callback(null) } - const query = { referal_id: referalId } - return User.findOne(query, { _id: 1 }, function (error, user) { - if (error != null) { - return callback(error) - } - if (user == null || user._id == null) { - return callback(null) - } - - if (referalSource === 'bonus') { - User.updateOne( - query, - { - $push: { - refered_users: newUserId, - }, - $inc: { - refered_user_count: 1, - }, + if (referalSource === 'bonus') { + User.updateOne( + query, + { + $push: { + refered_users: newUserId, }, - {}, - function (err) { - if (err != null) { - OError.tag(err, 'something went wrong allocating referal', { - referalId, - newUserId, - }) - return callback(err) - } - FeaturesUpdater.refreshFeatures(user._id, 'referral', callback) + $inc: { + refered_user_count: 1, + }, + }, + {}, + function (err) { + if (err != null) { + OError.tag(err, 'something went wrong allocating referal', { + referalId, + newUserId, + }) + return callback(err) } - ) - } else { - callback() - } - }) + FeaturesUpdater.refreshFeatures(user._id, 'referral', callback) + } + ) + } else { + callback() + } + }) +} + +module.exports = { + allocate, + promises: { + allocate: promisify(allocate), }, }