Skip to content

Commit

Permalink
Merge branch 'MEP-4859' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMurdzia committed Apr 26, 2024
2 parents 5ed5292 + dea3586 commit 7d69fc6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
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
26 changes: 26 additions & 0 deletions _common/models/ReceivingCredential.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ export default class ReceivingCredential {
return results.entries[0];
}

/**
* 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];
}

/**
* 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

0 comments on commit 7d69fc6

Please sign in to comment.