import type { Zone, ZoneAddLocationAction, ZoneChangeNameAction, ZoneDraft, ZoneRemoveLocationAction, ZoneSetDescriptionAction, ZoneSetKeyAction, ZoneUpdateAction, } from "@commercetools/platform-sdk"; import type { Config } from "#src/config.ts"; import { ZoneDraftSchema } from "#src/schemas/generated/zone.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 ZoneRepository extends AbstractResourceRepository<"zone"> { constructor(config: Config) { super("zone", config); this.actions = new ZoneUpdateHandler(config.storage); this.draftSchema = ZoneDraftSchema; } async create(context: RepositoryContext, draft: ZoneDraft): Promise { const resource: Zone = { ...getBaseResourceProperties(context.clientId), key: draft.key, locations: draft.locations || [], name: draft.name, description: draft.description, }; return await this.saveNew(context, resource); } } class ZoneUpdateHandler extends AbstractUpdateHandler implements Partial> { addLocation( context: RepositoryContext, resource: Writable, { location }: ZoneAddLocationAction, ) { resource.locations.push(location); } changeName( context: RepositoryContext, resource: Writable, { name }: ZoneChangeNameAction, ) { resource.name = name; } removeLocation( context: RepositoryContext, resource: Writable, { location }: ZoneRemoveLocationAction, ) { resource.locations = resource.locations.filter( (loc) => !(loc.country === location.country && loc.state === location.state), ); } setDescription( context: RepositoryContext, resource: Writable, { description }: ZoneSetDescriptionAction, ) { resource.description = description; } setKey( context: RepositoryContext, resource: Writable, { key }: ZoneSetKeyAction, ) { resource.key = key; } }