Files
xboct-flow/src/commands/saveTaskRaw.ts
T

55 lines
1.4 KiB
TypeScript

import { errAsync, okAsync, ResultAsync } from "neverthrow";
import { applyRawTaskContent } from "@/dashboard/applyRawTaskContent";
import { appError, AppError, AppErrorVariant } from "@/error";
import { Task } from "@/model/task";
import { ITaskRepository } from "@/ports";
export type SaveTaskRawInput = {
folderPath?: string;
id?: string;
raw?: string;
};
export function saveTaskRaw(
repo: ITaskRepository,
input: SaveTaskRawInput,
): ResultAsync<Task, AppError> {
if (input.folderPath === undefined) {
return errAsync(appError(AppErrorVariant.NO_FOLDER));
}
if (input.id === undefined) {
return errAsync(appError(AppErrorVariant.NO_ID));
}
if (input.raw === undefined) {
return errAsync(appError(AppErrorVariant.NO_RAW));
}
const folderPath = input.folderPath;
const id = input.id;
const raw = input.raw;
return ResultAsync.fromSafePromise(repo.getById(folderPath, id)).andThen(
(existing) => {
if (!existing) {
return errAsync(appError(AppErrorVariant.NOT_FOUND, { id }));
}
const applied = applyRawTaskContent(raw, existing);
if (applied.isErr()) {
return errAsync(applied.error);
}
const next = applied.value;
return ResultAsync.fromSafePromise(repo.update(folderPath, next)).andThen(
() =>
ResultAsync.fromSafePromise(repo.getById(folderPath, id)).andThen(
(updated) =>
updated
? okAsync(updated)
: errAsync(appError(AppErrorVariant.NOT_FOUND, { id })),
),
);
},
);
}