mirror of
https://github.com/domfelipe/mika-agent-assist.git
synced 2026-08-07 12:36:50 +00:00
Changes
Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
parent
eaf69d5de9
commit
929e641792
4 changed files with 68 additions and 31 deletions
|
|
@ -23,23 +23,49 @@ export interface ProviderEnv {
|
|||
redirectUri: string;
|
||||
}
|
||||
|
||||
function readFirstEnv(keys: string[]): string {
|
||||
for (const key of keys) {
|
||||
const value = Deno.env.get(key);
|
||||
if (value) return value;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
export function getProviderEnv(slug: ProviderSlug, redirectUri: string): ProviderEnv {
|
||||
const map: Record<ProviderSlug, [string, string]> = {
|
||||
google_workspace: ["GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_SECRET"],
|
||||
notion: ["NOTION_CLIENT_ID", "NOTION_CLIENT_SECRET"],
|
||||
todoist: ["TODOIST_CLIENT_ID", "TODOIST_CLIENT_SECRET"],
|
||||
calcom: ["CALCOM_CLIENT_ID", "CALCOM_CLIENT_SECRET"],
|
||||
microsoft_365: ["MICROSOFT_CLIENT_ID", "MICROSOFT_CLIENT_SECRET"],
|
||||
const map: Record<ProviderSlug, { id: string[]; secret: string[] }> = {
|
||||
google_workspace: {
|
||||
id: ["GOOGLE_CLIENT_ID", "GOOGLE_OAUTH_CLIENT_ID"],
|
||||
secret: ["GOOGLE_CLIENT_SECRET", "GOOGLE_OAUTH_CLIENT_SECRET"],
|
||||
},
|
||||
notion: {
|
||||
id: ["NOTION_CLIENT_ID", "NOTION_OAUTH_CLIENT_ID"],
|
||||
secret: ["NOTION_CLIENT_SECRET", "NOTION_OAUTH_CLIENT_SECRET"],
|
||||
},
|
||||
todoist: {
|
||||
id: ["TODOIST_CLIENT_ID", "TODOIST_OAUTH_CLIENT_ID"],
|
||||
secret: ["TODOIST_CLIENT_SECRET", "TODOIST_OAUTH_CLIENT_SECRET"],
|
||||
},
|
||||
calcom: {
|
||||
id: ["CALCOM_CLIENT_ID", "CALCOM_OAUTH_CLIENT_ID"],
|
||||
secret: ["CALCOM_CLIENT_SECRET", "CALCOM_OAUTH_CLIENT_SECRET"],
|
||||
},
|
||||
microsoft_365: {
|
||||
id: ["MICROSOFT_CLIENT_ID", "MICROSOFT_OAUTH_CLIENT_ID"],
|
||||
secret: ["MICROSOFT_CLIENT_SECRET", "MICROSOFT_OAUTH_CLIENT_SECRET"],
|
||||
},
|
||||
};
|
||||
const [idKey, secretKey] = map[slug];
|
||||
const clientId = Deno.env.get(idKey) ?? "";
|
||||
const clientSecret = Deno.env.get(secretKey) ?? "";
|
||||
const keys = map[slug];
|
||||
const clientId = readFirstEnv(keys.id);
|
||||
const clientSecret = readFirstEnv(keys.secret);
|
||||
if (!clientId || !clientSecret) {
|
||||
throw new Error(`Credenciais OAuth ausentes para ${slug} (${idKey}/${secretKey})`);
|
||||
throw new Error(
|
||||
`Credenciais OAuth ausentes para ${slug} (${keys.id.join(" ou ")}/${keys.secret.join(" ou ")})`,
|
||||
);
|
||||
}
|
||||
return { clientId, clientSecret, redirectUri };
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Monta a URL de autorização para iniciar o fluxo OAuth.
|
||||
*/
|
||||
|
|
@ -415,8 +441,9 @@ export async function revokeToken(
|
|||
}
|
||||
case "todoist": {
|
||||
// Todoist precisa client_id/secret + access_token no body
|
||||
const clientId = Deno.env.get("TODOIST_CLIENT_ID") ?? "";
|
||||
const clientSecret = Deno.env.get("TODOIST_CLIENT_SECRET") ?? "";
|
||||
const clientId = readFirstEnv(["TODOIST_CLIENT_ID", "TODOIST_OAUTH_CLIENT_ID"]);
|
||||
const clientSecret = readFirstEnv(["TODOIST_CLIENT_SECRET", "TODOIST_OAUTH_CLIENT_SECRET"]);
|
||||
|
||||
const body = new URLSearchParams({
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ Deno.serve(async (req) => {
|
|||
// 1. Checa dependências
|
||||
const { data: dependentJobs } = await admin
|
||||
.from("scheduled_jobs")
|
||||
.select("id, name")
|
||||
.select("id, name, status")
|
||||
.eq("user_id", userId)
|
||||
.neq("status", "archived")
|
||||
.contains("required_mcp_slugs", [slug]);
|
||||
|
|
@ -104,7 +104,8 @@ Deno.serve(async (req) => {
|
|||
}
|
||||
|
||||
let pausedCount = 0;
|
||||
if (dependentJobs && dependentJobs.length > 0 && force_pause_jobs) {
|
||||
const jobsToPause = (dependentJobs ?? []).filter((job) => job.status === "active");
|
||||
if (jobsToPause.length > 0 && force_pause_jobs) {
|
||||
const { error: pErr } = await admin
|
||||
.from("scheduled_jobs")
|
||||
.update({
|
||||
|
|
@ -112,11 +113,13 @@ Deno.serve(async (req) => {
|
|||
auto_paused_reason: `Integração ${slug} foi desconectada`,
|
||||
})
|
||||
.eq("user_id", userId)
|
||||
.eq("status", "active")
|
||||
.neq("status", "archived")
|
||||
.contains("required_mcp_slugs", [slug]);
|
||||
if (!pErr) pausedCount = dependentJobs.length;
|
||||
if (!pErr) pausedCount = jobsToPause.length;
|
||||
}
|
||||
|
||||
|
||||
// 2. Busca tokens ANTES de qualquer delete
|
||||
const accessToken = integ.access_token_vault_id
|
||||
? await getDecryptedSecret(admin, integ.access_token_vault_id)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue