1 module konnexengine.integrations.soundee.webhooks; 2 3 import std.conv: to; 4 import std.string: capitalize; 5 import vibe.core.log: logInfo; 6 import vibe.data.json: Json; 7 import konnexengine.integrations.mailerlite.api; 8 9 void processSale(Json webhook, AuthInfo auth) 10 { 11 string email = webhook["object"]["customer"]["email"].to!string; 12 string name = capitalize(webhook["object"]["customer"]["firstName"].to!string); 13 string lastname = capitalize(webhook["object"]["customer"]["lastName"].to!string); 14 string country = webhook["object"]["country"]["countryName"].to!string; 15 string licenseType = webhook["object"]["items"][0]["license"]["name"].to!string; 16 17 // Check if customer is not already a subscriber 18 auto i = SubscriberIdentifier(email); 19 Json subscriber = getSubscriberBy!SubscriberIdentifier(i, auth); 20 string type = subscriber["type"].to!string; 21 scope(failure) 22 { 23 // Add subscriber 24 logInfo(i.identifier ~ " is not a subscriber."); 25 logInfo("Adding: " ~ i.identifier ~ "..."); 26 auto c = CustomerDetails(email,name,lastname,country); 27 auto s = createActiveSubscriberFrom!CustomerDetails(c, auth); 28 logInfo("Added successfully."); 29 logInfo("Setting " ~ i.identifier ~ " status to 'active'..."); 30 // Set Active 31 logInfo(i.identifier ~ " is not a subscriber."); 32 Json error = subscriber["error"]; 33 logInfo("2"~error.to!string); 34 } 35 scope(success) 36 { 37 if(type != "active") 38 { 39 logInfo("Setting " ~ i.identifier ~ " status to 'active'..."); 40 // Set Active 41 auto c = CustomerDetails(email,name,lastname,country); 42 auto s = updateActiveSubscriberFrom!CustomerDetails(c, auth); 43 logInfo(i.identifier ~ " is not a subscriber."); 44 } 45 // logInfo("1"~type); 46 } 47 scope(exit) 48 { 49 // Add to license group 50 auto sl = NewSubscriberToGroup(licenseType, email, name, lastname); 51 auto slg = addSubscriberToGroup!NewSubscriberToGroup(sl, auth); 52 // Add to customer group 53 auto s = NewSubscriberToGroup("Customer", email, name, lastname); 54 auto sg = addSubscriberToGroup!NewSubscriberToGroup(s, auth); 55 // Notify of sale 56 } 57 }