diff --git a/application-templates/javascript/event/src/connector/actions.js b/application-templates/javascript/event/src/connector/actions.js index 7d4bc94..ae728ee 100644 --- a/application-templates/javascript/event/src/connector/actions.js +++ b/application-templates/javascript/event/src/connector/actions.js @@ -1,38 +1,46 @@ const CUSTOMER_CREATE_SUBSCRIPTION_KEY = 'myconnector-customerCreateSubscription'; -export async function createGcpPubSubCustomerCreateSubscription( +export async function createCustomerCreateSubscription( apiRoot, topicName, projectId ) { - const destination = { - type: 'GoogleCloudPubSub', - topic: topicName, - projectId, - }; - await createSubscription(apiRoot, destination); -} + const { + body: { results: subscriptions }, + } = await apiRoot + .subscriptions() + .get({ + queryArgs: { + where: `key = "${CUSTOMER_CREATE_SUBSCRIPTION_KEY}"`, + }, + }) + .execute(); -export async function createAzureServiceBusCustomerCreateSubscription( - apiRoot, - connectionString -) { - const destination = { - type: 'AzureServiceBus', - connectionString: connectionString, - }; - await createSubscription(apiRoot, destination); -} + if (subscriptions.length > 0) { + const subscription = subscriptions[0]; + + await apiRoot + .subscriptions() + .withKey({ key: CUSTOMER_CREATE_SUBSCRIPTION_KEY }) + .delete({ + queryArgs: { + version: subscription.version, + }, + }) + .execute(); + } -async function createSubscription(apiRoot, destination) { - await deleteCustomerCreateSubscription(apiRoot); await apiRoot .subscriptions() .post({ body: { key: CUSTOMER_CREATE_SUBSCRIPTION_KEY, - destination, + destination: { + type: 'GoogleCloudPubSub', + topic: topicName, + projectId, + }, messages: [ { resourceTypeId: 'customer', diff --git a/application-templates/javascript/event/src/connector/post-deploy.js b/application-templates/javascript/event/src/connector/post-deploy.js index a17eb92..cf53931 100644 --- a/application-templates/javascript/event/src/connector/post-deploy.js +++ b/application-templates/javascript/event/src/connector/post-deploy.js @@ -3,46 +3,22 @@ dotenv.config(); import { createApiRoot } from '../client/create.client.js'; import { assertError, assertString } from '../utils/assert.utils.js'; -import { - createGcpPubSubCustomerCreateSubscription, - createAzureServiceBusCustomerCreateSubscription, -} from './actions.js'; +import { createCustomerCreateSubscription } from './actions.js'; const CONNECT_GCP_TOPIC_NAME_KEY = 'CONNECT_GCP_TOPIC_NAME'; const CONNECT_GCP_PROJECT_ID_KEY = 'CONNECT_GCP_PROJECT_ID'; -const CONNECT_PROVIDER_KEY = 'CONNECT_PROVIDER'; -const CONNECT_AZURE_CONNECTION_STRING_KEY = 'CONNECT_AZURE_CONNECTION_STRING'; async function postDeploy(properties) { - const connectProvider = properties.get(CONNECT_PROVIDER_KEY); - assertString(connectProvider, CONNECT_PROVIDER_KEY); - const apiRoot = createApiRoot(); + const topicName = properties.get(CONNECT_GCP_TOPIC_NAME_KEY); + const projectId = properties.get(CONNECT_GCP_PROJECT_ID_KEY); - switch (connectProvider) { - case 'AZURE': { - const connectionString = properties.get( - CONNECT_AZURE_CONNECTION_STRING_KEY - ); - assertString(connectionString, CONNECT_AZURE_CONNECTION_STRING_KEY); - await createAzureServiceBusCustomerCreateSubscription( - apiRoot, - connectionString - ); - break; - } - default: { - const topicName = properties.get(CONNECT_GCP_TOPIC_NAME_KEY); - const projectId = properties.get(CONNECT_GCP_PROJECT_ID_KEY); - assertString(topicName, CONNECT_GCP_TOPIC_NAME_KEY); - assertString(projectId, CONNECT_GCP_PROJECT_ID_KEY); - await createGcpPubSubCustomerCreateSubscription( - apiRoot, - topicName, - projectId - ); - } - } + assertString(topicName, CONNECT_GCP_TOPIC_NAME_KEY); + assertString(projectId, CONNECT_GCP_PROJECT_ID_KEY); + + const apiRoot = createApiRoot(); + await createCustomerCreateSubscription(apiRoot, topicName, projectId); } + async function run() { try { const properties = new Map(Object.entries(process.env)); diff --git a/application-templates/typescript/event/src/connector/actions.ts b/application-templates/typescript/event/src/connector/actions.ts index 78cf9cc..22ccb7b 100644 --- a/application-templates/typescript/event/src/connector/actions.ts +++ b/application-templates/typescript/event/src/connector/actions.ts @@ -1,48 +1,48 @@ -import { - AzureServiceBusDestination, - Destination, - GoogleCloudPubSubDestination, -} from '@commercetools/platform-sdk'; import { ByProjectKeyRequestBuilder } from '@commercetools/platform-sdk/dist/declarations/src/generated/client/by-project-key-request-builder'; const CUSTOMER_CREATE_SUBSCRIPTION_KEY = 'myconnector-customerCreateSubscription'; -export async function createGcpPubSubCustomerCreateSubscription( +export async function createCustomerCreateSubscription( apiRoot: ByProjectKeyRequestBuilder, topicName: string, projectId: string ): Promise { - const destination: GoogleCloudPubSubDestination = { - type: 'GoogleCloudPubSub', - topic: topicName, - projectId, - }; - await createSubscription(apiRoot, destination); -} + const { + body: { results: subscriptions }, + } = await apiRoot + .subscriptions() + .get({ + queryArgs: { + where: `key = "${CUSTOMER_CREATE_SUBSCRIPTION_KEY}"`, + }, + }) + .execute(); -export async function createAzureServiceBusCustomerCreateSubscription( - apiRoot: ByProjectKeyRequestBuilder, - connectionString: string -): Promise { - const destination: AzureServiceBusDestination = { - type: 'AzureServiceBus', - connectionString: connectionString, - }; - await createSubscription(apiRoot, destination); -} + if (subscriptions.length > 0) { + const subscription = subscriptions[0]; + + await apiRoot + .subscriptions() + .withKey({ key: CUSTOMER_CREATE_SUBSCRIPTION_KEY }) + .delete({ + queryArgs: { + version: subscription.version, + }, + }) + .execute(); + } -async function createSubscription( - apiRoot: ByProjectKeyRequestBuilder, - destination: Destination -) { - await deleteCustomerCreateSubscription(apiRoot); await apiRoot .subscriptions() .post({ body: { key: CUSTOMER_CREATE_SUBSCRIPTION_KEY, - destination, + destination: { + type: 'GoogleCloudPubSub', + topic: topicName, + projectId, + }, messages: [ { resourceTypeId: 'customer', diff --git a/application-templates/typescript/event/src/connector/post-deploy.ts b/application-templates/typescript/event/src/connector/post-deploy.ts index 3270ee4..98beee6 100644 --- a/application-templates/typescript/event/src/connector/post-deploy.ts +++ b/application-templates/typescript/event/src/connector/post-deploy.ts @@ -3,45 +3,20 @@ dotenv.config(); import { createApiRoot } from '../client/create.client'; import { assertError, assertString } from '../utils/assert.utils'; -import { - createAzureServiceBusCustomerCreateSubscription, - createGcpPubSubCustomerCreateSubscription, -} from './actions'; +import { createCustomerCreateSubscription } from './actions'; const CONNECT_GCP_TOPIC_NAME_KEY = 'CONNECT_GCP_TOPIC_NAME'; const CONNECT_GCP_PROJECT_ID_KEY = 'CONNECT_GCP_PROJECT_ID'; -const CONNECT_PROVIDER_KEY = 'CONNECT_PROVIDER'; -const CONNECT_AZURE_CONNECTION_STRING_KEY = 'CONNECT_AZURE_CONNECTION_STRING'; async function postDeploy(properties: Map): Promise { - const connectProvider = properties.get(CONNECT_PROVIDER_KEY); - assertString(connectProvider, CONNECT_PROVIDER_KEY); - const apiRoot = createApiRoot(); + const topicName = properties.get(CONNECT_GCP_TOPIC_NAME_KEY); + const projectId = properties.get(CONNECT_GCP_PROJECT_ID_KEY); - switch (connectProvider) { - case 'AZURE': { - const connectionString = properties.get( - CONNECT_AZURE_CONNECTION_STRING_KEY - ); - assertString(connectionString, CONNECT_AZURE_CONNECTION_STRING_KEY); - await createAzureServiceBusCustomerCreateSubscription( - apiRoot, - connectionString - ); - break; - } - default: { - const topicName = properties.get(CONNECT_GCP_TOPIC_NAME_KEY); - const projectId = properties.get(CONNECT_GCP_PROJECT_ID_KEY); - assertString(topicName, CONNECT_GCP_TOPIC_NAME_KEY); - assertString(projectId, CONNECT_GCP_PROJECT_ID_KEY); - await createGcpPubSubCustomerCreateSubscription( - apiRoot, - topicName, - projectId - ); - } - } + assertString(topicName, CONNECT_GCP_TOPIC_NAME_KEY); + assertString(projectId, CONNECT_GCP_PROJECT_ID_KEY); + + const apiRoot = createApiRoot(); + await createCustomerCreateSubscription(apiRoot, topicName, projectId); } async function run(): Promise {