Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PetDesk App] Add asgardeo registration webhook #460

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"image": "ballerina/ballerina-devcontainer:2201.3.2",
"extensions": ["WSO2.ballerina"],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
org = "WSO2"
name = "asgardeo_registration_webhook"
version = "0.1.0"
distribution = "2201.3.2"

[build-options]
observabilityIncluded = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import ballerinax/trigger.asgardeo;
import ballerina/log;
import ballerinax/salesforce;
import ballerina/http;

// Create Salesforce client configuration by reading from environment.
configurable string salesforceAppClientId = ?;
configurable string salesforceAppClientSecret = ?;
configurable string salesforceAppRefreshToken = ?;
configurable string salesforceAppRefreshUrl = ?;
configurable string salesforceAppBaseUrl = ?;

// Using direct-token config for client configuration
salesforce:ConnectionConfig sfConfig = {
baseUrl: salesforceAppBaseUrl,
auth: {
clientId: salesforceAppClientId,
clientSecret: salesforceAppClientSecret,
refreshToken: salesforceAppRefreshToken,
refreshUrl: salesforceAppRefreshUrl
}
};

configurable asgardeo:ListenerConfig config = ?;

listener http:Listener httpListener = new(8090);
listener asgardeo:Listener webhookListener = new(config,httpListener);

service asgardeo:RegistrationService on webhookListener {

remote function onAddUser(asgardeo:AddUserEvent event ) returns error? {

salesforce:Client baseClient = check new (sfConfig);

log:printInfo(event.toJsonString());

json responseData = event.eventData.toJson();

map<json> mj = <map<json>> responseData;
map<json> userClaims = <map<json>> mj.get("claims");

string lastName = <string>userClaims["http://wso2.org/claims/lastname"];
string firstName = <string>userClaims["http://wso2.org/claims/givenname"];
string email = <string>userClaims["http://wso2.org/claims/emailaddress"];

record {} leadRecord = {
"Company": string `${firstName}_${lastName}`,
"Email": email,
"FirstName": firstName,
"LastName": lastName
};

salesforce:CreationResponse|error res = baseClient->create("Lead", leadRecord);

if res is salesforce:CreationResponse {
log:printInfo("Lead Created Successfully. Lead ID : " + res.id);
} else {
log:printError(msg = res.message());
}
}

remote function onConfirmSelfSignup(asgardeo:GenericEvent event ) returns error? {

log:printInfo(event.toJsonString());
}

remote function onAcceptUserInvite(asgardeo:GenericEvent event ) returns error? {

log:printInfo(event.toJsonString());
}
}

service /ignore on httpListener {}

Loading