Skip to content

Commit

Permalink
feat(service-account): add a static method to create signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Coeurderoy committed Sep 17, 2020
1 parent b246840 commit 2761229
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/core/Folder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { IFolder, ISignature, FolderState, PostLegalDocumentDTO, MultiResourceCreationResponse, LegalFile } from '../lib';
import { IFolder, FolderState, PostLegalDocumentDTO, MultiResourceCreationResponse, LegalFile } from '../lib';
import { RequestBuilder } from '../RequestBuilder';
import { LegalDocument } from './LegalDocument';
import { SupportingDocument } from './SupportingDocument';
import { Signature } from '.';

/**
* Folder instance
Expand Down Expand Up @@ -35,7 +36,10 @@ export class Folder implements IFolder {

public legalDocuments: LegalDocument[];

public signatures: ISignature[];
/**
* Signature instances attached to the folder
*/
public signatures: Signature[];

/**
* Define the global folder status
Expand All @@ -50,7 +54,7 @@ export class Folder implements IFolder {
this.expiredAt = params.expiredAt;
this.updatedAt = params.updatedAt;
this.lastFileUploadedAt = params.lastFileUploadedAt;
this.signatures = params.signatures ?? [];
this.signatures = params.signatures?.map((signature) => new Signature(this.id, signature, requestBuilder)) ?? [];
this.state = params.state;
this.legalDocuments = params.legalDocuments.map((doc) => new LegalDocument(doc, this.id, requestBuilder));
this.supportingDocuments = params.supportingDocuments.map((doc) => new SupportingDocument(doc, this.id));
Expand Down
5 changes: 4 additions & 1 deletion src/core/LegalDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export class LegalDocument extends Document implements ILegalDocument {

return {
...res,
elements: res.elements.map((element) => ({ ...element, resource: new LegalDocument(element.resource, folderId, requestBuilder) })),
elements: res.elements.map((element) => ({
...element,
resource: new LegalDocument(element.resource, folderId, requestBuilder),
})),
};
}

Expand Down
12 changes: 11 additions & 1 deletion src/core/ServiceAccount.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { RequestBuilder } from '../RequestBuilder';
import { IServiceAccount } from '..';
import { PostSubscriptionDTO } from '../lib';
import { PostSignatureDTO, PostSubscriptionDTO } from '../lib';
import { Subscription } from './Subscription';
import { BanksUser } from './BanksUser';
import { Application } from './Application';
import { Signature } from './Signature';

/**
* Service account class
Expand Down Expand Up @@ -136,4 +137,13 @@ export class ServiceAccount {
public async getApplicationById(id: string): Promise<Application> {
return Application.getApplicationById(id, this.requestBuilder);
}

/**
* Create a signature from a given folder id
* @param folderId Unique folder identifier
* @param signatureBody Signature instance to create
*/
public async createSignature(folderId: string, signatureBody: PostSignatureDTO): Promise<Signature> {
return Signature.create(this.requestBuilder, folderId, signatureBody);
}
}
2 changes: 1 addition & 1 deletion src/lib/Algoan.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
LegalDocumentCategory,
LegalFileType,
RejectionCode,
SignatureState
SignatureState,
} from './Folder.interface';

/**
Expand Down
42 changes: 41 additions & 1 deletion test/service-account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { RequestBuilder } from '../src/RequestBuilder';
import { ServiceAccount } from '../src/core/ServiceAccount';
import { Subscription } from '../src/core/Subscription';
import { BanksUser } from '../src/core/BanksUser';
import { EventName } from '../src';
import { EventName, Holder, Signature } from '../src';
import { getFakeAlgoanServer, getOAuthServer } from './utils/fake-server.utils';
import { serviceAccounts as serviceAccountsSample } from './samples/service-accounts';
import { subscriptions as subscriptionSample } from './samples/subscriptions';
import { banksUser as banksUserSample } from './samples/banks-users';
import { applicationSample } from './samples/application';
import { Application } from '../src/core/Application';
import { folderSample } from './samples/folder';

describe('Tests related to the ServiceAccount class', () => {
const baseUrl: string = 'http://localhost:3000';
Expand Down Expand Up @@ -274,4 +275,43 @@ describe('Tests related to the ServiceAccount class', () => {
expect(application).toBeInstanceOf(Application);
});
});

describe('static createSignature()', () => {
let signatureAPI: nock.Scope;
beforeEach(() => {
getOAuthServer({
baseUrl,
isRefreshToken: false,
isUserPassword: false,
nbOfCalls: 1,
expiresIn: 500,
refreshExpiresIn: 2000,
});
requestBuilder = new RequestBuilder(baseUrl, {
clientId: 'a',
clientSecret: 's',
});
signatureAPI = getFakeAlgoanServer({
baseUrl,
path: `/v1/folders/${folderSample.id}/signatures`,
response: folderSample.signatures[0],
method: 'post',
});
});
it('should create a new Signature', async () => {
const serviceAccount: ServiceAccount = new ServiceAccount(baseUrl, {
clientId: 'a',
clientSecret: 'b',
id: '1',
createdAt: new Date().toISOString(),
});
const signature: Signature = await serviceAccount.createSignature(folderSample.id, {
partnerId: 'provider_id',
legalDocumentIds: ['doc_id'],
holder: Holder.APPLICANT,
});
expect(signatureAPI.isDone()).toBeTruthy();
expect(signature).toBeInstanceOf(Signature);
});
});
});

0 comments on commit 2761229

Please sign in to comment.