mirror of
https://github.com/Luchexx/wacrm.git
synced 2026-11-02 09:19:51 +00:00
Merge pull request #211 from ArnasDon/fix/flows-pipelines-account-id
fix(account-sharing): stamp account_id on flow & pipeline inserts (#210)
This commit is contained in:
commit
934dcafffd
@ -27,6 +27,7 @@ import { Label } from "@/components/ui/label";
|
|||||||
import { GitBranch, Plus, ChevronDown, Settings } from "lucide-react";
|
import { GitBranch, Plus, ChevronDown, Settings } from "lucide-react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { useCan } from "@/hooks/use-can";
|
import { useCan } from "@/hooks/use-can";
|
||||||
|
import { useAuth } from "@/hooks/use-auth";
|
||||||
import { GatedButton } from "@/components/ui/gated-button";
|
import { GatedButton } from "@/components/ui/gated-button";
|
||||||
|
|
||||||
// Pipeline creation is admin-class (settings-tier write under
|
// Pipeline creation is admin-class (settings-tier write under
|
||||||
@ -47,6 +48,7 @@ export default function PipelinesPage() {
|
|||||||
const supabase = createClient();
|
const supabase = createClient();
|
||||||
const canEditSettings = useCan("edit-settings");
|
const canEditSettings = useCan("edit-settings");
|
||||||
const canCreateDeals = useCan("send-messages");
|
const canCreateDeals = useCan("send-messages");
|
||||||
|
const { accountId } = useAuth();
|
||||||
|
|
||||||
const [pipelines, setPipelines] = useState<Pipeline[]>([]);
|
const [pipelines, setPipelines] = useState<Pipeline[]>([]);
|
||||||
const [selectedPipelineId, setSelectedPipelineId] = useState<string>("");
|
const [selectedPipelineId, setSelectedPipelineId] = useState<string>("");
|
||||||
@ -111,10 +113,12 @@ export default function PipelinesPage() {
|
|||||||
} = await supabase.auth.getSession();
|
} = await supabase.auth.getSession();
|
||||||
const user = session?.user;
|
const user = session?.user;
|
||||||
if (!user) return null;
|
if (!user) return null;
|
||||||
|
// pipelines.account_id is NOT NULL post-017 with no DB default.
|
||||||
|
if (!accountId) return null;
|
||||||
|
|
||||||
const { data: pipeline, error } = await supabase
|
const { data: pipeline, error } = await supabase
|
||||||
.from("pipelines")
|
.from("pipelines")
|
||||||
.insert({ user_id: user.id, name: "Sales Pipeline" })
|
.insert({ user_id: user.id, account_id: accountId, name: "Sales Pipeline" })
|
||||||
.select()
|
.select()
|
||||||
.single();
|
.single();
|
||||||
|
|
||||||
@ -132,7 +136,7 @@ export default function PipelinesPage() {
|
|||||||
await supabase.from("pipeline_stages").insert(stagesPayload);
|
await supabase.from("pipeline_stages").insert(stagesPayload);
|
||||||
|
|
||||||
return pipeline as Pipeline;
|
return pipeline as Pipeline;
|
||||||
}, [supabase]);
|
}, [supabase, accountId]);
|
||||||
|
|
||||||
// Initial load + seed-if-empty
|
// Initial load + seed-if-empty
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -254,10 +258,16 @@ export default function PipelinesPage() {
|
|||||||
setCreating(false);
|
setCreating(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// pipelines.account_id is NOT NULL post-017 with no DB default.
|
||||||
|
if (!accountId) {
|
||||||
|
toast.error("Your profile is not linked to an account.");
|
||||||
|
setCreating(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const { data: pipeline, error } = await supabase
|
const { data: pipeline, error } = await supabase
|
||||||
.from("pipelines")
|
.from("pipelines")
|
||||||
.insert({ user_id: user.id, name })
|
.insert({ user_id: user.id, account_id: accountId, name })
|
||||||
.select()
|
.select()
|
||||||
.single();
|
.single();
|
||||||
|
|
||||||
|
|||||||
@ -49,7 +49,23 @@ export async function POST(request: Request) {
|
|||||||
if (!guard.ok) {
|
if (!guard.ok) {
|
||||||
return NextResponse.json(guard.body, { status: guard.status })
|
return NextResponse.json(guard.body, { status: guard.status })
|
||||||
}
|
}
|
||||||
const { userId } = guard
|
const { userId, supabase } = guard
|
||||||
|
|
||||||
|
// Resolve the caller's account_id — `flows.account_id` is NOT NULL
|
||||||
|
// post-017, so an INSERT without it trips the not-null constraint
|
||||||
|
// even though the admin client below bypasses RLS.
|
||||||
|
const { data: profile } = await supabase
|
||||||
|
.from('profiles')
|
||||||
|
.select('account_id')
|
||||||
|
.eq('user_id', userId)
|
||||||
|
.single()
|
||||||
|
const accountId = profile?.account_id as string | undefined
|
||||||
|
if (!accountId) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'Your profile is not linked to an account.' },
|
||||||
|
{ status: 403 },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const body = (await request.json().catch(() => null)) as
|
const body = (await request.json().catch(() => null)) as
|
||||||
| {
|
| {
|
||||||
@ -85,6 +101,7 @@ export async function POST(request: Request) {
|
|||||||
.from('flows')
|
.from('flows')
|
||||||
.insert({
|
.insert({
|
||||||
user_id: userId,
|
user_id: userId,
|
||||||
|
account_id: accountId,
|
||||||
name: body.name?.trim() || template.name,
|
name: body.name?.trim() || template.name,
|
||||||
description: template.description,
|
description: template.description,
|
||||||
status: 'draft',
|
status: 'draft',
|
||||||
@ -133,6 +150,7 @@ export async function POST(request: Request) {
|
|||||||
.from('flows')
|
.from('flows')
|
||||||
.insert({
|
.insert({
|
||||||
user_id: userId,
|
user_id: userId,
|
||||||
|
account_id: accountId,
|
||||||
name: body.name.trim(),
|
name: body.name.trim(),
|
||||||
description: body.description ?? null,
|
description: body.description ?? null,
|
||||||
status: 'draft',
|
status: 'draft',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user