Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let index = _.findIndex(user.addresses, (obj) => {
return obj._id.toString() == addressObj._id;
});
// If the address is not found, return failure
if (index == -1) {
result = {httpStatus: httpStatus.NOT_FOUND, status: "failed", errorDetails: httpStatus.getStatusText(httpStatus.NOT_FOUND)};
return result;
}
// Otherwise, Update the address values
user.addresses[index] = addressObj;
user = await user.save();
// Return the proper response depending on whether save was successful
result = user ? {httpStatus: httpStatus.OK, status: "successful", responseData: user.addresses} : {httpStatus: httpStatus.BAD_REQUEST, status: "failed", errorDetails: httpStatus.getStatusText(httpStatus.BAD_REQUEST)};
return result;
}
catch(err) {
logger.error("Error in updateAddress Service", {meta: err});
result = {httpStatus: httpStatus.BAD_REQUEST, status: "failed", errorDetails: err};
return result;
}
},
.then(function(res) {
expect(res).to.have.status(HttpStatus.BAD_REQUEST);
expect(res).to.be.json;
requiredFields.verifyErrorResponse(res.body);
});
});
(
error: Error,
request: Request,
response: Response,
next: NextFunction
) => {
console.log(error.message, error.stack);
const isAPIError = error instanceof APIError;
if (!isAPIError) {
console.error(request.url, error.message, error.stack);
}
response
.status(
error instanceof ClientError
? HttpStatus.BAD_REQUEST
: HttpStatus.INTERNAL_SERVER_ERROR
)
.json({ message: isAPIError ? error.message : '' });
}
);
router.post('/', authMiddleware, async ({user: {email, firstName, lastName}}, res) => {
logger.info(`POST /user - creating new user where email = ${email}`);
try {
const existingUser = await dao.findUserByEmail(email);
if (existingUser) {
logger.info(`POST /user - user already exists where email = ${email}`);
return res.sendStatus(HttpStatus.BAD_REQUEST);
}
const newUser = await dao.createUser({email, firstName, lastName});
logger.info(`POST /user - user created successfully where email = ${email}`);
res.status(HttpStatus.CREATED).send(new UserDTO(newUser));
} catch (e) {
logger.error(`Error creating user where email = ${email}`);
res.sendStatus(HttpStatus.INTERNAL_SERVER_ERROR);
}
});
public uploadAttachment(attachmentData: AttachmentData): string {
if (!attachmentData.type) {
throw createAPIException(
HttpStatus.BAD_REQUEST,
ErrorCodes.MissingProperty,
'You must specify type property for the attachment'
);
}
if (!attachmentData.originalBase64) {
throw createAPIException(
HttpStatus.BAD_REQUEST,
ErrorCodes.MissingProperty,
'You must specify originalBase64 byte[] for the attachment'
);
}
const attachment: any = { ...attachmentData, id: uniqueId() };
this.attachments[attachment.id] = attachment;
return attachment.id;
}
}
senderService.sendPaymentMail(
user,
language,
currency,
recipient,
authorizationKey,
amountMoney
);
return res.status(HttpStatus.OK).json({
success: true
});
} catch (error) {
const err: IResponseError = {
success: false,
code: HttpStatus.BAD_REQUEST,
error
};
next(err);
}
}
);
function(err, response, body) {
common.logHttpResponse(response, 'Storage - checkNameAvailability', true);
if (err) {
callback(err);
} else {
if (body.nameAvailable) {
callback(null);
} else {
var error = new Error(body.message);
if (body.reason === 'AccountNameInvalid') {
error.statusCode = HttpStatus.BAD_REQUEST;
} else {
error.statusCode = HttpStatus.CONFLICT;
}
callback(error);
}
}
}
);
public static uploadAttachment(attachmentData: IAttachmentData): string {
if (!attachmentData.type)
throw ResponseTypes.createAPIException(HttpStatus.BAD_REQUEST, ErrorCodes.MissingProperty, "You must specify type property for the attachment");
if (!attachmentData.originalBase64)
throw ResponseTypes.createAPIException(HttpStatus.BAD_REQUEST, ErrorCodes.MissingProperty, "You must specify originalBase64 byte[] for the attachment");
let attachment: any = attachmentData;
attachment.id = uniqueId();
AttachmentsController.attachments[attachment.id] = attachment;
return attachment.id;
}
endpoint: string,
): Promise {
const requestPayload = {
signedTransaction,
txOrigin,
};
const response = await fetchAsync(`${endpoint}/v2/request_transaction?chainId=${this.chainId}`, {
body: JSON.stringify(requestPayload),
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
});
const isError = response.status !== HttpStatus.OK;
const isValidationError = response.status === HttpStatus.BAD_REQUEST;
const json = isError && !isValidationError ? undefined : await response.json();
const result = {
isError,
status: response.status,
body: isError ? undefined : json,
error: isError ? json : undefined,
request: requestPayload,
coordinatorOperator: endpoint,
};
return result;
}