78 lines
2.7 KiB
TypeScript
78 lines
2.7 KiB
TypeScript
import { getCollection } from '../../lib/datastore.js';
|
|
import type { FilterMap } from '@bytelyst/datastore';
|
|
import type { IntakeRuleDoc, IntakeJobDoc, IntakeJobStatus } from './types.js';
|
|
|
|
// ── Intake Rules ─────────────────────────────────────────────────
|
|
|
|
function rulesCollection() {
|
|
return getCollection<IntakeRuleDoc>('note_intake_rules', '/userId');
|
|
}
|
|
|
|
export async function createIntakeRule(doc: IntakeRuleDoc): Promise<IntakeRuleDoc> {
|
|
return rulesCollection().create(doc);
|
|
}
|
|
|
|
export async function getIntakeRule(id: string, userId: string): Promise<IntakeRuleDoc | null> {
|
|
return rulesCollection().findById(id, userId);
|
|
}
|
|
|
|
export async function listIntakeRules(
|
|
userId: string,
|
|
productId: string,
|
|
): Promise<IntakeRuleDoc[]> {
|
|
const filter: FilterMap = { productId };
|
|
// Fetch both user rules and built-in rules
|
|
const userRules = await rulesCollection().findMany({ filter: { ...filter, userId }, sort: { priority: 1 }, limit: 100, offset: 0 });
|
|
const builtinRules = await rulesCollection().findMany({ filter: { ...filter, userId: '__builtin__' }, sort: { priority: 1 }, limit: 100, offset: 0 });
|
|
return [...userRules, ...builtinRules];
|
|
}
|
|
|
|
export async function updateIntakeRule(
|
|
id: string,
|
|
userId: string,
|
|
updates: Partial<IntakeRuleDoc>,
|
|
): Promise<IntakeRuleDoc> {
|
|
return rulesCollection().update(id, userId, updates);
|
|
}
|
|
|
|
export async function deleteIntakeRule(id: string, userId: string): Promise<void> {
|
|
await rulesCollection().delete(id, userId);
|
|
}
|
|
|
|
// ── Intake Jobs ──────────────────────────────────────────────────
|
|
|
|
function jobsCollection() {
|
|
return getCollection<IntakeJobDoc>('note_intake_jobs', '/userId');
|
|
}
|
|
|
|
export async function createIntakeJob(doc: IntakeJobDoc): Promise<IntakeJobDoc> {
|
|
return jobsCollection().create(doc);
|
|
}
|
|
|
|
export async function getIntakeJob(id: string, userId: string): Promise<IntakeJobDoc | null> {
|
|
return jobsCollection().findById(id, userId);
|
|
}
|
|
|
|
export async function listIntakeJobs(
|
|
userId: string,
|
|
productId: string,
|
|
options?: { status?: IntakeJobStatus; since?: string; limit?: number; offset?: number },
|
|
): Promise<IntakeJobDoc[]> {
|
|
const filter: FilterMap = { userId, productId };
|
|
if (options?.status) filter.status = options.status;
|
|
return jobsCollection().findMany({
|
|
filter,
|
|
sort: { startedAt: -1 },
|
|
limit: options?.limit ?? 20,
|
|
offset: options?.offset ?? 0,
|
|
});
|
|
}
|
|
|
|
export async function updateIntakeJob(
|
|
id: string,
|
|
userId: string,
|
|
updates: Partial<IntakeJobDoc>,
|
|
): Promise<IntakeJobDoc> {
|
|
return jobsCollection().update(id, userId, updates);
|
|
}
|