Skip to content

Commit a7c2e2e

Browse files
committed
✨ add GET call-log-metadata
1 parent 5ffde2e commit a7c2e2e

File tree

4 files changed

+180
-126
lines changed

4 files changed

+180
-126
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import { NextFunction, Response } from 'express';
2+
import {
3+
Adapter,
4+
BridgeRequest,
5+
CallEvent,
6+
CallEventWithIntegrationEntities,
7+
ServerError,
8+
} from '../models';
9+
import { errorLogger, infoLogger } from '../util';
10+
import { shouldSkipCallEvent } from '../util/call-event.util';
11+
12+
export class CallLogController {
13+
constructor(private readonly adapter: Adapter) {}
14+
15+
public async createCallLogForPhoneNumber(
16+
req: BridgeRequest<CallEvent>,
17+
res: Response,
18+
next: NextFunction,
19+
): Promise<void> {
20+
const { providerConfig } = req;
21+
22+
try {
23+
infoLogger(
24+
'createCallLogForPhoneNumber',
25+
`START`,
26+
providerConfig?.apiKey,
27+
);
28+
29+
if (!providerConfig) {
30+
throw new ServerError(400, 'Missing config parameters');
31+
}
32+
33+
if (!this.adapter.createCallLogForPhoneNumber) {
34+
throw new ServerError(
35+
501,
36+
'Creating call log for phoneNumber is not implemented',
37+
);
38+
}
39+
40+
if (shouldSkipCallEvent(req.body)) {
41+
infoLogger(
42+
'createCallLogForPhoneNumber',
43+
`Skipping call log for call id ${req.body.id}`,
44+
providerConfig.apiKey,
45+
);
46+
res.status(200).send([]);
47+
return;
48+
}
49+
50+
infoLogger(
51+
'createCallLogForPhoneNumber',
52+
`Creating call Log for PhoneNumber…`,
53+
providerConfig.apiKey,
54+
);
55+
56+
const loggedEntities = await this.adapter.createCallLogForPhoneNumber(
57+
providerConfig,
58+
req.body,
59+
);
60+
61+
infoLogger('createCallLogForPhoneNumber', `END`, providerConfig.apiKey);
62+
res.status(200).send(loggedEntities);
63+
} catch (error) {
64+
errorLogger(
65+
'createCallLogForPhoneNumber',
66+
'Could not create call log for phoneNumber:',
67+
providerConfig?.apiKey,
68+
error || 'Unknown',
69+
);
70+
next(error);
71+
}
72+
}
73+
74+
public async createOrUpdateCallLogsForEntities(
75+
req: BridgeRequest<CallEventWithIntegrationEntities>,
76+
res: Response,
77+
next: NextFunction,
78+
): Promise<void> {
79+
const { providerConfig } = req;
80+
81+
try {
82+
infoLogger(
83+
'createOrUpdateCallLogForEntities',
84+
`START`,
85+
providerConfig?.apiKey,
86+
);
87+
88+
if (!providerConfig) {
89+
throw new ServerError(400, 'Missing config parameters');
90+
}
91+
92+
if (!this.adapter.createOrUpdateCallLogForEntities) {
93+
throw new ServerError(
94+
501,
95+
'Updating call log with entities is not implemented',
96+
);
97+
}
98+
99+
if (shouldSkipCallEvent(req.body)) {
100+
infoLogger(
101+
'createOrUpdateCallLogForEntities',
102+
`Skipping call log for call id ${req.body.id}`,
103+
providerConfig.apiKey,
104+
);
105+
res.status(200).send([]);
106+
return;
107+
}
108+
109+
infoLogger(
110+
'createOrUpdateCallLogForEntities',
111+
`Creating and updating call Logs…`,
112+
providerConfig.apiKey,
113+
);
114+
115+
const entitiesWithCallLogReferences =
116+
await this.adapter.createOrUpdateCallLogForEntities(
117+
providerConfig,
118+
req.body,
119+
);
120+
121+
infoLogger(
122+
'createOrUpdateCallLogForEntities',
123+
`END`,
124+
providerConfig.apiKey,
125+
);
126+
res.status(200).send(entitiesWithCallLogReferences);
127+
} catch (error) {
128+
errorLogger(
129+
'createOrUpdateCallLogForEntities',
130+
'Could not update call logs with entities:',
131+
providerConfig?.apiKey,
132+
error || 'Unknown',
133+
);
134+
next(error);
135+
}
136+
}
137+
138+
async getCallLogMetadata(
139+
req: BridgeRequest<void>,
140+
res: Response,
141+
next: NextFunction,
142+
) {
143+
const { providerConfig } = req;
144+
145+
if (!providerConfig) {
146+
next(new Error('Provider config not found'));
147+
return;
148+
}
149+
150+
if (!this.adapter.getCallLogMetadata) {
151+
next(new Error('Method not implemented'));
152+
return;
153+
}
154+
155+
try {
156+
infoLogger('getCallLogMetadata', 'START', providerConfig.apiKey);
157+
const metadata = await this.adapter.getCallLogMetadata(providerConfig);
158+
159+
infoLogger(
160+
'getCallLogMetadata',
161+
`Found ${metadata.options?.length} options for Call Log Object`,
162+
providerConfig.apiKey,
163+
);
164+
165+
infoLogger('getCallLogMetadata', 'END', providerConfig.apiKey);
166+
res.json(metadata);
167+
} catch (err) {
168+
next(err);
169+
}
170+
}
171+
}

