Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
slifty committed Jan 7, 2025
1 parent 21f4c5f commit 7a9358c
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 50 deletions.
1 change: 0 additions & 1 deletion src/database/operations/applicationFormFields/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './createApplicationFormField';
export * from './loadApplicationFormField';
export * from './loadApplicationFormFieldBundle';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
import { loadBundle } from '../generic/loadBundle';
import type { JsonResultSet, Bundle, ApplicationForm } from '../../../types';
import { generateBundleLoader } from '../generators';
import type { ApplicationForm } from '../../../types';

export const loadApplicationFormBundle = async (
limit: number | undefined,
offset: number,
): Promise<Bundle<ApplicationForm>> => {
const bundle = await loadBundle<JsonResultSet<ApplicationForm>>(
'applicationForms.selectWithPagination',
{
limit,
offset,
},
'application_forms',
);
const entries = bundle.entries.map((entry) => entry.object);
return {
...bundle,
entries,
};
};
export const loadApplicationFormBundle = generateBundleLoader<
ApplicationForm,
[]
>('applicationForms.selectWithPagination', 'application_forms', []);
38 changes: 38 additions & 0 deletions src/database/operations/generators/generateBundleLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { db } from '../../db';
import { loadTableMetrics } from '../generic/loadTableMetrics';
import type { AuthContext, Bundle, JsonResultSet } from '../../../types';

// type EveryAttributeOf<T extends object | null | undefined> = Array<keyof T> & ([keyof T] extends [Array<keyof T>[number]] ? unknown : never);

export const generateBundleLoader =
<T, P extends [...args: unknown[]]>(
queryName: string,
tableName: string,
parameterNames: { [K in keyof P]: string },
) =>
async (
authContext: AuthContext | undefined,
limit: number | undefined,
offset: number | undefined,
...args: P
): Promise<Bundle<T>> => {
const queryParameters = parameterNames.reduce(
(acc, parameterName, index) => ({
...acc,
[parameterName]: args[index],
}),
{
limit,
offset,
isAdmin: authContext?.role.isAdministrator,
keycloakUserId: authContext?.user.keycloakUserId,
},
);
const result = await db.sql<JsonResultSet<T>>(queryName, queryParameters);
const entries = result.rows.map((row) => row.object);
const metrics = await loadTableMetrics(tableName);
return {
entries,
total: metrics.count,
};
};
1 change: 1 addition & 0 deletions src/database/operations/generators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './generateBundleLoader';
Empty file.
2 changes: 1 addition & 1 deletion src/handlers/applicationFormsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const getApplicationForms = (
): void => {
const paginationParameters = extractPaginationParameters(req);
const { offset, limit } = getLimitValues(paginationParameters);
loadApplicationFormBundle(limit, offset)
loadApplicationFormBundle(undefined, limit, offset)
.then((applicationForms) => {
res.status(200).contentType('application/json').send(applicationForms);
})
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/__tests__/processBulkUploadTask.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ describe('processBulkUploadTask', () => {

const {
entries: [applicationForm],
} = await loadApplicationFormBundle(NO_LIMIT, NO_OFFSET);
} = await loadApplicationFormBundle(undefined, NO_LIMIT, NO_OFFSET);
if (applicationForm === undefined) {
fail('The application form was not created');
}
Expand Down

0 comments on commit 7a9358c

Please sign in to comment.