import {z} from 'zod' export type GetApiAppsGovFrV1ReportsSiloEntryIdArgs = {silo_entry_id: string}; export type GetApiAppsGovFrV1ReportsSiloEntryIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const reportingPeriod = z.object({id: z.string(), siren: z.string(), flux_kind: z.enum(["tx", "py"]), role: z.enum(["seller", "buyer", ""]), period_start: z.string(), period_end: z.string(), sequence: z.number().int(), previous_period_id: z.string().optional(), status: z.enum(["generated", "submitted", "filed", "rejected"]), flux_code: z.string().optional(), ack_ref: z.string().optional(), submitted_at: z.string().optional(), created_at: z.string(), updated_at: z.string()}); export const reportingKindStatus = z.object({kind: z.enum(["tx", "py"]), cadence: z.enum(["ten_day", "monthly", "bimonthly"]), open_period_start: z.string(), open_period_end: z.string(), next_due_period_start: z.string(), next_due_period_end: z.string(), next_due_deadline: z.string(), last_period_end: z.string().optional(), last_filed_period: reportingPeriod.optional()}); export const reportingSummary = z.object({silo_entry_id: z.string(), siren: z.string(), enabled: z.boolean(), regime: z.enum(["real_normal_monthly", "real_normal_quarterly", "simplified", "vat_franchise"]).optional(), kinds: z.array(reportingKindStatus)}); 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 }; /** * Fetch the e-reporting summary for a party * * GET /apps/gov-fr/v1/reports/{silo_entry_id} */ export const getApiAppsGovFrV1ReportsSiloEntryId = async (args: GetApiAppsGovFrV1ReportsSiloEntryIdArgs, options?: GetApiAppsGovFrV1ReportsSiloEntryIdOptions) => { const {baseUrl = "https://api.invopop.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/apps/gov-fr/v1/reports/${encodeURIComponent(String(args.silo_entry_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(reportingSummary, await res.json()) };