@fayz-ai/financial 0.1.13 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ // src/data/sync-status.ts
2
+ var KNOWN = /* @__PURE__ */ new Set(["ok", "error", "pending", "revoked"]);
3
+ var SEVERITY = { error: 3, revoked: 2, pending: 1, ok: 0 };
4
+ function summarizeSyncStatus(rows) {
5
+ if (rows.length === 0) return null;
6
+ let lastSyncedAt = null;
7
+ let status = null;
8
+ let error = null;
9
+ for (const row of rows) {
10
+ if (row.lastSyncedAt && (!lastSyncedAt || Date.parse(row.lastSyncedAt) > Date.parse(lastSyncedAt))) lastSyncedAt = row.lastSyncedAt;
11
+ const s = row.lastSyncStatus && KNOWN.has(row.lastSyncStatus) ? row.lastSyncStatus : null;
12
+ if (!s) continue;
13
+ if (!status || SEVERITY[s] > SEVERITY[status]) {
14
+ status = s;
15
+ error = s === "error" ? row.lastSyncError ?? null : null;
16
+ }
17
+ }
18
+ return { lastSyncedAt, status, error };
19
+ }
20
+
21
+ export { summarizeSyncStatus };
22
+ //# sourceMappingURL=chunk-3QNU7XUB.js.map
23
+ //# sourceMappingURL=chunk-3QNU7XUB.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/data/sync-status.ts"],"names":[],"mappings":";AA2BA,IAAM,KAAA,uBAAiC,GAAA,CAAI,CAAC,MAAM,OAAA,EAAS,SAAA,EAAW,SAAS,CAAC,CAAA;AAEhF,IAAM,QAAA,GAA+C,EAAE,KAAA,EAAO,CAAA,EAAG,SAAS,CAAA,EAAG,OAAA,EAAS,CAAA,EAAG,EAAA,EAAI,CAAA,EAAE;AAExF,SAAS,oBAAoB,IAAA,EAA4D;AAC9F,EAAA,IAAI,IAAA,CAAK,MAAA,KAAW,CAAA,EAAG,OAAO,IAAA;AAC9B,EAAA,IAAI,YAAA,GAA8B,IAAA;AAClC,EAAA,IAAI,MAAA,GAAoC,IAAA;AACxC,EAAA,IAAI,KAAA,GAAuB,IAAA;AAC3B,EAAA,KAAA,MAAW,OAAO,IAAA,EAAM;AAEtB,IAAA,IAAI,GAAA,CAAI,YAAA,KAAiB,CAAC,YAAA,IAAgB,KAAK,KAAA,CAAM,GAAA,CAAI,YAAY,CAAA,GAAI,IAAA,CAAK,KAAA,CAAM,YAAY,CAAA,CAAA,iBAAmB,GAAA,CAAI,YAAA;AACvH,IAAA,MAAM,CAAA,GAAI,IAAI,cAAA,IAAkB,KAAA,CAAM,IAAI,GAAA,CAAI,cAAc,CAAA,GAAK,GAAA,CAAI,cAAA,GAAwC,IAAA;AAC7G,IAAA,IAAI,CAAC,CAAA,EAAG;AACR,IAAA,IAAI,CAAC,MAAA,IAAU,QAAA,CAAS,CAAC,CAAA,GAAI,QAAA,CAAS,MAAM,CAAA,EAAG;AAC7C,MAAA,MAAA,GAAS,CAAA;AACT,MAAA,KAAA,GAAQ,CAAA,KAAM,OAAA,GAAU,GAAA,CAAI,aAAA,IAAiB,IAAA,GAAO,IAAA;AAAA,IACtD;AAAA,EACF;AACA,EAAA,OAAO,EAAE,YAAA,EAAc,MAAA,EAAQ,KAAA,EAAM;AACvC","file":"chunk-3QNU7XUB.js","sourcesContent":["// ---------------------------------------------------------------------------\n// The last pull from the bank, folded across statement sources.\n//\n// An account can carry more than one active source, and the header without an\n// account picked speaks for all of them. The rule: one failing source makes the\n// whole answer an error — a green \"synced 3 min ago\" that hides a broken feed is\n// the exact lie the status is there to prevent — and the time shown is the most\n// recent pull.\n// ---------------------------------------------------------------------------\n\nexport type StatementSyncState = 'ok' | 'error' | 'pending' | 'revoked'\n\nexport interface StatementSyncStatus {\n /** When the last pull ran; null when no source has pulled yet. */\n lastSyncedAt: string | null\n /** Null when no source has reported yet. */\n status: StatementSyncState | null\n /** The provider's reason, when `status` is 'error'. */\n error: string | null\n}\n\nexport interface StatementSourceSyncRow {\n lastSyncedAt?: string | null\n lastSyncStatus?: string | null\n lastSyncError?: string | null\n}\n\nconst KNOWN: ReadonlySet<string> = new Set(['ok', 'error', 'pending', 'revoked'])\n/** Worst first: the status the merchant must act on wins. */\nconst SEVERITY: Record<StatementSyncState, number> = { error: 3, revoked: 2, pending: 1, ok: 0 }\n\nexport function summarizeSyncStatus(rows: StatementSourceSyncRow[]): StatementSyncStatus | null {\n if (rows.length === 0) return null\n let lastSyncedAt: string | null = null\n let status: StatementSyncState | null = null\n let error: string | null = null\n for (const row of rows) {\n // Compared as instants: PostgREST and the mock do not agree on the offset spelling.\n if (row.lastSyncedAt && (!lastSyncedAt || Date.parse(row.lastSyncedAt) > Date.parse(lastSyncedAt))) lastSyncedAt = row.lastSyncedAt\n const s = row.lastSyncStatus && KNOWN.has(row.lastSyncStatus) ? (row.lastSyncStatus as StatementSyncState) : null\n if (!s) continue\n if (!status || SEVERITY[s] > SEVERITY[status]) {\n status = s\n error = s === 'error' ? row.lastSyncError ?? null : null\n }\n }\n return { lastSyncedAt, status, error }\n}\n"]}
@@ -1,16 +1,17 @@
1
+ import { T } from './chunk-4FWWKKZB.js';
1
2
  import { hasCriteriaValue } from './chunk-J7BWATQU.js';
