import type { ProductSelection, ProductSelectionChangeNameAction, ProductSelectionDraft, ProductSelectionSetCustomTypeAction, ProductSelectionUpdateAction, } from "@commercetools/platform-sdk"; import type { Config } from "#src/config.ts"; import { ProductSelectionDraftSchema } from "#src/schemas/generated/product-selection.ts"; import { getBaseResourceProperties } from "../helpers.ts"; import type { Writable } from "../types.ts"; import type { RepositoryContext, UpdateHandlerInterface } from "./abstract.ts"; import { AbstractResourceRepository, AbstractUpdateHandler, } from "./abstract.ts"; export class ProductSelectionRepository extends AbstractResourceRepository<"product-selection"> { constructor(config: Config) { super("product-selection", config); this.actions = new ProductSelectionUpdateHandler(this._storage); this.draftSchema = ProductSelectionDraftSchema; } async create( context: RepositoryContext, draft: ProductSelectionDraft, ): Promise { const resource: ProductSelection = { ...getBaseResourceProperties(context.clientId), productCount: 0, key: draft.key, name: draft.name, mode: "Individual", }; return await this.saveNew(context, resource); } } class ProductSelectionUpdateHandler extends AbstractUpdateHandler implements Partial< UpdateHandlerInterface > { changeName( context: RepositoryContext, resource: Writable, { name }: ProductSelectionChangeNameAction, ) { resource.name = name; } async setCustomType( context: RepositoryContext, resource: Writable, { type, fields }: ProductSelectionSetCustomTypeAction, ) { await this._setCustomType(context, resource, { type, fields }); } }