diff --git a/server/delighted.coffee b/server/delighted.coffee index 78062f303..1e603b451 100644 --- a/server/delighted.coffee +++ b/server/delighted.coffee @@ -4,23 +4,23 @@ log = require 'winston' DELIGHTED_EMAIL_DELAY = 1 * 86400 # in seconds -module.exports.addDelightedUser = addDelightedUser = (user) -> - return unless key = config.mail.delightedAPIKey - #return unless user.isEmailSubscriptionEnabled 'generalNews' # Doesn't work? Just returns undefined... - return if user.get('emails')?.generalNews?.enabled is false # Workaround. - name = user.get('name') - if first = user.get('firstName') and last = user.get('lastName') - name = first + ' ' + last +module.exports.addDelightedUser = addDelightedUser = (user, trialRequest) -> + props = trialRequest.get('properties') + name = props.firstName + ' ' + props.lastName form = - email: user.get('email') + email: props.email name: name delay: DELIGHTED_EMAIL_DELAY properties: - id: user.id + id: trialRequest.get('applicant') locale: user.get('preferredLanguage') testGroupNumber: user.get('testGroupNumber') gender: user.get('gender') lastLevel: user.get('lastLevel') + @postPeople(form) + +module.exports.postPeople = (form) -> + return unless key = config.mail.delightedAPIKey request.post {uri: "https://#{key}:@api.delightedapp.com/v1/people.json", form: form}, (err, res, body) -> return log.error 'Error sending Delighted request:', err or body if err or /error/i.test body #log.info "Got DelightedApp response: #{body}" diff --git a/server/middleware/trial-requests.coffee b/server/middleware/trial-requests.coffee index ba8bbc11d..14b3fb1fb 100644 --- a/server/middleware/trial-requests.coffee +++ b/server/middleware/trial-requests.coffee @@ -6,6 +6,7 @@ database = require '../commons/database' mongoose = require 'mongoose' TrialRequest = require '../models/TrialRequest' User = require '../models/User' +delighted = require '../delighted' module.exports = post: wrap (req, res) -> @@ -27,6 +28,7 @@ module.exports = trialRequest.set 'type', attrs.type database.validateDoc(trialRequest) trialRequest = yield trialRequest.save() + delighted.addDelightedUser req.user, trialRequest res.status(201).send(trialRequest.toObject({req: req})) put: wrap (req, res) -> diff --git a/server/models/User.coffee b/server/models/User.coffee index b9aaaa71e..36ecaa9db 100644 --- a/server/models/User.coffee +++ b/server/models/User.coffee @@ -12,7 +12,6 @@ config = require '../../server_config' stripe = require('stripe')(config.stripe.secretKey) sendwithus = require '../sendwithus' -delighted = require '../delighted' UserSchema = new mongoose.Schema({ dateCreated: @@ -238,7 +237,6 @@ UserSchema.methods.register = (done) -> address: @get 'email' sendwithus.api.send data, (err, result) -> log.error "sendwithus post-save error: #{err}, result: #{result}" if err - delighted.addDelightedUser @ @saveActiveUser 'register' UserSchema.methods.hasSubscription = -> diff --git a/spec/server/functional/trial_request.spec.coffee b/spec/server/functional/trial_request.spec.coffee index 9e65c85fc..1020d22af 100644 --- a/spec/server/functional/trial_request.spec.coffee +++ b/spec/server/functional/trial_request.spec.coffee @@ -6,6 +6,7 @@ User = require '../../../server/models/User' TrialRequest = require '../../../server/models/TrialRequest' Prepaid = require '../../../server/models/Prepaid' request = require '../request' +delighted = require '../../../server/delighted' fixture = { type: 'subscription' @@ -14,12 +15,18 @@ fixture = { age: '14-17' numStudents: 14 heardAbout: 'magical interwebs' + firstName: 'First' + lastName: 'Last' } describe 'POST /db/trial.request', -> + + beforeEach utils.wrap (done) -> + yield utils.clearModels([User, TrialRequest]) + spyOn(delighted, 'postPeople') + done() it 'sets type and properties given', utils.wrap (done) -> - yield utils.clearModels([User, TrialRequest]) @user = yield utils.initUser() yield utils.loginUser(@user) fixture.properties.email = @user.get('email') @@ -32,7 +39,6 @@ describe 'POST /db/trial.request', -> done() it 'sets applicant to the user\'s id', utils.wrap (done) -> - yield utils.clearModels([User, TrialRequest]) @user = yield utils.initUser() yield utils.loginUser(@user) fixture.properties.email = @user.get('email') @@ -44,7 +50,6 @@ describe 'POST /db/trial.request', -> done() it 'creates trial request for anonymous user', utils.wrap (done) -> - yield utils.clearModels([User, TrialRequest]) @user = yield utils.initUser({anonymous: true}) yield utils.loginUser(@user) email = 'someone@test.com' @@ -57,7 +62,6 @@ describe 'POST /db/trial.request', -> done() it 'prevents trial request for anonymous user with conflicting email', utils.wrap (done) -> - yield utils.clearModels([User, TrialRequest]) @otherUser = yield utils.initUser() @user = yield utils.initUser({anonymous: true}) yield utils.loginUser(@user) @@ -66,7 +70,6 @@ describe 'POST /db/trial.request', -> done() it 'updates an existing TrialRequest if there is one', utils.wrap (done) -> - yield utils.clearModels([User, TrialRequest]) @user = yield utils.initUser() yield utils.loginUser(@user) fixture.properties.email = @user.get('email') @@ -87,6 +90,17 @@ describe 'POST /db/trial.request', -> count = yield TrialRequest.count() expect(count).toBe(1) done() + + it 'creates a delighted profile', utils.wrap (done) -> + @user = yield utils.initUser({gender: 'male', lastLevel: 'abcd', preferredLanguage: 'de', testGroupNumber: 1}) + yield utils.loginUser(@user) + fixture.properties.email = @user.get('email') + [res, body] = yield request.postAsync(getURL('/db/trial.request'), { json: fixture }) + expect(delighted.postPeople).toHaveBeenCalled() + args = delighted.postPeople.calls.argsFor(0) + expect(args[0].email).toBe(@user.get('email')) + expect(args[0].name).toBe('First Last') + done() describe 'GET /db/trial.request', ->