This repository has been archived by the owner on Mar 9, 2020. It is now read-only.
/
profiles.ts
418 lines (390 loc) · 12.5 KB
/
profiles.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* Implements the API handlers for the Profile resource.
*/
import * as express from "express";
import { isLeft, isRight } from "fp-ts/lib/Either";
import { isNone, isSome } from "fp-ts/lib/Option";
import { ExtendedProfile } from "../api/definitions/ExtendedProfile";
import { FiscalCode } from "../api/definitions/FiscalCode";
import { LimitedProfile } from "../api/definitions/LimitedProfile";
import { Profile as LimitedOrExtendedProfile } from "../api/definitions/Profile";
import {
AzureUserAttributesMiddleware,
IAzureUserAttributes
} from "io-functions-commons/dist/src/utils/middlewares/azure_user_attributes";
import {
ClientIp,
ClientIpMiddleware
} from "io-functions-commons/dist/src/utils/middlewares/client_ip_middleware";
import {
AzureApiAuthMiddleware,
IAzureApiAuthorization,
UserGroup
} from "io-functions-commons/dist/src/utils/middlewares/azure_api_auth";
import { FiscalCodeMiddleware } from "io-functions-commons/dist/src/utils/middlewares/fiscalcode";
import {
IRequestMiddleware,
withRequestMiddlewares,
wrapRequestHandler
} from "io-functions-commons/dist/src/utils/request_middleware";
import {
IResponseErrorQuery,
ResponseErrorQuery
} from "io-functions-commons/dist/src/utils/response";
import {
IResponseErrorConflict,
IResponseErrorInternal,
IResponseErrorNotFound,
IResponseErrorValidation,
IResponseSuccessJson,
ResponseErrorConflict,
ResponseErrorFromValidationErrors,
ResponseErrorInternal,
ResponseErrorNotFound,
ResponseSuccessJson
} from "italia-ts-commons/lib/responses";
import {
checkSourceIpForHandler,
clientIPAndCidrTuple as ipTuple
} from "io-functions-commons/dist/src/utils/source_ip_check";
import { Context } from "@azure/functions";
import {
IProfileBlockedInboxOrChannels,
Profile,
ProfileModel,
RetrievedProfile
} from "io-functions-commons/dist/src/models/profile";
import { ServiceModel } from "io-functions-commons/dist/src/models/service";
import { ContextMiddleware } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";
import { BlockedInboxOrChannelEnum } from "../api/definitions/BlockedInboxOrChannel";
import { ServiceId } from "../api/definitions/ServiceId";
export interface IProfileCreatedEvent {
readonly kind: "ProfileCreatedEvent";
readonly fiscalCode: FiscalCode;
readonly newProfile: ExtendedProfile;
}
export interface IProfileUpdatedEvent {
readonly kind: "ProfileUpdatedEvent";
readonly fiscalCode: FiscalCode;
readonly oldProfile: ExtendedProfile;
readonly newProfile: ExtendedProfile;
}
function toExtendedProfile(profile: RetrievedProfile): ExtendedProfile {
return {
accepted_tos_version: profile.acceptedTosVersion,
blocked_inbox_or_channels: profile.blockedInboxOrChannels,
email: profile.email,
is_inbox_enabled: profile.isInboxEnabled === true,
is_webhook_enabled: profile.isWebhookEnabled === true,
preferred_languages: profile.preferredLanguages,
version: profile.version
};
}
/**
* Whether the sender service is allowed to send
* notififications to the user identified by this profile
*/
export function isSenderAllowed(
blockedInboxOrChannels: IProfileBlockedInboxOrChannels | undefined,
serviceId: ServiceId
): boolean {
return (
blockedInboxOrChannels === undefined ||
blockedInboxOrChannels[serviceId] === undefined ||
!blockedInboxOrChannels[serviceId].has(BlockedInboxOrChannelEnum.INBOX)
);
}
function toLimitedProfile(
profile: RetrievedProfile,
senderAllowed: boolean
): LimitedProfile {
return {
preferred_languages: profile.preferredLanguages,
// computed property
sender_allowed: senderAllowed
};
}
/**
* Type of a GetProfile handler.
*
* GetProfile expects a FiscalCode as input and returns a Profile or
* a Not Found error.
*/
type IGetProfileHandler = (
auth: IAzureApiAuthorization,
clientIp: ClientIp,
attrs: IAzureUserAttributes,
fiscalCode: FiscalCode
) => Promise<
| IResponseSuccessJson<LimitedOrExtendedProfile>
| IResponseErrorNotFound
| IResponseErrorQuery
>;
/**
* Type of an UpsertProfile handler.
*
* UpsertProfile expects a FiscalCode and a Profile as input and
* returns a Profile or a Validation or a Generic error.
*/
type IUpsertProfileHandler = (
context: Context,
auth: IAzureApiAuthorization,
clientIp: ClientIp,
attrs: IAzureUserAttributes,
fiscalCode: FiscalCode,
profileModelPayload: ExtendedProfile
) => Promise<
// tslint:disable-next-line:max-union-size
| IResponseSuccessJson<ExtendedProfile>
| IResponseErrorValidation
| IResponseErrorQuery
| IResponseErrorInternal
| IResponseErrorConflict
>;
/**
* Return a type safe GetProfile handler.
*/
export function GetProfileHandler(
profileModel: ProfileModel
): IGetProfileHandler {
return async (auth, _, userAttributes, fiscalCode) => {
const errorOrMaybeProfile = await profileModel.findOneProfileByFiscalCode(
fiscalCode
);
if (isRight(errorOrMaybeProfile)) {
const maybeProfile = errorOrMaybeProfile.value;
if (isSome(maybeProfile)) {
const profile = maybeProfile.value;
if (auth.groups.has(UserGroup.ApiFullProfileRead)) {
// if the client is a trusted application we return the
// extended profile
return ResponseSuccessJson(toExtendedProfile(profile));
} else {
// or else, we return a limited profile
return ResponseSuccessJson(
toLimitedProfile(
profile,
isSenderAllowed(
profile.blockedInboxOrChannels,
userAttributes.service.serviceId
)
)
);
}
} else {
return ResponseErrorNotFound(
"Profile not found",
"The profile you requested was not found in the system."
);
}
} else {
return ResponseErrorQuery(
"Error while retrieving the profile",
errorOrMaybeProfile.value
);
}
};
}
/**
* Wraps a GetProfile handler inside an Express request handler.
*/
export function GetProfile(
serviceModel: ServiceModel,
profileModel: ProfileModel
): express.RequestHandler {
const handler = GetProfileHandler(profileModel);
const azureUserAttributesMiddleware = AzureUserAttributesMiddleware(
serviceModel
);
const middlewaresWrap = withRequestMiddlewares(
AzureApiAuthMiddleware(
new Set([UserGroup.ApiLimitedProfileRead, UserGroup.ApiFullProfileRead])
),
ClientIpMiddleware,
azureUserAttributesMiddleware,
FiscalCodeMiddleware
);
return wrapRequestHandler(
middlewaresWrap(
checkSourceIpForHandler(handler, (_, c, u, __) => ipTuple(c, u))
)
);
}
/**
* A middleware that extracts a Profile payload from a request.
*/
export const ProfilePayloadMiddleware: IRequestMiddleware<
"IResponseErrorValidation",
ExtendedProfile
> = request =>
new Promise(resolve => {
const validation = ExtendedProfile.decode(request.body);
const result = validation.mapLeft(
ResponseErrorFromValidationErrors(ExtendedProfile)
);
resolve(result);
});
async function createNewProfileFromPayload(
profileModel: ProfileModel,
fiscalCode: FiscalCode,
profileModelPayload: ExtendedProfile
): Promise<IResponseSuccessJson<ExtendedProfile> | IResponseErrorQuery> {
const profile: Profile = {
acceptedTosVersion: profileModelPayload.accepted_tos_version,
blockedInboxOrChannels: profileModelPayload.blocked_inbox_or_channels,
email: profileModelPayload.email,
fiscalCode,
isInboxEnabled: profileModelPayload.is_inbox_enabled,
isWebhookEnabled: profileModelPayload.is_webhook_enabled,
preferredLanguages: profileModelPayload.preferred_languages
};
const errorOrProfile = await profileModel.create(profile, profile.fiscalCode);
const errorOrProfileAsPublicExtendedProfile = errorOrProfile.map(
toExtendedProfile
);
if (isRight(errorOrProfileAsPublicExtendedProfile)) {
return ResponseSuccessJson(errorOrProfileAsPublicExtendedProfile.value);
} else {
return ResponseErrorQuery(
"Error while creating a new profile",
errorOrProfileAsPublicExtendedProfile.value
);
}
}
async function updateExistingProfileFromPayload(
profileModel: ProfileModel,
existingProfile: RetrievedProfile,
profileModelPayload: ExtendedProfile
): Promise<
| IResponseSuccessJson<ExtendedProfile>
| IResponseErrorQuery
| IResponseErrorInternal
> {
const profile: Profile = {
acceptedTosVersion: profileModelPayload.accepted_tos_version,
blockedInboxOrChannels: profileModelPayload.blocked_inbox_or_channels,
email: profileModelPayload.email,
fiscalCode: existingProfile.fiscalCode,
isInboxEnabled: profileModelPayload.is_inbox_enabled,
isWebhookEnabled: profileModelPayload.is_webhook_enabled,
preferredLanguages: profileModelPayload.preferred_languages
};
const errorOrMaybeProfile = await profileModel.update(
existingProfile.id,
existingProfile.fiscalCode,
p => {
return {
...p,
...profile
};
}
);
if (isLeft(errorOrMaybeProfile)) {
return ResponseErrorQuery(
"Error while updating the existing profile",
errorOrMaybeProfile.value
);
}
const maybeProfile = errorOrMaybeProfile.value;
return maybeProfile.foldL<
IResponseErrorInternal | IResponseSuccessJson<ExtendedProfile>
>(
() =>
// this should never happen since if the profile doesn't exist this function
// will never be called, but let's deal with this anyway, you never know
ResponseErrorInternal(
"Error while updating the existing profile, the profile does not exist!"
),
p => ResponseSuccessJson(toExtendedProfile(p))
);
}
/**
* This handler will receive attributes for a profile and create a
* profile with those attributes if the profile does not yet exist or
* update the profile with it already exist.
*/
export function UpsertProfileHandler(
profileModel: ProfileModel
): IUpsertProfileHandler {
return async (context, _, __, ___, fiscalCode, profileModelPayload) => {
const errorOrMaybeProfile = await profileModel.findOneProfileByFiscalCode(
fiscalCode
);
if (isLeft(errorOrMaybeProfile)) {
return ResponseErrorQuery("Error", errorOrMaybeProfile.value);
}
const maybeProfile = errorOrMaybeProfile.value;
if (isNone(maybeProfile)) {
// create a new profile
const response = await createNewProfileFromPayload(
profileModel,
fiscalCode,
profileModelPayload
);
// if we successfully created the user's profile
// broadcast a profile-created event
if (response.kind === "IResponseSuccessJson") {
// tslint:disable-next-line:no-object-mutation
context.bindings.profileEvent = {
fiscalCode,
kind: "ProfileCreatedEvent",
newProfile: response.value
};
}
return response;
} else {
const existingProfile = maybeProfile.value;
// verify that the client asked to update the latest version
if (profileModelPayload.version !== existingProfile.version) {
return ResponseErrorConflict(
`Version ${profileModelPayload.version} is not the latest version.`
);
}
// update existing profile
const response = await updateExistingProfileFromPayload(
profileModel,
existingProfile,
profileModelPayload
);
// if we successfully updated the user's profile
// broadcast a profile-updated event
if (response.kind === "IResponseSuccessJson") {
// tslint:disable-next-line:no-object-mutation
context.bindings.profileEvent = {
fiscalCode: existingProfile.fiscalCode,
kind: "ProfileUpdatedEvent",
newProfile: response.value,
oldProfile: toExtendedProfile(existingProfile)
};
}
return response;
}
};
}
/**
* Wraps an UpsertProfile handler inside an Express request handler.
*/
export function UpsertProfile(
serviceModel: ServiceModel,
profileModel: ProfileModel
): express.RequestHandler {
const handler = UpsertProfileHandler(profileModel);
const azureUserAttributesMiddleware = AzureUserAttributesMiddleware(
serviceModel
);
const middlewaresWrap = withRequestMiddlewares(
ContextMiddleware(),
AzureApiAuthMiddleware(new Set([UserGroup.ApiProfileWrite])),
ClientIpMiddleware,
azureUserAttributesMiddleware,
FiscalCodeMiddleware,
ProfilePayloadMiddleware
);
return wrapRequestHandler(
middlewaresWrap(
checkSourceIpForHandler(handler, (_, __, c, u, ___, ____) =>
ipTuple(c, u)
)
)
);
}