Skip to content

Commit

Permalink
Merge branch 'dev' into stage
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMurdzia committed Apr 29, 2024
2 parents 6303b0c + 6d82713 commit c32c2c1
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 19 deletions.
16 changes: 14 additions & 2 deletions Credentials/controllers/receiving/Create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkIfRequestBodyExists, checkIfPositiveIntegerNumber, checkIfTypeIsString } from "../../../_common/utils/Request.utils";
import { checkIfPositiveIntegerNumber, checkIfRequestBodyExists, checkIfTypeIsString } from "../../../_common/utils/Request.utils";

import { HttpRequest } from "@azure/functions";
import ReceivingCredential from '../../../_common/models/ReceivingCredential.model';
Expand All @@ -21,11 +21,23 @@ export const createReceive = async (req: HttpRequest) => {
checkIfTypeIsString(username, 'username');

// Check if row with id_account already exists
const response_from_db = await ReceivingCredential.get(id_account);
let response_from_db = await ReceivingCredential.get(id_account);

// If exists throw error 409 - Conflict
throwIfDatabaseResourceExists(response_from_db, 'id_account');

// Check if row with uuid already exists
response_from_db = await ReceivingCredential.getUUID(uuid);

// If exists throw error 409 - Conflict
throwIfDatabaseResourceExists(response_from_db, 'uuid');

// Check if row with uuid already exists
response_from_db = await ReceivingCredential.getUsername(username);

// If exists throw error 409 - Conflict
throwIfDatabaseResourceExists(response_from_db, 'username');

await ReceivingCredential.create(id_account, uuid, username);

return {
Expand Down
31 changes: 22 additions & 9 deletions Credentials/controllers/receiving/Get.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
import { checkIfPositiveIntegerStringOrNumber } from "../../../_common/utils/Request.utils";
import { checkIfPositiveIntegerStringOrNumber, checkIfTypeIsString } from "../../../_common/utils/Request.utils";

import { HttpRequest } from "@azure/functions";
import ReceivingCredential from '../../../_common/models/ReceivingCredential.model';
import { checkReceivingRequestQueryParamsForGet } from '../../../_common/utils/ReceivingRequest.utils';

export const getReceive = async (req: HttpRequest) => {
const { id_account } = req.query;
const { id_account, uuid } = req.query;

let response_from_db

try {
// Chack body params
checkReceivingRequestQueryParamsForGet(id_account);
if (uuid) {
// Check if uuid is a string
checkIfTypeIsString(uuid, 'uuid');

checkIfPositiveIntegerStringOrNumber(id_account, 'id_account');
// Check if row with id_account already exists
response_from_db = await ReceivingCredential.getUUID(uuid);
}
else {
// Chack body params
checkReceivingRequestQueryParamsForGet(id_account);

// Check if row with id_account already exists
const response_from_db = await ReceivingCredential.get(Number(id_account));
checkIfPositiveIntegerStringOrNumber(id_account, 'id_account');

// Check if row with id_account already exists
response_from_db = await ReceivingCredential.get(Number(id_account));
}

if (!response_from_db) {
const property_name = uuid ? 'uuid' : 'id_account';

return {
status: 404,
body: {
status: 'Not found',
field_name: 'id_account',
description: 'Resource with the provided id_account does not exist.'
field_name: property_name,
description: `Resource with the provided ${property_name} does not exist.`
},
headers: {
'Content-Type': 'application/json'
Expand Down
10 changes: 8 additions & 2 deletions Credentials/controllers/receiving/Update.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { checkIfRequestBodyExists, checkIfPositiveIntegerNumber, checkIfTypeIsString } from "../../../_common/utils/Request.utils";
import { checkIfPositiveIntegerNumber, checkIfRequestBodyExists, checkIfTypeIsString } from "../../../_common/utils/Request.utils";
import { checkReceivingRequestBodyParamsForCreateOrUpdate, throwIfDuplicateObject } from "../../../_common/utils/ReceivingRequest.utils";

import { HttpRequest } from "@azure/functions";
import ReceivingCredential from '../../../_common/models/ReceivingCredential.model';
import { checkReceivingRequestBodyParamsForCreateOrUpdate } from "../../../_common/utils/ReceivingRequest.utils";

export const updateReceive = async (req: HttpRequest) => {
// Check if request body exists
Expand Down Expand Up @@ -39,6 +39,12 @@ export const updateReceive = async (req: HttpRequest) => {
}
}
else {
// Check if username or uuid already exists
const response = await ReceivingCredential.getByUUIDOrUsername(uuid, username);

throwIfDuplicateObject(response, 'uuid', uuid, id_account);
throwIfDuplicateObject(response, 'username', username, id_account);

// Update object properties
response_from_db.username = username;
response_from_db.uuid = uuid;
Expand Down
7 changes: 7 additions & 0 deletions Credentials/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AzureFunction, Context, HttpRequest } from "@azure/functions"

import { ErrorLogs } from "../_common/models/ErrorLogs.model";
import { create } from "./controllers/fetching/Create";
import { createReceive } from "./controllers/receiving/Create";
import { get } from "./controllers/fetching/Get";
Expand Down Expand Up @@ -86,6 +87,12 @@ const httpTrigger: AzureFunction = async function (context: Context, req: HttpRe
'Content-Type': 'application/json'
}
};

ErrorLogs.insert(
{ error: error, req: req },
'Unexpected error occurred.',
'--- Internal error ---'
);
}
};

Expand Down
80 changes: 80 additions & 0 deletions _common/models/ReceivingCredential.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,86 @@ export default class ReceivingCredential {
return results.entries[0];
}

/**
* Get object by UUID or username.
* @param {string} uuid - The UUID of the credential.
* @param {string} username - The username associated with the credential.
* @returns {object} - Return object from DB
**/
static getByUUIDOrUsername = async (uuid: string, username: string) => {
// Define the query
const query = new AzureStorage.TableQuery().where('uuid eq ? or username eq ?', uuid, username);
// 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 Receiving Credentials ---');
reject(error);
}
else {
resolve(result);
}
});
});
return results.entries.map(entry => ({
uuid: entry.uuid._,
username: entry.username._,
id_account: entry.id_account._,
}));
}

