Skip to content

Commit 1c245bc

Browse files
committed
rename webhook related events
1 parent 8200f72 commit 1c245bc

10 files changed

+82
-57
lines changed

src/models/adapter.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
LoggedIntegrationEntity,
1515
} from '.';
1616
import { IntegrationEntityType } from './integration-entity.model';
17-
import { ContactChangeEvent } from './contact-change-event.model';
17+
import { IntegrationsEvent } from './integrations-event.model';
1818

1919
export interface Adapter {
2020
getToken?: (config: Config) => Promise<{ apiKey: string }>;
@@ -88,6 +88,6 @@ export interface Adapter {
8888
req: Request,
8989
res?: Response,
9090
) => Promise<{ apiKey: string; apiUrl: string }>;
91-
handleWebhook?: (req: Request) => Promise<ContactChangeEvent[]>;
91+
handleWebhook?: (req: Request) => Promise<IntegrationsEvent[]>;
9292
verifyWebhookRequest?: (req: Request) => Promise<boolean>;
9393
}

src/models/contact-change-event.model.ts

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

src/models/controller.model.ts

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import {
1010
CallEventWithIntegrationEntities,
1111
Contact,
1212
ContactCache,
13-
ContactChangeEvent,
1413
ContactDelta,
1514
ContactTemplate,
1615
ContactUpdate,
1716
IntegrationEntityType,
17+
IntegrationsEvent,
18+
PubSubClient,
19+
PubSubIntegrationsEventMessage,
1820
ServerError,
1921
} from '.';
2022
import { calendarEventsSchema, contactsSchema } from '../schemas';
@@ -32,12 +34,11 @@ import {
3234
import { CacheItemStateType } from './cache-item-state.model';
3335
import { CalendarFilterOptions } from './calendar-filter-options.model';
3436
import { IntegrationErrorType } from './integration-error.model';
35-
import { PubSubClient } from './pubsub-client.model';
36-
import { PubSubContactChangeEventMessage } from './pubsub-contact-change-event-message.model';
37+
3738
import {
3839
PubSubContactsMessage,
3940
PubSubContactsState,
40-
} from './pubsub-contacts-message.model';
41+
} from './pubsub/pubsub-contacts-message.model';
4142

4243
const CONTACT_FETCH_TIMEOUT = 5000;
4344

