Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
};
}
const maybeDefaultEmail = defaultAddresses
.chain(addresses => fromNullable(addresses.email))
.map(email => Tuple2(email, NotificationAddressSourceEnum.DEFAULT_ADDRESS));
const maybeEmail = maybeProfileEmail.alt(maybeDefaultEmail);
const maybeEmailNotification: Option<
NotificationChannelEmail
> = maybeEmail.map(({ e1: toAddress, e2: addressSource }) => {
return {
addressSource,
status: NotificationChannelStatusEnum.QUEUED,
toAddress
};
});
// check whether there's at least a channel we can send the notification to
if (isNone(maybeEmailNotification)) {
// no channels to notify the user
return left(ProcessingError.NO_ADDRESSES);
}
// create a new Notification object with the configured notification channels
// only some of the channels may be configured, for the channel that have not
// generated a notification, we set the field to undefined
const notification: NewNotification = {
// if we have an emailNotification, we initialize its status
emailNotification: maybeEmailNotification.toUndefined(),
fiscalCode: newMessageWithoutContent.fiscalCode,
// tslint:disable-next-line:no-useless-cast
id: ulid() as NonEmptyString,
kind: "INewNotification",
messageId: newMessageWithoutContent.id
};
required: Object.keys(headerProperties),
},
// body as an object for generation
isNone(bodySchema)
? undefined
: {
definitions,
...(isReference(bodySchema.value)
? changeRef(bodySchema.value)
: changeRefs(bodySchema.value)),
});
// Notify call tracker
const serviceName = toSchemas
.composeLens(keyLens())
.getOption(schemaRecord);
if (!isNone(serviceName) && store.cores[serviceName.value]) {
store.cores[serviceName.value].track({ req, res });
}
listeners.forEach((listener: IListener) => listener.notify({ req, res }));
jsf.reset();
return res;
};
}
async function getMessageNotificationStatuses(
notificationModel: NotificationModel,
notificationStatusModel: NotificationStatusModel,
messageId: NonEmptyString
): Promise>> {
const errorOrMaybeNotification = await notificationModel.findNotificationForMessage(
messageId
);
if (isRight(errorOrMaybeNotification)) {
// It may happen that the notification object is not yet created in the database
// due to some latency, so it's better to not fail here but return an empty object
const maybeNotification = errorOrMaybeNotification.value;
if (isNone(maybeNotification)) {
winston.debug(
`getMessageNotificationStatuses|Notification not found|messageId=${messageId}`
);
return right>(none);
}
const notification = maybeNotification.value;
// collect the statuses of all channels
const channelStatusesPromises = Object.keys(NotificationChannelEnum)
.map(k => NotificationChannelEnum[k as NotificationChannelEnum])
.map(async channel => ({
channel,
status: await getChannelStatus(
notificationStatusModel,
notification.id,
channel
const transformers = [
// first transformer is the matcher
matcher,
// subsequent developer-defined transformers
...Object.values(store.cores).map(core =>
hoistTransformer(core.transformer),
),
];
const schemas = Object.entries(store.cores).reduce(
(a, [n, x]) => ({ ...a, [n]: x.schema }),
{},
);
const schemaRecord = transformers.reduce((a, b) => b(req, a), schemas);
const schema: Option = toSchema.getOption(schemaRecord);
const operation: Option = drillDownToOperation(schemaRecord);
if (isNone(operation) || isNone(schema)) {
throw Error(
`Cannot find a matcher for this request: ${JSON.stringify(
req,
null,
2,
)}`,
);
}
const codes: Array = Object.keys(
operation.value.responses,
) as Array;
const code = codes[Math.floor(Math.random() * codes.length)];
const statusCode = code === "default" ? 500 : +code;
const definitions = makeDefinitionsFromSchema(schema.value);
const operationLevelHeaders = headersFromOperation(
schema.value,
return async (userAuth, _, userAttributes, fiscalCode, messageId) => {
const errorOrMaybeDocument = await messageModel.findMessageForRecipient(
fiscalCode,
messageId
);
if (isLeft(errorOrMaybeDocument)) {
// the query failed
return ResponseErrorQuery(
"Error while retrieving the message",
errorOrMaybeDocument.value
);
}
const maybeDocument = errorOrMaybeDocument.value;
if (isNone(maybeDocument)) {
// the document does not exist
return ResponseErrorNotFound(
"Message not found",
"The message that you requested was not found in the system."
);
}
const retrievedMessage = maybeDocument.value;
// whether the user is a trusted application (i.e. can access all messages for any recipient)
const canListMessages = userAuth.groups.has(UserGroup.ApiMessageList);
// the user is allowed to see the message when he is either
// a trusted application or he is the sender of the message
const isUserAllowed =
canListMessages ||
function parseClass(moduleName: string, c: ast.ClassDeclaration): Parser {
const name = c.getName()
if (name === undefined) {
return E.left([`Missing class name in module ${moduleName}`])
} else {
const annotation = getAnnotation(c.getJsDocs())
const { description, since, deprecated, examples } = getAnnotationInfo(annotation)
const signature = getClassDeclarationSignature(c)
if (O.isNone(since)) {
return E.left([`missing @since tag in ${name} documentation`])
}
return pipe(
sequenceS(E.either)({
methods: traverse(c.getInstanceMethods(), parseMethod),
staticMethods: traverse(c.getStaticMethods(), parseMethod)
}),
E.map(({ methods, staticMethods }) =>
class_(documentable(name, description, since.value, deprecated, examples), signature, methods, staticMethods)
)
)
}
}
if (isNone(progressA.total) || isNone(progressB.total)) {
return progress({
loaded: progressA.loaded + progressB.loaded,
total: none,
});
}
const totalA = progressA.total.value;
const totalB = progressB.total.value;
const total = totalA + totalB;
const loaded = (progressA.loaded * totalA + progressB.loaded * totalB) / (total * total);
return progress({
loaded,
total: some(total),
});
}
const noA = isNone(a.progress);
const noB = isNone(b.progress);
if (noA && !noB) {
return b;
}
if (!noA && noB) {
return a;
}
return pending;
};
export function fromOption(option: Option<a>, error: Lazy): RemoteData {
if (isNone(option)) {
return failure(error());
} else {
return success(option.value);
}
}
</a>