Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature-01-refactor-controllers #8

Merged
merged 6 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
264 changes: 148 additions & 116 deletions README.md

Large diffs are not rendered by default.

11,671 changes: 9,036 additions & 2,635 deletions package-lock.json

Large diffs are not rendered by default.

41 changes: 40 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
"main": "handler.js",
"scripts": {
"serverless-offline": "sls offline start",
"start": "npm run serverless-offline"
"start": "npm run format-md && npm run serverless-offline",
"start:dev": "nodemon -e js,ts,yml,json --exec \"sls offline start\"",
"format-prettier": "prettier --write \"{src,test}/**/*.{js,ts}\"",
"check": "remark . --quiet --frail",
"format-remark": "remark . --quiet --frail --output",
"format-md": "remark . --output"
},
"repository": {
"type": "git",
Expand All @@ -19,6 +24,15 @@
},
"homepage": "https://github.com/andresWeitzel/CRUD_Bucket_S3_AWS#readme",
"devDependencies": {
"nodemon": "^3.0.1",
"remark-cli": "^11.0.0",
"remark-lint-emphasis-marker": "^3.1.2",
"remark-lint-list-item-indent": "^3.1.2",
"remark-lint-strong-marker": "^3.1.2",
"remark-lint-table-cell-padding": "^4.1.3",
"remark-preset-lint-consistent": "^5.1.2",
"remark-preset-lint-recommended": "^6.1.3",
"serverless": "^3.35.2",
"serverless-offline": "^12.0.4",
"serverless-offline-ssm": "^6.2.0",
"serverless-s3-local": "^0.7.1"
Expand All @@ -28,5 +42,30 @@
"node-input-validator": "^4.5.1",
"prettier": "^2.8.8",
"validator": "^13.9.0"
},
"remarkConfig": {
"settings": {
"emphasis": "*",
"strong": "*"
},
"plugins": [
"remark-preset-lint-consistent",
"remark-preset-lint-recommended",
"remark-lint-table-cell-padding",
"remark-lint",
"remark-lint-list-item-indent",
[
"remark-lint-emphasis-marker",
"*"
],
[
"remark-lint-strong-marker",
"*"
],
[
"remark-lint-heading-style",
"atx"
]
]
}
}
258 changes: 124 additions & 134 deletions src/controllers/deleteObject.js
Original file line number Diff line number Diff line change
@@ -1,142 +1,132 @@
"use strict";
//Enums
const {
statusCode
} = require("../enums/http/statusCode");
//Helpers
const {
bodyResponse
} = require("../helpers/http/bodyResponse");
const {
validateHeadersParams,
} = require("../helpers/validator/http/requestHeadersParams");
const {
validateAuthHeaders
} = require("../helpers/auth/headers");
const {
formatToString
} = require("../helpers/format/formatToString");
const {
formatToJson
} = require("../helpers/format/formatToJson");
//Enums
const { statusCode } = require("../enums/http/statusCode");
const {
initBucketIfEmpty
} = require("../helpers/bucket/operations/initBucket");
const {
readBucket
} = require("../helpers/bucket/operations/readBucket");
validateHeadersMessage,
} = require("../enums/validation/errors/status-message");
//Helpers
const { bodyResponse } = require("../helpers/http/bodyResponse");
const {
appendBucket
} = require("../helpers/bucket/operations/appendBucket");
validateHeadersParams,
} = require("../helpers/validator/http/requestHeadersParams");
const { validateAuthHeaders } = require("../helpers/auth/headers");
const { formatToString } = require("../helpers/format/formatToString");
const { formatToJson } = require("../helpers/format/formatToJson");
const {
findByUuid
} = require("../helpers/bucket/operations/findByUuid");


//Const/Vars
let eventHeaders;
let validateReqParams;
let validateAuth;
let obj;
let indexObj;
let uuidInput;
let bucketContent;
let bucketContentResult;
let msg;
let code;

