feat(projects): explicit organization (executor) selector
Backend:
- POST /api/projects accepts optional organizationId; default = active org
- GET /api/projects (list) and GET /:id include organization {id,name,shortName}
- /:id and /:id/documents no longer filter by active org — direct link works
across organizations; UI offers to switch active to project's org
Frontend:
- Project create modal: «Фирма (исполнитель)» dropdown, default = active
- Projects list shows «Фирма» column when user has >1 organization
- ProjectEdit shows org-banner with project's organization;
if active org differs, banner has «переключить активную на эту →» button
- ProjectEdit fetches BankAccounts from project's org (not active)
- Bumped clients fetch limit to 1000 to match API max
Org of an existing project cannot be changed (would orphan documents/lines);
to switch fenced — create new project under desired org.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -264,6 +264,7 @@ export type ProjectStatus = 'active' | 'completed' | 'cancelled';
|
||||
export type ProjectSummary = {
|
||||
id: string;
|
||||
organizationId: string;
|
||||
organization: { id: string; name: string; shortName: string | null } | null;
|
||||
name: string;
|
||||
status: ProjectStatus;
|
||||
defaultClientId: string | null;
|
||||
|
||||
@@ -37,14 +37,14 @@ export function ProjectEditPage() {
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [savedAt, setSavedAt] = useState<Date | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [orgId, setOrgId] = useState<string | null>(null);
|
||||
const [activeOrgId, setActiveOrgId] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!id) return;
|
||||
void Promise.all([
|
||||
api.get<Project>(`/api/projects/${id}`),
|
||||
api.get<{ items: DocumentSummary[] }>(`/api/projects/${id}/documents`),
|
||||
api.get<{ items: Client[] }>('/api/clients?limit=500'),
|
||||
api.get<{ items: Client[] }>('/api/clients?limit=1000'),
|
||||
api.get<{ items: DocumentTemplate[] }>('/api/templates'),
|
||||
api.get<{ id: string }>('/api/active-organization'),
|
||||
])
|
||||
@@ -54,13 +54,26 @@ export function ProjectEditPage() {
|
||||
setDocs(ds.items);
|
||||
setClients(cs.items);
|
||||
setTemplates(ts.items);
|
||||
setOrgId(ao.id);
|
||||
const ba = await api.get<{ items: BankAccount[] }>(`/api/organizations/${ao.id}/bank-accounts`);
|
||||
setActiveOrgId(ao.id);
|
||||
// Банк-счета и шаблоны проекта берём из ФИРМЫ ПРОЕКТА, а не из активной.
|
||||
const ba = await api.get<{ items: BankAccount[] }>(
|
||||
`/api/organizations/${p.organizationId}/bank-accounts`,
|
||||
);
|
||||
setBankAccounts(ba.items);
|
||||
})
|
||||
.catch((e) => setError(String(e)));
|
||||
}, [id]);
|
||||
|
||||
async function switchToProjectOrg() {
|
||||
if (!project) return;
|
||||
try {
|
||||
await api.post('/api/active-organization', { id: project.organizationId });
|
||||
window.location.reload();
|
||||
} catch (e) {
|
||||
setError(String(e));
|
||||
}
|
||||
}
|
||||
|
||||
async function save() {
|
||||
if (!id || !draft) return;
|
||||
setSaving(true);
|
||||
@@ -94,6 +107,8 @@ export function ProjectEditPage() {
|
||||
if (error && !project) return <main className="content"><div className="error-text">{error}</div></main>;
|
||||
if (!project) return <main className="content"><p className="hint">Загрузка…</p></main>;
|
||||
|
||||
const isCrossOrg = activeOrgId && project.organizationId && activeOrgId !== project.organizationId;
|
||||
|
||||
return (
|
||||
<main className="content">
|
||||
<header className="page-head">
|
||||
@@ -101,6 +116,16 @@ export function ProjectEditPage() {
|
||||
<Button onClick={() => navigate('/projects')}>← К проектам</Button>
|
||||
</header>
|
||||
|
||||
<div className="org-banner">
|
||||
<span className="hint">Фирма-исполнитель:</span>
|
||||
<b>{project.organization?.shortName || project.organization?.name || '—'}</b>
|
||||
{isCrossOrg ? (
|
||||
<Button variant="ghost" onClick={switchToProjectOrg}>
|
||||
переключить активную на эту →
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<section className="form-grid">
|
||||
<Field
|
||||
label="Название"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Link, useNavigate } from 'react-router-dom';
|
||||
import { api, ApiError, type ProjectSummary, type ProjectStatus } from '../api.js';
|
||||
import { api, ApiError, type Organization, type ProjectStatus, type ProjectSummary } from '../api.js';
|
||||
import { Button, EmptyState, Field, Modal, Select } from '../components/ui.js';
|
||||
|
||||
const STATUS_LABEL: Record<ProjectStatus, string> = {
|
||||
@@ -11,9 +11,11 @@ const STATUS_LABEL: Record<ProjectStatus, string> = {
|
||||
|
||||
export function ProjectsPage() {
|
||||
const [items, setItems] = useState<ProjectSummary[] | null>(null);
|
||||
const [orgs, setOrgs] = useState<Organization[]>([]);
|
||||
const [activeOrgId, setActiveOrgId] = useState<string | null>(null);
|
||||
const [status, setStatus] = useState<ProjectStatus | ''>('');
|
||||
const [q, setQ] = useState('');
|
||||
const [creating, setCreating] = useState<{ name: string } | null>(null);
|
||||
const [creating, setCreating] = useState<{ name: string; organizationId: string } | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [fieldErrors, setFieldErrors] = useState<Record<string, string>>({});
|
||||
const navigate = useNavigate();
|
||||
@@ -33,6 +35,18 @@ export function ProjectsPage() {
|
||||
|
||||
useEffect(() => { void load(); /* eslint-disable-next-line */ }, [status, q]);
|
||||
|
||||
useEffect(() => {
|
||||
void Promise.all([
|
||||
api.get<{ items: Organization[] }>('/api/organizations'),
|
||||
api.get<{ id: string }>('/api/active-organization'),
|
||||
])
|
||||
.then(([list, active]) => {
|
||||
setOrgs(list.items);
|
||||
setActiveOrgId(active.id);
|
||||
})
|
||||
.catch(() => {});
|
||||
}, []);
|
||||
|
||||
async function create() {
|
||||
if (!creating) return;
|
||||
if (!creating.name.trim()) {
|
||||
@@ -43,6 +57,7 @@ export function ProjectsPage() {
|
||||
try {
|
||||
const p = await api.post<ProjectSummary>('/api/projects', {
|
||||
name: creating.name.trim(),
|
||||
organizationId: creating.organizationId,
|
||||
status: 'active',
|
||||
defaultClientId: null,
|
||||
defaultTemplateId: null,
|
||||
@@ -72,11 +87,18 @@ export function ProjectsPage() {
|
||||
}
|
||||
}
|
||||
|
||||
const showOrgColumn = orgs.length > 1;
|
||||
|
||||
return (
|
||||
<main className="content">
|
||||
<header className="page-head">
|
||||
<h2>Проекты</h2>
|
||||
<Button variant="primary" onClick={() => setCreating({ name: '' })}>+ Новый проект</Button>
|
||||
<Button
|
||||
variant="primary"
|
||||
onClick={() => setCreating({ name: '', organizationId: activeOrgId ?? '' })}
|
||||
>
|
||||
+ Новый проект
|
||||
</Button>
|
||||
</header>
|
||||
|
||||
<div className="toolbar">
|
||||
@@ -105,13 +127,14 @@ export function ProjectsPage() {
|
||||
<p className="hint">Загрузка…</p>
|
||||
) : items.length === 0 ? (
|
||||
<EmptyState>
|
||||
Проектов пока нет. Создайте первый — внутри проекта зададите дефолтного клиента, шаблон и банк-счёт, чтобы создавать документы быстро.
|
||||
Проектов пока нет в активной фирме. Создай первый — внутри зададишь клиента по умолчанию, шаблон, банк-счёт.
|
||||
</EmptyState>
|
||||
) : (
|
||||
<table className="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Название</th>
|
||||
{showOrgColumn ? <th>Фирма</th> : null}
|
||||
<th>Клиент по умолчанию</th>
|
||||
<th>Шаблон</th>
|
||||
<th>Документов</th>
|
||||
@@ -123,6 +146,7 @@ export function ProjectsPage() {
|
||||
{items.map((p) => (
|
||||
<tr key={p.id}>
|
||||
<td><Link to={`/projects/${p.id}`}>{p.name}</Link></td>
|
||||
{showOrgColumn ? <td className="hint">{p.organization?.shortName || p.organization?.name || '—'}</td> : null}
|
||||
<td>{p.defaultClient?.name ?? '—'}</td>
|
||||
<td>{p.defaultTemplate?.name ?? '—'}</td>
|
||||
<td>{p._count?.documents ?? 0}</td>
|
||||
@@ -153,16 +177,35 @@ export function ProjectsPage() {
|
||||
}
|
||||
>
|
||||
{creating ? (
|
||||
<Field
|
||||
label="Название"
|
||||
value={creating.name}
|
||||
onChange={(e) => {
|
||||
setCreating({ name: e.target.value });
|
||||
if (fieldErrors.name) setFieldErrors((fe) => { const n = { ...fe }; delete n.name; return n; });
|
||||
}}
|
||||
placeholder="напр. Свадьба Ивановых, июнь 2026"
|
||||
error={fieldErrors.name}
|
||||
/>
|
||||
<div className="form-grid">
|
||||
<Field
|
||||
label="Название"
|
||||
value={creating.name}
|
||||
onChange={(e) => {
|
||||
setCreating({ ...creating, name: e.target.value });
|
||||
if (fieldErrors.name) setFieldErrors((fe) => { const n = { ...fe }; delete n.name; return n; });
|
||||
}}
|
||||
placeholder="напр. Свадьба Ивановых, июнь 2026"
|
||||
error={fieldErrors.name}
|
||||
/>
|
||||
<label className="field">
|
||||
<span className="field__label">Фирма (исполнитель)</span>
|
||||
<select
|
||||
className="field__input"
|
||||
value={creating.organizationId}
|
||||
onChange={(e) => setCreating({ ...creating, organizationId: e.target.value })}
|
||||
>
|
||||
{orgs.map((o) => (
|
||||
<option key={o.id} value={o.id}>
|
||||
{o.shortName || o.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<p className="hint" style={{ gridColumn: '1 / -1' }}>
|
||||
Документы внутри проекта будут выставляться от этой компании. После создания фирму поменять нельзя — заведи новый проект, если нужно из другой фирмы.
|
||||
</p>
|
||||
</div>
|
||||
) : null}
|
||||
</Modal>
|
||||
</main>
|
||||
|
||||
@@ -302,6 +302,19 @@ body {
|
||||
.import-banner { background: #14213d; border-color: #1e3a8a; color: #93c5fd; }
|
||||
}
|
||||
|
||||
/* === org banner (project page) === */
|
||||
.org-banner {
|
||||
display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
|
||||
padding: 8px 12px; margin-bottom: 16px;
|
||||
background: #f1f5f9; border: 1px solid #cbd5e1; border-radius: 6px;
|
||||
font-size: 14px;
|
||||
}
|
||||
.org-banner b { color: #1e3a8a; }
|
||||
@media (prefers-color-scheme: dark) {
|
||||
.org-banner { background: #1c1f24; border-color: #2a2e35; }
|
||||
.org-banner b { color: #93c5fd; }
|
||||
}
|
||||
|
||||
/* === inn lookup === */
|
||||
.inn-lookup { margin-top: 6px; display: flex; flex-direction: column; gap: 4px; }
|
||||
.inn-lookup__error { font-size: 12px; color: #c0392b; }
|
||||
|
||||
Reference in New Issue
Block a user