-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature-01-refactor-controllers (#8)
* Update deleteObject.js * Added configs and scripts for package.json * Updated upload controller * Updated uploadObject controller * Updated controllers * Updated and checked controllers
- Loading branch information
1 parent
6bc74ae
commit ece46e6
Showing
24 changed files
with
9,753 additions
and
3,321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "all" | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
Oops, something went wrong.