Skip to content

Commit 971133d

Browse files
committed
💥 rename pubsub topic env vars and use generic client
1 parent 8c01d05 commit 971133d

File tree

3 files changed

+49
-76
lines changed

3 files changed

+49
-76
lines changed

‎src/models/controller.model.ts‎

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Ajv from 'ajv';
22
import { NextFunction, Request, Response } from 'express';
3+
import { isEqual, uniqWith } from 'lodash';
34
import { stringify } from 'querystring';
45
import {
56
Adapter,
@@ -32,13 +33,11 @@ import { CacheItemStateType } from './cache-item-state.model';
3233
import { CalendarFilterOptions } from './calendar-filter-options.model';
3334
import { IntegrationErrorType } from './integration-error.model';
3435
import { PubSubClient } from './pubsub-client.model';
36+
import { PubSubContactChangeEventMessage } from './pubsub-contact-change-event-message.model';
3537
import {
3638
PubSubContactsMessage,
3739
PubSubContactsState,
3840
} from './pubsub-contacts-message.model';
39-
import { PubSubContactChangeEventClient } from './pubsub-contact-change-event-client.model';
40-
import { PubSubContactChangeEventMessage } from './pubsub-contact-change-event-message.model';
41-
import { isEqual, uniqWith } from 'lodash';
4241

4342
const CONTACT_FETCH_TIMEOUT = 5000;
4443

@@ -56,8 +55,9 @@ export class Controller {
5655
private adapter: Adapter;
5756
private contactCache: ContactCache | null;
5857
private ajv: Ajv;
59-
private pubSubClient: PubSubClient | null = null;
60-
private pubSubContactsChangedClient: PubSubContactChangeEventClient | null =
58+
private pubSubContactStreamingClient: PubSubClient<PubSubContactsMessage> | null =
59+
null;
60+
private pubSubContactChangesClient: PubSubClient<PubSubContactChangeEventMessage> | null =
6161
null;
6262
private integrationName: string = 'UNKNOWN';
6363
private streamingPromises = new Map<string, Promise<void>>();
@@ -68,50 +68,57 @@ export class Controller {
6868
this.ajv = new Ajv();
6969

7070
if (this.adapter.streamContacts) {
71-
const {
72-
PUBSUB_TOPIC_NAME: topicName,
73-
INTEGRATION_NAME: integrationName,
74-
} = process.env;
71+
this.initContactStreaming();
72+
}
7573

76-
if (!topicName) {
77-
throw new Error('No PUBSUB_TOPIC_NAME provided.');
78-
}
74+
if (this.adapter.handleWebhook) {
75+
this.initContactChanges();
76+
}
77+
78+
if (this.adapter.streamContacts || this.adapter.handleWebhook) {
79+
const { INTEGRATION_NAME: integrationName } = process.env;
7980

8081
if (!integrationName) {
8182
throw new Error('No INTEGRATION_NAME provided.');
8283
}
8384

8485
this.integrationName = integrationName;
85-
this.pubSubClient = new PubSubClient(topicName);
86-
infoLogger(
87-
'Controller',
88-
`Initialized PubSub client with topic ${topicName}`,
89-
);
9086
}
87+
}
9188

92-
if (this.adapter.handleWebhook) {
93-
const {
94-
PUBSUB_CONTACTS_CHANGED_TOPIC_NAME: contactsChangedTopicName,
95-
INTEGRATION_NAME: integrationName,
96-
} = process.env;
89+
private initContactStreaming() {
90+
const {
91+
PUBSUB_TOPIC_NAME: topicNameLegacy,
92+
PUBSUB_TOPIC_NAME_CONTACT_STREAMING: topicName,
93+
} = process.env;
9794

98-
if (!contactsChangedTopicName) {
99-
throw new Error('No PUBSUB_CONTACTS_CHANGED_TOPIC_NAME provided.');
100-
}
95+
const topicNameProvided = topicName ?? topicNameLegacy;
10196

102-
if (!integrationName) {
103-
throw new Error('No INTEGRATION_NAME provided.');
104-
}
105-
this.integrationName = integrationName;
97+
if (!topicNameProvided) {
98+
throw new Error('No PUBSUB_TOPIC_NAME_CONTACT_STREAMING provided.');
99+
}
106100

107-
this.pubSubContactsChangedClient = new PubSubContactChangeEventClient(
108-
contactsChangedTopicName,
109-
);
110-
infoLogger(
111-
'Controller',
112-
`Initialized PubSub client with topic ${contactsChangedTopicName}`,
113-
);
101+
this.pubSubContactStreamingClient = new PubSubClient(topicNameProvided);
102+
103+
infoLogger(
104+
'Controller',
105+
`Initialized PubSub client for topic ${topicName}`,
106+
);
107+
}
108+
109+
private initContactChanges() {
110+
const { PUBSUB_TOPIC_NAME_CONTACT_CHANGES: topicName } = process.env;
111+
112+
if (!topicName) {
113+
throw new Error('No PUBSUB_TOPIC_NAME_CONTACT_CHANGES provided.');
114114
}
115+
116+
this.pubSubContactChangesClient = new PubSubClient(topicName);
117+
118+
infoLogger(
119+
'Controller',
120+
`Initialized PubSub client for topic ${topicName}`,
121+
);
115122
}
116123

117124
public async isValidToken(
@@ -314,7 +321,7 @@ export class Controller {
314321
integrationName: this.integrationName,
315322
};
316323

317-
await this.pubSubClient?.publishMessage(message);
324+
await this.pubSubContactStreamingClient?.publishMessage(message);
318325
} catch (error) {
319326
errorLogger(
320327
'streamContacts',
@@ -330,7 +337,7 @@ export class Controller {
330337

331338
const streamingPromise = streamContacts()
332339
.then(() =>
333-
this.pubSubClient?.publishMessage({
340+
this.pubSubContactStreamingClient?.publishMessage({
334341
userId: providerConfig.userId,
335342
timestamp,
336343
contacts: [],
@@ -345,7 +352,7 @@ export class Controller {
345352
providerConfig.apiKey,
346353
error,
347354
);
348-
return this.pubSubClient?.publishMessage({
355+
return this.pubSubContactStreamingClient?.publishMessage({
349356
userId: providerConfig.userId,
350357
timestamp,
351358
contacts: [],
@@ -1427,7 +1434,7 @@ export class Controller {
14271434
...changeEvent,
14281435
};
14291436

1430-
this.pubSubContactsChangedClient?.publishMessage(message);
1437+
this.pubSubContactChangesClient?.publishMessage(message);
14311438
});
14321439

14331440
infoLogger('handleWebhook', 'END', '');

‎src/models/pubsub-client.model.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { PubSub } from '@google-cloud/pubsub';
22
import { timeout } from '../util/timeout';
3-
import { PubSubContactsMessage } from './pubsub-contacts-message.model';
43

54
const PUBLISH_TIMEOUT = 10_000;
65

7-
export class PubSubClient {
6+
export class PubSubClient<T> {
87
private client: PubSub;
98
private topicName: string;
109

@@ -13,7 +12,7 @@ export class PubSubClient {
1312
this.topicName = topicName;
1413
}
1514

16-
async publishMessage(message: PubSubContactsMessage) {
15+
async publishMessage(message: T) {
1716
if (!this.topicName) {
1817
throw new Error('No pubsub topic name provided.');
1918
}

‎src/models/pubsub-contact-change-event-client.model.ts‎

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)