Skip to content

Commit d8a2c75

Browse files
committed
Enforce config type
1 parent b09a132 commit d8a2c75

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

apps/functions-subscription/src/adapters/azure/functions/__tests__/process-activations-changes.test.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import * as O from 'fp-ts/Option';
99
import * as TE from 'fp-ts/TaskEither';
1010
import { makeActivationsChangesHandler } from '../process-activations-changes';
1111

12-
const config = { activations: { consumer: 'on' as const, maxFetchSize: 1 } };
12+
const config = {
13+
activations: {
14+
consumer: 'on' as const,
15+
maxFetchSize: 1,
16+
connectionString: 'aConnectionString',
17+
},
18+
};
1319

1420
describe('makeActivationJobConsumerHandler', () => {
1521
it('should call processActivationJob as expected', async () => {

apps/functions-subscription/src/config.ts

+47-23
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,32 @@ import {
88
WithinRangeInteger,
99
} from '@pagopa/ts-commons/lib/numbers';
1010

11+
type OnOrOff = 'on' | 'off';
12+
13+
interface ActiveConsumer {
14+
readonly consumer: Extract<OnOrOff, 'on'>;
15+
readonly connectionString: string;
16+
}
17+
interface InactiveConsumer {
18+
readonly consumer: Extract<OnOrOff, 'off'>;
19+
}
20+
type Consumer = ActiveConsumer | InactiveConsumer;
21+
22+
const OnOrOffCodec = t.keyof<{
23+
readonly [K in OnOrOff]: unknown;
24+
}>({
25+
on: null,
26+
off: null,
27+
});
28+
1129
export interface Config {
12-
readonly subscriptionHistory: {
13-
readonly consumer: 'on' | 'off';
14-
};
15-
readonly subscriptionRequest: {
16-
readonly consumer: 'on' | 'off';
17-
};
18-
readonly activations: {
19-
readonly consumer: 'on' | 'off';
20-
readonly maxFetchSize: number;
21-
};
30+
readonly subscriptionHistory: Consumer;
31+
readonly subscriptionRequest: Consumer;
32+
readonly activations: Consumer & { readonly maxFetchSize: number };
2233
readonly events: {
23-
readonly producer: 'on' | 'off';
24-
};
25-
readonly trials: {
26-
readonly consumer: 'on' | 'off';
34+
readonly producer: OnOrOff;
2735
};
36+
readonly trials: Consumer;
2837
readonly servicebus: {
2938
readonly namespace: string;
3039
readonly names: {
@@ -54,12 +63,7 @@ export interface Config {
5463
};
5564
}
5665

57-
const OnOrOffCodec = t.keyof({
58-
on: null,
59-
off: null,
60-
});
61-
62-
const EnvsCodec = t.strict({
66+
const EnvsCodec = t.type({
6367
COSMOSDB_ENDPOINT: NonEmptyString,
6468
COSMOSDB_DATABASE_NAME: NonEmptyString,
6569
EVENTHUB_NAMESPACE: NonEmptyString,
@@ -79,6 +83,14 @@ const EnvsCodec = t.strict({
7983
SUBSCRIPTION_ID: NonEmptyString,
8084
SERVICE_BUS_RESOURCE_GROUP_NAME: NonEmptyString,
8185
SERVICE_BUS_LOCATION: NonEmptyString,
86+
SubscriptionHistoryCosmosConnection: t.union([NonEmptyString, t.undefined]),
87+
SubscriptionHistoryCosmosConnection__accountName: NonEmptyString,
88+
ActivationConsumerCosmosDBConnection: t.union([NonEmptyString, t.undefined]),
89+
ActivationConsumerCosmosDBConnection__accountEndpoint: NonEmptyString,
90+
SubscriptionRequestEventHubConnection: t.union([NonEmptyString, t.undefined]),
91+
SubscriptionRequestEventHubConnection__accountName: NonEmptyString,
92+
TrialsCosmosConnection: t.union([NonEmptyString, t.undefined]),
93+
TrialsCosmosConnection__accountEndpoint: NonEmptyString,
8294
});
8395

8496
export const parseConfig = (
@@ -91,19 +103,31 @@ export const parseConfig = (
91103
(envs) => ({
92104
subscriptionHistory: {
93105
consumer: envs.SUBSCRIPTION_HISTORY_CONSUMER,
106+
connectionString:
107+
envs.SubscriptionHistoryCosmosConnection ||
108+
envs.SubscriptionHistoryCosmosConnection__accountName,
94109
},
95110
subscriptionRequest: {
96-
consumer: envs.SUBSCRIPTION_REQUEST_CONSUMER,
111+
consumer: envs.SUBSCRIPTION_HISTORY_CONSUMER,
112+
connectionString:
113+
envs.SubscriptionRequestEventHubConnection ||
114+
envs.SubscriptionRequestEventHubConnection__accountName,
97115
},
98116
activations: {
99-
consumer: envs.ACTIVATION_CONSUMER,
117+
consumer: envs.SUBSCRIPTION_HISTORY_CONSUMER,
118+
connectionString:
119+
envs.ActivationConsumerCosmosDBConnection ||
120+
envs.ActivationConsumerCosmosDBConnection__accountEndpoint,
100121
maxFetchSize: envs.ACTIVATION_MAX_FETCH_SIZE,
101122
},
102123
events: {
103124
producer: envs.EVENTS_PRODUCER,
104125
},
105126
trials: {
106-
consumer: envs.TRIAL_CONSUMER,
127+
consumer: envs.SUBSCRIPTION_HISTORY_CONSUMER,
128+
connectionString:
129+
envs.TrialsCosmosConnection ||
130+
envs.TrialsCosmosConnection__accountEndpoint,
107131
},
108132
servicebus: {
109133
namespace: envs.SERVICEBUS_NAMESPACE,

0 commit comments

Comments
 (0)