import { trackSmallStepNotionExport } from "https://esm.town/v/thomasatflexos/trackSmallStepNotionExport";
import { createSmallStepAsNotionPage } from "https://esm.town/v/thomasatflexos/createSmallStepAsNotionPage";
import { createSmallStepNotionDatabase } from "https://esm.town/v/thomasatflexos/createSmallStepNotionDatabase";
import { getSmallStepPage } from "https://esm.town/v/thomasatflexos/getSmallStepPage";
import { getAccessTokenUsingBotId } from "https://esm.town/v/thomasatflexos/getAccessTokenUsingBotId";
export let importSmallStepsIntoNotion = async (
req: express.Request,
res: express.Response,
) => {
let bot_id = req.body.bot_id;
let title = req.body.title;
let stepData = req.body.steps;
if (!bot_id || !title || !stepData) {
res.json({
status: 400,
message: "Missing parameters: bot_id or title or steps",
});
}
stepData = stepData.reverse();
const accessToken = await getAccessTokenUsingBotId(
bot_id,
);
if (!accessToken) {
res.json({
status: 400,
message: "This user has not authorizen Notion integration yet",
});
}
const smallStepPage = await getSmallStepPage(
accessToken,
);
const newSmallStepDatabase = await createSmallStepNotionDatabase(smallStepPage.id, title, accessToken);
const results = [];
for (const datum in stepData) {
let stepName = stepData[datum].stepName;
let content = stepData[datum].text;
let newPage = await createSmallStepAsNotionPage(
newSmallStepDatabase.id,
stepName,
content,
accessToken,
);
results.push(newPage);
}
await trackSmallStepNotionExport(bot_id, title);
res.json({
status: 200,
message: `Created ${results.length} pages in Notion`,
});
};