import { DefaultSettingsCrudOptions , Settings , SettingsStorageOperations , SettingsStorageOperationsCreateParams , SettingsStorageOperationsGetParams , SettingsStorageOperationsUpdateParams } from "@webiny/api-page-builder/types" ; import { Entity } from "dynamodb-toolbox" ; import { cleanupItem } from "@webiny/db-dynamodb/utils/cleanup" ; import WebinyError from "@webiny/error" ; const extractFromStorage = ( settings ) : Settings => { return { ... settings , tenant : ! settings . tenant ? false : settings . tenant , locale : ! settings . locale ? false : settings . locale } ; } ; const prepareForStorage = ( settings ) : Settings => { return { ... settings , tenant : ! settings . tenant ? null : settings . tenant , locale : ! settings . locale ? null : settings . locale } ; } ; /**
 * Because it is a possibility that tenant and locale are set as false (for the global settings) we must take
 * it in consideration and create the partition key for the global settings.
 */ interface PartitionKeyParams { tenant : string | boolean ; locale : string | boolean ; } const createPartitionKey = ( params ) : string => { const { tenant , locale } = params ; const parts = [ ] ; if ( tenant !== false ) { parts . push ( `T#${ tenant }` ) ; } if ( locale !== false ) { parts . push ( `L#${ locale }` ) ; } parts . push ( "PB#SETTINGS" ) ; return parts . join ( "#" ) ; } ; /**
 * We expect any object that has type property in it.
 * This way we can either receive a settings object or where conditions
 */ interface SortKeyParams { type : string ; } const createSortKey = ( params ) : string => { const { type } = params ; switch ( type ) { case "default" : return type ; default : throw new WebinyError ( "Unsupported type for the sort key." , "UNSUPPORTED_TYPE" , { type } ) ; } } ; const createType = ( ) : string => { return "pb.settings" ; } ; export interface Params { entity : Entity < any > ; } export const createSettingsStorageOperations = ( { entity } ) : SettingsStorageOperations => { const get = async ( params ) => { const { where } = params ; const keys = { PK : createPartitionKey ( where ) , SK : createSortKey ( where ) } ; try { const result = await entity . get ( keys ) ; if ( ! result || ! result . Item ) { return null ; } const settings = cleanupItem ( entity , result . Item ) ; return extractFromStorage ( settings ) ; } catch ( ex ) { throw new WebinyError ( ex . message || "Could not load settings record." , ex . code || "SETTINGS_GET_ERROR" , { keys } ) ; } } ; const create = async ( params ) => { const { settings } = params ; const keys = { PK : createPartitionKey ( settings ) , SK : createSortKey ( settings ) } ; try { await entity . put ( { ... prepareForStorage ( settings ) , TYPE : createType ( ) , ... keys } ) ; return settings ; } catch ( ex ) { throw new WebinyError ( ex . message || "Could not create settings record." , ex . code || "SETTINGS_CREATE_ERROR" , { settings , keys } ) ; } } ; const update = async ( params ) => { const { original , settings } = params ; const keys = { PK : createPartitionKey ( settings ) , SK : createSortKey ( settings ) } ; try { await entity . put ( { ... prepareForStorage ( settings ) , TYPE : createType ( ) , ... keys } ) ; return settings ; } catch ( ex ) { throw new WebinyError ( ex . message || "Could not update settings record." , ex . code || "SETTINGS_UPDATE_ERROR" , { original , settings , keys } ) ; } } ; /**
     * We can simply return the partition key for this storage operations.
     */ const createCacheKey = ( params ) : string => { return createPartitionKey ( params ) ; } ; return { get , create , update , createCacheKey } ; } ;