/**
* Get object
* @param {string} uuid - Azure Tenant ID
* @return {object} - Return object from DB
**/
static getUUID = async (uuid: string) => {
// Define the query
const query = new AzureStorage.TableQuery().where('uuid eq ?', uuid);

// 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 Receiving Credentials ---');

reject(error);
}
else {
resolve(result);
}
});
});

return results.entries[0];
}

/**
* Get object
* @param {string} username - Client username
* @return {object} - Return object from DB
**/
static getUsername = async (username: string) => {
// Define the query
const query = new AzureStorage.TableQuery().where('username eq ?', username);

// 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 Receiving Credentials ---');

reject(error);
}
else {
resolve(result);
}
});
});

return results.entries[0];
}

/**
* Retrieves all receiving credentials from the database.
* @returns {Promise<Array<{ uuid: string, id_account: string, username: string }>>} A promise that resolves to an array of receiving credentials.
Expand Down
17 changes: 11 additions & 6 deletions _common/utils/DatabaseResponse.utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { error } from "console";

/**
* 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.
* @param field_name - The name of the field 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) => {
export const throwIfDatabaseResourceExists = (object: any, field_name: string) => {
if (object) {
throw {
status: 409,
error: true,
body: {
status: 'Conflict',
description: `Resource with the provided ${file_name} already exists.`
field_name: field_name,
description: `Resource with the provided ${field_name} already exists.`
},
headers: {
'Content-Type': 'application/json'
Expand All @@ -22,16 +26,17 @@ export const throwIfDatabaseResourceExists = (object: any, file_name: string) =>
/**
* 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.
* @param field_name - The name of the field 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) => {
export const throwIfDatabaseResourceNotExists = (object: any, field_name: string) => {
if (!object) {
throw {
status: 404,
error: true,
body: {
status: 'Not Found',
description: `Resource with the provided ${file_name} does not exists.`
description: `Resource with the provided ${field_name} does not exists.`
},
headers: {
'Content-Type': 'application/json'
Expand Down
18 changes: 18 additions & 0 deletions _common/utils/ReceivingRequest.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,21 @@ export const checkReceivingRequestBodyParamsForCreateOrUpdate = (uuid: string, u
};
}
}

export const throwIfDuplicateObject = (response: any[], field: string, value: string | number, id_account: number) => {
const duplicateObject = response.find(obj => obj[field] === value && obj.id_account !== id_account);

if (duplicateObject) {
throw {
status: 400,
body: {
status: 'Bad Request',
field_name: field,
description: `An object with the same ${field} but a different id_account already exists.`
},
headers: {
'Content-Type': 'application/json'
}
}
}
}

0 comments on commit c32c2c1

Please sign in to comment.