159 lines
5.3 KiB
TypeScript
159 lines
5.3 KiB
TypeScript
import { InvalidRequestError } from './core/errors.ts';
|
|
import { applyExclusions } from './core/exclusion.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 FolderSyncChoice {
|
|
/** Every folder the form offered. */
|
|
shown: string[];
|
|
/** The ones left ticked. */
|
|
synced: string[];
|
|
}
|
|
|
|
export interface ProjectServiceDeps {
|
|
projects: ProjectRepository;
|
|
items: ItemRepository;
|
|
sourceResolver: SourceRootResolver;
|
|
targetResolver: TargetRootResolver;
|
|
activity: { isRunning(projectId: number): 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: number, 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: number): 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: number): Promise<void> {
|
|
await this.requireIdle(id);
|
|
await this.deps.projects.reset(id);
|
|
}
|
|
|
|
async delete(id: number): Promise<void> {
|
|
await this.requireIdle(id);
|
|
await this.deps.projects.delete(id);
|
|
}
|
|
|
|
/**
|
|
* Saves which of the folders on the admin's form sync. Folders that weren't on the form
|
|
* (e.g. deleted in SharePoint since) keep their setting. The status view updates straight
|
|
* away; ACC catches up on the next run.
|
|
*/
|
|
async setFolderSync(id: number, choice: FolderSyncChoice): Promise<void> {
|
|
const project = await this.requireIdle(id);
|
|
const items = new Map(
|
|
(await this.deps.items.listByProject(id)).map((item) => [item.sourceId, item])
|
|
);
|
|
const shown = new Set(choice.shown);
|
|
const synced = new Set(choice.synced);
|
|
const excludedFolderIds = [
|
|
...project.excludedFolderIds.filter((folderId) => !shown.has(folderId)),
|
|
...[...shown].filter(
|
|
(folderId) => !synced.has(folderId) && items.get(folderId)?.desired.isFolder
|
|
)
|
|
];
|
|
|
|
await this.deps.projects.update(id, { excludedFolderIds });
|
|
const rootId = project.source.rootItemId;
|
|
const { maxAttempts } = this.deps;
|
|
await this.deps.items.save(id, applyExclusions(rootId, items, excludedFolderIds, maxAttempts));
|
|
}
|
|
|
|
/** Give a failed item a fresh set of attempts on the next run. */
|
|
async retryItem(projectId: number, 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: number): Promise<Project> {
|
|
const project = await this.deps.projects.get(id);
|
|
if (!project) throw new InvalidRequestError('Project not found.');
|
|
return project;
|
|
}
|
|
|
|
private async requireIdle(id: number): 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;
|
|
}
|