Skip to content

Instantly share code, notes, and snippets.

@jeann2013
Created June 26, 2019 21:47
Show Gist options
  • Save jeann2013/37881a9554e34cf1432e82b52acac62f to your computer and use it in GitHub Desktop.
Save jeann2013/37881a9554e34cf1432e82b52acac62f to your computer and use it in GitHub Desktop.
Functions Cloud of Firebase
import * as functions from 'firebase-functions';
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var fetch = require('isomorphic-fetch');
var btoa = require('btoa');
var md5 = require('md5');
exports.subscribeUser = functions.auth.user().onCreate(event => {
const email = event.email;
if (event.uid === "" ) {
return 0
}
/**
* @Author Jean Carlos Nunez
* @todo Function that subscribe list in mailChimp
* @param email
*/
function setEmailMailChimp (email: string | undefined = '') {
var MAILCHIMP_API_KEY = '625d4178e3b7051c531dd0369f9498bd-us15';
let listId = 'c240bcf534';
var url = 'https://us15.api.mailchimp.com/3.0/lists/' + listId + '/members'
var method = 'POST'
var headers = {
'authorization': "Basic " + btoa('randomstring:' + MAILCHIMP_API_KEY),
'Accept': 'application/json',
'Content-Type': 'application/json'
}
var body = JSON.stringify(
{
email_address: email.toLocaleLowerCase(),
status: 'subscribed',
merge_fields : { 'UID': event.uid,'GID':'GMB','ACCOUNTS': '0','LOCATIONS':'0','USERS':'0'}
})
return fetch(url, {
method,
headers,
body
}).then((resp: { json: () => void; }) => resp.json())
.then((resp: any) => {
console.log(resp)
})
}
return setEmailMailChimp(email);
});
exports.updateLocationsUser = functions.database
.ref('/group_stats/{uid}')
.onUpdate(async (event, context) => {
// @ts-ignore
let email = context.params.email
// @ts-ignore
let uid = context.params.uid
// @ts-ignore
let counts = event.after.child('counts').toJSON();
// @ts-ignore
let accounts = counts['accounts'];
// @ts-ignore
let locations = counts['locations'];
// @ts-ignore
let users = counts['users'];
/**
* @Author Jean Carlos Nunez
* @todo Function that update accounts,locations and users on MailChimp
* @param email
* @param accounts
* @param locations
* @param users
* @param uid
*/
function updateToEmailMailChimp (email: string | undefined = '',
accounts: string | undefined = '' ,
locations: string | undefined = '',
users: string | undefined = '',
uid: string | undefined = '')
{
let emailMD5 = md5(email);
var MAILCHIMP_API_KEY = '625d4178e3b7051c531dd0369f9498bd-us15';
let listId = 'c240bcf534';
var url = 'https://us15.api.mailchimp.com/3.0/lists/' + listId + '/members/'+emailMD5;
var method = 'PATCH'
var headers = {
'authorization': "Basic " + btoa('user:' + MAILCHIMP_API_KEY),
'Accept': 'application/json',
'Content-Type': 'application/json'
}
var body = JSON.stringify(
{
email_address: email.toLocaleLowerCase(),
status: 'subscribed',
merge_fields : { 'UID': uid,'GID':'GMB','ACCOUNTS': accounts,'LOCATIONS':locations,'USERS':users}
})
return fetch(url, {
method,
headers,
body
}).then((resp: { json: () => void; }) => resp.json())
.then((resp: any) => {
console.log(resp)
})
}
if(email != undefined &&
accounts != undefined && locations != undefined
&& users != undefined && uid != undefined) {
return updateToEmailMailChimp(email, accounts, locations, users, uid);
}else{return 'Error, Values Not Found!';}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment