38 lines
1.5 KiB
TypeScript
38 lines
1.5 KiB
TypeScript
import { bootstrapScript, describeTarget, runWithDb } from "./_bootstrap";
|
|
|
|
const workspaceId = process.argv[2];
|
|
const action = process.argv[3];
|
|
if (!workspaceId || !action || !["on", "off"].includes(action)) {
|
|
console.error("uso: npm run flag -- <workspace_id> on|off");
|
|
process.exit(1);
|
|
}
|
|
const enabled = action === "on";
|
|
|
|
bootstrapScript("set-flag");
|
|
|
|
runWithDb("set-flag", "admin", async (db) => {
|
|
const existing = await db.withService((query) =>
|
|
query.query<{ name: string }>(`select name from public.workspaces where id = $1`, [workspaceId]),
|
|
);
|
|
if (!existing[0]) {
|
|
throw new Error(`workspace ${workspaceId} não existe em ${describeTarget()}. Nada foi alterado.`);
|
|
}
|
|
await db.withService(async (query) => {
|
|
await query.query(
|
|
`insert into public.workspace_module_flags (workspace_id, module_key, enabled)
|
|
values ($1, 'spreadsheet_adapter', $2)
|
|
on conflict (workspace_id, module_key) do update set enabled = excluded.enabled, updated_at = now()`,
|
|
[workspaceId, enabled],
|
|
);
|
|
await query.query(
|
|
`insert into public.audit_log (workspace_id, actor_id, action, resource_type, resource_id, result, metadata)
|
|
select $1, m.user_id, 'module.flag_changed', 'workspace', $1, 'success', jsonb_build_object('enabled', $2::boolean)
|
|
from public.workspace_members m
|
|
where m.workspace_id = $1 and m.role = 'owner'
|
|
limit 1`,
|
|
[workspaceId, enabled],
|
|
);
|
|
});
|
|
console.log(`spreadsheet_adapter ${enabled ? "ligado" : "desligado"} para "${existing[0].name}" (${workspaceId})`);
|
|
});
|