@davoodmehraban89 / Factor-easy / commits / fe3ab8a
Fork 0

سامانه صدور فاکتور و حسابداری

fix(finance): include archived identity baseline prerequisite

davoodmehraban89 committed Nov 15, 2026 at 13:01 UTC fe3ab8a4870bd3f808cfe5d94432c5d5c3db6361
3 files changed +56 -2
PROJECT_STATE.md
+6 -1
@@ -315,4 +315,9 @@ Owner requests layer-by-layer click/functional testing of all controls and compa
315 - Owner approved proceeding after the read-only audit. Created `integration/wp1-ledger-core-20261115` from current main `dab1a45fcfc4f12e675cfe055e477138cd3e99fa`; source candidate is PR195 head `1e03964aeeb0714130ca561f424916bd06259a2e`, stacked on stale PR194. No direct merge/cherry-pick of either stale PR.
316 - Port scope is intentionally DB-only: 16 candidate financial migrations (11 archived prerequisites + 5 additive corrections), 16 conservative DOWN files, 17 WP1 SQL regression fixtures, disposable PostgreSQL 17 bootstrap/harness, WP1 test runners and a dedicated disposable-ledger workflow. No client/UI sync, purchases UI, login/bootstrap, org/RBAC, generic Quality workflow, or PR194 bulk artifacts were ported.
317 - No Production database, migration, financial record, Auth/RLS/role/grant, deployment, or live service was changed. Docker is unavailable in the current execution environment, so no local PostgreSQL test was run. Exact-head GitHub Actions and independent SQL/ACL/RLS review are required before any merge consideration.
318 -- Explicit remaining blockers: review migration ordering/compatibility against current main baseline; exact-head CI; independent SQL/ACL/RLS review; real Auth/JWT/PostgREST and live schema parity; manual journal UI adapter; purchase correction/cancel transaction; cheque source adapter; entity-backed dimensions/account rules; source-by-source acceptance; performance; backup/restore; G1 rollout. Conservative DOWN scripts are not pristine-schema rollback. Guard defaults remain audit/shadow; no Production enforcement/cutover authorized.
\ No newline at end of file
318 +- Explicit remaining blockers: review migration ordering/compatibility against current main baseline; exact-head CI; independent SQL/ACL/RLS review; real Auth/JWT/PostgREST and live schema parity; manual journal UI adapter; purchase correction/cancel transaction; cheque source adapter; entity-backed dimensions/account rules; source-by-source acceptance; performance; backup/restore; G1 rollout. Conservative DOWN scripts are not pristine-schema rollback. Guard defaults remain audit/shadow; no Production enforcement/cutover authorized.
319 +
320 +### 2026-11-15 — WP1 first CI failure and baseline prerequisite repair
321 +- Exact PR206 head `e4e0e29b7bfcb207d007b4cac801be1ec1101dfe` ran the new disposable workflow. PostgreSQL 17 image setup passed; the baseline RED step failed before WP1 SQL execution. Captured artifact `wp1-baseline-red.zip` shows `public.handle_new_user()` references `profiles.phone`, but current main source migrations did not include historical `20261007165208_phone_identity_profile.sql`.
322 +- Root cause is a missing historical migration archive in current main, not a financial regression. The WP1 disposable harness already includes `20261007165208` in its selected migration allowlist, so port that one archived prerequisite from WP0 PR194; do not import unrelated PR194 changes. It is documented as already applied to Production and recovered read-only from migration history; repository archive addition is not a Production reapply.
323 +- No WP1 financial test stage ran on this first attempt. Next exact-head workflow must rerun after adding the archive. Do not mark WP1 green until all workflow steps pass.
\ No newline at end of file
docs/audits/WP1_DB_ONLY_INTEGRATION_CANDIDATE_2026-11-15_FA.md
+11 -1
@@ -36,4 +36,14 @@ Docker در محیط اجرایی فعلی نصب نیست؛ بنابراین ا
36
37 ## تصمیم
38
39 -این PR فقط یک **integration candidate برای review و CI** است، نه تأیید merge و نه مجوز اجرای Production. اگر CI یا بازبینی SQL/ACL شکست بخورد، تا رفع و تکرار آزمون‌ها ادغام ممنوع است.
\ No newline at end of file
39 +این PR فقط یک **integration candidate برای review و CI** است، نه تأیید merge و نه مجوز اجرای Production. اگر CI یا بازبینی SQL/ACL شکست بخورد، تا رفع و تکرار آزمون‌ها ادغام ممنوع است.
40 +
41 +## CI finding and prerequisite repair — 2026-11-15
42 +
43 +The first exact-head WP1 workflow run stopped in the baseline RED fixture before applying any WP1 migration. The captured PostgreSQL error was `column "phone" of relation "profiles" does not exist`, raised by the existing `public.handle_new_user()` trigger when the synthetic `auth.users` row was inserted.
44 +
45 +Root cause: the current main source tree does not contain the historical migration `20261007165208_phone_identity_profile.sql`, while the disposable WP1 harness explicitly includes this timestamp in its selected baseline migration set. The archived migration exists on the WP0 candidate branch and records that it was recovered read-only from `supabase_migrations.schema_migrations` on 2026-11-02 as already applied in Production.
46 +
47 +Repair: port only this single historical prerequisite migration from WP0 into this candidate. This is a source-history/schema-parity repair, not a WP1 financial migration and not an instruction to reapply it in Production. The harness already selects this exact migration timestamp; no test was weakened and no synthetic schema shortcut was introduced.
48 +
49 +The next exact-head CI run must prove the baseline stage progresses past auth-user bootstrap. The remaining WP1 SQL stages are still unverified until the workflow completes.
\ No newline at end of file
supabase/migrations/20261007165208_phone_identity_profile.sql new
+39
@@ -0,0 +1,39 @@
1 +-- Post-hoc archive of migration already applied to production as 20261007165208.
2 +-- Recovered read-only from supabase_migrations.schema_migrations on 2026-11-02.
3 +-- Do NOT re-apply this migration to production. Statement body below preserved verbatim.
4 +
5 +alter table public.profiles alter column email drop not null;
6 +alter table public.profiles add column if not exists phone text;
7 +create unique index if not exists profiles_phone_unique on public.profiles(phone) where phone is not null;
8 +
9 +create or replace function public.handle_new_user()
10 +returns trigger
11 +language plpgsql security definer set search_path=''
12 +as $$
13 +declare v_username text;v_org_name text;v_org_id uuid;v_identity text;
14 +begin
15 + v_identity:=coalesce(nullif(lower(new.email),''),nullif(new.phone,''));
16 + if v_identity is null then raise exception 'Finora requires an email or phone identity for signup';end if;
17 + v_username:=lower(coalesce(nullif(new.raw_user_meta_data->>'username',''),case when new.email is not null then split_part(new.email,'@',1) else regexp_replace(new.phone,'[^0-9+]','','g') end));
18 + v_org_name:=left(coalesce(nullif(trim(new.raw_user_meta_data->>'full_name'),''),nullif(trim(new.raw_user_meta_data->>'name'),''),nullif(v_username,''),'Finora')||' - شخصی',160);
19 + insert into public.profiles(id,email,phone,username,role,full_name)
20 + values(new.id,case when new.email is null then null else lower(new.email) end,new.phone,v_username,'user',nullif(trim(coalesce(new.raw_user_meta_data->>'full_name',new.raw_user_meta_data->>'name','')),''));
21 + insert into public.organizations(owner_user_id,name,status) values(new.id,v_org_name,'active') returning id into v_org_id;
22 + insert into public.organization_members(organization_id,user_id,status,is_owner) values(v_org_id,new.id,'active',true);
23 + insert into public.licenses(user_id,organization_id,plan,status,starts_at,ends_at,max_companies,max_users,modules,limits)
24 + values(new.id,v_org_id,'trial','trial',current_date,current_date+15,1,1,array['full_suite']::text[],'{}'::jsonb);
25 + return new;
26 +end $$;
27 +revoke all on function public.handle_new_user() from public,anon,authenticated;
28 +
29 +create or replace function public.update_my_identity_profile(new_username text,new_full_name text default null)
30 +returns void language plpgsql security definer set search_path='' as $$
31 +declare v_uid uuid:=auth.uid();v_username text:=lower(trim(coalesce(new_username,'')));
32 +begin
33 + if v_uid is null then raise exception 'Authentication required';end if;
34 + if v_username !~ '^[a-z0-9_.-]{3,40}$' then raise exception 'Username must be 3-40 latin letters, digits, dot, underscore or dash';end if;
35 + if exists(select 1 from public.profiles where lower(username)=v_username and id<>v_uid) then raise exception 'Username already in use';end if;
36 + update public.profiles set username=v_username,full_name=coalesce(nullif(trim(new_full_name),''),full_name) where id=v_uid;
37 +end $$;
38 +revoke all on function public.update_my_identity_profile(text,text) from public,anon;
39 +grant execute on function public.update_my_identity_profile(text,text) to authenticated;
\ No newline at end of file