@profullstack/gov-contracts 0.1.0 → 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.
package/src/store.js CHANGED
@@ -8,6 +8,7 @@ import { DatabaseSync } from 'node:sqlite';
8
8
  import { createHash, randomBytes, randomUUID } from 'node:crypto';
9
9
  import { mkdirSync } from 'node:fs';
10
10
  import { dirname } from 'node:path';
11
+ import { nicheMatch } from './niche.js';
11
12
 
12
13
  let db;
13
14
 
@@ -111,6 +112,11 @@ CREATE TABLE IF NOT EXISTS runs (
111
112
  at TEXT NOT NULL
112
113
  );
113
114
  `);
115
+ // 0.2.0: niche match per solicitation (niche.js), refreshed when the profile changes.
116
+ const cols = new Set(db.prepare('PRAGMA table_info(solicitations)').all().map((c) => c.name));
117
+ if (!cols.has('niche_score')) db.exec('ALTER TABLE solicitations ADD COLUMN niche_score INTEGER');
118
+ if (!cols.has('niches')) db.exec("ALTER TABLE solicitations ADD COLUMN niches TEXT");
119
+ db.exec('CREATE INDEX IF NOT EXISTS sol_niche ON solicitations(niche_score)');
114
120
  return db;
115
121
  }
116
122
 
@@ -126,7 +132,7 @@ const sha = (s) => createHash('sha256').update(String(s)).digest('hex');
126
132
 
127
133
  function solRow(r) {
128
134
  if (!r) return null;
129
- return { ...r, contacts: pj(r.contacts, []), fit: pj(r.fit) };
135
+ return { ...r, contacts: pj(r.contacts, []), fit: pj(r.fit), ...(r.niches !== undefined ? { niches: pj(r.niches, []) } : {}) };
130
136
  }
131
137
 
132
138
  /**
@@ -139,21 +145,25 @@ export function upsertSolicitation(s) {
139
145
  const id = `${s.source}:${s.ext_id}`;
140
146
  const prev = d.prepare('SELECT * FROM solicitations WHERE id = ?').get(id);
141
147
  const t = now();
148
+ const m = nicheMatch({ title: s.title, description: s.description ?? prev?.description, naics: s.naics ?? prev?.naics });
142
149
  if (!prev) {
143
- d.prepare(`INSERT INTO solicitations (id, source, ext_id, number, title, agency, level, region, url, notice_type, posted_at, due_at, naics, set_aside, description, contacts, prefilter, status, first_seen, updated_at)
144
- VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`).run(
150
+ d.prepare(`INSERT INTO solicitations (id, source, ext_id, number, title, agency, level, region, url, notice_type, posted_at, due_at, naics, set_aside, description, contacts, prefilter, niche_score, niches, status, first_seen, updated_at)
151
+ VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`).run(
145
152
  id, s.source, String(s.ext_id), s.number ?? null, s.title, s.agency ?? null, s.level ?? null, s.region ?? null, s.url ?? null,
146
153
  s.notice_type ?? null, s.posted_at ?? null, s.due_at ?? null, s.naics ?? null, s.set_aside ?? null, s.description ?? null,
147
- j(s.contacts || []), s.prefilter ?? 0, s.prefilter > 0 ? 'new' : 'skipped', t, t,
154
+ j(s.contacts || []), s.prefilter ?? m.score, m.score, j(m.niches), (s.prefilter ?? m.score) > 0 ? 'new' : 'skipped', t, t,
148
155
  );
149
156
  return 'new';
150
157
  }
151
158
  const changed = (s.due_at ?? null) !== prev.due_at || ((s.description ?? null) !== null && s.description !== prev.description);
152
- if (!changed && prev.title === s.title) return 'same';
159
+ if (!changed && prev.title === s.title) {
160
+ if (prev.niche_score == null) d.prepare('UPDATE solicitations SET niche_score = ?, niches = ? WHERE id = ?').run(m.score, j(m.niches), id);
161
+ return 'same';
162
+ }
153
163
  d.prepare(`UPDATE solicitations SET number = ?, title = ?, agency = ?, url = ?, notice_type = ?, due_at = ?, naics = COALESCE(?, naics), set_aside = COALESCE(?, set_aside),
