Skip to content

Commit

Permalink
updated export
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorfdl committed Nov 8, 2023
1 parent 7979d07 commit 7fc4089
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2,607 deletions.
5 changes: 3 additions & 2 deletions src/cleanAnalysis.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Account, Analysis, Utils } from "@tago-io/sdk";
import { Data } from "@tago-io/sdk/out/common/common.types";
import { TagoContext } from "@tago-io/sdk/out/modules/Analysis/analysis.types";

import { EntityType, IExport } from "./exportTypes";
import auditLogSetup from "./lib/auditLogSetup";
import validation from "./lib/validation";
import { initializeValidation } from "./lib/validation";

const IMPORT_ORDER: EntityType = ["devices", "dictionaries", "accessManagement", "run_buttons", "analysis", "actions", "dictionaries", "dashboards"];

Expand Down Expand Up @@ -40,7 +41,7 @@ async function startCleaner(context: TagoContext, scope: Data[]) {
}
const main_account = new Account({ token: environment.account_token });
const config_dev = await Utils.getDevice(main_account, scope[0].device);
const validate = validation("cleaner_validation", config_dev);
const validate = initializeValidation("cleaner_validation", config_dev);

const target_token = scope.find((x) => x.variable === "cleaner_target_token");
const entities = scope.find((x) => x.variable === "cleaner_entity_list" && x.metadata?.sentValues);
Expand Down
52 changes: 39 additions & 13 deletions src/lib/validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { DateTime } from "luxon";

import { Device } from "@tago-io/sdk";

type validation_type = "success" | "danger" | "warning" | string;
interface IValidateOptions {
show_markdown?: boolean;
user_id?: string;
}

/**
* Setup function to send validation data to widgets.
*
Expand All @@ -8,23 +16,41 @@ import { Device } from "@tago-io/sdk";
* @param device device associated to the variable in the widget
* @param show_markdown enable/disable markdown
*/
type validation_type = "success" | "danger" | "warning" | string;

export default function validation(validation_var: string, device: Device, show_markdown?: boolean) {
return function _(message: string, type: validation_type) {
function initializeValidation(validation_var: string, device: Device, opts?: IValidateOptions) {
let i = 0;
return async function _(message: string, type: validation_type) {
if (!message || !type) {
throw "Missing message or type";
}
device.sendData({
variable: validation_var,
value: message,
metadata: {
type: ["success", "danger", "warning"].includes(type) ? type : null,
color: !["success", "danger", "warning"].includes(type) ? type : null,
show_markdown,
},
});

i += 1;
// clean the bucket
await device
.deleteData({
variables: validation_var,
qty: 999,
end_date: DateTime.now().minus({ minutes: 1 }).toJSDate(),
})
.catch(console.log);

await device
.sendData({
variable: validation_var,
value: message,
time: DateTime.now()
.plus({ milliseconds: i * 200 })
.toJSDate(), //increment time by i
metadata: {
type: ["success", "danger", "warning"].includes(type) ? type : null,
color: !["success", "danger", "warning"].includes(type) ? type : undefined,
show_markdown: !!opts?.show_markdown,
user_id: opts?.user_id,
},
})
.catch(console.error);

return message;
};
}

export { initializeValidation };
10 changes: 7 additions & 3 deletions src/services/actionsExport.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { Account } from "@tago-io/sdk";

import { IExportHolder } from "../exportTypes";
import filterExport from "../lib/filterExport";
import replaceObj from "../lib/replaceObj";

async function actionsExport(account: Account, import_account: Account, export_holder: IExportHolder) {
console.info("Exporting actions: started");

const list = await account.actions.list({ amount: 99, fields: ["id", "name", "tags"], filter: { tags: [{ key: "export_id" }] } });
const import_list = await import_account.actions.list({ amount: 99, fields: ["id", "tags"], filter: { tags: [{ key: "export_id" }] } });
const list = await account.actions.list({ amount: 900, fields: ["id", "name", "tags"], filter: { tags: [{ key: "export_id" }] } });
const import_list = await import_account.actions.list({ amount: 900, fields: ["id", "tags"], filter: { tags: [{ key: "export_id" }] } });

for (const { id: action_id, tags: action_tags, name } of list) {
console.info(`Exporting action ${name}`);
const action = await account.actions.info(action_id);
const export_id = action.tags.find((tag) => tag.key === "export_id")?.value;
if (!export_id) {
continue;
}
console.info(`Exporting action ${name}`);

let { id: target_id } = import_list.find((action) => action.tags.find((tag) => tag.key === "export_id" && tag.value == export_id)) || { id: null };

Expand Down
16 changes: 13 additions & 3 deletions src/services/runButtonsExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async function runButtonsExport(account: Account, import_account: Account, expor
console.info("Run Buttons: started");

const runInfo = await account.run.info();

const targetRunInfo = await import_account.run.info();

export_holder.dashboards[runInfo.url] = targetRunInfo.url;
Expand All @@ -59,10 +60,19 @@ async function runButtonsExport(account: Account, import_account: Account, expor
targetRunInfo.email_templates[template_name] = email_obj;
}

// @ts-expect-error
delete targetRunInfo.created_at;
const fieldsToEdit: any = {
// @ts-expect-error SDK doesn't have custom fields property yet
custom_fields: targetRunInfo.custom_fields,
signin_buttons: targetRunInfo.signin_buttons,
email_templates: targetRunInfo.email_templates,
sidebar_buttons: targetRunInfo.sidebar_buttons,
};

if (!targetRunInfo.dictionary) {
fieldsToEdit.dictionary = runInfo.dictionary;
}

await import_account.run.edit(targetRunInfo).catch((error) => {
await import_account.run.edit(fieldsToEdit).catch((error) => {
console.dir(JSON.stringify(error, null, 5));
throw error;
});
Expand Down
Loading

0 comments on commit 7fc4089

Please sign in to comment.