Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 12x | import CoreFormsEnum, { getFormName } from '@orchesty/nodejs-sdk/dist/lib/Application/Base/CoreFormsEnum';
import { ApplicationInstall } from '@orchesty/nodejs-sdk/dist/lib/Application/Database/ApplicationInstall';
import Field from '@orchesty/nodejs-sdk/dist/lib/Application/Model/Form/Field';
import FieldType from '@orchesty/nodejs-sdk/dist/lib/Application/Model/Form/FieldType';
import Form from '@orchesty/nodejs-sdk/dist/lib/Application/Model/Form/Form';
import FormStack from '@orchesty/nodejs-sdk/dist/lib/Application/Model/Form/FormStack';
import { ABasicApplication } from '@orchesty/nodejs-sdk/dist/lib/Authorization/Type/Basic/ABasicApplication';
import { CLIENT_ID, CLIENT_SECRET } from '@orchesty/nodejs-sdk/dist/lib/Authorization/Type/OAuth2/IOAuth2Application';
import CacheService from '@orchesty/nodejs-sdk/dist/lib/Cache/CacheService';
import logger from '@orchesty/nodejs-sdk/dist/lib/Logger/Logger';
import RequestDto from '@orchesty/nodejs-sdk/dist/lib/Transport/Curl/RequestDto';
import { defaultRanges } from '@orchesty/nodejs-sdk/dist/lib/Transport/Curl/ResultCodeRange';
import { HttpMethods, parseHttpMethod } from '@orchesty/nodejs-sdk/dist/lib/Transport/HttpMethods';
import AProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/AProcessDto';
import { CommonHeaders, JSON_TYPE } from '@orchesty/nodejs-sdk/dist/lib/Utils/Headers';
export const MONEYS_URL = 'moneys5Url';
export default abstract class MoneyS45Base extends ABasicApplication {
public constructor(
private readonly cache: CacheService,
) {
super();
}
public async getRequestDto(
dto: AProcessDto,
applicationInstall: ApplicationInstall,
method: HttpMethods | string,
url?: string,
data?: string,
): Promise<RequestDto> {
const headers = {
[CommonHeaders.AUTHORIZATION]: `Bearer ${await this.getApiToken(applicationInstall, dto)}`,
[CommonHeaders.CONTENT_TYPE]: JSON_TYPE,
};
return new RequestDto(
`${this.getDecoratedUrl(applicationInstall)}/${url}`,
parseHttpMethod(method),
dto,
data,
headers,
);
}
public async getApiToken(
applicationInstall: ApplicationInstall,
processDto: AProcessDto,
): Promise<string> {
try {
const cacheKey = `${
this.getName()
}ApiKey_${applicationInstall.getUser()}`;
const headers = {
[CommonHeaders.CONTENT_TYPE]: 'application/x-www-form-urlencoded',
[CommonHeaders.ACCEPT]: JSON_TYPE,
};
const clientId = applicationInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM][CLIENT_ID];
const clientSecret = applicationInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM][CLIENT_SECRET];
const body = `grant_type=client_credentials&client_id=${clientId}&client_secret=${clientSecret}`;
const requestDto = new RequestDto(
`${this.getDecoratedUrl(applicationInstall)}/connect/token`,
HttpMethods.POST,
processDto,
body,
headers,
);
return await this.cache.entry(
cacheKey,
requestDto,
(dto) => {
const dtoBody = dto.getJsonBody() as IResponseJson;
return {
expire: Number(dtoBody.expires_in) - 120,
dataToStore: dtoBody.access_token,
};
},
defaultRanges,
);
} catch (e) {
if (e instanceof Error) {
logger.error(e.message || 'Unknown error in Money S5 application.', processDto);
}
throw e;
}
}
public getDecoratedUrl(app: ApplicationInstall): string {
return app.getSettings()?.[CoreFormsEnum.AUTHORIZATION_FORM]?.[MONEYS_URL] ?? '';
}
public getFormStack(): FormStack {
const form = new Form(CoreFormsEnum.AUTHORIZATION_FORM, getFormName(CoreFormsEnum.AUTHORIZATION_FORM))
.addField(new Field(FieldType.TEXT, CLIENT_ID, 'Client ID', undefined, true))
.addField(new Field(FieldType.TEXT, CLIENT_SECRET, 'Client Secret', undefined, true))
.addField(new Field(FieldType.TEXT, MONEYS_URL, 'Url', undefined, true));
return new FormStack().addForm(form);
}
public isAuthorized(applicationInstall: ApplicationInstall): boolean {
const authorizationForm = applicationInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM];
return authorizationForm?.[CLIENT_ID] && authorizationForm?.[CLIENT_SECRET] && authorizationForm?.[MONEYS_URL];
}
}
interface IResponseJson {
access_token: string;
expires_in: string;
token_type: string;
}
|