154
- description = COALESCE(?, description), contacts = ?, prefilter = ?, updated_at = ? WHERE id = ?`).run(
164
+ description = COALESCE(?, description), contacts = ?, prefilter = ?, niche_score = ?, niches = ?, updated_at = ? WHERE id = ?`).run(
155
165
  s.number ?? prev.number, s.title, s.agency ?? prev.agency, s.url ?? prev.url, s.notice_type ?? prev.notice_type, s.due_at ?? prev.due_at,
156
- s.naics ?? null, s.set_aside ?? null, s.description ?? null, j(s.contacts || pj(prev.contacts, [])), s.prefilter ?? prev.prefilter, t, id,
166
+ s.naics ?? null, s.set_aside ?? null, s.description ?? null, j(s.contacts || pj(prev.contacts, [])), s.prefilter ?? prev.prefilter, m.score, j(m.niches), t, id,
157
167
  );
158
168
  return 'updated';
159
169
  }
@@ -181,7 +191,18 @@ export function setSolicitation(id, fields) {
181
191
  }
182
192
 
183
193
  /** Open = not past due (or no due date). `q` matches title, agency, number and description. */
184
- export function searchSolicitations({ q, status, level, source, minFit, open = true, limit = 50, offset = 0, order = 'fit' } = {}) {
194
+ /**
195
+ * `niche`: 'in' = any of our niches, 'out' = none, or a niche name; anything
196
+ * else (or 'all') = no niche filter.
197
+ */
198
+ function nicheWhere(niche, where, args, col = '') {
199
+ if (!niche || niche === 'all') return;
200
+ if (niche === 'in') where.push(`${col}niche_score > 0`);
201
+ else if (niche === 'out') where.push(`COALESCE(${col}niche_score, 0) = 0`);
202
+ else { where.push(`EXISTS (SELECT 1 FROM json_each(${col}niches) WHERE value = ?)`); args.push(String(niche).slice(0, 80)); }
203
+ }
204
+
205
+ export function searchSolicitations({ q, status, level, source, minFit, niche, open = true, limit = 50, offset = 0, order = 'fit' } = {}) {
185
206
  const where = [];
186
207
  const args = [];
187
208
  if (q) {
@@ -193,9 +214,10 @@ export function searchSolicitations({ q, status, level, source, minFit, open = t
193
214
  if (level) { where.push('level = ?'); args.push(level); }
194
215
  if (source) { where.push('source = ?'); args.push(source); }
195
216
  if (minFit != null) { where.push('fit_score >= ?'); args.push(Number(minFit)); }
217
+ nicheWhere(niche, where, args);
196
218
  if (open) { where.push("(due_at IS NULL OR due_at >= ?)"); args.push(now().slice(0, 10)); }
197
- const ord = order === 'due' ? 'due_at IS NULL, due_at ASC' : order === 'new' ? 'first_seen DESC' : 'fit_score IS NULL, fit_score DESC, prefilter DESC, due_at ASC';
198
- const sql = `SELECT id, source, number, title, agency, level, region, url, notice_type, due_at, naics, set_aside, prefilter, status, fit_score, fit, first_seen
219
+ const ord = order === 'due' ? 'due_at IS NULL, due_at ASC' : order === 'new' ? 'first_seen DESC' : order === 'niche' ? 'niche_score DESC, due_at IS NULL, due_at ASC' : 'fit_score IS NULL, fit_score DESC, niche_score DESC, prefilter DESC, due_at ASC';
220
+ const sql = `SELECT id, source, number, title, agency, level, region, url, notice_type, due_at, naics, set_aside, prefilter, niche_score, niches, status, fit_score, fit, first_seen
199
221
  FROM solicitations ${where.length ? 'WHERE ' + where.join(' AND ') : ''} ORDER BY ${ord} LIMIT ? OFFSET ?`;
200
222
  const rows = openDb().prepare(sql).all(...args, Math.min(Number(limit) || 50, 200), Math.max(0, Number(offset) || 0));
201
223
  return rows.map(solRow);
@@ -207,6 +229,12 @@ export function dueFor(status, limit = 5) {
207
229
  ORDER BY prefilter DESC, due_at IS NULL, due_at ASC LIMIT ?`).all(status, now().slice(0, 10), limit).map(solRow);
208
230
  }
209
231
 
232
+ /** Open solicitations per niche name, most first. */
233
+ export function nicheCounts() {
234
+ return openDb().prepare(`SELECT value AS niche, COUNT(*) AS n FROM solicitations, json_each(solicitations.niches)
235
+ WHERE (due_at IS NULL OR due_at >= ?) GROUP BY value ORDER BY n DESC`).all(now().slice(0, 10));
236
+ }
237
+
210
238
  export function stats() {
211
239
  const d = openDb();
212
240
  const today = now().slice(0, 10);
@@ -217,6 +245,8 @@ export function stats() {
217
245
  by_status: by('status'),
218
246
  by_source: by('source'),
219
247
  by_level: by('level'),
248
+ in_niche: d.prepare('SELECT COUNT(*) AS n FROM solicitations WHERE niche_score > 0 AND (due_at IS NULL OR due_at >= ?)').get(today).n,
249
+ by_niche: Object.fromEntries(nicheCounts().map((r) => [r.niche, r.n])),
220
250
  drafts: d.prepare('SELECT COUNT(*) AS n FROM proposals').get().n,
221
251
  interventions_open: d.prepare("SELECT COUNT(*) AS n FROM interventions WHERE status = 'open'").get().n,
222
252
  last_runs: d.prepare('SELECT job, ok, detail, at FROM runs WHERE id IN (SELECT MAX(id) FROM runs GROUP BY job) ORDER BY job').all().map((r) => ({ ...r, ok: !!r.ok, detail: pj(r.detail, r.detail) })),
@@ -289,15 +319,22 @@ export function queueIntervention(iv) {
289
319
  return { id, created: true };
290
320
  }
291
321
 
292
- export function listInterventions({ status = 'open', kind, limit = 100 } = {}) {
322
+ export function listInterventions({ status = 'open', kind, niche, limit = 100 } = {}) {
293
323
  const where = [];
294
324
  const args = [];
295
325
  if (status && status !== 'all') { where.push('i.status = ?'); args.push(status); }
296
326
  if (kind) { where.push('i.kind = ?'); args.push(kind); }
297
- return openDb().prepare(`SELECT i.*, s.title AS solicitation_title, s.agency, s.number, s.fit_score
327
+ // Cards with no solicitation (source setup) are always in.
328
+ if (niche && niche !== 'all') {
329
+ const w = [];
330
+ nicheWhere(niche, w, args, 's.');
331
+ where.push(`(i.solicitation_id IS NULL OR ${w[0]})`);
332
+ }
333
+ return openDb().prepare(`SELECT i.*, s.title AS solicitation_title, s.agency, s.number, s.fit_score, s.niche_score, s.niches
298
334
  FROM interventions i LEFT JOIN solicitations s ON s.id = i.solicitation_id
299
335
  ${where.length ? 'WHERE ' + where.join(' AND ') : ''}
300
- ORDER BY i.status = 'open' DESC, i.priority DESC, i.due_at IS NULL, i.due_at ASC, i.created_at ASC LIMIT ?`).all(...args, Math.min(Number(limit) || 100, 500));
336
+ ORDER BY i.status = 'open' DESC, i.priority DESC, i.due_at IS NULL, i.due_at ASC, i.created_at ASC LIMIT ?`).all(...args, Math.min(Number(limit) || 100, 500))
337
+ .map((r) => ({ ...r, niches: pj(r.niches, []) }));
301
338
  }
302
339
 
303
340
  export function getIntervention(id) {
@@ -349,4 +386,5 @@ export function deleteSession(token) {
349
386
  export function _reset() {
350
387
  const d = openDb();
351
388
  for (const t of ['interventions', 'proposals', 'documents', 'solicitations', 'sessions', 'accounts', 'runs']) d.exec(`DELETE FROM ${t}`);
389
+ for (const t of ['profile', 'profile_pages', 'profile_pages_fts']) { try { d.exec(`DELETE FROM ${t}`); } catch { /* not created yet */ } }
352
390
  }