import type { AttributeGroup, AttributeGroupChangeNameAction, AttributeGroupDraft, AttributeGroupSetAttributesAction, AttributeGroupSetDescriptionAction, AttributeGroupSetKeyAction, AttributeGroupUpdateAction, } from "@commercetools/platform-sdk"; import type { Config } from "#src/config.ts"; import { AttributeGroupDraftSchema } from "#src/schemas/generated/attribute-group.ts"; import { getBaseResourceProperties } from "../helpers.ts"; import type { Writable } from "../types.ts"; import type { UpdateHandlerInterface } from "./abstract.ts"; import { AbstractResourceRepository, AbstractUpdateHandler, type RepositoryContext, } from "./abstract.ts"; export class AttributeGroupRepository extends AbstractResourceRepository<"attribute-group"> { constructor(config: Config) { super("attribute-group", config); this.actions = new AttributeGroupUpdateHandler(this._storage); this.draftSchema = AttributeGroupDraftSchema; } async create( context: RepositoryContext, draft: AttributeGroupDraft, ): Promise { const resource: AttributeGroup = { ...getBaseResourceProperties(context.clientId), name: draft.name, description: draft.description, key: draft.key, attributes: draft.attributes, }; return await this.saveNew(context, resource); } } class AttributeGroupUpdateHandler extends AbstractUpdateHandler implements Partial> { changeName( _context: RepositoryContext, resource: Writable, { name }: AttributeGroupChangeNameAction, ) { resource.name = name; } setAttributes( _context: RepositoryContext, resource: Writable, { attributes }: AttributeGroupSetAttributesAction, ) { resource.attributes = attributes; } setDescription( _context: RepositoryContext, resource: Writable, { description }: AttributeGroupSetDescriptionAction, ) { resource.description = description; } setKey( _context: RepositoryContext, resource: Writable, { key }: AttributeGroupSetKeyAction, ) { resource.key = key; } }