import {z} from 'zod' export type Rounding = 'UP' | 'DOWN' | 'NEAREST' | 'NONE'; export type Aggregation = 'SUM' | 'MIN' | 'MAX' | 'COUNT' | 'LATEST' | 'MEAN' | 'UNIQUE' | 'CUSTOM_SQL'; export type AggregationRequest = {version?: number | undefined, customFields?: {[key: string]: unknown} | undefined, name: string, rounding: Rounding, quantityPerUnit: number, unit: string, code?: string | undefined, accountingProductId?: string | undefined, meterId: string, targetField: string, aggregation: Aggregation, segmentedFields?: Array | undefined, segments?: Array<{[key: string]: string}> | undefined, defaultValue?: number | undefined, customSql?: string | undefined}; export type UpdateApiOrganizationsOrgIdAggregationsIdArgs = {orgId: string, id: string, body: AggregationRequest}; export type UpdateApiOrganizationsOrgIdAggregationsIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const rounding = z.enum(["UP", "DOWN", "NEAREST", "NONE"]); export const aggregation = z.enum(["SUM", "MIN", "MAX", "COUNT", "LATEST", "MEAN", "UNIQUE", "CUSTOM_SQL"]); export const aggregationResponse = z.object({id: z.string(), version: z.number().int().optional(), customFields: z.record(z.string(), z.unknown()).optional(), name: z.string().optional(), rounding: rounding.optional(), quantityPerUnit: z.number().optional(), unit: z.string().optional(), code: z.string().optional(), segments: z.array(z.record(z.string(), z.string())).optional(), accountingProductId: z.string().optional(), meterId: z.string().optional(), targetField: z.string().optional(), aggregation: aggregation.optional(), segmentedFields: z.array(z.string()).optional(), defaultValue: z.number().optional(), customSql: z.string().optional(), productId: z.string().optional(), dtCreated: z.string().optional(), dtLastModified: z.string().optional(), createdBy: z.string().optional(), lastModifiedBy: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Update Aggregation * * PUT /organizations/{orgId}/aggregations/{id} */ export const updateApiOrganizationsOrgIdAggregationsId = async (args: UpdateApiOrganizationsOrgIdAggregationsIdArgs, options?: UpdateApiOrganizationsOrgIdAggregationsIdOptions) => { const {baseUrl = "https://api.m3ter.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/organizations/${encodeURIComponent(String(args.orgId))}/aggregations/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(aggregationResponse, await res.json()) };