Repository navigation
Expand file tree
/
Copy pathCreatePageRevisionFromRepository.ts
More file actions
56 lines (49 loc) · 2.43 KB
/
Copy pathCreatePageRevisionFromRepository.ts
File metadata and controls
56 lines (49 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Result } from "@webiny/feature/api";
import { CreateEntryRevisionFromUseCase } from "@webiny/api-headless-cms/features/contentEntry/CreateEntryRevisionFrom/index.js";
import { GetEntryByIdUseCase } from "@webiny/api-headless-cms/features/contentEntry/GetEntryById";
import { PageModel, type WbPage } from "~/domain/page/abstractions.js";
import { EntryToPageMapper } from "~/domain/page/EntryToPageMapper.js";
import {
PageNotFoundError,
PagePersistenceError,
PageValidationError
} from "~/domain/page/errors.js";
import { CreatePageRevisionFromRepository as RepositoryAbstraction } from "./abstractions.js";
class CreatePageRevisionFromRepositoryImpl implements RepositoryAbstraction.Interface {
constructor(
private createRevisionFrom: CreateEntryRevisionFromUseCase.Interface,
private getEntryById: GetEntryByIdUseCase.Interface,
private pageModel: PageModel.Interface
) {}
async execute(params: RepositoryAbstraction.Params): RepositoryAbstraction.Return {
// First, get the existing page to validate it exists
const getResult = await this.getEntryById.execute<WbPage>(this.pageModel, params.id);
if (getResult.isFail()) {
if (getResult.error.code === "Cms/Entry/NotFound") {
return Result.fail(new PageNotFoundError(params.id));
}
return Result.fail(new PagePersistenceError(getResult.error));
}
// Create revision from the existing page
const result = await this.createRevisionFrom.execute(this.pageModel, params.id, {
location: getResult.value.location,
values: params.values
});
if (result.isFail()) {
if (result.error.code === "Cms/Entry/ValidationError") {
return Result.fail(new PageValidationError(result.error.message));
}
if (result.error.code === "Cms/Entry/NotFound") {
return Result.fail(new PageNotFoundError(params.id));
}
return Result.fail(new PagePersistenceError(result.error));
}
// Map CmsEntry to WbPage domain type
const page = EntryToPageMapper.toPage(result.value);
return Result.ok(page);
}
}
export const CreatePageRevisionFromRepository = RepositoryAbstraction.createImplementation({
implementation: CreatePageRevisionFromRepositoryImpl,
dependencies: [CreateEntryRevisionFromUseCase, GetEntryByIdUseCase, PageModel]
});