diff --git a/Credentials/controllers/fetching/Create.ts b/Credentials/controllers/fetching/Create.ts index 69508e8..c4af609 100644 --- a/Credentials/controllers/fetching/Create.ts +++ b/Credentials/controllers/fetching/Create.ts @@ -1,52 +1,35 @@ -import { checkIfTypeIsString, checkRequestBodyParamsForCreateOrUpdate } from "../../../_helpers/RequestParamsHelper"; +import { checkIfRequestBodyExists, checkIfTypeIsString } from "../../../_common/utils/Request.utils"; +import { checkFetchingRequestBodyParamsForCreateOrUpdate } from "../../../_common/utils/FetchingRequest.utils"; import Credential from '../../../_common/models/Credential.model'; import { HttpRequest } from "@azure/functions"; import { Password } from '../../models/Password'; +import { throwIfDatabaseResourceExists } from "../../../_common/utils/DatabaseResponse.utils"; export const create = async (req: HttpRequest) => { - const { id_connection, password } = req.body; + // Check if request body exists + checkIfRequestBodyExists(req.body); - try { - // Chack body params - checkRequestBodyParamsForCreateOrUpdate(id_connection, password); + const { id_connection, password } = req.body; - // Check connection - checkIfTypeIsString(id_connection, 'id_connection'); + // Chack body params + checkFetchingRequestBodyParamsForCreateOrUpdate(id_connection, password); - // Check password - checkIfTypeIsString(password, 'password'); + // Check connection + checkIfTypeIsString(id_connection, 'id_connection'); - // Check if row with id_connection already exists - const response_from_db = await Credential.get(id_connection); + // Check password + checkIfTypeIsString(password, 'password'); - if (response_from_db) { - return { - status: 409, - body: { - status: 'Conflict', - description: 'Resource with the provided id_connection already exists.' - } - }; - } + // Check if row with id_connection already exists + const response_from_db = await Credential.get(id_connection); - const encrypt_password = Password.encryptPassword(password); + // If exists throw error 409 - Conflict + throwIfDatabaseResourceExists(response_from_db, 'id_connection'); - await Credential.create(encrypt_password, id_connection); - } - catch (error) { - if (error.status) { - return error; - } + const encrypt_password = Password.encryptPassword(password); - return { - status: 500, - body: { - status: 'Internal error', - description: 'An unexpected error occurred. Please try again later.' - } - }; - } + await Credential.create(encrypt_password, id_connection); return { status: 201, diff --git a/Credentials/controllers/fetching/Get.ts b/Credentials/controllers/fetching/Get.ts index f89c271..4ac8d10 100644 --- a/Credentials/controllers/fetching/Get.ts +++ b/Credentials/controllers/fetching/Get.ts @@ -1,50 +1,30 @@ -import { checkIfTypeIsString, checkRequestQueryParamsForGetOrRemove } from "../../../_helpers/RequestParamsHelper"; +import { checkIfTypeIsString } from "../../../_common/utils/Request.utils"; +import { checkFetchingRequestQueryParamsForGetOrDelete } from "../../../_common/utils/FetchingRequest.utils"; import Credential from '../../../_common/models/Credential.model'; import { HttpRequest } from "@azure/functions"; +import { throwIfDatabaseResourceNotExists } from "../../../_common/utils/DatabaseResponse.utils"; export const get = async (req: HttpRequest) => { const { id_connection } = req.query; - try { - // Chack body params - checkRequestQueryParamsForGetOrRemove(id_connection); + // Chack body params + checkFetchingRequestQueryParamsForGetOrDelete(id_connection); - // Check connection - checkIfTypeIsString(id_connection, 'id_connection'); + // Check connection + checkIfTypeIsString(id_connection, 'id_connection'); - // Check if row with id_connection already exists - const response_from_db = await Credential.get(id_connection); + // Check if row with id_connection already exists + const response_from_db = await Credential.get(id_connection); - if (!response_from_db) { - return { - status: 404, - body: { - status: 'Not found', - description: 'Resource with the provided id_connection not exists.' - } - }; - } + // If not exists throw error 404 - Not found + throwIfDatabaseResourceNotExists(response_from_db, 'id_connection'); - return { - status: 200, - body: { - status: 'OK', - payload: response_from_db - } - }; - } - catch (error) { - if (error.status) { - return error; + return { + status: 200, + body: { + status: 'OK', + payload: response_from_db } - - return { - status: 500, - body: { - status: 'Internal error', - description: 'An unexpected error occurred. Please try again later.' - } - }; - } -} + }; +} \ No newline at end of file diff --git a/Credentials/controllers/fetching/Remove.ts b/Credentials/controllers/fetching/Remove.ts index 80661b8..ac935db 100644 --- a/Credentials/controllers/fetching/Remove.ts +++ b/Credentials/controllers/fetching/Remove.ts @@ -1,46 +1,29 @@ -import { checkIfTypeIsString, checkRequestQueryParamsForGetOrRemove } from "../../../_helpers/RequestParamsHelper"; +import { checkIfRequestBodyExists, checkIfTypeIsString } from "../../../_common/utils/Request.utils"; +import { checkFetchingRequestBodyParamsForDelete } from "../../../_common/utils/FetchingRequest.utils"; import Credential from '../../../_common/models/Credential.model'; import { HttpRequest } from "@azure/functions"; +import { throwIfDatabaseResourceNotExists } from "../../../_common/utils/DatabaseResponse.utils"; export const remove = async (req: HttpRequest) => { - const { id_connection } = req.body; + // Check if request body exists + checkIfRequestBodyExists(req.body); - try { - // Chack body params - checkRequestQueryParamsForGetOrRemove(id_connection); + const { id_connection } = req.body; - // Check connection - checkIfTypeIsString(id_connection, 'id_connection'); + // Chack body params + checkFetchingRequestBodyParamsForDelete(id_connection); - // Check if row with id_connection already exists - let response_from_db = await Credential.get(id_connection.toString()); + // Check connection + checkIfTypeIsString(id_connection, 'id_connection'); - if (!response_from_db) { - return { - status: 404, - body: { - status: 'Not found', - description: 'Resource with the provided id_connection does not exist.' - } - }; - } + // Check if row with id_connection already exists + let response_from_db = await Credential.get(id_connection.toString()); - await Credential.delete(response_from_db); - } - catch (error) { - if (error.status) { - return error; - } + // If not exists throw error 404 - Not found + throwIfDatabaseResourceNotExists(response_from_db, 'id_connection'); - return { - status: 500, - body: { - status: 'Internal error', - description: 'An unexpected error occurred. Please try again later.' - } - }; - } + await Credential.delete(response_from_db); return { status: 200, diff --git a/Credentials/controllers/fetching/Update.ts b/Credentials/controllers/fetching/Update.ts index 11cecf8..20a9610 100644 --- a/Credentials/controllers/fetching/Update.ts +++ b/Credentials/controllers/fetching/Update.ts @@ -1,58 +1,39 @@ -import { checkIfTypeIsString, checkRequestBodyParamsForCreateOrUpdate } from "../../../_helpers/RequestParamsHelper"; +import { checkIfRequestBodyExists, checkIfTypeIsString } from "../../../_common/utils/Request.utils"; +import { checkFetchingRequestBodyParamsForCreateOrUpdate } from "../../../_common/utils/FetchingRequest.utils"; import Credential from '../../../_common/models/Credential.model'; import { HttpRequest } from "@azure/functions"; import { Password } from "../../models/Password"; +import { throwIfDatabaseResourceNotExists } from "../../../_common/utils/DatabaseResponse.utils"; export const update = async (req: HttpRequest) => { + // Check if request body exists + checkIfRequestBodyExists(req.body); + const { id_connection, password } = req.body; - try { - // Chack body params - checkRequestBodyParamsForCreateOrUpdate(id_connection, password); + // Chack body params + checkFetchingRequestBodyParamsForCreateOrUpdate(id_connection, password); - // Check connection - checkIfTypeIsString(id_connection, 'id_connection'); + // Check connection + checkIfTypeIsString(id_connection, 'id_connection'); - // Check password - checkIfTypeIsString(password, 'password'); + // Check password + checkIfTypeIsString(password, 'password'); - const encrypt_password = Password.encryptPassword(password); + const encrypt_password = Password.encryptPassword(password); - // Check if row with id_connection already exists - let response_from_db = await Credential.get(id_connection.toString()); + // Check if row with id_connection already exists + let response_from_db = await Credential.get(id_connection.toString()); - // If not exist create - if (!response_from_db) { - return { - status: 404, - body: { - status: 'Not found', - description: 'Resource with the provided ID Credential does not exist.' - } - }; - } - else { - // Update object properties - response_from_db.password = encrypt_password; - response_from_db.updated_at = new Date().toISOString(); + // If not exists throw error 404 - Not found + throwIfDatabaseResourceNotExists(response_from_db, 'id_connection'); - await Credential.update(response_from_db); - } - } - catch (error) { - if (error.status) { - return error; - } + // Update object properties + response_from_db.password = encrypt_password; + response_from_db.updated_at = new Date().toISOString(); - return { - status: 500, - body: { - status: 'Internal error', - description: 'An unexpected error occurred. Please try again later.' - } - }; - } + await Credential.update(response_from_db); return { status: 200, diff --git a/Credentials/controllers/receiving/Get.ts b/Credentials/controllers/receiving/Get.ts index b94f78d..77bd11d 100644 --- a/Credentials/controllers/receiving/Get.ts +++ b/Credentials/controllers/receiving/Get.ts @@ -1,4 +1,5 @@ -import { checkIfTypeIsString, checkRequestBodyParamsForGet, checkRequestQueryParamsForGetOrRemove } from "../../../_helpers/RequestParamsHelper"; +import { checkIfTypeIsString } from "../../../_common/utils/Request.utils"; +import { checkReceivingRequestBodyParamsForGet } from '../../../_common/utils/ReceivingRequest.utils'; import { HttpRequest } from "@azure/functions"; import ReceivingCredential from '../../../_common/models/ReceivingCredential.model'; @@ -8,7 +9,7 @@ export const getReceive = async (req: HttpRequest) => { try { // Chack body params - checkRequestBodyParamsForGet(uuid); + checkReceivingRequestBodyParamsForGet(uuid); // Check connection checkIfTypeIsString(uuid, 'uuid'); diff --git a/Credentials/index.ts b/Credentials/index.ts index e1d4f11..f00119b 100644 --- a/Credentials/index.ts +++ b/Credentials/index.ts @@ -7,40 +7,49 @@ import { remove } from "./controllers/fetching/Remove"; import { update } from "./controllers/fetching/Update"; const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise { - const { receive } = req.query; - if (receive) { - context.res = await getReceive(req); - - return; - } - - switch (req.method) { - case 'POST': - context.res = await create(req); - - break; - case 'GET': - context.res = await get(req); - - break; - case 'PUT': - context.res = await update(req); - - break; - - case 'DELETE': - context.res = await remove(req); - break; - default: - context.res = { - status: 405, - body: { - status: 'Method not allowed.' - } - }; - - break; + try { + const { receive } = req.query; + + if (receive) { + context.res = await getReceive(req); + return; + } + + switch (req.method) { + case 'POST': + context.res = await create(req); + break; + + case 'GET': + context.res = await get(req); + break; + + case 'PUT': + context.res = await update(req); + break; + + case 'DELETE': + context.res = await remove(req); + break; + + default: + context.res = { + status: 405, + body: { + status: 'Method not allowed.' + } + }; + break; + } + } catch (error) { + context.res = { + status: error.status ?? 500, + body: error.body ?? { + status: 'Internal error', + description: 'An unexpected error occurred. Please try again later.' + } + }; } }; diff --git a/_common/models/Credential.model.ts b/_common/models/Credential.model.ts index e2831bc..a32a8b3 100644 --- a/_common/models/Credential.model.ts +++ b/_common/models/Credential.model.ts @@ -30,7 +30,7 @@ export default class Credential { await new Promise((resolve, reject) => { this.table_service.insertEntity(this.table_name, object, function (error, result, response) { if (error) { - ErrorLogs.insert({}, `Problem when trying to create new object: ${error}`, '--- Create ---'); + ErrorLogs.insert(object, `Problem when trying to create new object: ${error}`, '--- Create ---'); reject(error); } @@ -54,7 +54,7 @@ export default class Credential { const results: any = await new Promise((resolve, reject) => { this.table_service.queryEntities(this.table_name, query, null, (error, result) => { if (error) { - ErrorLogs.insert({}, `Problem when trying to get object: ${error}`, '--- Get ---'); + ErrorLogs.insert(query, `Problem when trying to get object: ${error}`, '--- Get ---'); reject(error); } @@ -76,7 +76,7 @@ export default class Credential { static update = async (entity: any) => await new Promise((resolve, reject) => { this.table_service.replaceEntity(this.table_name, entity, (error, result) => { if (error) { - ErrorLogs.insert({}, `Problem when trying to update object: ${error}`, '--- Update ---'); + ErrorLogs.insert(entity, `Problem when trying to update object: ${error}`, '--- Update ---'); reject(error); } @@ -95,7 +95,7 @@ export default class Credential { static delete = async (entity: object) => await new Promise((resolve, reject) => { this.table_service.deleteEntity(this.table_name, entity, (error, response) => { if (error) { - ErrorLogs.insert({}, `Problem when trying to remove object: ${error}`, '--- Remove ---'); + ErrorLogs.insert(entity, `Problem when trying to remove object: ${error}`, '--- Remove ---'); reject(error); } diff --git a/_common/models/CredentialPrzemtable.model.ts b/_common/models/CredentialPrzemtable.model.ts deleted file mode 100644 index 2ef74f7..0000000 --- a/_common/models/CredentialPrzemtable.model.ts +++ /dev/null @@ -1,104 +0,0 @@ -import * as AzureStorage from 'azure-storage'; -import { ErrorLogs } from './ErrorLogs.model'; -import { v4 as uuidv4 } from 'uuid'; -import { CONNECTION_STRING, TABLE_NAME } from '../parameters/EnvParameters'; - -export default class CredentialPrzemtable { - private static table_name = 'Przemtable'; - private static connection_string = CONNECTION_STRING; - private static table_service = AzureStorage.createTableService(this.connection_string); - - /** - * Create new object - * @param {string} PartitionKey - Client PartitionKey - * @param {json} data - data - * @return {void} - Return void - **/ - static create = async (PartitionKey: string, data: any) => { - - // Create an entity object - const object = { - RowKey: PartitionKey, - PartitionKey: PartitionKey, - myVar: data.myVar, - }; - - // Create object - await new Promise((resolve, reject) => { - this.table_service.insertEntity(this.table_name, object, function (error, result, response) { - if (error) { - ErrorLogs.insert({}, `Problem when trying to create new object: ${error}`, '--- Create ---'); - - reject(error); - } - else { - resolve(result); - } - }); - }); - } - - /** - * Get object - * @param {string} PartitionKey - Client PartitionKey - * @return {object} - Return object from DB - **/ - static get = async (PartitionKey: string) => { - // Define the query - const query = new AzureStorage.TableQuery().where('PartitionKey eq ?', PartitionKey); - - // Get objects from DB - const results: any = await new Promise((resolve, reject) => { - this.table_service.queryEntities(this.table_name, query, null, (error, result) => { - if (error) { - ErrorLogs.insert({}, `Problem when trying to get object: ${error}`, '--- Get ---'); - - reject(error); - } - else { - resolve(result); - } - }); - }); - - return results.entries[0]; - } - - /** - * Update object - * @param {object} entity - Object from DB - * @return {void} - Return void - **/ - // TODO: Add interfaces - static update = async (entity: any) => await new Promise((resolve, reject) => { - this.table_service.replaceEntity(this.table_name, entity, (error, result) => { - if (error) { - ErrorLogs.insert({}, `Problem when trying to update object: ${error}`, '--- Update ---'); - - reject(error); - } - else { - resolve(result); - } - }); - }); - - /** - * Delete object from DB - * @param {object} entity - DB entity - * @param {string} row_key - Row Key - * @return {void} - Return void - **/ - static delete = async (entity: object) => await new Promise((resolve, reject) => { - this.table_service.deleteEntity(this.table_name, entity, (error, response) => { - if (error) { - ErrorLogs.insert({}, `Problem when trying to remove object: ${error}`, '--- Remove ---'); - - reject(error); - } - else { - resolve(response); - } - }); - }); -} diff --git a/_common/utils/DatabaseResponse.utils.ts b/_common/utils/DatabaseResponse.utils.ts new file mode 100644 index 0000000..33516f1 --- /dev/null +++ b/_common/utils/DatabaseResponse.utils.ts @@ -0,0 +1,35 @@ +/** + * Throws an error if a database resource already exists. + * @param object - The object to check if it exists. + * @param file_name - The name of the file associated with the resource. + * @throws {Object} - An error object with status 409 and a body containing the conflict details. + */ +export const throwIfDatabaseResourceExists = (object: any, file_name: string) => { + if (object) { + throw { + status: 409, + body: { + status: 'Conflict', + description: `Resource with the provided ${file_name} already exists.` + } + }; + } +} + +/** + * Throws an error if the database resource does not exist. + * @param object - The object to check if it exists. + * @param file_name - The name of the file associated with the resource. + * @throws {Object} - An error object with status 404 and a description of the error. + */ +export const throwIfDatabaseResourceNotExists = (object: any, file_name: string) => { + if (!object) { + throw { + status: 404, + body: { + status: 'Not Found', + description: `Resource with the provided ${file_name} does not exists.` + } + }; + } +} \ No newline at end of file diff --git a/_common/utils/FetchingRequest.utils.ts b/_common/utils/FetchingRequest.utils.ts new file mode 100644 index 0000000..6b9278b --- /dev/null +++ b/_common/utils/FetchingRequest.utils.ts @@ -0,0 +1,37 @@ +import { returnRequiredParamsErrorMessage } from '../../_common/utils/Request.utils'; + +export const checkFetchingRequestBodyParamsForDelete = (id_connection: string) => { + if (!id_connection) { + throw { + status: 400, + body: { + status: 'Bad Request', + description: returnRequiredParamsErrorMessage(['id_connection'], 'body') + } + }; + } +} + +export const checkFetchingRequestBodyParamsForCreateOrUpdate = (id_connection: string, password: string) => { + if (!id_connection || !password) { + throw { + status: 400, + body: { + status: 'Bad Request', + description: returnRequiredParamsErrorMessage(['id_connection', 'password'], 'body') + } + }; + } +} + +export const checkFetchingRequestQueryParamsForGetOrDelete = (id_connection: string) => { + if (!id_connection) { + throw { + status: 400, + body: { + status: 'Bad Request', + description: returnRequiredParamsErrorMessage(['id_connection'], 'query') + } + }; + } +} \ No newline at end of file diff --git a/_common/utils/ReceivingRequest.utils.ts b/_common/utils/ReceivingRequest.utils.ts new file mode 100644 index 0000000..b4d1600 --- /dev/null +++ b/_common/utils/ReceivingRequest.utils.ts @@ -0,0 +1,14 @@ +import { returnRequiredParamsErrorMessage } from './Request.utils'; + +export const checkReceivingRequestBodyParamsForGet = (uuid: string) => { + if (!uuid) { + throw { + status: 400, + body: { + status: 'Bad Request', + description: returnRequiredParamsErrorMessage(['uuid'], 'body') + } + }; + } +} + diff --git a/_common/utils/Request.utils.ts b/_common/utils/Request.utils.ts new file mode 100644 index 0000000..ecac5e6 --- /dev/null +++ b/_common/utils/Request.utils.ts @@ -0,0 +1,39 @@ +type RequestParamsType = 'body' | 'query'; + +export const returnRequiredParamsErrorMessage = (params: Array, source: RequestParamsType) => `Missing some required ${source.toString()} params ( check: ${params.join(', ')} ).`; + + +/** + * Checks if a value is of type string and throws an error if it's not. + * @param value - The value to check. + * @param field_name - The name of the field being checked (used in the error message). + * @throws Throws a 400 Bad Request error if the value is not a string. + */ +export const checkIfTypeIsString = (value: any, field_name: string) => { + if (typeof value !== 'string') { + throw { + status: 400, + body: { + status: 'Bad Request', + description: `Invalid data format: ${field_name} must be a string.` + } + }; + } +}; + +/** + * Checks if the request body exists. + * @param body - The request body. + * @throws Throws an error if the request body is missing. + */ +export const checkIfRequestBodyExists = (body: any) => { + if (!body) { + throw { + status: 400, + body: { + status: 'Bad Request', + description: 'Request body is missing.' + } + }; + } +} \ No newline at end of file diff --git a/_helpers/RequestParamsHelper.ts b/_helpers/RequestParamsHelper.ts deleted file mode 100644 index f990802..0000000 --- a/_helpers/RequestParamsHelper.ts +++ /dev/null @@ -1,69 +0,0 @@ -type RequertParamsType = 'body' | 'query'; - -const returnRequiredParamsErrorMessage = (params: Array, source: RequertParamsType) => `Missing some required ${source.toString()} params ( check: ${params.join(', ')} ).`; - -export const checkRequestQueryParamsForDelete = (id_connection: string) => { - if (!id_connection) { - throw { - status: 400, - body: { - status: 'Error', - description: returnRequiredParamsErrorMessage(['id_connection'], 'query') - } - }; - } -} - -export const checkRequestBodyParamsForCreateOrUpdate = (id_connection: string, password: string) => { - if (!id_connection || !password) { - throw { - status: 400, - body: { - status: 'Error', - description: returnRequiredParamsErrorMessage(['id_connection', 'password'], 'body') - } - }; - } -} - -export const checkRequestQueryParamsForGetOrRemove = (id_connection: string) => { - if (!id_connection) { - throw { - status: 400, - body: { - status: 'Error', - description: returnRequiredParamsErrorMessage(['id_connection'], 'body') - } - }; - } -} - -export const checkRequestBodyParamsForGet = (uuid: string) => { - if (!uuid) { - throw { - status: 400, - body: { - status: 'Error', - description: returnRequiredParamsErrorMessage(['uuid'], 'body') - } - }; - } -} - -/** - * Checks if a value is of type string and throws an error if it's not. - * @param value - The value to check. - * @param field_name - The name of the field being checked (used in the error message). - * @throws Throws a 400 Bad Request error if the value is not a string. - */ -export const checkIfTypeIsString = (value: any, field_name: string) => { - if (typeof value !== 'string') { - throw { - status: 400, - body: { - status: 'Error', - description: `Invalid data format: ${field_name} must be a string.` - } - }; - } -}; diff --git a/_helpers/RequestParamsHelperPrzemtable.ts b/_helpers/RequestParamsHelperPrzemtable.ts deleted file mode 100644 index 1616b03..0000000 --- a/_helpers/RequestParamsHelperPrzemtable.ts +++ /dev/null @@ -1,69 +0,0 @@ -type RequertParamsType = 'body' | 'query'; - -const returnRequiredParamsErrorMessage = (params: Array, source: RequertParamsType) => `Missing some required ${source.toString()} params ( check: ${params.join(', ')} ).`; - -export const checkRequestQueryParamsForDelete = (id_connection: string) => { - if (!id_connection) { - throw { - status: 400, - body: { - status: 'Error', - description: returnRequiredParamsErrorMessage(['id_connection'], 'query') - } - }; - } -} - -export const checkRequestBodyParamsForCreateOrUpdate = (uuid: string) => { - if (!uuid) { - throw { - status: 400, - body: { - status: 'Error', - description: returnRequiredParamsErrorMessage(['uuid'], 'body') - } - }; - } -} - -export const checkRequestQueryParamsForGetOrRemove = (PartitionKey: string) => { - if (!PartitionKey) { - throw { - status: 400, - body: { - status: 'Error', - description: returnRequiredParamsErrorMessage(['PartitionKey'], 'body') - } - }; - } -} - -export const checkRequestBodyParamsForGet = (uuid: string) => { - if (!uuid) { - throw { - status: 400, - body: { - status: 'Error', - description: returnRequiredParamsErrorMessage(['uuid'], 'body') - } - }; - } -} - -/** - * Checks if a value is of type string and throws an error if it's not. - * @param value - The value to check. - * @param field_name - The name of the field being checked (used in the error message). - * @throws Throws a 400 Bad Request error if the value is not a string. - */ -export const checkIfTypeIsString = (value: any, field_name: string) => { - if (typeof value !== 'string') { - throw { - status: 400, - body: { - status: 'Error', - description: `Invalid data format: ${field_name} must be a string.` - } - }; - } -}; diff --git a/przemtableGet/controllers/fetching/Create.ts b/przemtableGet/controllers/fetching/Create.ts deleted file mode 100644 index 8525a6b..0000000 --- a/przemtableGet/controllers/fetching/Create.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { checkIfTypeIsString, checkRequestBodyParamsForCreateOrUpdate } from "../../../_helpers/RequestParamsHelperPrzemtable"; - -import CredentialPrzemtable from '../../../_common/models/CredentialPrzemtable.model'; -import { HttpRequest } from "@azure/functions"; -import { v4 as uuidv4 } from 'uuid'; - -export const create = async (req: HttpRequest) => { -const PartitionKey = uuidv4(); - - try { - // Check if row with uuid already exists - const response_from_db = await CredentialPrzemtable.get(PartitionKey); - - if (response_from_db) { - return { - status: 409, - body: { - status: 'Fail', - description: 'Resource with the provided PartitionKey already exists.' - } - }; - } - - await CredentialPrzemtable.create(PartitionKey, req.body); - } - catch (error) { - if (error.status) { - return error; - } - - return { - status: 500, - body: { - status: 'Error', - description: 'An unexpected error occurred. Please try again later.' - } - }; - } - - return { - status: 201, - body: { - status: 'OK', - PartitionKey: PartitionKey, - description: 'New resource created successfully.' - } - }; -} diff --git a/przemtableGet/controllers/fetching/Get.ts b/przemtableGet/controllers/fetching/Get.ts deleted file mode 100644 index 5fca132..0000000 --- a/przemtableGet/controllers/fetching/Get.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { checkIfTypeIsString, checkRequestQueryParamsForGetOrRemove } from "../../../_helpers/RequestParamsHelperPrzemtable"; - -import { HttpRequest } from "@azure/functions"; -import CredentialPrzemtable from "../../../_common/models/CredentialPrzemtable.model"; - -export const get = async (req: HttpRequest) => { - const { PartitionKey } = req.query; - - try { - // Chack body params - checkRequestQueryParamsForGetOrRemove(PartitionKey); - - // Check connection - checkIfTypeIsString(PartitionKey, 'PartitionKey'); - - - // Check if row with uuid already exists - const response_from_db = await CredentialPrzemtable.get(PartitionKey); - - if (!response_from_db) { - return { - status: 404, - body: { - status: 'Not found', - PartitionKey: PartitionKey, - description: 'Resource with the provided PartitionKey not exists.' - } - }; - } - - return { - status: 200, - body: { - status: 'OK', - PartitionKey: PartitionKey, - payload: response_from_db - } - }; - } - catch (error) { - if (error.status) { - return error; - } - - return { - status: 500, - body: { - status: 'Internal error', - description: 'An unexpected error occurred. Please try again later.' - } - }; - } -} diff --git a/przemtableGet/controllers/fetching/Remove.ts b/przemtableGet/controllers/fetching/Remove.ts deleted file mode 100644 index 639a634..0000000 --- a/przemtableGet/controllers/fetching/Remove.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { checkIfTypeIsString, checkRequestQueryParamsForGetOrRemove } from "../../../_helpers/RequestParamsHelperPrzemtable"; - -import { HttpRequest } from "@azure/functions"; -import CredentialPrzemtable from "../../../_common/models/CredentialPrzemtable.model"; - -export const remove = async (req: HttpRequest) => { - const { PartitionKey } = req.query; - - try { - // Chack body params - checkRequestQueryParamsForGetOrRemove(PartitionKey); - - // Check connection - checkIfTypeIsString(PartitionKey, 'PartitionKey'); - - // Check if row with PartitionKey already exists - let response_from_db = await CredentialPrzemtable.get(PartitionKey); - - if (!response_from_db) { - return { - status: 404, - body: { - status: 'Fail', - description: 'Resource with the provided uuid does not exist.' - } - }; - } - - await CredentialPrzemtable.delete(response_from_db); - } - catch (error) { - if (error.status) { - return error; - } - - return { - status: 500, - body: { - status: 'Error', - description: 'An unexpected error occurred. Please try again later.' - } - }; - } - - return { - status: 200, - body: { - status: 'OK', - PartitionKey: PartitionKey, - description: 'Resource deleted successfully.' - } - }; -} diff --git a/przemtableGet/controllers/fetching/Update.ts b/przemtableGet/controllers/fetching/Update.ts deleted file mode 100644 index 207c6f6..0000000 --- a/przemtableGet/controllers/fetching/Update.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { checkIfTypeIsString, checkRequestBodyParamsForCreateOrUpdate } from "../../../_helpers/RequestParamsHelperPrzemtable"; - -import { HttpRequest } from "@azure/functions"; -import CredentialPrzemtable from "../../../_common/models/CredentialPrzemtable.model"; - -export const update = async (req: HttpRequest) => { - const { PartitionKey } = req.query; - - try { - // Check if row with PartitionKey already exists - let response_from_db = await CredentialPrzemtable.get(PartitionKey); - - // If not exist create - if (!response_from_db) { - return { - status: 404, - body: { - status: 'Fail', - description: 'Resource with the provided ID Credential does not exist.' - } - }; - } - else { - // Update object properties - response_from_db = { - ...response_from_db, - ...req.body, - updated_at: new Date().toISOString() - } - - await CredentialPrzemtable.update(response_from_db); - } - } - catch (error) { - if (error.status) { - return error; - } - - return { - status: 500, - body: { - status: 'Error', - description: 'An unexpected error occurred. Please try again later.' - } - }; - } - - return { - status: 201, - body: { - status: 'Created', - PartitionKey: PartitionKey, - description: 'Resource updated successfully.' - } - }; -} diff --git a/przemtableGet/controllers/receiving/Get.ts b/przemtableGet/controllers/receiving/Get.ts deleted file mode 100644 index 71dd55a..0000000 --- a/przemtableGet/controllers/receiving/Get.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { checkIfTypeIsString, checkRequestBodyParamsForGet, checkRequestQueryParamsForGetOrRemove } from "../../../_helpers/RequestParamsHelperPrzemtable"; - -import { HttpRequest } from "@azure/functions"; -import ReceivingCredential from '../../../_common/models/ReceivingCredential.model'; - -export const getReceive = async (req: HttpRequest) => { - const { uuid } = req.body; - - try { - // Chack body params - checkRequestBodyParamsForGet(uuid); - - // Check connection - checkIfTypeIsString(uuid, 'uuid'); - - // Check if row with uuid already exists - const response_from_db = await ReceivingCredential.get(uuid); - - if (!response_from_db) { - return { - status: 403, - body: { - status: 'Forbidden', - description: "Username not found." - } - }; - } - - return { - status: 200, - body: { - status: 'OK', - uuid: uuid, - payload: response_from_db - } - }; - } - catch (error) { - if (error.status) { - return error; - } - - return { - status: 500, - body: { - status: 'Internal error', - description: 'An unexpected error occurred. Please try again later.' - } - }; - } -} diff --git a/przemtableGet/function.json b/przemtableGet/function.json deleted file mode 100644 index da11886..0000000 --- a/przemtableGet/function.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "bindings": [ - { - "authLevel": "anonymous", - "type": "httpTrigger", - "direction": "in", - "name": "req", - "methods": [ - "get", - "post", - "put", - "delete" - ] - }, - { - "type": "http", - "direction": "out", - "name": "res" - } - ], - "scriptFile": "../dist/przemtableGet/index.js" -} diff --git a/przemtableGet/index.ts b/przemtableGet/index.ts deleted file mode 100644 index 6ce940d..0000000 --- a/przemtableGet/index.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { AzureFunction, Context, HttpRequest } from "@azure/functions" - -import { get } from "./controllers/fetching/Get"; -import { getReceive } from "./controllers/receiving/Get"; -import { create } from "./controllers/fetching/Create"; -import { update } from "./controllers/fetching/Update"; -import { remove } from "./controllers/fetching/Remove"; - -const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise { - const { receive } = req.query; - - if (receive) { - context.res = await getReceive(req); - - return; - } - - switch (req.method) { - case 'POST': - context.res = await create(req); - break; - - case 'GET': - context.res = await get(req); - break; - - case 'PUT': - context.res = await update(req); - break; - - case 'DELETE': - context.res = await remove(req); - break; - - default: - context.res = { - status: 405, - body: { - status: 'Method not allowed' - } - }; - break; - } -}; - -export default httpTrigger; diff --git a/przemtableGet/models/Password.ts b/przemtableGet/models/Password.ts deleted file mode 100644 index 727ec6a..0000000 --- a/przemtableGet/models/Password.ts +++ /dev/null @@ -1,24 +0,0 @@ -import * as CryptoJS from 'crypto-js'; -const { ENCRYPTION_KEY_SECRET } = require('../../_common/parameters/EnvParameters'); - -export class Password { - /** - * Encrypt a password - * @param password Password to encrypt. - * @return Encrypted password as a base64-encoded string. - */ - public static encryptPassword = (password: string): string => CryptoJS.AES.encrypt(password, ENCRYPTION_KEY_SECRET).toString(); - - /** - * Decrypt an encrypted password - * @param encrypted_password Encrypted password as a base64-encoded string. - * @return Decrypted password. - */ - public static decryptPassword = (encrypted_password: string): string => { - const bytes = CryptoJS.AES.decrypt(encrypted_password, ENCRYPTION_KEY_SECRET); - - const decrypted_password = bytes.toString(CryptoJS.enc.Utf8); - - return decrypted_password; - } -}