2
3
  import { ensureReferenceRates } from './chunk-4S5GK776.js';
3
4
  import { createIdempotencyKey } from './chunk-IP5YH4S5.js';
4
5
  import { NAME_AMOUNT_TOLERANCE, matchStatementLines, scorePair } from './chunk-ZFUQBYBG.js';
5
6
  import { emptyStatement, ledgerBalance, buildStatement } from './chunk-HJPUHTI2.js';
6
7
  import { transferLegs } from './chunk-YUNLQYOZ.js';
7
- import { cancellationReason, invoiceTotal, itemsDescription, summarizeInvoiceLines, lineTotal } from './chunk-5RMPZIMO.js';
8
+ import { cancellationReason, invoiceTotal, itemsDescription, summarizeInvoiceLines, lineTotal } from './chunk-ZWPYYIZW.js';
8
9
  import { paymentFee, ledgerMethod, toLedgerDirection, fromLedgerDirection } from './chunk-4GXBOEI3.js';
9
10
  import { sessionCashFlow } from './chunk-56NQHYL7.js';
10
11
  import { runReconcileAsBill, runReconcileLink } from './chunk-FNXIAIUZ.js';
11
12
  import { transferForStatementLine, statementLegOf, candidateTolerance, candidateScore, statementLinesPage, pageTotals } from './chunk-MCSPCXQK.js';
12
13
  import { round2 } from './chunk-XVELDCSL.js';
13
- import { T } from './chunk-4FWWKKZB.js';
14
+ import { summarizeSyncStatus } from './chunk-3QNU7XUB.js';
14
15
  import { getActiveTenantId, getSupabaseClientOptional } from '@fayz-ai/core';
15
16
 
16
17
  var INVESTMENT_CODES = ["INVESTMENTS", "AUTOMATICINVESTMENT", "PROCEEDSINTERESTSANDDIVIDENDS", "MUTUALFUNDS", "FIXEDINCOME"];
