This repository has been archived by the owner on Mar 9, 2020. It is now read-only.
/
compute_visible_services.ts
135 lines (118 loc) · 4.33 KB
/
compute_visible_services.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as documentDbUtils from "io-functions-commons/dist/src/utils/documentdb";
import * as winston from "winston";
import { Context } from "@azure/functions";
import { DocumentClient } from "documentdb";
import { pick } from "italia-ts-commons/lib/types";
import { insert, lookup, remove, StrMap } from "fp-ts/lib/StrMap";
import {
RetrievedService,
SERVICE_COLLECTION_NAME,
ServiceModel
} from "io-functions-commons/dist/src/models/service";
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { isLeft } from "fp-ts/lib/Either";
import { isNone, isSome } from "fp-ts/lib/Option";
import { VisibleService } from "io-functions-commons/dist/src/models/visible_service";
import { configureAzureContextTransport } from "io-functions-commons/dist/src/utils/logging";
interface IOutputBindings {
readonly visibleServicesBlob: Record<string, VisibleService>;
}
// Whether we're in a production environment
const isProduction = process.env.NODE_ENV === "production";
const cosmosDbUri = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);
const servicesCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
SERVICE_COLLECTION_NAME
);
const documentClient = new DocumentClient(cosmosDbUri, {
masterKey: cosmosDbKey
});
const serviceModel = new ServiceModel(documentClient, servicesCollectionUrl);
function reduceServicesToVisibleServices(
visibleServicesObj: StrMap<VisibleService>,
service: RetrievedService
): StrMap<VisibleService> {
// we use an in-memory object (map)
// to temporary store visible services
const maybeVisibleService = lookup(service.serviceId, visibleServicesObj);
if (
!service.isVisible &&
isSome(maybeVisibleService) &&
service.version > maybeVisibleService.value.version
) {
// if the service is not visible anymore
// delete it from the visible services list
return remove(service.serviceId, visibleServicesObj);
} else if (
// if the service is visible and we don't have a related
// cached visible service yet...
service.isVisible &&
(isNone(maybeVisibleService) ||
// ... or if the updated service is visible and the version
// is greater than the stored version of the cached visible service
service.version > maybeVisibleService.value.version)
) {
// store the visible service into the object (map)
return insert(
service.serviceId,
pick(
[
"departmentName",
"id",
"organizationFiscalCode",
"organizationName",
"serviceId",
"serviceName",
"version"
],
service
),
visibleServicesObj
);
}
return visibleServicesObj;
}
/**
* A function to cache the list of visible (active) services.
*
* Scheduled using a timer trigger, see
* https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/azure-functions/functions-bindings-timer.md
*/
export async function index(context: Context): Promise<void | IOutputBindings> {
const logLevel = isProduction ? "info" : "debug";
configureAzureContextTransport(context, winston, logLevel);
try {
// iterate over the whole services collection and collect visible services
const servicesCollectionIterator = await serviceModel.getCollectionIterator();
const servicesIterator = documentDbUtils.reduceResultIterator(
servicesCollectionIterator,
reduceServicesToVisibleServices
);
const errorOrVisibleServices = await documentDbUtils.iteratorToValue(
servicesIterator,
new StrMap<VisibleService>({})
);
if (isLeft(errorOrVisibleServices)) {
winston.error(
"ComputeVisibleServices|Error computing visible services: %s",
errorOrVisibleServices.value
);
return;
}
const visibleServices = errorOrVisibleServices.value;
winston.debug(
"ComputeVisibleServices|Visible services: %s",
JSON.stringify(visibleServices.value)
);
// write to blob storage using output bindings
return {
visibleServicesBlob: visibleServices.value
};
} catch (e) {
winston.error("ComputeVisibleServices error: %s", JSON.stringify(e));
return;
}
}