UNPKG

@cityssm/consigno-cloud-api

Version:

An unofficial wrapper around the ConsignO Cloud API.

186 lines (139 loc) 4.29 kB
// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair /* eslint-disable sonarjs/class-name */ import Debug from 'debug' import { DEBUG_NAMESPACE } from '../../debug.config.js' import { type ConsignoCloudErrorJson, ConsignoCloudError } from '../../error.js' import type { ConsignoCloudAPIType } from '../../index.js' import type { ActionMode, AuthenticationMethodReference, CreateWorkflowStatus, Language, PDFAPolicy, SignerType } from '../../lookups.js' import type { ConsignoCloudResponseWorkflow } from './types.js' const debug = Debug(`${DEBUG_NAMESPACE}:workflows:createWorkflow`) // eslint-disable-next-line jsdoc/require-jsdoc export async function createWorkflow( this: ConsignoCloudAPIType, workflowDefinition: CreateWorkflowRequest ): Promise<ConsignoCloudResponseWorkflow> { await this.ensureActiveAuthToken() const endpointUrl = `${this.baseUrl}/workflows` debug('Endpoint URL:', endpointUrl) const response = await fetch(endpointUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Auth-Token': this.authToken ?? '' }, body: JSON.stringify(workflowDefinition) }) if (!response.ok) { const errorJson = (await response.json()) as ConsignoCloudErrorJson throw new ConsignoCloudError(errorJson) } this.updateAuthTokenLastUsedMillis() // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion return (await response.json()) as unknown as ConsignoCloudResponseWorkflow } interface Document_UploadAsData { name: string /** * The content of the document, base64 encoded. */ data: string } interface Document_PreviouslyUploaded { documentId: string } interface Signer_Type_Certifio { type: 'certifio' subjectDN: string } interface Signer_Type_Other { type: Exclude<(typeof SignerType)[keyof typeof SignerType], 'certifio'> } interface SignerAndContact_AMR_Secret { amr: Array< (typeof AuthenticationMethodReference)[keyof typeof AuthenticationMethodReference] > secretQuestion: string secretAnswer: string isSecretAnswerChanged: true } interface SignerAndContact_AMR_NoSecret { amr: Array< Exclude< (typeof AuthenticationMethodReference)[keyof typeof AuthenticationMethodReference], 'secret' > > } export interface CreateWorkflowAnchor { tag: string xOffset: number yOffset: number /** Default = 165 */ height?: number /* Default = 37 */ width?: number assignedTo: `${number}` page: string skipIfNotFound?: boolean } export interface CreateWorkflowRequest { name: string expiresOn: `${number}-${number}-${number}` | number pdfaPolicy: (typeof PDFAPolicy)[keyof typeof PDFAPolicy] documents: Array< (Document_PreviouslyUploaded | Document_UploadAsData) & { fields?: Array<{ x: number y: number /** Default = 165 */ height?: number /* Default = 37 */ width?: number assignedTo: `${number}` page: string }> anchors?: CreateWorkflowAnchor[] } > /** 0 = create, 1 = create and launch */ status: (typeof CreateWorkflowStatus)[keyof typeof CreateWorkflowStatus] actions: Array<{ mode?: (typeof ActionMode)[keyof typeof ActionMode] returnUrl?: string zoneLabel: string step: number ref: `${number}` signer: (Signer_Type_Certifio | Signer_Type_Other) & (SignerAndContact_AMR_NoSecret | SignerAndContact_AMR_Secret) & { firstName: string lastName: string email: string phone: string lang?: (typeof Language)[keyof typeof Language] clientUserId?: string role?: string placeHolder?: boolean } }> notifications: Array<{ contact: (SignerAndContact_AMR_NoSecret | SignerAndContact_AMR_Secret) & { firstName: string lastName: string email: string phone: string lang?: (typeof Language)[keyof typeof Language] } }> webhooks?: Array<{ url: string /** Default = false */ insecure?: boolean }> }