Export data to Zoho Creator Form Record

/**
 * Questmate Custom Completion Item - Add Zoho Creator Form Record
 *
 * Permissions:
 * @UseApp {ZOHO}
 *
 * Changelog:
 * v0.1 Initial release
 *
 * eslint-disable
 */

enableDebugMode();

return defineCustomItem((Questmate) => {
  Questmate.registerItemRunHandler(async ({ useConfigData, useQuest }) => {
    const [workspaceName] = useConfigData("workspaceName");
    const [applicationId] = useConfigData("applicationId");
    const [formId] = useConfigData("formId");
    const [fieldToItemMap] = useConfigData("fieldToItemMap");

    const quest = await useQuest();
    const questRun = await quest.getRun();

    const newRecordData = {};

    Object.keys(fieldToItemMap).forEach((fieldLinkName) => {
      newRecordData[fieldLinkName] =
        questRun.getItem(fieldToItemMap[fieldLinkName]).stringValue || "";
    });

    const insertRecordRequest = await fetch(
      `https://creator.zoho.com.au/api/v2/${workspaceName}/${applicationId}/form/${formId}`,
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          data: newRecordData,
        }),
      }
    );

    const insertRecordResponseData = await insertRecordRequest.json();
    console.log(JSON.stringify(insertRecordResponseData));
  });

  Questmate.registerView(
    "ITEM_CONFIG_VIEW",
    async ({ useConfigData, useQuest }) => {
      const [selectedApplicationId, setSelectedApplicationId] = useConfigData(
        "applicationId",
        null
      );
      const [selectedWorkspaceName, setSelectedWorkspaceName] = useConfigData(
        "workspaceName",
        null
      );
      const [selectedFormId, setSelectedFormId] = useConfigData("formId", null);
      const [fieldToItemMap, setFieldToItemMap] = useConfigData(
        "fieldToItemMap",
        {}
      );

      const viewComponents = [];
      viewComponents.push({
        id: "application",
        title: "Application",
        type: "dropdown",
        value: selectedApplicationId,
        getOptions: async () => {
          return (await applicationsDataSource.retrieve()).map(
            (application) => ({
              label: application.application_name,
              value: application.link_name,
            })
          );
        },
        onSelect: (value) => {
          if (value !== selectedApplicationId) {
            setSelectedApplicationId(value);
            setSelectedWorkspaceName(value ? "sascha_fullset" : null);
            setSelectedFormId(null);
          }
        },
        optionNoun: "Application",
        optionPluralNoun: "Applications",
      });

      if (selectedApplicationId !== null) {
        viewComponents.push({
          id: "form",
          title: "Form",
          type: "dropdown",
          value: selectedFormId,
          getOptions: async () => {
            return (
              await formsDataSource.retrieve(
                selectedApplicationId,
                selectedWorkspaceName
              )
            ).map((form) => ({
              label: form.display_name,
              value: form.link_name,
            }));
          },
          onSelect: (value) => {
            if (value !== selectedFormId) {
              setSelectedFormId(value);
            }
          },
          optionNoun: "Form",
          optionPluralNoun: "Forms",
        });
      }

      if (selectedFormId !== null) {
        const fields = await fieldsDataSource.retrieve(
          selectedApplicationId,
          selectedWorkspaceName,
          selectedFormId
        );
        viewComponents.push({
          id: `TextBlock1`,
          type: "text",
          content: "Choose the item to map to each field below.",
        });
        fields.forEach((field) => {
          viewComponents.push({
            id: field.link_name,
            title: field.display_name,
            type: "ItemPicker",
            value: fieldToItemMap[field.link_name],
            onSelect: (itemId) => {
              if (itemId !== fieldToItemMap[field.link_name]) {
                setFieldToItemMap({
                  ...fieldToItemMap,
                  [field.link_name]: itemId,
                });
              }
            },
          });
        });
      }

      return {
        components: viewComponents,
      };
    }
  );

  const applicationsDataSource = Questmate.registerDataSource({
    id: "applications",
    initialData: [],
    refreshInterval: 120,
    fetcher: () => async () => {
      const applicationsData = await fetch(
        "https://creator.zoho.com.au/api/v2/applications"
      );
      const jsonData = await applicationsData.json();
      console.log(JSON.stringify(jsonData));
      const { applications } = jsonData;
      return {
        data: applications,
      };
    },
  });

  const formsDataSource = Questmate.registerDataSource({
    id: "forms",
    initialData: [],
    refreshInterval: 120,
    fetcher: () => async (applicationId, selectedWorkspaceName) => {
      const formsData = await fetch(
        `https://creator.zoho.com.au/api/v2/${selectedWorkspaceName}/${applicationId}/forms`
      );
      const jsonData = await formsData.json();
      console.log(JSON.stringify(jsonData));
      const { forms } = jsonData;
      return {
        data: forms,
      };
    },
  });

  const fieldsDataSource = Questmate.registerDataSource({
    id: "fields",
    initialData: [],
    refreshInterval: 120,
    fetcher:
      () => async (applicationId, selectedWorkspaceName, selectedFormId) => {
        const fieldsData = await fetch(
          `https://creator.zoho.com.au/api/v2/${selectedWorkspaceName}/${applicationId}/form/${selectedFormId}/fields`
        );
        const jsonData = await fieldsData.json();
        console.log(JSON.stringify(jsonData));
        const { fields } = jsonData;
        return {
          data: fields,
        };
      },
  });
});