building out next phase of sync

This commit is contained in:
C-West8
2026-10-22 22:42:07 -05:00
parent db4abbeefb
commit 42a1ce7d30
34 changed files with 934 additions and 107 deletions

View File

@@ -0,0 +1,125 @@
import { InvalidRequestError } from './core/errors.ts';
import type { Project, ProjectChanges } from './core/model.ts';
import { withStatus } from './core/plan.ts';
import type {
ItemRepository,
ProjectRepository,
SourceRootResolver,
TargetRootResolver
} from './core/ports.ts';
export const ROOTS_LOCKED =
'Roots are locked after the first sync. Reset the project to change them.';
export const BUSY = 'A sync is running for this project. Try again when it finishes.';
export interface NewProjectInput {
name: string;
sourceInput: string;
targetInput: string;
}
export interface ProjectEdit {
name: string;
enabled: boolean;
/** Blank or omitted leaves the root unchanged. */
sourceInput?: string;
targetInput?: string;
}
export interface ProjectServiceDeps {
projects: ProjectRepository;
items: ItemRepository;
sourceResolver: SourceRootResolver;
targetResolver: TargetRootResolver;
activity: { isRunning(projectId: string): boolean };
maxAttempts: number;
}
/** Admin operations on projects, enforcing the rules that keep sync state consistent. */
export class ProjectService {
constructor(private readonly deps: ProjectServiceDeps) {}
async create(input: NewProjectInput): Promise<Project> {
const [source, target] = await Promise.all([
this.deps.sourceResolver.resolveSourceRoot(required(input.sourceInput, 'SharePoint folder')),
this.deps.targetResolver.resolveTargetRoot(required(input.targetInput, 'ACC folder'))
]);
return this.deps.projects.create({
name: validName(input.name),
source: source.root,
sourceDisplayPath: source.displayPath,
target: target.root,
targetDisplayPath: target.displayPath
});
}
async update(id: string, edit: ProjectEdit): Promise<void> {
const project = await this.require(id);
const changes: ProjectChanges = { name: validName(edit.name), enabled: edit.enabled };
const sourceInput = edit.sourceInput?.trim();
const targetInput = edit.targetInput?.trim();
if ((sourceInput || targetInput) && project.firstSyncedAt) {
throw new InvalidRequestError(ROOTS_LOCKED);
}
if (sourceInput) {
const source = await this.deps.sourceResolver.resolveSourceRoot(sourceInput);
Object.assign(changes, { source: source.root, sourceDisplayPath: source.displayPath });
}
if (targetInput) {
const target = await this.deps.targetResolver.resolveTargetRoot(targetInput);
Object.assign(changes, { target: target.root, targetDisplayPath: target.displayPath });
}
await this.deps.projects.update(id, changes);
}
async resume(id: string): Promise<void> {
await this.require(id);
await this.deps.projects.resume(id);
}
/** Forget all sync state; the next run starts from a full listing and roots unlock. */
async reset(id: string): Promise<void> {
await this.requireIdle(id);
await this.deps.projects.reset(id);
}
async delete(id: string): Promise<void> {
await this.requireIdle(id);
await this.deps.projects.delete(id);
}
/** Give a failed item a fresh set of attempts on the next run. */
async retryItem(projectId: string, sourceId: string): Promise<void> {
await this.requireIdle(projectId);
const item = await this.deps.items.get(projectId, sourceId);
if (!item) throw new InvalidRequestError('That file is no longer tracked.');
if (item.status !== 'failed') return;
const fresh = withStatus({ ...item, attemptCount: 0, lastError: null }, this.deps.maxAttempts);
await this.deps.items.save(projectId, [fresh]);
}
private async require(id: string): Promise<Project> {
const project = await this.deps.projects.get(id);
if (!project) throw new InvalidRequestError('Project not found.');
return project;
}
private async requireIdle(id: string): Promise<Project> {
const project = await this.require(id);
if (this.deps.activity.isRunning(id)) throw new InvalidRequestError(BUSY);
return project;
}
}
function required(value: string, label: string): string {
const trimmed = value.trim();
if (!trimmed) throw new InvalidRequestError(`${label} is required.`);
return trimmed;
}
function validName(name: string): string {
const trimmed = required(name, 'Name');
if (trimmed.length > 100) throw new InvalidRequestError('Name must be 100 characters or fewer.');
return trimmed;
}