Hello everyone.
In this tutorial, I’ll explain how to create a run user account by analysis, as well as how to build an input form widget to achieve just that. Creating a run user account by analysis is a very powerful tool when it’s necessary to force some parameters in a fixed time and date format. Also, you can include it externally from TagoIO and adapt it to your application’s needs.
For creating the Analysis you have two options using TagoIO’s SDK:
If you want to skip this tutorial and simply use the Analysis and Dashboard templates, they are right here:
What are the requirements for this tutorial?
First, go to the Analysis menu and click to add a new empty analysis.
Then you can copy and paste the following code:
const { Analysis, Account, Utils } = require("@tago-io/sdk");
async function myAnalysis(context, scope) {
if (!Array.isArray(scope) || scope.length < 7) {
throw "Scope is missing";
}
const [email, name, password, company, language, date_format, time_format] = scope;
const environment = Utils.envToJson(context.environment);
if (!environment.runUrl || !environment.timezone || !environment.accountToken) {
throw "Environment is missing";
}
const data = {
active: true,
email: email.value,
name: name.value,
password: password.value,
company: company.value,
language: language.value,
options: {
date_format: date_format.value,
time_format: time_format.value,
},
timezone: environment.timezone
};
try {
const account = new Account({ token: environment.accountToken });
await account.run.userCreate(data);
} catch (ex) {
context.log(ex);
}
}
module.exports = new Analysis(myAnalysis);
Now you have to write the environment variables used for this Analysis: the runUrl
, the timezone
, and the accountToken
. To do that, simply go to the Environment tab and write it all down like in the following example:
From the TagoIO’s run URL, you’ll need to remove the “HTTPS”. For example:
My URL is https://61423ee4495a0a001a7f5edd.run.tago.io, so then when writing it down in the Analysis, I have to remove the HTTPS and it becomes: 61423ee4495a0a001a7f5edd.run.tago.io.
You can see that the timezone for these accounts has been fixed in the code. In this tutorial, I will be fixing it in UTC but you can use America/New_York, America/Chicago, America/Mexico_City, America/Sao_Paulo, Europe/London, and more.
After the analysis is created, click on the save button, and let’s start to create the input form that will send all the necessary fields for this Analysis.
The input form will take the user’s data information and trigger the analysis so it can create the account. First of all, we need to add the required fields described here (email, name, password); all other fields are optional when creating an account, but for this tutorial we are going to add fields for the company, language, date and time format.
The timezone is also a required field but was fixed in the Analysis code, and you are able to adapt it as you wish
Start to create the email field, and it should look something like this, with the single option checked.
Then create the others: name (text field), password (password field), and company (text field). For the dropdown fields like language, date format, and time format, you have to give options that are accepted by TagoIO. You can not include a date format that we don’t have the option for. Because of that, I suggest you use the following options:
After creating the fields, you have to configure the submit button so it doesn’t send this data to the bucket and trigger our analysis created earlier.
Now that everything is successfully created, you can check that when you submit the form with at least the required fields filled out, a new run user will be created in the User Management menu. This input form can also be embedded, and you can use it on your website.
Just like the other analysis, you can create a new one and copy-paste the following code:
const { Analysis, RunUser, Utils } = require("@tago-io/sdk");
async function myAnalysis(context, scope) {
if (!Array.isArray(scope) || scope.length < 7) {
throw "Scope is missing";
}
const [email, name, password, company, language, date_format, time_format] = scope;
const environment = Utils.envToJson(context.environment);
if (!environment.runUrl || !environment.timezone) {
throw "Environment is missing";
}
const data = {
active: true,
email: email.value,
name: name.value,
password: password.value,
company: company.value,
language: language.value,
options: {
date_format: date_format.value,
time_format: time_format.value,
},
timezone: environment.timezone
};
try {
await RunUser.create(environment.runUrl, data);
} catch (ex) {
context.log(ex);
}
}
module.exports = new Analysis(myAnalysis);
This time only two environment variables are used: runUrl
and timezone
. Simply fill them out with the desired data, and save.
I hope this helps with your application! Stay tuned to this community to learn more awesome things.
Regards,
Filipe.