|
| 1 | +import { createAbstraction, type Result } from "@webiny/feature/api"; |
| 2 | +import type { IEventHandler } from "@webiny/api-core/features/eventPublisher/index.js"; |
| 3 | +import type { DomainEvent } from "@webiny/api-core/features/eventPublisher/index.js"; |
| 4 | +import type { WbPage } from "~/domain/page/abstractions.js"; |
| 5 | +import type { |
| 6 | + PageNotAuthorizedError, |
| 7 | + PageNotFoundError, |
| 8 | + PagePersistenceError |
| 9 | +} from "~/domain/page/errors.js"; |
| 10 | + |
| 11 | +// ============================================================================ |
| 12 | +// Type Definitions |
| 13 | +// ============================================================================ |
| 14 | + |
| 15 | +export interface IDeleteWbPageRevisionParams { |
| 16 | + id: string; |
| 17 | +} |
| 18 | + |
| 19 | +// ============================================================================ |
| 20 | +// Repository Abstraction |
| 21 | +// ============================================================================ |
| 22 | + |
| 23 | +export interface IDeletePageRevisionRepository { |
| 24 | + execute(params: IDeleteWbPageRevisionParams): Promise<Result<void, RepositoryError>>; |
| 25 | +} |
| 26 | + |
| 27 | +export interface IDeletePageRevisionRepositoryErrors { |
| 28 | + notFound: PageNotFoundError; |
| 29 | + persistence: PagePersistenceError; |
| 30 | +} |
| 31 | + |
| 32 | +type RepositoryError = |
| 33 | + IDeletePageRevisionRepositoryErrors[keyof IDeletePageRevisionRepositoryErrors]; |
| 34 | + |
| 35 | +export const DeletePageRevisionRepository = createAbstraction<IDeletePageRevisionRepository>( |
| 36 | + "Wb/DeletePageRevisionRepository" |
| 37 | +); |
| 38 | + |
| 39 | +export namespace DeletePageRevisionRepository { |
| 40 | + export type Interface = IDeletePageRevisionRepository; |
| 41 | + export type Params = IDeleteWbPageRevisionParams; |
| 42 | + export type Return = Promise<Result<void, RepositoryError>>; |
| 43 | + export type Error = RepositoryError; |
| 44 | +} |
| 45 | + |
| 46 | +// ============================================================================ |
| 47 | +// Use Case Abstraction |
| 48 | +// ============================================================================ |
| 49 | + |
| 50 | +export interface IDeletePageRevisionUseCase { |
| 51 | + execute(params: IDeleteWbPageRevisionParams): Promise<Result<void, UseCaseError>>; |
| 52 | +} |
| 53 | + |
| 54 | +export interface IDeletePageRevisionUseCaseErrors { |
| 55 | + notAuthorized: PageNotAuthorizedError; |
| 56 | + notFound: PageNotFoundError; |
| 57 | + persistence: PagePersistenceError; |
| 58 | +} |
| 59 | + |
| 60 | +type UseCaseError = IDeletePageRevisionUseCaseErrors[keyof IDeletePageRevisionUseCaseErrors]; |
| 61 | + |
| 62 | +/** Delete a single page revision. */ |
| 63 | +export const DeletePageRevisionUseCase = createAbstraction<IDeletePageRevisionUseCase>( |
| 64 | + "Wb/DeletePageRevisionUseCase" |
| 65 | +); |
| 66 | + |
| 67 | +export namespace DeletePageRevisionUseCase { |
| 68 | + export type Interface = IDeletePageRevisionUseCase; |
| 69 | + export type Params = IDeleteWbPageRevisionParams; |
| 70 | + export type Return = Promise<Result<void, UseCaseError>>; |
| 71 | + export type Error = UseCaseError; |
| 72 | +} |
| 73 | + |
| 74 | +// ============================================================================ |
| 75 | +// Event Payload Types |
| 76 | +// ============================================================================ |
| 77 | + |
| 78 | +export interface PageRevisionBeforeDeletePayload { |
| 79 | + page: WbPage; |
| 80 | +} |
| 81 | + |
| 82 | +export interface PageRevisionAfterDeletePayload { |
| 83 | + page: WbPage; |
| 84 | +} |
| 85 | + |
| 86 | +// ============================================================================ |
| 87 | +// Event Handler Abstractions |
| 88 | +// ============================================================================ |
| 89 | + |
| 90 | +/** Hook into page lifecycle before a page revision is deleted. */ |
| 91 | +export const PageRevisionBeforeDeleteEventHandler = createAbstraction< |
| 92 | + IEventHandler<DomainEvent<PageRevisionBeforeDeletePayload>> |
| 93 | +>("Wb/PageRevisionBeforeDeleteEventHandler"); |
| 94 | + |
| 95 | +export namespace PageRevisionBeforeDeleteEventHandler { |
| 96 | + export type Interface = IEventHandler<DomainEvent<PageRevisionBeforeDeletePayload>>; |
| 97 | + export type Event = DomainEvent<PageRevisionBeforeDeletePayload>; |
| 98 | + export type Page = WbPage; |
| 99 | +} |
| 100 | + |
| 101 | +/** Hook into page lifecycle after a page revision is deleted. */ |
| 102 | +export const PageRevisionAfterDeleteEventHandler = createAbstraction< |
| 103 | + IEventHandler<DomainEvent<PageRevisionAfterDeletePayload>> |
| 104 | +>("Wb/PageRevisionAfterDeleteEventHandler"); |
| 105 | + |
| 106 | +export namespace PageRevisionAfterDeleteEventHandler { |
| 107 | + export type Interface = IEventHandler<DomainEvent<PageRevisionAfterDeletePayload>>; |
| 108 | + export type Event = DomainEvent<PageRevisionAfterDeletePayload>; |
| 109 | + export type Page = WbPage; |
| 110 | +} |
0 commit comments