import {z} from 'zod' export type ReportingUnit = {organizationId?: string | undefined, reportingUnitId?: string | undefined, controlReportingUnitId?: string | undefined, tags?: Array | undefined, label?: string | undefined, description?: string | undefined, resolution?: number | undefined, periodChangeStartYear?: number | undefined, periodChangeEndYear?: number | undefined, forestBaselineYear?: number | undefined, boundaryPath?: string | undefined, controlBoundaryPath?: string | undefined}; export type UpdateApiReportingUnitArgs = {body: ReportingUnit}; export type UpdateApiReportingUnitOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const reportingUnitEntry: z.ZodType = z.object({organizationId: z.string().optional(), reportingUnitId: z.string().optional(), branchId: z.string().optional(), controlReportingUnitId: z.string().optional(), controlForReportingUnitId: z.string().optional(), tags: z.array(z.string().min(1).max(255)).optional(), label: z.string().optional(), description: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), analysisSubmittedAt: z.string().optional(), analysisCompletedAt: z.string().optional(), qualityControlledAt: z.string().optional(), deletedAt: z.string().optional(), refundedAt: z.string().optional(), usersNotifiedAt: z.string().optional(), webhookNotifiedAt: z.string().optional(), periodChangeStartYear: z.number().int().optional(), periodChangeEndYear: z.number().int().optional(), boundaryPath: z.string().optional(), hidden: z.boolean().optional(), controlReportingUnit: z.lazy(() => reportingUnitEntry.optional()), areaKm2: z.number().optional(), analyzedAreaKm2: z.number().optional(), pointX: z.number().optional(), pointY: z.number().optional(), pointR: z.number().optional(), srcOrganizationId: z.string().optional(), dataPath: z.string().optional(), periodChangeStartYearStock: z.number().optional(), periodChangeStartYearStockError: z.number().optional(), periodChangeEndYearStock: z.number().optional(), periodChangeEndYearStockError: z.number().optional(), periodChange: z.number().optional(), periodChangeError: z.number().optional(), periodChangePercent: z.number().optional(), periodChangeGain: z.number().optional(), periodChangeGainError: z.number().optional(), periodChangeLoss: z.number().optional(), periodChangeLossError: z.number().optional(), dryrun: z.boolean().optional(), calibratedTo: z.string().optional(), calibratedFrom: z.string().optional(), mangrove: z.boolean().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 }; /** * Create or update a reporting unit. * * PUT /reportingUnit */ export const updateApiReportingUnit = async (args: UpdateApiReportingUnitArgs, options?: UpdateApiReportingUnitOptions) => { const {baseUrl = "https://app.chloris.earth/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/reportingUnit${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(reportingUnitEntry, await res.json()) };