Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
>(
() =>
// 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!"
),
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;
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,
error => ResponseErrorQuery("Error while retrieving the service", error),
maybeService =>
serviceId
);
if (isRight(errorOrMaybeService)) {
const maybeService = errorOrMaybeService.value;
if (isNone(maybeService)) {
return ResponseErrorNotFound(
"Service not found",
"The service you requested was not found in the system."
);
} else {
return ResponseSuccessJson(
retrievedServiceToPublic(maybeService.value)
);
}
} else {
return ResponseErrorQuery(
"Error while retrieving the service",
errorOrMaybeService.value
);
}
};
}
error => ResponseErrorQuery("CreateServiceHandler error", error),
createdService =>
}
const serviceFromPayload = errorOrServiceFromPayload.value;
const errorOrMaybeUpdatedService = await serviceModel.update(
existingService.id,
existingService.serviceId,
currentService => {
return {
...currentService,
...serviceFromPayload,
serviceId
};
}
);
if (isLeft(errorOrMaybeUpdatedService)) {
return ResponseErrorQuery(
"Error while updating the existing service",
errorOrMaybeUpdatedService.value
);
}
const maybeUpdatedService = errorOrMaybeUpdatedService.value;
if (isNone(maybeUpdatedService)) {
return ResponseErrorInternal("Error while updating the existing service");
}
return ResponseSuccessJson(
retrievedServiceToPublic(maybeUpdatedService.value)
);
};
}
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
);
}
}
return async (_, __, ___, serviceId, serviceModelPayload) => {
if (serviceModelPayload.service_id !== serviceId) {
return ResponseErrorValidation(
"Error validating payload",
"Value of `service_id` in the request body must match " +
"the value of `service_id` path parameter"
);
}
const errorOrMaybeService = await serviceModel.findOneByServiceId(
serviceId
);
if (isLeft(errorOrMaybeService)) {
return ResponseErrorQuery(
"Error trying to retrieve existing service",
errorOrMaybeService.value
);
}
const maybeService = errorOrMaybeService.value;
if (isNone(maybeService)) {
return ResponseErrorNotFound(
"Error",
"Could not find a service with the provided serviceId"
);
}
const existingService = maybeService.value;
const errorOrServiceFromPayload = servicePayloadToService(
serviceModelPayload