Skip to content

Commit

Permalink
Status adjustment and added version parameter to health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
przemyslawss committed Mar 21, 2024
1 parent 1aa8394 commit d9ef9a1
Show file tree
Hide file tree
Showing 19 changed files with 543 additions and 29 deletions.
6 changes: 3 additions & 3 deletions Credentials/controllers/fetching/Create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const create = async (req: HttpRequest) => {
return {
status: 409,
body: {
status: 'Fail',
status: 'Conflict',
description: 'Resource with the provided id_connection already exists.'
}
};
Expand All @@ -42,7 +42,7 @@ export const create = async (req: HttpRequest) => {
return {
status: 500,
body: {
status: 'Error',
status: 'Internal error',
description: 'An unexpected error occurred. Please try again later.'
}
};
Expand All @@ -51,7 +51,7 @@ export const create = async (req: HttpRequest) => {
return {
status: 201,
body: {
status: 'OK',
status: 'Created',
description: 'New resource created successfully.'
}
};
Expand Down
4 changes: 2 additions & 2 deletions Credentials/controllers/fetching/Get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const get = async (req: HttpRequest) => {
return {
status: 404,
body: {
status: 'Fail',
status: 'Not found',
description: 'Resource with the provided id_connection not exists.'
}
};
Expand All @@ -42,7 +42,7 @@ export const get = async (req: HttpRequest) => {
return {
status: 500,
body: {
status: 'Error',
status: 'Internal error',
description: 'An unexpected error occurred. Please try again later.'
}
};
Expand Down
4 changes: 2 additions & 2 deletions Credentials/controllers/fetching/Remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const remove = async (req: HttpRequest) => {
return {
status: 404,
body: {
status: 'Fail',
status: 'Not found',
description: 'Resource with the provided id_connection does not exist.'
}
};
Expand All @@ -36,7 +36,7 @@ export const remove = async (req: HttpRequest) => {
return {
status: 500,
body: {
status: 'Error',
status: 'Internal error',
description: 'An unexpected error occurred. Please try again later.'
}
};
Expand Down
4 changes: 2 additions & 2 deletions Credentials/controllers/fetching/Update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const update = async (req: HttpRequest) => {
return {
status: 404,
body: {
status: 'Fail',
status: 'Not found',
description: 'Resource with the provided ID Credential does not exist.'
}
};
Expand All @@ -48,7 +48,7 @@ export const update = async (req: HttpRequest) => {
return {
status: 500,
body: {
status: 'Error',
status: 'Internal error',
description: 'An unexpected error occurred. Please try again later.'
}
};
Expand Down
6 changes: 3 additions & 3 deletions Credentials/controllers/receiving/Get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export const getReceive = async (req: HttpRequest) => {

if (!response_from_db) {
return {
status: 403,
status: 404,
body: {
status: 'Fail',
status: 'Not found',
description: "Username not found."
}
};
Expand All @@ -42,7 +42,7 @@ export const getReceive = async (req: HttpRequest) => {
return {
status: 500,
body: {
status: 'Error',
status: 'Internal error',
description: 'An unexpected error occurred. Please try again later.'
}
};
Expand Down
4 changes: 2 additions & 2 deletions Credentials/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ const httpTrigger: AzureFunction = async function (context: Context, req: HttpRe
break;
default:
context.res = {
status: 500,
status: 405,
body: {
status: 'Internal error.'
status: 'Method not allowed.'
}
};

Expand Down
3 changes: 2 additions & 1 deletion Health/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { AzureFunction, Context, HttpRequest } from "@azure/functions"
import { ErrorLogs } from "../_common/models/ErrorLogs.model";
import { IPValidator } from "../_common/models/IPValidator.model";
import { checkJWT } from "../_common/utils/CheckJWT.utils";
import { VERSION } from "../_common/parameters/Parameters";

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest) {
let response: any = { // TODO: Add the correct response type
status: 200,
body: {
status: 'OK',
version: '1.0.1'
version: VERSION
}
};

Expand Down
104 changes: 104 additions & 0 deletions _common/models/CredentialPrzemtable.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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);
}
});
});
}
1 change: 1 addition & 0 deletions _common/parameters/Parameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const VERSION = '1.0.2';
69 changes: 69 additions & 0 deletions _helpers/RequestParamsHelperPrzemtable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
type RequertParamsType = 'body' | 'query';

const returnRequiredParamsErrorMessage = (params: Array<string>, 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.`
}
};
}
};
14 changes: 0 additions & 14 deletions local.settings.json

This file was deleted.

48 changes: 48 additions & 0 deletions przemtableGet/controllers/fetching/Create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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.'
}
};
}
Loading

0 comments on commit d9ef9a1

Please sign in to comment.