/**
* @description Function to delete an object according to its uuid from the s3 repository
* @param {Object} event Object type
* @returns a body response with http code and message
*/
module.exports.handler = async (event) => {
try {
//Init
obj = null;
bucketContent = null;
uuidInput = null;
indexObj = null;
bucketContentResult = null;
msg = null;
code = null;


//-- start with validation Headers ---
eventHeaders = await event.headers;

validateReqParams = await validateHeadersParams(eventHeaders);

if (!validateReqParams) {
return await bodyResponse(
statusCode.BAD_REQUEST,
"Bad request, check missing or malformed headers"
);
}

validateAuth = await validateAuthHeaders(eventHeaders);

if (!validateAuth) {
initBucketIfEmpty,
} = require("../helpers/bucket/operations/initBucket");
const { readBucket } = require("../helpers/bucket/operations/readBucket");
const { appendBucket } = require("../helpers/bucket/operations/appendBucket");
const { findByUuid } = require("../helpers/bucket/operations/findByUuid");
//Const
// validate msg
const HEADERS_PARAMS_ERROR_MESSAGE =
validateHeadersMessage.HEADERS_PARAMS_ERROR_MESSAGE;
const HEADERS_AUTH_ERROR_MESSAGE =
validateHeadersMessage.HEADERS_AUTH_ERROR_MESSAGE;
//statu-code
const INTERNAL_SERVER_ERROR_CODE = statusCode.INTERNAL_SERVER_ERROR;
const INTERNAL_SERVER_ERROR_MESSAGE =
"An unexpected error has occurred. The object could not delete from the bucket.";
const BAD_REQUEST_CODE = statusCode.BAD_REQUEST;
const BAD_REQUEST_UUID_MESSAGE =
"The object requested from delete is not found inside the bucket according to the uuid ";
const BAD_REQUEST_DELETE_OBJECT_MESSAGE =
"The object has been deleted correctly according to the id ";
const UNAUTHORIZED_CODE = statusCode.UNAUTHORIZED;
const OK_CODE = statusCode.OK;
const DELETE_OBJECT_ERROR_DETAIL = "Error in delete-object lambda function.";
//Vars
let eventHeaders;
let validateReqParams;
let validateAuth;
let obj;
let indexObj;
let uuidInput;
let bucketContent;
let bucketContentResult;
let msgResponse;
let msgLog;

/**
* @description Function to delete an object according to its uuid from the s3 repository
* @param {Object} event Object type
* @returns a body response with http code and message
*/
module.exports.handler = async (event) => {
try {
//Init
obj = null;
bucketContent = null;
uuidInput = null;
indexObj = null;
bucketContentResult = null;
msgResponse = null;
msgLog = null;

//-- start with validation Headers ---
eventHeaders = await event.headers;

validateReqParams = await validateHeadersParams(eventHeaders);

if (!validateReqParams) {
return await bodyResponse(BAD_REQUEST_CODE, HEADERS_PARAMS_ERROR_MESSAGE);
}

validateAuth = await validateAuthHeaders(eventHeaders);

if (!validateAuth) {
return await bodyResponse(UNAUTHORIZED_CODE, HEADERS_AUTH_ERROR_MESSAGE);
}
//-- end with validation Headers ---

//-- start with bucket operations ---

await initBucketIfEmpty();

uuidInput = parseInt(await event.pathParameters.uuid);

bucketContent = await readBucket();

obj = await findByUuid(bucketContent, uuidInput);

if (obj == null) {
return await bodyResponse(
BAD_REQUEST_CODE,
BAD_REQUEST_UUID_MESSAGE + uuidInput
);
} else if (obj != null) {
bucketContent = await formatToJson(bucketContent);

indexObj = await bucketContent.indexOf(obj);

//Remove the object with the entered uuid
await bucketContent.splice(indexObj, 1);

//convert json to string format to save if is not a string format
bucketContent = await formatToString(bucketContent);

bucketContentResult = await appendBucket(bucketContent);

//-- end with bucket operations ---

if (bucketContentResult != null) {
return await bodyResponse(
statusCode.UNAUTHORIZED,
"Not authenticated, check x_api_key and Authorization"
OK_CODE,
BAD_REQUEST_DELETE_OBJECT_MESSAGE + uuidInput
);
}
//-- end with validation Headers ---



//-- start with bucket operations ---

await initBucketIfEmpty();

uuidInput = parseInt(await event.pathParameters.uuid);

bucketContent = await readBucket();

obj = await findByUuid(bucketContent, uuidInput);

if (obj == null) {
return await bodyResponse(
statusCode.BAD_REQUEST,
"The object requested according to the uuid, is not found inside the bucket.")
} else if (obj != null) {

bucketContent = await formatToJson(bucketContent);

indexObj = await bucketContent.indexOf(obj);

//Remove the object with the entered uuid
await bucketContent.splice(indexObj, 1);

//convert json to string format to save if is not a string format
bucketContent = await formatToString(bucketContent);

bucketContentResult = await appendBucket(bucketContent);

//-- end with bucket operations ---

if (bucketContentResult != null) {
return await bodyResponse(
statusCode.OK,
`Removed object with uuid ${uuidInput} successfully.`
);
}

} else {
return await bodyResponse(
statusCode.INTERNAL_SERVER_ERROR,
"An unexpected error has occurred. The object could not removed from the bucket."
)
}


} catch (error) {
code = statusCode.INTERNAL_SERVER_ERROR;
msg = `Error in DELETE OBJECT lambda. Caused by ${error}. Stack error type : ${error.stack}`;
console.error(msg);

return await requestResult(code, msg);
} else {
return await bodyResponse(
INTERNAL_SERVER_ERROR_CODE,
INTERNAL_SERVER_ERROR_MESSAGE
);
}

}
} catch (error) {
msgResponse = DELETE_OBJECT_ERROR_DETAIL;
msgLog = msgResponse + `Caused by ${error}`;
console.log(msgLog);

return await bodyResponse(INTERNAL_SERVER_ERROR_CODE, msgResponse);
}
};
Loading
Loading