Skip to content

Commit

Permalink
refactor: rename closures to use 'create' prefix for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
psanders committed Dec 28, 2024
1 parent 515ef7a commit ebc2bdd
Show file tree
Hide file tree
Showing 64 changed files with 345 additions and 173 deletions.
4 changes: 2 additions & 2 deletions mods/apiserver/src/calls/buildService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { createCall } from "./createCall";
import { createCallPublisher } from "./createCallPublisher";
import { getCall } from "./getCall";
import { listCalls } from "./listCalls";
import { makeTrackCall } from "./makeTrackCall";
import { createTrackCall } from "./createTrackCall";
import { InfluxDBClient } from "@fonoster/common";
import { prisma } from "../core/db";
import { NATS_URL } from "../envs";
Expand All @@ -41,7 +41,7 @@ async function buildService(influxdb: InfluxDBClient) {
createCall: createCall(prisma, callPublisher),
listCalls: listCalls(influxdb),
getCall: getCall(influxdb),
trackCall: makeTrackCall(nc)
trackCall: createTrackCall(nc)
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const FINAL_STATUSES = [

const logger = getLogger({ service: "apiserver", filePath: __filename });

function makeTrackCall(nc: NatsConnection) {
function createTrackCall(nc: NatsConnection) {
const trackingCallsMap = new Map<string, CallStream>();
const subscription = nc.subscribe(CALLS_TRACK_CALL_SUBJECT);

Expand Down Expand Up @@ -68,7 +68,7 @@ function makeTrackCall(nc: NatsConnection) {
}
};

const fn = (call: { request: BaseApiObject }) => {
const trackCall = (call: { request: BaseApiObject }) => {
const stream = call as unknown as CallStream;
const { ref } = call.request;

Expand All @@ -77,7 +77,7 @@ function makeTrackCall(nc: NatsConnection) {
trackingCallsMap.set(ref, stream);
};

return withErrorHandlingAndValidation(fn, V.baseApiObjectSchema);
return withErrorHandlingAndValidation(trackCall, V.baseApiObjectSchema);
}

export { makeTrackCall };
export { createTrackCall };
4 changes: 2 additions & 2 deletions mods/apiserver/src/core/runServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ import {
NATS_URL
} from "../envs";
import { connectToAri } from "../voice/connectToAri";
import { makeCheckMethodAuthorized } from "@fonoster/authz";
import { createCheckMethodAuthorized } from "@fonoster/authz";

const logger = getLogger({ service: "apiserver", filePath: __filename });

const authorization = createAuthInterceptor(IDENTITY_PUBLIC_KEY, allowList);
const checkMethodAuthorized = makeCheckMethodAuthorized(
const checkMethodAuthorized = createCheckMethodAuthorized(
`${AUTHZ_SERVICE_HOST}:${AUTHZ_SERVICE_PORT}`,
AUTHZ_SERVICE_METHODS
);
Expand Down
4 changes: 2 additions & 2 deletions mods/apiserver/src/core/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { buildApplicationsService } from "../applications";
import { buildCallsService } from "../calls";
import { influxdb } from "../calls/influxdb";
import { buildSecretsService } from "../secrets";
import { makeCheckNumberPreconditions } from "../utils";
import { createCheckNumberPreconditions } from "../utils";

const applicationsService = buildApplicationsService(prisma);
const secretsService = buildSecretsService(prisma);
Expand All @@ -44,7 +44,7 @@ const credentialsService = buildCredentialsService(routrConfig);
const trunksService = buildTrunksService(routrConfig);
const numbersService = buildNumbersService(
routrConfig,
makeCheckNumberPreconditions(prisma)
createCheckNumberPreconditions(prisma)
);
const aclsService = buildAclsService(routrConfig);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { GrpcError } from "@fonoster/common";
import { status } from "@grpc/grpc-js";
import { Prisma } from "../core/db";

function makeCheckNumberPreconditions(prisma: Prisma) {
function createCheckNumberPreconditions(prisma: Prisma) {
return async function checkNumberPreconditions({ appRef, accessKeyId }) {
if (!appRef) {
// Not needed to check for the precondition
Expand All @@ -40,4 +40,4 @@ function makeCheckNumberPreconditions(prisma: Prisma) {
};
}

export { makeCheckNumberPreconditions };
export { createCheckNumberPreconditions };
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { NatsConnection } from "nats";
import { mapDialStatus } from "./mapDialStatus";
import { CALLS_TRACK_CALL_SUBJECT } from "../envs";

function makeHandleDialEventsWithNats(nc: NatsConnection) {
return async (callRef: string, event: { dialstatus: string }) => {
function createHandleDialEventsWithNats(nc: NatsConnection) {
return async function handleDialEventsWithNats(callRef: string, event: { dialstatus: string }) {
const mappedStatus = mapDialStatus(event.dialstatus);
if (!mappedStatus) return; // Ignore the event if status is not mapped

Expand All @@ -32,4 +32,4 @@ function makeHandleDialEventsWithNats(nc: NatsConnection) {
};
}

export { makeHandleDialEventsWithNats };
export { createHandleDialEventsWithNats };
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import { mapDialStatus } from "./mapDialStatus";
import { VoiceClient } from "../voice/types";

function makeHandleDialEventsWithVoiceClient(voiceClient: VoiceClient) {
return async (event: { dialstatus: string }) => {
function createHandleDialEventsWithVoiceClient(voiceClient: VoiceClient) {
return async function handleDialEventsWithVoiceClient(event: { dialstatus: string }) {
const mappedStatus = mapDialStatus(event.dialstatus);
if (!mappedStatus) return; // Ignore the event if status is not mapped

Expand All @@ -32,4 +32,4 @@ function makeHandleDialEventsWithVoiceClient(voiceClient: VoiceClient) {
};
}

export { makeHandleDialEventsWithVoiceClient };
export { createHandleDialEventsWithVoiceClient };
6 changes: 3 additions & 3 deletions mods/apiserver/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from "./makeCheckNumberPreconditions";
export * from "./makeHandleDialEventsWithNats";
export * from "./makeHandleDialEventsWithVoiceClient";
export * from "./createCheckNumberPreconditions";
export * from "./createHandleDialEventsWithNats";
export * from "./createHandleDialEventsWithVoiceClient";
58 changes: 29 additions & 29 deletions mods/apiserver/src/voice/VoiceDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ import { getLogger } from "@fonoster/logger";
import { Channel, Client, Dial, StasisStart } from "ari-client";
import { NatsConnection } from "nats";
import {
answerHandler,
dialHandler,
gatherHandler,
hangupHandler,
muteHandler,
playDtmfHandler,
playHandler,
playbackControlHandler,
sayHandler,
streamGatherHandler,
unmuteHandler
createAnswerHandler,
createDialHandler,
createGatherHandler,
createHangupHandler,
createMuteHandler,
createPlayDtmfHandler,
createPlayHandler,
createPlaybackControlHandler,
createSayHandler,
createStreamGatherHandler,
createUnmuteHandler,
createStreamHandler
} from "./handlers";
import { streamHandler } from "./handlers/Stream";
import { AriEvent as AE, ChannelVar, VoiceClient } from "./types";
import { makeHandleDialEventsWithNats } from "../utils";
import { makeGetChannelVarWithoutThrow } from "./utils/makeGetChannelVarWithoutThrow";
import { createHandleDialEventsWithNats } from "../utils";
import { createGetChannelVarWithoutThrow } from "./utils";

const logger = getLogger({ service: "apiserver", filePath: __filename });

Expand Down Expand Up @@ -75,7 +75,7 @@ class VoiceDispatcher {
async handleStasisStart(event: StasisStart, channel: Channel) {
const { ari, voiceClients, createVoiceClient, isHandledElsewhere } = this;

const getChannelVar = makeGetChannelVarWithoutThrow(channel);
const getChannelVar = createGetChannelVarWithoutThrow(channel);
const appRef = (await getChannelVar(ChannelVar.APP_REF))?.value;

// This check feels strange but is necessary as ARI calls this event twice
Expand All @@ -96,24 +96,24 @@ class VoiceDispatcher {

voiceClients.set(channel.id, vc);

vc.on(SC.ANSWER_REQUEST, answerHandler(ari, vc).bind(this));
vc.on(SC.HANGUP_REQUEST, hangupHandler(ari, vc).bind(this));
vc.on(SC.MUTE_REQUEST, muteHandler(ari, vc).bind(this));
vc.on(SC.UNMUTE_REQUEST, unmuteHandler(ari, vc).bind(this));
vc.on(SC.PLAY_REQUEST, playHandler(ari, vc).bind(this));
vc.on(SC.PLAY_DTMF_REQUEST, playDtmfHandler(ari, vc).bind(this));
vc.on(SC.SAY_REQUEST, sayHandler(ari, vc).bind(this));
vc.on(SC.GATHER_REQUEST, gatherHandler(vc).bind(this));
vc.on(SC.DIAL_REQUEST, dialHandler(ari, vc).bind(this));
vc.on(SC.ANSWER_REQUEST, createAnswerHandler(ari, vc).bind(this));
vc.on(SC.HANGUP_REQUEST, createHangupHandler(ari, vc).bind(this));
vc.on(SC.MUTE_REQUEST, createMuteHandler(ari, vc).bind(this));
vc.on(SC.UNMUTE_REQUEST, createUnmuteHandler(ari, vc).bind(this));
vc.on(SC.PLAY_REQUEST, createPlayHandler(ari, vc).bind(this));
vc.on(SC.PLAY_DTMF_REQUEST, createPlayDtmfHandler(ari, vc).bind(this));
vc.on(SC.SAY_REQUEST, createSayHandler(ari, vc).bind(this));
vc.on(SC.GATHER_REQUEST, createGatherHandler(vc).bind(this));
vc.on(SC.DIAL_REQUEST, createDialHandler(ari, vc).bind(this));
vc.on(
SC.PLAYBACK_CONTROL_REQUEST,
playbackControlHandler(ari, vc).bind(this)
createPlaybackControlHandler(ari, vc).bind(this)
);
vc.on(SC.START_STREAM_GATHER_REQUEST, streamGatherHandler(vc).bind(this));
vc.on(SC.START_STREAM_GATHER_REQUEST, createStreamGatherHandler(vc).bind(this));
vc.on(SC.STOP_STREAM_GATHER_REQUEST, () => {
vc.stopStreamGather();
});
vc.on(SC.START_STREAM_REQUEST, streamHandler(vc).bind(this));
vc.on(SC.START_STREAM_REQUEST, createStreamHandler(vc).bind(this));
} catch (err) {
logger.error("error handling stasis start", { error: err.message });
}
Expand All @@ -131,13 +131,13 @@ class VoiceDispatcher {
}

async handleDial(event: Dial, channel: Channel) {
makeHandleDialEventsWithNats(this.nc)(channel.id, event);
createHandleDialEventsWithNats(this.nc)(channel.id, event);
}

async isHandledElsewhere(channel: Channel) {
return (
(
await makeGetChannelVarWithoutThrow(channel)(
await createGetChannelVarWithoutThrow(channel)(
ChannelVar.FROM_EXTERNAL_MEDIA
)
)?.value === "true"
Expand Down
8 changes: 4 additions & 4 deletions mods/apiserver/src/voice/connectToAri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { getLogger } from "@fonoster/logger";
import ariClient from "ari-client";
import { connect } from "nats";
import wait from "wait-port";
import { makeCreateContainer } from "./integrations";
import { makeCreateVoiceClient } from "./makeCreateVoiceClient";
import { createCreateContainer } from "./integrations";
import { createCreateVoiceClient } from "./createCreateVoiceClient";
import { AriEvent } from "./types";
import { VoiceDispatcher } from "./VoiceDispatcher";
import { prisma } from "../core/db";
Expand Down Expand Up @@ -63,14 +63,14 @@ async function connectToAri(filesServer) {

logger.info("asterisk is ready");

const createContainer = makeCreateContainer(prisma, INTEGRATIONS_FILE);
const createContainer = createCreateContainer(prisma, INTEGRATIONS_FILE);

const nats = await connect({ servers: NATS_URL, maxReconnectAttempts: -1 });

const dispatcher = new VoiceDispatcher(
ari,
nats,
makeCreateVoiceClient(createContainer, filesServer)
createCreateVoiceClient(createContainer, filesServer)
);

dispatcher.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { getLogger } from "@fonoster/logger";
import { Channel, Client, StasisStart } from "ari-client";
import { CreateContainer } from "./integrations/types";
import { ChannelVar, VoiceClient } from "./types";
import { makeGetChannelVarWithoutThrow } from "./utils/makeGetChannelVarWithoutThrow";
import { createGetChannelVarWithoutThrow } from "./utils/createGetChannelVarWithoutThrow";
import { VoiceClientImpl } from "./VoiceClientImpl";
import { identityConfig } from "../core/identityConfig";
import { mapCallDirectionToEnum } from "../events/mapCallDirectionToEnum";
Expand All @@ -31,17 +31,17 @@ const logger = getLogger({ service: "apiserver", filePath: __filename });
const createToken = createCallAccessToken(identityConfig);

// Note: By the time the call arrives here the owner of the app MUST be authenticated
function makeCreateVoiceClient(createContainer: CreateContainer, filesServer) {
return async (params: {
function createCreateVoiceClient(createContainer: CreateContainer, filesServer) {
return async function createVoiceClient(params: {
ari: Client;
event: StasisStart;
channel: Channel;
}): Promise<VoiceClient> => {
}): Promise<VoiceClient> {
const { ari, event, channel } = params;
const { id: sessionRef, caller } = event.channel;
const { name: callerName, number: callerNumber } = caller;

const getChannelVar = makeGetChannelVarWithoutThrow(channel);
const getChannelVar = createGetChannelVarWithoutThrow(channel);

// Variables set by Asterisk's dialplan
const callDirection = (await getChannelVar(ChannelVar.CALL_DIRECTION))
Expand Down Expand Up @@ -79,4 +79,4 @@ function makeCreateVoiceClient(createContainer: CreateContainer, filesServer) {
};
}

export { makeCreateVoiceClient };
export { createCreateVoiceClient };
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Client } from "ari-client";
import { VoiceClient } from "../types";
import { withErrorHandling } from "./utils/withErrorHandling";

function answerHandler(ari: Client, voiceClient: VoiceClient) {
function createAnswerHandler(ari: Client, voiceClient: VoiceClient) {
return withErrorHandling(async (request: VerbRequest) => {
const { sessionRef } = request;

Expand All @@ -35,4 +35,4 @@ function answerHandler(ari: Client, voiceClient: VoiceClient) {
});
}

export { answerHandler };
export { createAnswerHandler };
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Client } from "ari-client";
import { VoiceClient } from "../types";
import { withErrorHandling } from "./utils/withErrorHandling";

function hangupHandler(ari: Client, voiceClient: VoiceClient) {
function createHangupHandler(ari: Client, voiceClient: VoiceClient) {
return withErrorHandling(async (request: VerbRequest) => {
const { sessionRef } = request;

Expand All @@ -40,4 +40,4 @@ function hangupHandler(ari: Client, voiceClient: VoiceClient) {
});
}

export { hangupHandler };
export { createHangupHandler };
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Client } from "ari-client";
import { VoiceClient } from "../types";
import { withErrorHandling } from "./utils/withErrorHandling";

function muteHandler(ari: Client, voiceClient: VoiceClient) {
function createMuteHandler(ari: Client, voiceClient: VoiceClient) {
return withErrorHandling(async (request: MuteRequest) => {
const { sessionRef, direction } = request;

Expand All @@ -38,4 +38,4 @@ function muteHandler(ari: Client, voiceClient: VoiceClient) {
});
}

export { muteHandler };
export { createMuteHandler };
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Client } from "ari-client";
import { VoiceClient } from "../types";
import { withErrorHandling } from "./utils/withErrorHandling";

function playDtmfHandler(ari: Client, voiceClient: VoiceClient) {
function createPlayDtmfHandler(ari: Client, voiceClient: VoiceClient) {
return withErrorHandling(async (request: PlayDtmfRequest) => {
const { sessionRef, digits } = request;

Expand All @@ -38,4 +38,4 @@ function playDtmfHandler(ari: Client, voiceClient: VoiceClient) {
});
}

export { playDtmfHandler };
export { createPlayDtmfHandler };
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { VoiceClient } from "../types";
import { awaitForPlaybackFinished } from "./utils/awaitForPlaybackFinished";
import { withErrorHandling } from "./utils/withErrorHandling";

function playHandler(ari: Client, voiceClient: VoiceClient) {
function createPlayHandler(ari: Client, voiceClient: VoiceClient) {
return withErrorHandling(async (request: PlayRequest) => {
const { sessionRef } = request;

Expand All @@ -46,4 +46,4 @@ function playHandler(ari: Client, voiceClient: VoiceClient) {
});
}

export { playHandler };
export { createPlayHandler };
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const requestSchema = z.object({
})
});

function playbackControlHandler(ari: Client, voiceClient: VoiceClient) {
function createPlaybackControlHandler(ari: Client, voiceClient: VoiceClient) {
return withErrorHandling(
async (playbackControlReq: PlaybackControlRequest) => {
requestSchema.parse(playbackControlReq);
Expand Down Expand Up @@ -63,4 +63,4 @@ function playbackControlHandler(ari: Client, voiceClient: VoiceClient) {
);
}

export { playbackControlHandler };
export { createPlaybackControlHandler };
Loading

0 comments on commit ebc2bdd

Please sign in to comment.