Skip to content

Commit e5be78b

Browse files
committed
♻️ Add new ContactChangeEvent type
1 parent 0830d02 commit e5be78b

File tree

6 files changed

+45
-25
lines changed

6 files changed

+45
-25
lines changed

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ export function start(
132132
app.get('/account/id', (req, res, next) =>
133133
controller.getAccountId(req, res, next),
134134
);
135-
app.post('/webhook', (req, res, next) => controller.handleWebhook(req, res));
135+
136+
app.post('/webhook', (req, res, next) =>
137+
controller.handleWebhook(req, res, next),
138+
);
136139

137140
app.use(errorHandlerMiddleware);
138141

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 { ContactsChangedData } from './contacts-changed.model';
17+
import { ContactChangeEvent } from './contacts-changed.model';
1818

1919
export interface Adapter {
2020
getToken?: (config: Config) => Promise<{ apiKey: string }>;
@@ -84,6 +84,6 @@ export interface Adapter {
8484
req: Request,
8585
res?: Response,
8686
) => Promise<{ apiKey: string; apiUrl: string }>;
87-
handleWebhook?: (req: Request) => Promise<ContactsChangedData>;
87+
handleWebhook?: (req: Request) => Promise<ContactChangeEvent[]>;
8888
verifyWebhookRequest?: (req: Request) => Promise<boolean>;
8989
}
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { IntegrationEntityType } from './integration-entity.model';
22

3-
export type ContactsChangedData = {
4-
integrationName: string;
5-
data: {
6-
integrationAccountId: string;
7-
type: ContactsChangedDataType;
8-
contactId: string;
9-
contactType: IntegrationEntityType;
10-
}[];
3+
export type ContactChangeEvent = {
4+
accountId: string;
5+
changeType: ContactChangeEventType;
6+
contactId: string;
7+
contactType: IntegrationEntityType;
118
};
129

13-
export enum ContactsChangedDataType {
14-
UPDATE = 'UPDATE',
15-
CREATE = 'CREATE',
16-
DELETE = 'DELETE',
10+
export enum ContactChangeEventType {
11+
CREATED = 'CREATED',
12+
UPDATED = 'UPDATED',
13+
DELETED = 'DELETED',
1714
}

src/models/controller.model.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import {
99
CallEventWithIntegrationEntities,
1010
Contact,
1111
ContactCache,
12+
ContactChangeEvent,
1213
ContactDelta,
1314
ContactTemplate,
1415
ContactUpdate,
15-
ContactsChangedData,
1616
ServerError,
1717
} from '.';
1818
import { calendarEventsSchema, contactsSchema } from '../schemas';
@@ -36,6 +36,7 @@ import {
3636
PubSubContactsState,
3737
} from './pubsub-contacts-message.model';
3838
import { PubSubContactsChangedClient } from './pubsub-contacts-changed-client.model';
39+
import { PubSubContactChangeEventMessage } from './pubsub-contacts-changed-message.model';
3940

4041
const CONTACT_FETCH_TIMEOUT = 5000;
4142

@@ -1354,7 +1355,11 @@ export class Controller {
13541355
}
13551356
}
13561357

1357-
public async handleWebhook(req: Request, res: Response): Promise<void> {
1358+
public async handleWebhook(
1359+
req: Request,
1360+
res: Response,
1361+
next: NextFunction,
1362+
): Promise<void> {
13581363
if (!this.adapter.handleWebhook) {
13591364
throw new ServerError(501, 'Webhook handling not implemented');
13601365
}
@@ -1371,16 +1376,29 @@ export class Controller {
13711376
infoLogger('handleWebhook', 'START', '');
13721377

13731378
try {
1374-
const contactsChangedData: ContactsChangedData =
1379+
const changeEvents: ContactChangeEvent[] =
13751380
await this.adapter.handleWebhook(req);
13761381

13771382
infoLogger(
13781383
'handleWebhook',
1379-
`Got ${contactsChangedData.data.length} changed contacts`,
1384+
`Got ${changeEvents.length} changed contacts`,
13801385
'',
13811386
);
13821387

1383-
this.pubSubContactsChangedClient?.publishMessage(contactsChangedData);
1388+
changeEvents.map((changeEvent: ContactChangeEvent) => {
1389+
infoLogger(
1390+
'handleWebhook',
1391+
`Publishing contact change event with accountId ${changeEvent.accountId} and contactId ${changeEvent.contactId}`,
1392+
'',
1393+
);
1394+
1395+
const message: PubSubContactChangeEventMessage = {
1396+
integrationName: this.integrationName,
1397+
...changeEvent,
1398+
};
1399+
1400+
this.pubSubContactsChangedClient?.publishMessage(message);
1401+
});
13841402

13851403
infoLogger('handleWebhook', 'END', '');
13861404
res.sendStatus(200);
@@ -1391,7 +1409,7 @@ export class Controller {
13911409
'',
13921410
error || 'Unknown',
13931411
);
1394-
res.sendStatus(500);
1412+
next(error);
13951413
}
13961414
}
13971415
}

src/models/pubsub-contacts-changed-client.model.ts

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

55
const PUBLISH_TIMEOUT = 10_000;
66

@@ -13,7 +13,7 @@ export class PubSubContactsChangedClient {
1313
this.topicName = topicName;
1414
}
1515

16-
async publishMessage(message: PubSubContactsChangedMessage) {
16+
async publishMessage(message: PubSubContactChangeEventMessage) {
1717
if (!this.topicName) {
1818
throw new Error('No pubsub topic name provided.');
1919
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
import { ContactsChangedData } from './contacts-changed.model';
1+
import { ContactChangeEvent } from './contacts-changed.model';
22

3-
export type PubSubContactsChangedMessage = ContactsChangedData;
3+
export type PubSubContactChangeEventMessage = {
4+
integrationName: string;
5+
} & ContactChangeEvent;

0 commit comments

Comments
 (0)