@@ -251,6 +252,7 @@ function rowToInvoiceLine(r) {
251
252
  totalInstallments: Number(r.installment_count) || 1,
252
253
  contactId: r.counterparty_person_id ?? void 0,
253
254
  contactName: r.counterparty_name ?? r.metadata?.contactName,
255
+ priceTableId: r.metadata?.priceTableId ?? void 0,
254
256
  observations: r.notes ?? void 0,
255
257
  itemsSummary: r.description ?? r.metadata?.itemsSummary,
256
258
  unitId: r.unit_id ?? void 0,
@@ -275,6 +277,7 @@ function rowToInvoice(r) {
275
277
  totalInstallments: Number(r.installment_count) || 1,
276
278
  contactId: r.counterparty_person_id ?? void 0,
277
279
  contactName: r.counterparty_name ?? r.metadata?.contactName,
280
+ priceTableId: r.metadata?.priceTableId ?? void 0,
278
281
  observations: r.notes ?? void 0,
279
282
  itemsSummary: r.description ?? r.metadata?.itemsSummary,
280
283
  unitId: r.unit_id ?? void 0,
@@ -677,7 +680,8 @@ function createSupabaseFinancialProvider() {
677
680
  p_metadata: {
678
681
  contactName: input.contactName,
679
682
  ...input.fiscalNumber ? { fiscalNumber: input.fiscalNumber } : {},
680
- ...input.currency ? { currency: input.currency } : {}
683
+ ...input.currency ? { currency: input.currency } : {},
684
+ ...input.priceTableId ? { priceTableId: input.priceTableId } : {}
681
685
  },
682
686
  p_idempotency_key: genId()
683
687
  });
@@ -719,12 +723,13 @@ function createSupabaseFinancialProvider() {
719
723
  if (partial.contactId !== void 0) row.counterparty_person_id = partial.contactId ?? null;
720
724
  if (partial.unitId !== void 0) row.unit_id = partial.unitId ?? null;
721
725
  if (partial.totalAmount !== void 0) row.total = partial.totalAmount;
722
- if (partial.contactName !== void 0 || "fiscalNumber" in partial) {
726
+ if (partial.contactName !== void 0 || "fiscalNumber" in partial || "priceTableId" in partial) {
723
727
  const { data: cur } = await pub.from("plg_financial_invoices").select("metadata").eq("id", id).maybeSingle();
724
728
  row.metadata = {
725
729
  ...cur?.metadata ?? {},
726
730
  ...partial.contactName !== void 0 ? { contactName: partial.contactName } : {},
727
- ..."fiscalNumber" in partial ? { fiscalNumber: partial.fiscalNumber ?? null } : {}
731
+ ..."fiscalNumber" in partial ? { fiscalNumber: partial.fiscalNumber ?? null } : {},
732
+ ..."priceTableId" in partial ? { priceTableId: partial.priceTableId ?? null } : {}
728
733
  };
729
734
  }
730
735
  if (Object.keys(row).length > 0) {
@@ -2401,6 +2406,18 @@ function createSupabaseFinancialProvider() {
2401
2406
  errors: results.map((r) => r.error).filter((e) => Boolean(e))
2402
2407
  };
2403
2408
  },
2409
+ async getStatementSyncStatus(input) {
2410
+ const { pub } = getClients();
2411
+ let query = pub.from(T.statementSources).select("last_synced_at,last_sync_status,last_sync_error").eq("is_active", true);
2412
+ if (input?.bankAccountId) query = query.eq("account_id", input.bankAccountId);
2413
+ const { data, error } = await query;
2414
+ if (error) throw error;
2415
+ return summarizeSyncStatus((data ?? []).map((r) => ({
2416
+ lastSyncedAt: r.last_synced_at,
2417
+ lastSyncStatus: r.last_sync_status,
2418
+ lastSyncError: r.last_sync_error
2419
+ })));
2420
+ },
2404
2421
  async suggestReconciliationBatch(lines) {
2405
2422
  const { core, pub } = getClients();
2406
2423
  const abertas = lines.filter((line) => !line.reconciledAt && (Number(line.amount) || 0) > 0);
@@ -2806,5 +2823,5 @@ function createSupabaseFinancialProvider() {
2806
2823
  }
2807
2824
 
2808
2825
  export { createSupabaseFinancialProvider, invoiceLineOrFilter };
2809
- //# sourceMappingURL=chunk-RI4IUJS4.js.map
2810
- //# sourceMappingURL=chunk-RI4IUJS4.js.map
2826
+ //# sourceMappingURL=chunk-5OIB54V6.js.map
2827
+ //# sourceMappingURL=chunk-5OIB54V6.js.map