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 17, 2024
2 parents b7f2196 + 09b13ad commit 589e912
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Credentials/controllers/receiving/GetAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { HttpRequest } from "@azure/functions";
import ReceivingCredential from '../../../_common/models/ReceivingCredential.model';

export const getAllReceive = async (req: HttpRequest) => {
try {
const response_from_db = await ReceivingCredential.getAll();

return {
status: 200,
body: {
status: 'OK',
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.'
}
};
}
}
5 changes: 4 additions & 1 deletion Credentials/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AzureFunction, Context, HttpRequest } from "@azure/functions"
import { create } from "./controllers/fetching/Create";
import { createReceive } from "./controllers/receiving/Create";
import { get } from "./controllers/fetching/Get";
import { getAllReceive } from "./controllers/receiving/GetAll";
import { getReceive } from "./controllers/receiving/Get";
import { remove } from "./controllers/fetching/Remove";
import { removeReceive } from "./controllers/receiving/Remove";
Expand All @@ -20,7 +21,9 @@ const httpTrigger: AzureFunction = async function (context: Context, req: HttpRe

break;
case 'GET':
context.res = await getReceive(req);
const { get_list } = req.query;

context.res = get_list ? await getAllReceive(req) : await getReceive(req);

break;
case 'PUT':
Expand Down
28 changes: 28 additions & 0 deletions _common/models/ReceivingCredential.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ export default class ReceivingCredential {
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.
*/
static getAll = async () => {
// Get objects from DB
const results: any = await new Promise((resolve, reject) => {
const query = new AzureStorage.TableQuery().select('uuid', 'username', 'id_account');
this.table_service.queryEntities(this.table_name, query, null, (error, result) => {
if (error) {
ErrorLogs.insert({}, `Problem when trying to get all objects: ${error}`, '--- Get All Receiving Credentials ---');

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

// Transform the results to the desired format
return results.entries.map(entry => ({
uuid: entry.uuid._,
id_account: entry.id_account._,
username: entry.username._
}));
}

/**
* Update object
* @param {object} entity - Object from DB
Expand Down

0 comments on commit 589e912

Please sign in to comment.