src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import cors from 'cors';
1313
import express from 'express';
1414
import { Server } from 'http';
1515
import { TokenCacheStorage } from './cache';
16+
import { CallLogController } from './controllers/call-log.controller';
1617
import { TaskController } from './controllers/task.controller';
1718
import { errorHandlerMiddleware, extractHeaderMiddleware } from './middlewares';
1819
import {
@@ -55,6 +56,7 @@ export function start(
5556

5657
const controller: Controller = new Controller(adapter, contactCache);
5758
const taskController: TaskController = new TaskController(adapter);
59+
const callLogController: CallLogController = new CallLogController(adapter);
5860

5961
app.get('/contacts', (req, res, next) =>
6062
controller.getContacts(req, res, next),
@@ -123,11 +125,15 @@ export function start(
123125
);
124126

125127
app.put('/call-log', (req, res, next) =>
126-
controller.createOrUpdateCallLogsForEntities(req, res, next),
128+
callLogController.createOrUpdateCallLogsForEntities(req, res, next),
127129
);
128130

129131
app.put('/call-log/phoneNumber', (req, res, next) =>
130-
controller.createCallLogForPhoneNumber(req, res, next),
132+
callLogController.createCallLogForPhoneNumber(req, res, next),
133+
);
134+
135+
app.get('/call-log-metadata', (req, res, next) =>
136+
callLogController.getCallLogMetadata(req, res, next),
131137
);
132138

133139
app.get('/health', (req, res, next) => controller.getHealth(req, res, next));

src/models/adapter.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export interface Adapter {
7979
config: Config,
8080
body: CallEvent,
8181
) => Promise<LoggedIntegrationEntity>;
82+
getCallLogMetadata?: (config: Config) => Promise<IntegrationDefinedOptions>;
8283
getEntity?: (
8384
providerConfig: Config,
8485
id: string,

src/models/controller.model.ts

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
CalendarEvent,
88
CalendarEventTemplate,
99
CallEvent,
10-
CallEventWithIntegrationEntities,
1110
Contact,
1211
ContactCache,
1312
ContactDelta,
@@ -1182,129 +1181,6 @@ export class Controller {
11821181
}
11831182
}
11841183

1185-
public async createCallLogForPhoneNumber(
1186-
req: BridgeRequest<CallEvent>,
1187-
res: Response,
1188-
next: NextFunction,
1189-
): Promise<void> {
1190-
const { providerConfig } = req;
1191-
1192-
try {
1193-
infoLogger(
1194-
'createCallLogForPhoneNumber',
1195-
`START`,
1196-
providerConfig?.apiKey,
1197-
);
1198-
1199-
if (!providerConfig) {
1200-
throw new ServerError(400, 'Missing config parameters');
1201-
}
1202-
1203-
if (!this.adapter.createCallLogForPhoneNumber) {
1204-
throw new ServerError(
1205-
501,
1206-
'Creating call log for phoneNumber is not implemented',
1207-
);
1208-
}
1209-
1210-
if (shouldSkipCallEvent(req.body)) {
1211-
infoLogger(
1212-
'createCallLogForPhoneNumber',
1213-
`Skipping call log for call id ${req.body.id}`,
1214-
providerConfig.apiKey,
1215-
);
1216-
res.status(200).send([]);
1217-
return;
1218-
}
1219-
1220-
infoLogger(
1221-
'createCallLogForPhoneNumber',
1222-
`Creating call Log for PhoneNumber…`,
1223-
providerConfig.apiKey,
1224-
);
1225-
1226-
const loggedEntities = await this.adapter.createCallLogForPhoneNumber(
1227-
providerConfig,
1228-
req.body,
1229-
);
1230-
1231-
infoLogger('createCallLogForPhoneNumber', `END`, providerConfig.apiKey);
1232-
res.status(200).send(loggedEntities);
1233-
} catch (error) {
1234-
errorLogger(
1235-
'createCallLogForPhoneNumber',
1236-
'Could not create call log for phoneNumber:',
1237-
providerConfig?.apiKey,
1238-
error || 'Unknown',
1239-
);
1240-
next(error);
1241-
}
1242-
}
1243-
1244-
public async createOrUpdateCallLogsForEntities(
1245-
req: BridgeRequest<CallEventWithIntegrationEntities>,
1246-
res: Response,
1247-
next: NextFunction,
1248-
): Promise<void> {
1249-
const { providerConfig } = req;
1250-
1251-
try {
1252-
infoLogger(
1253-
'createOrUpdateCallLogForEntities',
1254-
`START`,
1255-
providerConfig?.apiKey,
1256-
);
1257-
1258-
if (!providerConfig) {
1259-
throw new ServerError(400, 'Missing config parameters');
1260-
}
1261-
1262-
if (!this.adapter.createOrUpdateCallLogForEntities) {
1263-
throw new ServerError(
1264-
501,
1265-
'Updating call log with entities is not implemented',
1266-
);
1267-
}
1268-
1269-
if (shouldSkipCallEvent(req.body)) {
1270-
infoLogger(
1271-
'createOrUpdateCallLogForEntities',
1272-
`Skipping call log for call id ${req.body.id}`,
1273-
providerConfig.apiKey,
1274-
);
1275-
res.status(200).send([]);
1276-
return;
1277-
}
1278-
1279-
infoLogger(
1280-
'createOrUpdateCallLogForEntities',
1281-
`Creating and updating call Logs…`,
1282-
providerConfig.apiKey,
1283-
);
1284-
1285-
const entitiesWithCallLogReferences =
1286-
await this.adapter.createOrUpdateCallLogForEntities(
1287-
providerConfig,
1288-
req.body,
1289-
);
1290-
1291-
infoLogger(
1292-
'createOrUpdateCallLogForEntities',
1293-
`END`,
1294-
providerConfig.apiKey,
1295-
);
1296-
res.status(200).send(entitiesWithCallLogReferences);
1297-
} catch (error) {
1298-
errorLogger(
1299-
'createOrUpdateCallLogForEntities',
1300-
'Could not update call logs with entities:',
1301-
providerConfig?.apiKey,
1302-
error || 'Unknown',
1303-
);
1304-
next(error);
1305-
}
1306-
}
1307-
13081184
public async getEntity(
13091185
req: IntegrationEntityBridgeRequest,
13101186
res: Response,

0 commit comments

Comments
 (0)