import { BundleNotFoundError , EntrypointNotFoundError } from "../common/errors" ; import Usecase from "../common/Usecase" ; import { IConfiguration , validateConfiguration , } from "../entities/Configuration" ; import { IEntrypoint } from "../entities/Entrypoint" ; import { Operation } from "../entities/OperationLog" ; export default class UpdateEntrypoint extends Usecase { async exec ( id , patch ) { const existingEntrypoint = await this . storages . entrypoints . findOne ( id ) ; if ( ! existingEntrypoint ) { throw new EntrypointNotFoundError ( id , "id" ) ; } await this . authorizer . ensureCanUpdateEntrypoint ( existingEntrypoint . urlMatcher ) ; if ( patch . configuration ) { validateConfiguration ( patch . configuration , "configuration" ) ; } if ( patch . bundleId ) { const linkedBundleExists = await this . storages . bundles . oneExistsWithId ( patch . bundleId ) ; if ( ! linkedBundleExists ) { throw new BundleNotFoundError ( patch . bundleId , "id" ) ; } } const updatedEntrypoint = await this . storages . entrypoints . updateOne ( id , { bundleId : patch . bundleId , redirectTo : patch . redirectTo , configuration : patch . configuration , updatedAt : new Date ( ) , } ) ; await this . operationLogger . logOperation ( Operation . UpdateEntrypoint , { oldEntrypoint : existingEntrypoint , newEntrypoint : updatedEntrypoint , } ) ; return updatedEntrypoint ; } }