All files / src ShopifyApplication.ts

53.33% Statements 16/30
0% Branches 0/5
27.27% Functions 3/11
53.33% Lines 16/30

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 11317x   17x 17x 17x 17x   17x   17x 17x 17x   17x 17x   17x   17x 17x               1x                                                                                                                             15x                                            
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 AuthorizationTypeEnum from '@orchesty/nodejs-sdk/dist/lib/Authorization/AuthorizationTypeEnum';
import { OAuth2Provider } from '@orchesty/nodejs-sdk/dist/lib/Authorization/Provider/OAuth2/OAuth2Provider';
import { TOKEN } from '@orchesty/nodejs-sdk/dist/lib/Authorization/Type/Basic/ABasicApplication';
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 CurlSender from '@orchesty/nodejs-sdk/dist/lib/Transport/Curl/CurlSender';
import { Mixin } from 'ts-mixer';
import ABaseShopify, { PREMIUM_PLAN, SHOPIFY_URL } from './ABaseShopify';
 
export default class ShopifyApplication extends Mixin(AOAuth2Application, ABaseShopify) {
 
    public constructor(protected readonly curlSender: CurlSender, provider: OAuth2Provider) {
        super(provider);
    }
 
    public getAuthorizationType(): AuthorizationTypeEnum {
        return AuthorizationTypeEnum.OAUTH2;
    }
 
    public getWebhookSubscriptions(): WebhookSubscription[] {
        return [];
    }
 
    public isAuthorized(applicationInstall: ApplicationInstall): boolean {
        const authorizationForm = applicationInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM];
 
        return super.isAuthorized(applicationInstall)
      && authorizationForm?.[CLIENT_ID]
      && authorizationForm?.[CLIENT_SECRET]
      && authorizationForm?.[SHOPIFY_URL]
      && authorizationForm?.[PREMIUM_PLAN] !== undefined;
    }
 
    public getFormStack(): FormStack {
        const form = new Form(CoreFormsEnum.AUTHORIZATION_FORM, getFormName(CoreFormsEnum.AUTHORIZATION_FORM))
            .addField(new Field(FieldType.TEXT, CLIENT_ID, 'Client Id', null, true))
            .addField(new Field(FieldType.TEXT, CLIENT_SECRET, 'Client Secret', null, true))
            .addField(new Field(FieldType.TEXT, SHOPIFY_URL, 'Url', undefined, true));
 
        return new FormStack().addForm(form);
    }
 
    // This URL is dynamically replaced in getProviderCustomOptions method
    public getAuthUrl(): string {
        return 'https://xyz.myshopify.com/admin/oauth/authorize';
    }
 
    // This URL is dynamically replaced in getProviderCustomOptions method
    public getTokenUrl(): string {
        return 'https://xyz.myshopify.com/admin/oauth/access_token';
    }
 
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    public getScopes(applicationInstall: ApplicationInstall): string[] {
        return [
            'read_assigned_fulfillment_orders',
            'read_fulfillments',
            'read_merchant_managed_fulfillment_orders',
            'read_orders',
            'read_products',
            'read_shipping',
            'read_third_party_fulfillment_orders',
            'read_locations',
            'write_assigned_fulfillment_orders',
            'write_fulfillments',
            'write_merchant_managed_fulfillment_orders',
            'write_orders',
            'write_products',
            'write_shipping',
            'write_inventory',
            'write_third_party_fulfillment_orders',
        ];
    }
 
    public async setAuthorizationToken(
        applicationInstall: ApplicationInstall,
        token: Record<string, string>,
    ): Promise<void> {
        await super.setAuthorizationToken(applicationInstall, token);
        await this.checkShopPlan(applicationInstall);
    }
 
    protected getTokenForRequest(appInstall: ApplicationInstall): string {
        return appInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM][TOKEN].accessToken;
    }
 
    protected getProviderCustomOptions(applicationInstall: ApplicationInstall): Record<string, unknown> {
        const url = applicationInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM][SHOPIFY_URL];
        const authorizeUrl = new URL(`${url}/admin/oauth/authorize`);
        const tokenUrl = new URL(`${url}/admin/oauth/access_token`);
 
        return {
            auth: {
                authorizeHost: authorizeUrl.origin,
                authorizePath: authorizeUrl.pathname,
                tokenHost: tokenUrl.origin,
                tokenPath: tokenUrl.pathname,
            },
            options: {
                authorizationMethod: 'body',
            },
        };
    }
 
}