Skip to content

Instantly share code, notes, and snippets.

@remydagostino
Created March 26, 2014 02:13
Show Gist options
  • Save remydagostino/9775722 to your computer and use it in GitHub Desktop.
Save remydagostino/9775722 to your computer and use it in GitHub Desktop.
Example Wings Javascript Interface
var app = (function() {
var self = {};
self.storage = {
get: function(key) {
return localStorage.getItem(key);
},
set: function(key, value) {
if (typeof value == "undefined") {
return localStorage.removeItem(key);
}
else {
return localStorage.setItem(key, value);
}
},
getObj: function(key) {
var value = self.storage.get(key);
return (value ? JSON.parse(value) : value);
},
setObj: function(key, obj) {
return self.storage.set(key, (obj ? JSON.stringify(obj) : obj))
},
};
// Wings is the rex API
self.wings = {
requests: [],
request: function(service, method, data) {
var deferred = new $.Deferred();
var xhr = $.ajax(
"https://api-demo.rexsoftware.com/rex.php",
{
data: {
"method": service + "::" + method,
"args": JSON.stringify(data),
"token": self.storage.get("api_token"),
"use_status_codes": true
},
cache: false,
type: 'POST',
dataType: 'json',
}
).then(
function(response, state, xhr) { // Success
if (!response.error) {
deferred.resolve(response.result);
}
else {
deferred.reject(response.result.error, xhr);
}
},
function(xhr, state, status) { // Fail
deferred.reject(xhr.responseJSON.error, xhr.responseJSON);
}
);
self.wings.requests.push(xhr);
xhr.always(function() {
var index = self.wings.requests.indexOf(xhr);
if (index) {
self.wings.requests.splice(index, 1);
}
});
return deferred;
},
login: function(email, password, accountId) {
return self.wings.request("Authentication", "login", {
"email": email,
"password": password,
"account_id": accountId,
"application": "rex",
"token_lifetime": 3600
}).then(function(response) {
self.storage.set("api_token", response);
});
},
findContactDuplicate: function(contact) {
var deferred = new $.Deferred();
// Check that the contact is not a duplicate
if ((contact.firstName && contact.lastName) || contact.email) {
var checkDuplicate = {};
if (contact.firstName && contact.lastName) {
checkDuplicate.name_first = contact.firstName;
checkDuplicate.name_last = contact.lastName;
}
if (contact.email) {
checkDuplicate.email = contact.email;
}
self.wings.request("Contacts", "findPossibleDuplicates", checkDuplicate).then(
function(response) {
if (response.length > 0) {
deferred.resolve(response[0]);
}
else {
deferred.resolve(false);
}
},
function(error) {
deferred.reject(error);
}
);
}
else {
deferred.resolve(false)
}
return deferred;
},
createContact: function(contact) {
var contactData = {
"type": "person",
"_related": {}
};
contactData._related.contact_names = [{
"name_first" : contact.firstName,
"name_last" : contact.lastName
}];
if (contact.email) {
contactData._related.contact_emails = [{
"email_desc": "work",
"email_primary": true,
"email_address" : contact.email
}];
}
if (contact.phone) {
contactData._related.contact_phones = [{
"phone_type": "work",
"phone_primary": true,
"phone_number" : contact.phone
}];
}
return self.wings.request("Contacts", "create", { "data": contactData });
},
// Just updates the phone number and
updateContactDetails: function(apiContact, contact) {
var contactData = {
"_id": apiContact._id,
"_related": {
"contact_phones": [],
"contact_emails": []
}
};
var updatedPhone, hasPrimaryPhone, duplicatePhone;
if (contact.phone) {
// Check for a phone number
$.each(apiContact._related.contact_phones, function(index, phone) {
if (phone.phone_number == contact.phone) {
duplicatePhone = phone;
}
if (phone.phone_primary) {
hasPrimaryPhone = true;
}
});
if (!duplicatePhone) {
updatedPhone = {
"phone_type": "work",
"phone_primary": !hasPrimaryPhone,
"phone_number" : contact.phone
};
}
else if (duplicatePhone && !hasPrimaryPhone) {
updatedPhone = duplicatePhone;
updatedPhone.phone_primary = true;
}
}
// Check for an email address
var updatedEmail, hasPrimaryEmail, duplicateEmail;
if (contact.email) {
$.each(apiContact._related.contact_emails, function(index, email) {
if (email.email_address == contact.email) {
duplicateEmail = email;
}
if (email.email_primary) {
hasPrimaryEmail = true;
}
});
if (!duplicateEmail) {
updatedEmail = {
"email_desc": "work",
"email_primary": true,
"email_address" : contact.email
};
}
else if (duplicateEmail && !hasPrimaryEmail) {
updatedEmail = duplicateEmail;
updatedEmail.email_primary = true;
}
}
// Only make a contact update if something has actually changed
if (updatedPhone || updatedEmail) {
if (updatedPhone) {
contactData._related.contact_phones.push(updatedPhone);
}
if (updatedEmail) {
contactData._related.contact_emails.push(updatedEmail);
}
return self.wings.request("Contacts", "update", { "data": contactData });
}
else {
return false;
}
},
addContactNote: function(contactId, note) {
var noteData = {
"note_type": "note",
"note": note,
"_related": {
"note_contacts": [{
"contact_id": contactId
}]
}
};
return self.wings.request("Notes", "create", { "data": noteData });
}
};
return self;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment