import { WebApiClient } from '../web-api/web-api-client'; import { Signable } from '../web-api/api-query'; import { TacSigningProcess, NoAuthSigningProcess, CaseMobileSigningProcess } from './signing-process'; import { FilledSigningObject } from './filled-signing-object'; import { SigningObject } from './signing-object'; /** * @export * @class RequestSigner */ export class RequestSigner { private client: WebApiClient private url: string; /** * Creates an instance of RequestSigner. * @param {WebApiClient} client * @param {string} url * * @memberOf RequestSigner */ constructor(client: WebApiClient, url: string) { this.client = client; this.url = url; } /** * Obtains current signing info with full details from the API. * @param {string} signId * @param {Signable} order * * @memberOf RequestSigner */ getSigningInfo = (signId: string, order: Signable): Promise => { return this.client.callApi(`${this.url}/sign/${signId}`, 'GET') .then(response => { var filled = new FilledSigningObject(response, this.client, this.url, order); // update signInfo property on order to FilledSigningObject order.signing = filled; return filled; }); } /** * Starts signing process with TAC (that usually means signing using OneTimePassword from SMS) * This method will inform the API that the client will sign using TAC. * @param {string} signId * @returns {Promise} * * @memberOf RequestSigner */ startSigningWithTac = (signId: string): Promise => { return this.client.callApi(`${this.url}/sign/${signId}`, 'POST', null, { authorizationType: 'TAC' }) .then(response => { var signingObject = new SigningObject(response, this.client, this.url); return new TacSigningProcess(this, signingObject); }); } /** * Starts signing process with CASE_MOBILE (that usually means signing using OneTimePassword from SMS) * This method will inform the API that the client will sign using CASE_MOBILE. * @param {string} signId * @returns {Promise} * * @memberOf RequestSigner */ startSigningWithCaseMobile = (signId: string): Promise => { return this.client.callApi(`${this.url}/sign/${signId}`, 'POST', null, { authorizationType: 'CASE_MOBILE' }) .then(response => { var signingObject = new SigningObject(response, this.client, this.url); return new CaseMobileSigningProcess(this, signingObject); }); } /** * Starts signing process with NO_AUTHORIZATION (that usually means signing the order just by clicking some button in UI) * This method signalizes the intent to the API that this order will be signed using NO_AUTHORIZATION method * @param {string} signId * @returns {Promise} * * @memberOf RequestSigner */ startSigningWithNoAuthorization = (signId: string): Promise => { return this.client.callApi(`${this.url}/sign/${signId}`, 'POST', null, { authorizationType: 'NO_AUTHORIZATION' }) .then(response => { var signingObject = new SigningObject(response, this.client, this.url); return new NoAuthSigningProcess(this, signingObject); }); } /** * Finishes signing process with TAC (that usually means signing using OneTimePassword from SMS) * You can call this method only if you successfully called `startSigningWithTAC` before. * @param {string} oneTimePassword * @param {string} signId * @returns {Promise} * * @memberOf RequestSigner */ finishSigningWithTac = (oneTimePassword: string, signId: string): Promise => this.client.callApi(`${this.url}/sign/${signId}`, 'PUT', null, { authorizationType: 'TAC', oneTimePassword }).then(response => new SigningObject(response, this.client, this.url)); /** * Finishes signing process with CASE_MOBILE (that usually means signing using OneTimePassword from SMS) * You can call this method only if you successfully called `startSigningWithCaseMobile` before. * @param {string} oneTimePassword * @param {string} signId * @returns {Promise} * * @memberOf RequestSigner */ finishSigningWithCaseMobile = (oneTimePassword: string, signId: string): Promise => this.client.callApi(`${this.url}/sign/${signId}`, 'PUT', null, { authorizationType: 'CASE_MOBILE', oneTimePassword }).then(response => new SigningObject(response, this.client, this.url)); /** * This method signalizes the API that the user confirmed signing the order by consent (usually by clicking some button in the UI). * You can call this method only if you successfully called `startSigningWithNoAuthorization` before. * @param {string} signId * @returns {Promise} * * @memberOf RequestSigner */ finishSigningWithNoAuth = (signId: string): Promise => this.client.callApi(`${this.url}/sign/${signId}`, 'PUT', null, { authorizationType: 'NO_AUTHORIZATION' }).then(response => new SigningObject(response, this.client, this.url)); /** * Calls API on client, updates state on passed FilledSigningObject and returns it in Promise */ // cancelSigning = (filled: FilledSigningObject): Promise => { // return this.client.callApi(this.url, 'DELETE') // .then(response => { // return new SigningObject(filled.order, this.client, this.url); // }); // } }