import { ConflictingAppError } from "../common/errors" ; import generateId from "../common/generateId" ; import Usecase from "../common/Usecase" ; import { IApp , validateAppName } from "../entities/App" ; import { IConfiguration , validateConfiguration , } from "../entities/Configuration" ; import { Operation } from "../entities/OperationLog" ; export default class CreateApp extends Usecase { async exec ( partial ) { await this . authorizer . ensureCanCreateApp ( ) ; validateAppName ( partial . name ) ; if ( partial . defaultConfiguration ) { validateConfiguration ( partial . defaultConfiguration , "defaultConfiguration" ) ; } const conflictingAppExists = await this . storages . apps . oneExistsWithName ( partial . name ) ; if ( conflictingAppExists ) { throw new ConflictingAppError ( partial . name ) ; } const now = new Date ( ) ; const createdApp = await this . storages . apps . createOne ( { id : generateId ( ) , name : partial . name , defaultConfiguration : partial . defaultConfiguration || { } , createdAt : now , updatedAt : now , } ) ; await this . operationLogger . logOperation ( Operation . CreateApp , { createdApp , } ) ; return createdApp ; } }