@adtrackify/at-service-common 1.0.69 → 1.0.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +12 -12
- package/.vscode/settings.json +9 -9
- package/bitbucket-pipelines.yml +20 -20
- package/build.js +21 -21
- package/dist/index.d.ts +4 -4
- package/dist/index.js +22 -19
- package/dist/index.js.map +2 -2
- package/jest.config.ts +40 -40
- package/package.json +2 -2
- package/src/__tests__/helpers/subscription-helper.spec.ts +40 -40
- package/src/clients/generic/axios.d.ts +7 -7
- package/src/clients/generic/dynamodb-client.ts +113 -113
- package/src/clients/generic/eventbridge-client.ts +52 -52
- package/src/clients/internal-api/destinations-client.ts +58 -58
- package/src/clients/internal-api/index.ts +4 -4
- package/src/clients/internal-api/shopify-app-install-client.ts +59 -59
- package/src/clients/internal-api/users-auth-client.ts +111 -111
- package/src/clients/third-party/shopify-client.ts +128 -128
- package/src/helpers/index.ts +5 -5
- package/src/helpers/input-validation-helper.ts +21 -21
- package/src/helpers/shopify-helper.ts +39 -39
- package/src/helpers/subscription-helper.ts +224 -217
- package/src/index.ts +4 -4
- package/src/libs/http-error.ts +90 -99
- package/src/libs/index.ts +7 -7
- package/src/libs/url.ts +9 -9
- package/src/services/eventbridge-integration-service.ts +44 -44
- package/src/types/internal-events/event-detail-types.ts +9 -9
- package/tsconfig.json +35 -35
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import log from 'lambda-log';
|
|
2
|
-
import { ApiResponse } from '../../types/api-response';
|
|
3
|
-
import { axiosHttpService } from '../generic/http-client';
|
|
4
|
-
import { ShopifyAppInstall, ShopifyAppSubscriptionStatus } from '@adtrackify/at-tracking-event-types';
|
|
5
|
-
//const BASE_API_URL = process.env.BASE_API_URL;
|
|
6
|
-
//const DESTINATIONS_API_KEY = process.env.DESTINATIONS_API_KEY;
|
|
7
|
-
|
|
8
|
-
export interface ShopifyAppInstallResponseData {
|
|
9
|
-
shopifyAppInstall: ShopifyAppInstall;
|
|
10
|
-
[ key: string ]: any;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface UpdateShopifyAppInstallRequest {
|
|
14
|
-
appSubscriptionStatus?: ShopifyAppSubscriptionStatus,
|
|
15
|
-
pixelId?: string,
|
|
16
|
-
shopifyAppInstallId: string;
|
|
17
|
-
isAppEnabled?: boolean;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class ShopifyAppInstallClient {
|
|
21
|
-
|
|
22
|
-
public BASE_API_URL: string;
|
|
23
|
-
public SHOPIFY_APP_INSTALL_API_KEY: string;
|
|
24
|
-
|
|
25
|
-
constructor (baseApiUrl: string, shopifyAppInstallApiKey: string) {
|
|
26
|
-
this.BASE_API_URL = baseApiUrl;
|
|
27
|
-
this.SHOPIFY_APP_INSTALL_API_KEY = shopifyAppInstallApiKey;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
getConfig = () => {
|
|
31
|
-
const SERVICE_API_ROOT_URL = `${this.BASE_API_URL}/shopify-app-installs`;
|
|
32
|
-
return {
|
|
33
|
-
baseURL: SERVICE_API_ROOT_URL,
|
|
34
|
-
headers: {
|
|
35
|
-
common: {
|
|
36
|
-
'x-api-key': this.SHOPIFY_APP_INSTALL_API_KEY
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
getClient = async () => {
|
|
43
|
-
return axiosHttpService(this.getConfig());
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
updateShopifyAppInstall = async (shopifyAppInstallId: string, updateShopifyAppInstallRequest: UpdateShopifyAppInstallRequest): Promise<ApiResponse<ShopifyAppInstallResponseData>> => {
|
|
47
|
-
const client = await this.getClient();
|
|
48
|
-
const response = await client.put(`/${shopifyAppInstallId}`, updateShopifyAppInstallRequest);
|
|
49
|
-
log.info('updateShopifyAppInstall', { response });
|
|
50
|
-
return response as ApiResponse<ShopifyAppInstallResponseData>;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
getShopifyAppInstall = async (shopifyAppInstallId: string): Promise<ApiResponse<ShopifyAppInstallResponseData>> => {
|
|
54
|
-
const client = await this.getClient();
|
|
55
|
-
const response = await client.get(`/${shopifyAppInstallId}`);
|
|
56
|
-
log.info('getShopifyAppInstall', { response });
|
|
57
|
-
return response as ApiResponse<ShopifyAppInstallResponseData>;
|
|
58
|
-
};
|
|
59
|
-
}
|
|
1
|
+
import log from 'lambda-log';
|
|
2
|
+
import { ApiResponse } from '../../types/api-response';
|
|
3
|
+
import { axiosHttpService } from '../generic/http-client';
|
|
4
|
+
import { ShopifyAppInstall, ShopifyAppSubscriptionStatus } from '@adtrackify/at-tracking-event-types';
|
|
5
|
+
//const BASE_API_URL = process.env.BASE_API_URL;
|
|
6
|
+
//const DESTINATIONS_API_KEY = process.env.DESTINATIONS_API_KEY;
|
|
7
|
+
|
|
8
|
+
export interface ShopifyAppInstallResponseData {
|
|
9
|
+
shopifyAppInstall: ShopifyAppInstall;
|
|
10
|
+
[ key: string ]: any;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface UpdateShopifyAppInstallRequest {
|
|
14
|
+
appSubscriptionStatus?: ShopifyAppSubscriptionStatus,
|
|
15
|
+
pixelId?: string,
|
|
16
|
+
shopifyAppInstallId: string;
|
|
17
|
+
isAppEnabled?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class ShopifyAppInstallClient {
|
|
21
|
+
|
|
22
|
+
public BASE_API_URL: string;
|
|
23
|
+
public SHOPIFY_APP_INSTALL_API_KEY: string;
|
|
24
|
+
|
|
25
|
+
constructor (baseApiUrl: string, shopifyAppInstallApiKey: string) {
|
|
26
|
+
this.BASE_API_URL = baseApiUrl;
|
|
27
|
+
this.SHOPIFY_APP_INSTALL_API_KEY = shopifyAppInstallApiKey;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
getConfig = () => {
|
|
31
|
+
const SERVICE_API_ROOT_URL = `${this.BASE_API_URL}/shopify-app-installs`;
|
|
32
|
+
return {
|
|
33
|
+
baseURL: SERVICE_API_ROOT_URL,
|
|
34
|
+
headers: {
|
|
35
|
+
common: {
|
|
36
|
+
'x-api-key': this.SHOPIFY_APP_INSTALL_API_KEY
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
getClient = async () => {
|
|
43
|
+
return axiosHttpService(this.getConfig());
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
updateShopifyAppInstall = async (shopifyAppInstallId: string, updateShopifyAppInstallRequest: UpdateShopifyAppInstallRequest): Promise<ApiResponse<ShopifyAppInstallResponseData>> => {
|
|
47
|
+
const client = await this.getClient();
|
|
48
|
+
const response = await client.put(`/${shopifyAppInstallId}`, updateShopifyAppInstallRequest);
|
|
49
|
+
log.info('updateShopifyAppInstall', { response });
|
|
50
|
+
return response as ApiResponse<ShopifyAppInstallResponseData>;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
getShopifyAppInstall = async (shopifyAppInstallId: string): Promise<ApiResponse<ShopifyAppInstallResponseData>> => {
|
|
54
|
+
const client = await this.getClient();
|
|
55
|
+
const response = await client.get(`/${shopifyAppInstallId}`);
|
|
56
|
+
log.info('getShopifyAppInstall', { response });
|
|
57
|
+
return response as ApiResponse<ShopifyAppInstallResponseData>;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
import { User } from '@adtrackify/at-tracking-event-types';
|
|
2
|
-
import * as log from 'lambda-log';
|
|
3
|
-
import { HttpError, HttpStatusCodes } from '../../libs';
|
|
4
|
-
import { ApiResponse } from '../../types/api-response';
|
|
5
|
-
import { axiosHttpService } from '../generic/http-client';
|
|
6
|
-
|
|
7
|
-
export interface UserResponseData {
|
|
8
|
-
user: User;
|
|
9
|
-
[ key: string ]: any;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface UserSignupRequest {
|
|
13
|
-
email: string,
|
|
14
|
-
password: string,
|
|
15
|
-
givenName: string,
|
|
16
|
-
familyName: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class UsersAuthClient {
|
|
20
|
-
|
|
21
|
-
public SERVICE_API_ROOT_URL: string;
|
|
22
|
-
public BASE_API_URL: string;
|
|
23
|
-
public USERS_AUTH_API_KEY: string;
|
|
24
|
-
constructor (baseApiUrl: string, usersAuthApiKey?: string) {
|
|
25
|
-
this.BASE_API_URL = baseApiUrl;
|
|
26
|
-
this.USERS_AUTH_API_KEY = usersAuthApiKey as string;
|
|
27
|
-
this.SERVICE_API_ROOT_URL = `${this.BASE_API_URL}/auth`;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
getConfig = () => {
|
|
31
|
-
return {
|
|
32
|
-
baseURL: this.SERVICE_API_ROOT_URL,
|
|
33
|
-
headers: {
|
|
34
|
-
common: {
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
getClient = async () => {
|
|
41
|
-
return axiosHttpService(this.getConfig());
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
signupAndConfirmUser = async (userSignupRequest: any): Promise<any> => {
|
|
45
|
-
const user = await this.signupUser(userSignupRequest);
|
|
46
|
-
await this.adminConfirmUser(user.email);
|
|
47
|
-
// if fail - delete user and throw error
|
|
48
|
-
return user;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
signupUser = async (userSignupRequest: UserSignupRequest): Promise<any> => {
|
|
52
|
-
log.info('Attempting to signup user', { email: userSignupRequest.email });
|
|
53
|
-
|
|
54
|
-
const client = await this.getClient();
|
|
55
|
-
const response = await client.post('/signup', userSignupRequest);
|
|
56
|
-
|
|
57
|
-
// Check if Successful or throw error
|
|
58
|
-
if (response.status !== 200 || !response?.data?.user) {
|
|
59
|
-
const message = 'User Signup Failed';
|
|
60
|
-
log.error(message, { response });
|
|
61
|
-
throw new HttpError(HttpStatusCodes.INTERNAL_SERVER_ERROR, message);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
log.info('User Signup Successful', { response });
|
|
65
|
-
return response.data.user;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
//userName is same as user id
|
|
69
|
-
adminConfirmUser = async (email: string) => {
|
|
70
|
-
//confirm user
|
|
71
|
-
//@TODO update user auth service with admin confirm user endpoint
|
|
72
|
-
log.info('Attempting to admin confirm user', { email });
|
|
73
|
-
|
|
74
|
-
const client = await this.getClient();
|
|
75
|
-
const response = await client.post('/admin/confirm',
|
|
76
|
-
{
|
|
77
|
-
email
|
|
78
|
-
}, {
|
|
79
|
-
headers: {
|
|
80
|
-
'x-api-key': this.USERS_AUTH_API_KEY
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
// Check if Successful or throw error
|
|
86
|
-
if (response.status !== 200) {
|
|
87
|
-
const message = 'Admin User Confirmation Failed';
|
|
88
|
-
log.error(message, { response });
|
|
89
|
-
throw new HttpError(HttpStatusCodes.INTERNAL_SERVER_ERROR, message);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
log.info('Admin User Confirmation Successful', { response });
|
|
93
|
-
return response.data.user;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
getUserByEmail = async (email: string): Promise<ApiResponse<UserResponseData>> => {
|
|
97
|
-
const client = await this.getClient();
|
|
98
|
-
const getUserResponse = await client.get('/lookup', {
|
|
99
|
-
headers: {
|
|
100
|
-
'x-api-key': this.USERS_AUTH_API_KEY
|
|
101
|
-
},
|
|
102
|
-
params: {
|
|
103
|
-
email
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
log.info('getUserResponse', { getUserResponse });
|
|
107
|
-
return getUserResponse;
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
1
|
+
import { User } from '@adtrackify/at-tracking-event-types';
|
|
2
|
+
import * as log from 'lambda-log';
|
|
3
|
+
import { HttpError, HttpStatusCodes } from '../../libs';
|
|
4
|
+
import { ApiResponse } from '../../types/api-response';
|
|
5
|
+
import { axiosHttpService } from '../generic/http-client';
|
|
6
|
+
|
|
7
|
+
export interface UserResponseData {
|
|
8
|
+
user: User;
|
|
9
|
+
[ key: string ]: any;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface UserSignupRequest {
|
|
13
|
+
email: string,
|
|
14
|
+
password: string,
|
|
15
|
+
givenName: string,
|
|
16
|
+
familyName: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class UsersAuthClient {
|
|
20
|
+
|
|
21
|
+
public SERVICE_API_ROOT_URL: string;
|
|
22
|
+
public BASE_API_URL: string;
|
|
23
|
+
public USERS_AUTH_API_KEY: string;
|
|
24
|
+
constructor (baseApiUrl: string, usersAuthApiKey?: string) {
|
|
25
|
+
this.BASE_API_URL = baseApiUrl;
|
|
26
|
+
this.USERS_AUTH_API_KEY = usersAuthApiKey as string;
|
|
27
|
+
this.SERVICE_API_ROOT_URL = `${this.BASE_API_URL}/auth`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
getConfig = () => {
|
|
31
|
+
return {
|
|
32
|
+
baseURL: this.SERVICE_API_ROOT_URL,
|
|
33
|
+
headers: {
|
|
34
|
+
common: {
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
getClient = async () => {
|
|
41
|
+
return axiosHttpService(this.getConfig());
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
signupAndConfirmUser = async (userSignupRequest: any): Promise<any> => {
|
|
45
|
+
const user = await this.signupUser(userSignupRequest);
|
|
46
|
+
await this.adminConfirmUser(user.email);
|
|
47
|
+
// if fail - delete user and throw error
|
|
48
|
+
return user;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
signupUser = async (userSignupRequest: UserSignupRequest): Promise<any> => {
|
|
52
|
+
log.info('Attempting to signup user', { email: userSignupRequest.email });
|
|
53
|
+
|
|
54
|
+
const client = await this.getClient();
|
|
55
|
+
const response = await client.post('/signup', userSignupRequest);
|
|
56
|
+
|
|
57
|
+
// Check if Successful or throw error
|
|
58
|
+
if (response.status !== 200 || !response?.data?.user) {
|
|
59
|
+
const message = 'User Signup Failed';
|
|
60
|
+
log.error(message, { response });
|
|
61
|
+
throw new HttpError(HttpStatusCodes.INTERNAL_SERVER_ERROR, message);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
log.info('User Signup Successful', { response });
|
|
65
|
+
return response.data.user;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
//userName is same as user id
|
|
69
|
+
adminConfirmUser = async (email: string) => {
|
|
70
|
+
//confirm user
|
|
71
|
+
//@TODO update user auth service with admin confirm user endpoint
|
|
72
|
+
log.info('Attempting to admin confirm user', { email });
|
|
73
|
+
|
|
74
|
+
const client = await this.getClient();
|
|
75
|
+
const response = await client.post('/admin/confirm',
|
|
76
|
+
{
|
|
77
|
+
email
|
|
78
|
+
}, {
|
|
79
|
+
headers: {
|
|
80
|
+
'x-api-key': this.USERS_AUTH_API_KEY
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
// Check if Successful or throw error
|
|
86
|
+
if (response.status !== 200) {
|
|
87
|
+
const message = 'Admin User Confirmation Failed';
|
|
88
|
+
log.error(message, { response });
|
|
89
|
+
throw new HttpError(HttpStatusCodes.INTERNAL_SERVER_ERROR, message);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
log.info('Admin User Confirmation Successful', { response });
|
|
93
|
+
return response.data.user;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
getUserByEmail = async (email: string): Promise<ApiResponse<UserResponseData>> => {
|
|
97
|
+
const client = await this.getClient();
|
|
98
|
+
const getUserResponse = await client.get('/lookup', {
|
|
99
|
+
headers: {
|
|
100
|
+
'x-api-key': this.USERS_AUTH_API_KEY
|
|
101
|
+
},
|
|
102
|
+
params: {
|
|
103
|
+
email
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
log.info('getUserResponse', { getUserResponse });
|
|
107
|
+
return getUserResponse;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
}
|
|
111
|
+
|
|
@@ -1,128 +1,128 @@
|
|
|
1
|
-
import { axiosHttpService } from '../generic/http-client';
|
|
2
|
-
import * as log from 'lambda-log';
|
|
3
|
-
|
|
4
|
-
export class ShopifyClient {
|
|
5
|
-
static _shopify_api_version = process.env.SHOPIFY_API_VERSION as string;
|
|
6
|
-
static getConfig = (shopifyDomain: string, accessToken: string) => {
|
|
7
|
-
const config = {
|
|
8
|
-
baseURL: `https://${shopifyDomain}/admin/api/${this._shopify_api_version}`,
|
|
9
|
-
headers: {
|
|
10
|
-
common: {
|
|
11
|
-
'X-Shopify-Access-Token': accessToken,
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
};
|
|
15
|
-
return config;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
static getClient = (shopifyDomain: string, accessToken: string) => {
|
|
19
|
-
return axiosHttpService(
|
|
20
|
-
this.getConfig(shopifyDomain, accessToken)
|
|
21
|
-
);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
static registerApp = async (shop: string, code: string, appKey: string, appSecret: string) => {
|
|
25
|
-
const client = axiosHttpService();
|
|
26
|
-
const url = 'https://' + shop + '/admin/oauth/access_token';
|
|
27
|
-
const payload = {
|
|
28
|
-
client_id: appKey,
|
|
29
|
-
client_secret: appSecret,
|
|
30
|
-
code
|
|
31
|
-
};
|
|
32
|
-
const res = await client.post(url, payload);
|
|
33
|
-
return res;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
static registerWebhookTopic = async (shop: string, accessToken: string, eventBridgeArn: string, topic: string) => {
|
|
37
|
-
const client = axiosHttpService();
|
|
38
|
-
const url = `https://${shop}/admin/api/${this._shopify_api_version}/webhooks.json`;
|
|
39
|
-
const payload = {
|
|
40
|
-
webhook: {
|
|
41
|
-
topic,
|
|
42
|
-
address: eventBridgeArn,
|
|
43
|
-
format: 'json'
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
const res = await client.post(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
47
|
-
if (res.status >= 400) {
|
|
48
|
-
log.error('Failed to register Webhook Topic', { shop, accessToken, eventBridgeArn, topic, url, payload });
|
|
49
|
-
}
|
|
50
|
-
log.debug('Shopify Client Webhook Registration Response', { registrationResponse: res });
|
|
51
|
-
return res;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
static updateShopifyAppMetafield = async (shop: string, accessToken: string, pixelId: string) => {
|
|
55
|
-
const url = `https://${shop}/admin/api/${this._shopify_api_version}/metafields.json`;
|
|
56
|
-
const payload = {
|
|
57
|
-
metafield: {
|
|
58
|
-
namespace: 'adtr',
|
|
59
|
-
key: 'adtr.config',
|
|
60
|
-
value: pixelId,
|
|
61
|
-
type: 'single_line_text_field'
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
const res = await this.genericShopifyPost(url, accessToken, payload);
|
|
65
|
-
|
|
66
|
-
if (res.status >= 400) {
|
|
67
|
-
log.error('Failed to register Webhook Topic', { shop, accessToken, url, payload });
|
|
68
|
-
}
|
|
69
|
-
return res;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
static getShopifyStoreProperties = async (shop: string, accessToken: string) => {
|
|
73
|
-
const url = `https://${shop}/admin/api/${this._shopify_api_version}/shop.json`;
|
|
74
|
-
const res = await this.genericShopifyGet(url, accessToken);
|
|
75
|
-
|
|
76
|
-
if (res.status >= 400) {
|
|
77
|
-
log.error('Failed to get Shopify Store Properties', { shop, accessToken, url });
|
|
78
|
-
}
|
|
79
|
-
return res;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
static createAppSubscription = async (shop: string, accessToken: string,
|
|
83
|
-
planName: string, price: number, returnUrl: string, trialDays: number, test?: boolean) => {
|
|
84
|
-
const url = `https://${shop}/admin/api/${this._shopify_api_version}/recurring_application_charges.json`;
|
|
85
|
-
const recurring_application_charge = {
|
|
86
|
-
name: planName,
|
|
87
|
-
price,
|
|
88
|
-
return_url: returnUrl,
|
|
89
|
-
trial_days: trialDays,
|
|
90
|
-
test
|
|
91
|
-
};
|
|
92
|
-
const res = await this.genericShopifyPost(url, accessToken, { recurring_application_charge });
|
|
93
|
-
if (res.status >= 400) {
|
|
94
|
-
log.error('Failed to create App Subscription', { shop, accessToken, url });
|
|
95
|
-
}
|
|
96
|
-
return res;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
static listAppSubscriptions = async (shop: string, accessToken: string) => {
|
|
100
|
-
const url = `https://${shop}/admin/api/${this._shopify_api_version}/recurring_application_charges.json`;
|
|
101
|
-
const res = await this.genericShopifyGet(url, accessToken);
|
|
102
|
-
if (res.status >= 400) {
|
|
103
|
-
log.error('Failed to get App Subscriptions', { shop, accessToken, url });
|
|
104
|
-
}
|
|
105
|
-
return res;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
static genericShopifyPost = async (url: string, accessToken: string, payload: any) => {
|
|
109
|
-
const client = axiosHttpService();
|
|
110
|
-
const res = await client.post(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
111
|
-
log.debug('Shopify Client Response', { res });
|
|
112
|
-
return res;
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
static genericShopifyGet = async (url: string, accessToken: string) => {
|
|
116
|
-
const client = axiosHttpService();
|
|
117
|
-
const res = await client.get(url, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
118
|
-
log.debug('Shopify Client Response', { res });
|
|
119
|
-
return res;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
static genericShopifyPut = async (url: string, accessToken: string, payload: any) => {
|
|
123
|
-
const client = axiosHttpService();
|
|
124
|
-
const res = await client.put(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
125
|
-
log.debug('Shopify Client Response', { res });
|
|
126
|
-
return res;
|
|
127
|
-
};
|
|
128
|
-
}
|
|
1
|
+
import { axiosHttpService } from '../generic/http-client';
|
|
2
|
+
import * as log from 'lambda-log';
|
|
3
|
+
|
|
4
|
+
export class ShopifyClient {
|
|
5
|
+
static _shopify_api_version = process.env.SHOPIFY_API_VERSION as string;
|
|
6
|
+
static getConfig = (shopifyDomain: string, accessToken: string) => {
|
|
7
|
+
const config = {
|
|
8
|
+
baseURL: `https://${shopifyDomain}/admin/api/${this._shopify_api_version}`,
|
|
9
|
+
headers: {
|
|
10
|
+
common: {
|
|
11
|
+
'X-Shopify-Access-Token': accessToken,
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
return config;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
static getClient = (shopifyDomain: string, accessToken: string) => {
|
|
19
|
+
return axiosHttpService(
|
|
20
|
+
this.getConfig(shopifyDomain, accessToken)
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
static registerApp = async (shop: string, code: string, appKey: string, appSecret: string) => {
|
|
25
|
+
const client = axiosHttpService();
|
|
26
|
+
const url = 'https://' + shop + '/admin/oauth/access_token';
|
|
27
|
+
const payload = {
|
|
28
|
+
client_id: appKey,
|
|
29
|
+
client_secret: appSecret,
|
|
30
|
+
code
|
|
31
|
+
};
|
|
32
|
+
const res = await client.post(url, payload);
|
|
33
|
+
return res;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
static registerWebhookTopic = async (shop: string, accessToken: string, eventBridgeArn: string, topic: string) => {
|
|
37
|
+
const client = axiosHttpService();
|
|
38
|
+
const url = `https://${shop}/admin/api/${this._shopify_api_version}/webhooks.json`;
|
|
39
|
+
const payload = {
|
|
40
|
+
webhook: {
|
|
41
|
+
topic,
|
|
42
|
+
address: eventBridgeArn,
|
|
43
|
+
format: 'json'
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const res = await client.post(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
47
|
+
if (res.status >= 400) {
|
|
48
|
+
log.error('Failed to register Webhook Topic', { shop, accessToken, eventBridgeArn, topic, url, payload });
|
|
49
|
+
}
|
|
50
|
+
log.debug('Shopify Client Webhook Registration Response', { registrationResponse: res });
|
|
51
|
+
return res;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
static updateShopifyAppMetafield = async (shop: string, accessToken: string, pixelId: string) => {
|
|
55
|
+
const url = `https://${shop}/admin/api/${this._shopify_api_version}/metafields.json`;
|
|
56
|
+
const payload = {
|
|
57
|
+
metafield: {
|
|
58
|
+
namespace: 'adtr',
|
|
59
|
+
key: 'adtr.config',
|
|
60
|
+
value: pixelId,
|
|
61
|
+
type: 'single_line_text_field'
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const res = await this.genericShopifyPost(url, accessToken, payload);
|
|
65
|
+
|
|
66
|
+
if (res.status >= 400) {
|
|
67
|
+
log.error('Failed to register Webhook Topic', { shop, accessToken, url, payload });
|
|
68
|
+
}
|
|
69
|
+
return res;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
static getShopifyStoreProperties = async (shop: string, accessToken: string) => {
|
|
73
|
+
const url = `https://${shop}/admin/api/${this._shopify_api_version}/shop.json`;
|
|
74
|
+
const res = await this.genericShopifyGet(url, accessToken);
|
|
75
|
+
|
|
76
|
+
if (res.status >= 400) {
|
|
77
|
+
log.error('Failed to get Shopify Store Properties', { shop, accessToken, url });
|
|
78
|
+
}
|
|
79
|
+
return res;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
static createAppSubscription = async (shop: string, accessToken: string,
|
|
83
|
+
planName: string, price: number, returnUrl: string, trialDays: number, test?: boolean) => {
|
|
84
|
+
const url = `https://${shop}/admin/api/${this._shopify_api_version}/recurring_application_charges.json`;
|
|
85
|
+
const recurring_application_charge = {
|
|
86
|
+
name: planName,
|
|
87
|
+
price,
|
|
88
|
+
return_url: returnUrl,
|
|
89
|
+
trial_days: trialDays,
|
|
90
|
+
test
|
|
91
|
+
};
|
|
92
|
+
const res = await this.genericShopifyPost(url, accessToken, { recurring_application_charge });
|
|
93
|
+
if (res.status >= 400) {
|
|
94
|
+
log.error('Failed to create App Subscription', { shop, accessToken, url });
|
|
95
|
+
}
|
|
96
|
+
return res;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
static listAppSubscriptions = async (shop: string, accessToken: string) => {
|
|
100
|
+
const url = `https://${shop}/admin/api/${this._shopify_api_version}/recurring_application_charges.json`;
|
|
101
|
+
const res = await this.genericShopifyGet(url, accessToken);
|
|
102
|
+
if (res.status >= 400) {
|
|
103
|
+
log.error('Failed to get App Subscriptions', { shop, accessToken, url });
|
|
104
|
+
}
|
|
105
|
+
return res;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
static genericShopifyPost = async (url: string, accessToken: string, payload: any) => {
|
|
109
|
+
const client = axiosHttpService();
|
|
110
|
+
const res = await client.post(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
111
|
+
log.debug('Shopify Client Response', { res });
|
|
112
|
+
return res;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
static genericShopifyGet = async (url: string, accessToken: string) => {
|
|
116
|
+
const client = axiosHttpService();
|
|
117
|
+
const res = await client.get(url, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
118
|
+
log.debug('Shopify Client Response', { res });
|
|
119
|
+
return res;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
static genericShopifyPut = async (url: string, accessToken: string, payload: any) => {
|
|
123
|
+
const client = axiosHttpService();
|
|
124
|
+
const res = await client.put(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
125
|
+
log.debug('Shopify Client Response', { res });
|
|
126
|
+
return res;
|
|
127
|
+
};
|
|
128
|
+
}
|
package/src/helpers/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from './input-validation-helper';
|
|
2
|
-
export * from './logging-helper';
|
|
3
|
-
export * from './response-helper';
|
|
4
|
-
export * from './shopify-helper';
|
|
5
|
-
export * from './subscription-helper';
|
|
1
|
+
export * from './input-validation-helper';
|
|
2
|
+
export * from './logging-helper';
|
|
3
|
+
export * from './response-helper';
|
|
4
|
+
export * from './shopify-helper';
|
|
5
|
+
export * from './subscription-helper';
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import Joi from 'joi';
|
|
2
|
-
import * as log from 'lambda-log';
|
|
3
|
-
import { HttpError } from '../libs/http-error';
|
|
4
|
-
|
|
5
|
-
export const validateInput = (schema: Joi.ObjectSchema<any>, input: any) => {
|
|
6
|
-
const { error, value } = schema.validate(input);
|
|
7
|
-
if (error) {
|
|
8
|
-
log.info('', { error });
|
|
9
|
-
|
|
10
|
-
const httperr = HttpError.badRequest('Bad Request', {
|
|
11
|
-
errors: error.details.map(detail => ({
|
|
12
|
-
message: detail?.message,
|
|
13
|
-
key: detail?.context?.key,
|
|
14
|
-
path: detail?.path,
|
|
15
|
-
}))
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
log.info('', { httperr });
|
|
19
|
-
throw httperr;
|
|
20
|
-
}
|
|
21
|
-
return value;
|
|
1
|
+
import Joi from 'joi';
|
|
2
|
+
import * as log from 'lambda-log';
|
|
3
|
+
import { HttpError } from '../libs/http-error';
|
|
4
|
+
|
|
5
|
+
export const validateInput = (schema: Joi.ObjectSchema<any>, input: any) => {
|
|
6
|
+
const { error, value } = schema.validate(input);
|
|
7
|
+
if (error) {
|
|
8
|
+
log.info('', { error });
|
|
9
|
+
|
|
10
|
+
const httperr = HttpError.badRequest('Bad Request', {
|
|
11
|
+
errors: error.details.map(detail => ({
|
|
12
|
+
message: detail?.message,
|
|
13
|
+
key: detail?.context?.key,
|
|
14
|
+
path: detail?.path,
|
|
15
|
+
}))
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
log.info('', { httperr });
|
|
19
|
+
throw httperr;
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
22
|
};
|