All files / src ZendeskApplication.ts

53.65% Statements 22/41
25% Branches 2/8
13.33% Functions 2/15
53.65% Lines 22/41

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 118 119 120 121 1225x   5x 5x 5x 5x   5x 5x 5x 5x 5x     5x   5x 5x 5x             11x                                   6x 6x   6x 6x           6x 3x     6x                                                                                                                                
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 WebhookSubscription from '@orchesty/nodejs-sdk/dist/lib/Application/Model/Webhook/WebhookSubscription';
import OAuth2Dto from '@orchesty/nodejs-sdk/dist/lib/Authorization/Provider/Dto/OAuth2Dto';
import ScopeSeparatorEnum from '@orchesty/nodejs-sdk/dist/lib/Authorization/ScopeSeparatorEnum';
import AOAuth2Application from '@orchesty/nodejs-sdk/dist/lib/Authorization/Type/OAuth2/AOAuth2Application';
import { CLIENT_ID, CLIENT_SECRET } from '@orchesty/nodejs-sdk/dist/lib/Authorization/Type/OAuth2/IOAuth2Application';
import RequestDto from '@orchesty/nodejs-sdk/dist/lib/Transport/Curl/RequestDto';
import { HttpMethods } 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 SUBDOMAIN = 'subdomain';
export const NAME = 'zendesk';
export default class ZendeskApplication extends AOAuth2Application {
 
    public getDescription(): string {
        return 'Customer support software that helps companies manage customer problems through a ticketing system';
    }
 
    public getName(): string {
        return NAME;
    }
 
    public getPublicName(): string {
        return 'Zendesk';
    }
 
    public getLogo(): string {
        return 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDI1LjAuMCwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPgo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IlZyc3R2YV8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCIKCSB2aWV3Qm94PSIwIDAgMTAwIDEwMCIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgMTAwIDEwMDsiIHhtbDpzcGFjZT0icHJlc2VydmUiPgo8c3R5bGUgdHlwZT0idGV4dC9jc3MiPgoJLnN0MHtmaWxsOiMwMzM2M0Q7fQo8L3N0eWxlPgo8Zz4KCTxwYXRoIGNsYXNzPSJzdDAiIGQ9Ik00Ni4yLDMyLjJ2NTUuNEgwLjNMNDYuMiwzMi4yeiBNNDYuMiwxMi4zYzAsMTIuNy0xMC4zLDIzLTIzLDIzcy0yMy0xMC4zLTIzLTIzTDQ2LjIsMTIuM3ogTTUzLjgsODcuNwoJCWMwLTEyLjcsMTAuMy0yMywyMy0yM3MyMywxMC4zLDIzLDIzSDUzLjh6IE01My44LDY3LjhWMTIuM2g0NS45TDUzLjgsNjcuOHoiLz4KPC9nPgo8L3N2Zz4K';
    }
 
    public getRequestDto(
        dto: AProcessDto,
        applicationInstall: ApplicationInstall,
        method: HttpMethods,
        _url?: string,
        data?: unknown,
    ): Promise<RequestDto> | RequestDto {
        const subdomain = applicationInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM][SUBDOMAIN];
        const url = `https://${subdomain}.zendesk.com/api/v2${_url}`;
 
        const request = new RequestDto(url, method, dto);
        request.setHeaders({
            [CommonHeaders.CONTENT_TYPE]: JSON_TYPE,
            [CommonHeaders.ACCEPT]: JSON_TYPE,
            [CommonHeaders.AUTHORIZATION]: `Bearer ${this.getAccessToken(applicationInstall)}`,
        });
 
        if (data) {
            request.setJsonBody(data);
        }
 
        return request;
    }
 
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    public getScopes(applicationInstall: ApplicationInstall): string[] {
        return ['read', 'write'];
    }
 
    public getFormStack(): FormStack {
        const form = new Form(CoreFormsEnum.AUTHORIZATION_FORM, getFormName(CoreFormsEnum.AUTHORIZATION_FORM))
            .addField(new Field(FieldType.TEXT, SUBDOMAIN, 'Subdomain', undefined, true))
            .addField(new Field(FieldType.TEXT, CLIENT_ID, 'Client Id', undefined, true))
            .addField(new Field(FieldType.TEXT, CLIENT_SECRET, 'Client Secret', undefined, true));
 
        return new FormStack().addForm(form);
    }
 
    public isAuthorized(applicationInstall: ApplicationInstall): boolean {
        const authorizationForm = applicationInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM];
        return super.isAuthorized(applicationInstall)
            && authorizationForm?.[SUBDOMAIN]
            && authorizationForm?.[CLIENT_ID]
            && authorizationForm?.[CLIENT_SECRET];
    }
 
    public getAuthUrlWithSubdomain(applicationInstall: ApplicationInstall): string {
        return `https://${applicationInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM][SUBDOMAIN]}.zendesk.com/oauth/authorizations/new`;
    }
 
    public getAuthUrl(): string {
        throw new Error(`Dont use [${this.getAuthUrl.name}] use [${this.getAuthUrlWithSubdomain.bind(this).name}] instead.`);
    }
 
    public getTokenUrlWithSubdomain(applicationInstall: ApplicationInstall): string {
        return `https://${applicationInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM][SUBDOMAIN]}.zendesk.com/oauth/tokens`;
    }
 
    public getTokenUrl(): string {
        throw new Error(`Dont use [${this.getAuthUrl.name}] use [${this.getTokenUrlWithSubdomain.bind(this).name}] instead.`);
    }
 
    public createDto(applicationInstall: ApplicationInstall, redirectUrl?: string): OAuth2Dto {
        const dto = new OAuth2Dto(
            applicationInstall,
            this.getAuthUrlWithSubdomain(applicationInstall),
            this.getTokenUrlWithSubdomain(applicationInstall),
        );
        dto.setCustomAppDependencies(applicationInstall.getUser(), applicationInstall.getName());
        if (redirectUrl) {
            dto.setRedirectUrl(redirectUrl);
        }
 
        return dto;
    }
 
    public getWebhookSubscriptions(): WebhookSubscription[] {
        return [];
    }
 
    protected getScopesSeparator(): string {
        return ScopeSeparatorEnum.SPACE;
    }
 
}