import {z} from 'zod' export type FireAntApiModelsIRIRCampaignChangeRequest = {type?: (1 | 2) | undefined, title: string, body: string, targetType?: (1 | 2 | 3) | undefined, linkPostId?: number | undefined, linkRoute?: string | undefined, scheduledDate?: string | undefined}; export type CreateApiIrSymbolCampaignsArgs = {symbol: string, body: FireAntApiModelsIRIRCampaignChangeRequest}; export type CreateApiIrSymbolCampaignsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fireAntApiModelsIRIRCampaignItem = z.object({campaignId: z.number().int().optional(), companyId: z.number().int().optional(), type: z.union([z.literal(1), z.literal(2)]).optional(), title: z.string().optional(), body: z.string().optional(), targetType: z.union([z.literal(1), z.literal(2), z.literal(3)]).optional(), linkPostId: z.number().int().optional(), linkRoute: z.string().optional(), scheduledDate: z.string().optional(), status: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5)]).optional(), sendMethod: z.union([z.literal(0), z.literal(1), z.literal(2)]).optional(), targetCount: z.number().int().optional(), reachableCount: z.number().int().optional(), sentCount: z.number().int().optional(), failedCount: z.number().int().optional(), postEngagement24h: z.number().int().optional(), hangfireJobId: z.string().optional(), quotaConsumed: z.boolean().optional(), lastError: z.string().optional(), createdBy: z.string().optional(), createdDate: z.string().optional(), sentDate: 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 }; /** * Tạo chiến dịch thông báo * * POST /ir/{symbol}/campaigns */ export const createApiIrSymbolCampaigns = async (args: CreateApiIrSymbolCampaignsArgs, options?: CreateApiIrSymbolCampaignsOptions) => { const {baseUrl = "https://api.fireant.vn", 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(/\/+$/, '')}/ir/${encodeURIComponent(String(args.symbol))}/campaigns${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(fireAntApiModelsIRIRCampaignItem, await res.json()) };