mailchimp-app
Version:
A Mailchimp express sub-app to mount on any project, for easy integration
68 lines (51 loc) • 2.03 kB
JavaScript
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
module.exports = function( config, opts ){
opts = opts || {};
var mailchimp = require('./mc-mdw')( config, opts );
var errHandler = opts.errHandler ? opts.errHandler() : mailchimp.handle("error");
// Setup : ensure bodyparser and urlencoded are being used
// Merge req.query and req.body in req.sent object
app.use( bodyParser.json() );
app.use( bodyParser.urlencoded({ extended: true }) );
app.use( mailchimp.prepareRequest );
// Make sure each requests is authorized based on who knows the secret
app.use( mailchimp.authorize( config) );
// Test routes, hello triggers a success and goodbye an error
app.get('/hello', mailchimp.handle("hello"));
app.get('/goodbye', mailchimp.handle("goodbye"));
app.get('/users/:mailchimp_id', mailchimp.handle("get_user"));
app.get('/users', mailchimp.handle("list_users"));
app.post('/users', mailchimp.handle("subscribe"));
/*
http://developer.mailchimp.com
Patch example
----------------------------------------
{
'email_address': 'jack@gmail.com',
'language': 'fr',
'location': {
'latitud': 48.22,
'longitude': 2.34
},
'merge_fields': {
'FNAME': 'Léo',
'LNAME': 'Jacquemin'
}
}
-----------------------------------------
When the email_address is updated, the app takes care of 3 steps. Mailchimp has a logic rules that
makes it impossible for a user to just update his email. It has to be a new user. Hence the steps :
- deleting the former user
- creating a brand new one
- patching him with the previous values
*/
app.patch('/users/:mailchimp_id', mailchimp.handle("update"));
// Completely deletes the user from the list.
app.delete('/users/:mailchimp_id', mailchimp.handle("delete"));
// Indicates if the ressource wasnt found
app.all('*', mailchimp.handle("end"));
app.use( errHandler );
return app;
}