@@ -57,7 +58,7 @@ export class Controller {
5758
private ajv: Ajv;
5859
private pubSubContactStreamingClient: PubSubClient<PubSubContactsMessage> | null =
5960
null;
60-
private pubSubContactChangesClient: PubSubClient<PubSubContactChangeEventMessage> | null =
61+
private pubSubIntegrationEventsClient: PubSubClient<PubSubIntegrationsEventMessage> | null =
6162
null;
6263
private integrationName: string = 'UNKNOWN';
6364
private streamingPromises = new Map<string, Promise<void>>();
@@ -107,13 +108,13 @@ export class Controller {
107108
}
108109

109110
private initContactChanges() {
110-
const { PUBSUB_TOPIC_NAME_CONTACT_CHANGES: topicName } = process.env;
111+
const { PUBSUB_TOPIC_NAME_INTEGRATION_EVENTS: topicName } = process.env;
111112

112113
if (!topicName) {
113-
throw new Error('No PUBSUB_TOPIC_NAME_CONTACT_CHANGES provided.');
114+
throw new Error('No PUBSUB_TOPIC_NAME_INTEGRATION_EVENTS provided.');
114115
}
115116

116-
this.pubSubContactChangesClient = new PubSubClient(topicName);
117+
this.pubSubIntegrationEventsClient = new PubSubClient(topicName);
117118

118119
infoLogger(
119120
'Controller',
@@ -1411,36 +1412,42 @@ export class Controller {
14111412
const verified = await this.adapter.verifyWebhookRequest(req);
14121413

14131414
if (!verified) {
1414-
errorLogger('handleWebhook', 'Webhook verification failed', '');
1415+
errorLogger('handleWebhook', 'Webhook verification failed');
14151416
throw new ServerError(403, 'Webhook verification failed');
14161417
}
14171418

1418-
infoLogger('handleWebhook', 'START', '');
1419-
1420-
const changeEvents: ContactChangeEvent[] =
1421-
await this.adapter.handleWebhook(req);
1419+
infoLogger('handleWebhook', 'START');
14221420

1423-
infoLogger(
1424-
'handleWebhook',
1425-
`Got ${changeEvents.length} changed contacts`,
1426-
'',
1427-
);
1421+
const events: IntegrationsEvent[] = await this.adapter.handleWebhook(req);
14281422

1429-
const deduplicatedChangeEvents = uniqWith(changeEvents, isEqual);
1423+
infoLogger('handleWebhook', `Got ${events.length} events`);
14301424

1431-
deduplicatedChangeEvents.map((changeEvent: ContactChangeEvent) => {
1432-
infoLogger(
1433-
'handleWebhook',
1434-
`Publishing contact change event with accountId ${changeEvent.accountId} and contactId ${changeEvent.contactId}`,
1435-
'',
1436-
);
1425+
const deduplicatedEvents = uniqWith(events, isEqual);
14371426

1438-
const message: PubSubContactChangeEventMessage = {
1439-
integrationName: this.integrationName,
1440-
...changeEvent,
1441-
};
1427+
const publishResults = await Promise.allSettled(
1428+
deduplicatedEvents
1429+
.map<PubSubIntegrationsEventMessage>((event: IntegrationsEvent) => ({
1430+
integrationName: this.integrationName,
1431+
...event,
1432+
}))
1433+
.map((message) => {
1434+
infoLogger(
1435+
'handleWebhook',
1436+
`Publishing event ${message.type} with accountId ${message.accountId}`,
1437+
);
1438+
return this.pubSubIntegrationEventsClient?.publishMessage(message);
1439+
}),
1440+
);
14421441

1443-
this.pubSubContactChangesClient?.publishMessage(message);
1442+
publishResults.forEach((result) => {
1443+
if (result.status === 'rejected') {
1444+
errorLogger(
1445+
'handleWebhook',
1446+
`Could not publish event ${result.reason.type} with accountId ${result.reason.accountId}`,
1447+
'',
1448+
result.reason,
1449+
);
1450+
}
14441451
});
14451452

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

src/models/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
export * from './adapter.model';
2+
export * from './api-contact.model';
23
export * from './bridge-request.model';
4+
export * from './cache-item-state.model';
35
export * from './calendar-event.model';
46
export * from './calendar-filter-options.model';
57
export * from './call-direction.enum';
68
export * from './call-event.model';
79
export * from './config.model';
810
export * from './contact-cache.model';
911
export * from './contact.model';
10-
export * from './contact-change-event.model';
1112
export * from './controller.model';
1213
export * from './custom-router.model';
13-
export * from './server-error.model';
14-
export * from './user.model';
15-
export * from './integration-error.model';
14+
export * from './custom-routes.model';
1615
export * from './integration-entity.model';
16+
export * from './integration-error.model';
17+
export * from './integrations-event.model';
18+
export * from './pubsub';
19+
export * from './server-error.model';
20+
export * from './storage-adapter.model';
21+
export * from './token-cache.model';
1722
export * from './token.model';
23+
export * from './user.model';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { IntegrationEntityType } from './integration-entity.model';
2+
3+
export enum IntegrationsEventType {
4+
CONTACT_CREATED = 'CONTACT_CREATED',
5+
CONTACT_UPDATED = 'CONTACT_UPDATED',
6+
CONTACT_DELETED = 'CONTACT_DELETED',
7+
}
8+
9+
export type IntegrationsBaseEvent<T> = {
10+
type: T;
11+
accountId: string;
12+
};
13+
14+
export type IntegrationsContactEvent = IntegrationsBaseEvent<
15+
| IntegrationsEventType.CONTACT_CREATED
16+
| IntegrationsEventType.CONTACT_UPDATED
17+
| IntegrationsEventType.CONTACT_DELETED
18+
> & {
19+
contactId: string;
20+
contactType: IntegrationEntityType;
21+
};
22+
23+
export type IntegrationsEvent = IntegrationsContactEvent;

src/models/pubsub-contact-change-event-message.model.ts

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

src/models/pubsub/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './pubsub-client.model';
2+
export * from './pubsub-contacts-message.model';
3+
export * from './pubsub-integrations-event-message.model';

src/models/pubsub-client.model.ts renamed to src/models/pubsub/pubsub-client.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PubSub } from '@google-cloud/pubsub';
2-
import { timeout } from '../util/timeout';
2+
import { timeout } from '../../util/timeout';
33

44
const PUBLISH_TIMEOUT = 10_000;
55

src/models/pubsub-contacts-message.model.ts renamed to src/models/pubsub/pubsub-contacts-message.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Contact } from './contact.model';
1+
import { Contact } from '../contact.model';
22

33
export enum PubSubContactsState {
44
IN_PROGRESS = 'IN_PROGRESS',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { IntegrationsEvent } from '../integrations-event.model';
2+
3+
export type PubSubIntegrationsEventMessage = {
4+
integrationName: string;
5+
} & IntegrationsEvent;

0 commit comments

Comments
 (0)