Skip to content

Commit

Permalink
Make the frontend talk to the updated backend
Browse files Browse the repository at this point in the history
This makes the frontend talk to the refactored backend.  There are 2
tests which aren't implemented yet while we workout how to mock and
handle sessions in tests.
  • Loading branch information
j-maynard committed Sep 6, 2024
1 parent f3bc024 commit 4621b5e
Show file tree
Hide file tree
Showing 26 changed files with 743 additions and 301 deletions.
23 changes: 19 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"rimraf": "^3.0.2",
"shelljs": "^0.8.5",
"supertest": "^7.0.0",
"supertest-session": "^5.0.1",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "^5.5.2"
Expand Down
8 changes: 8 additions & 0 deletions src/@types/express-session/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'express-session';
import { DatasetDTO } from '../../dtos2/dataset-dto';

declare module 'express-session' {
interface SessionData {
currentDataset: DatasetDTO;
}
}
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const sessionConfig = {
resave: false,
saveUninitialized: false,
cookie: {
secure: true
secure: false
}
};

Expand Down
81 changes: 67 additions & 14 deletions src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { env } from 'process';

import pino from 'pino';

import { FileListError, FileList } from '../dtos/filelist';
import { ViewDTO, ViewErrDTO } from '../dtos/view-dto';
import { Healthcheck } from '../dtos/healthcehck';
import { UploadDTO, UploadErrDTO } from '../dtos/upload-dto';
import { FileListError, FileList } from '../dtos2/filelist';
import { ViewDTO, ViewErrDTO } from '../dtos2/view-dto';
import { Healthcheck } from '../dtos2/healthcehck';
import { UploadDTO, UploadErrDTO } from '../dtos2/upload-dto';
import { DatasetDTO } from '../dtos2/dataset-dto';

const logger = pino({
name: 'StatsWales-Alpha-App: API',
Expand Down Expand Up @@ -42,9 +43,6 @@ export class API {
}

public async getFileList(lang: string) {
console.log(
`Fetching file list from ${this.backend_protocol}://${this.backend_server}:${this.backend_port}/${lang}/dataset`
);
const filelist: FileList = await fetch(
`${this.backend_protocol}://${this.backend_server}:${this.backend_port}/${lang}/dataset`
)
Expand All @@ -66,9 +64,12 @@ export class API {
return filelist;
}

public async getFileData(lang: string, file_id: string, page_number: number, page_size: number) {
public async getDatasetView(lang: string, datasetId: string, page_number: number, page_size: number) {
logger.info(
`Fetching dataset view from ${this.backend_protocol}://${this.backend_server}:${this.backend_port}/${lang}/dataset/${datasetId}/view?page_number=${page_number}&page_size=${page_size}`
);
const file = await fetch(
`${this.backend_protocol}://${this.backend_server}:${this.backend_port}/${lang}/dataset/${file_id}/view?page_number=${page_number}&page_size=${page_size}`
`${this.backend_protocol}://${this.backend_server}:${this.backend_port}/${lang}/dataset/${datasetId}/view?page_number=${page_number}&page_size=${page_size}`
)
.then((response) => {
if (response.ok) {
Expand Down Expand Up @@ -101,16 +102,64 @@ export class API {
}
}
],
dataset_id: file_id
dataset_id: datasetId
} as ViewErrDTO;
});
return file;
}

public async uploadCSV(lang: string, file: Blob, filename: string) {
public async getDatasetDatafilePreview(
lang: string,
datasetId: string,
revisionId: string,
importId: string,
page_number: number,
page_size: number
) {
const file = await fetch(
`${this.backend_protocol}://${this.backend_server}:${this.backend_port}/${lang}/dataset/${datasetId}/revision/by-id/${revisionId}/import/by-id/${importId}/preview?page_number=${page_number}&page_size=${page_size}`
)
.then((response) => {
if (response.ok) {
return response.json();
}
const err = new HttpError(response.status);
err.handleMessage(response.text());
throw err;
})
.then((api_res) => {
return api_res as ViewDTO;
})
.catch((error) => {
logger.error(`An HTTP error occured with status ${error.status} and message "${error.message}"`);
return {
success: false,
status: error.status,
errors: [
{
field: 'file',
message: [
{
lang,
message: 'errors.dataset_missing'
}
],
tag: {
name: 'errors.dataset_missing',
params: {}
}
}
],
dataset_id: datasetId
} as ViewErrDTO;
});
return file;
}

public async uploadCSV(lang: string, file: Blob, filename: string, title: string) {
const formData = new FormData();
formData.append('csv', file, filename);
formData.append('internal_name', filename);
formData.set('csv', file, filename);
formData.set('title', title);

const processedCSV = await fetch(
`${this.backend_protocol}://${this.backend_server}:${this.backend_port}/${lang}/dataset/`,
Expand All @@ -128,7 +177,11 @@ export class API {
throw err;
})
.then((api_res) => {
return api_res as UploadDTO;
const datasetDTO = api_res as DatasetDTO;
return {
success: true,
dataset: datasetDTO
} as UploadDTO;
})
.catch((error) => {
logger.error(`An HTTP error occured with status ${error.status} and message "${error.message}"`);
Expand Down
7 changes: 0 additions & 7 deletions src/dtos/datafile-dto.ts

This file was deleted.

33 changes: 0 additions & 33 deletions src/dtos/dataset-dto.ts

This file was deleted.

70 changes: 70 additions & 0 deletions src/dtos2/dataset-dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
export class DatasetInfoDTO {
language?: string;
title?: string;
description?: string;
}

export class DimensionInfoDTO {
language?: string;
name: string;
description?: string;
notes?: string;
}

export class SourceDTO {
id: string;
import_id: string;
revision_id: string;
// Commented out as we don't have lookup tables yet
// lookup_table_revision_id?: string;
csv_field: string;
action: string;
}

export class DimensionDTO {
id: string;
type: string;
start_revision_id: string;
finish_revision_id?: string;
validator?: string;
sources?: SourceDTO[];
dimensionInfo?: DimensionInfoDTO[];
dataset_id?: string;
}

export class ImportDTO {
id: string;
revision_id: string;
mime_type: string;
filename: string;
hash: string;
uploaded_at: string;
type: string;
location: string;
sources?: SourceDTO[];
}

export class RevisionDTO {
id: string;
revision_index: number;
creation_date: string;
previous_revision_id?: string;
online_cube_filename?: string;
publish_date?: string;
approval_date?: string;
approved_by?: string;
created_by: string;
imports: ImportDTO[];
dataset_id?: string;
}

export class DatasetDTO {
id: string;
creation_date: string;
created_by: string;
live?: string;
archive?: string;
dimensions?: DimensionDTO[];
revisions: RevisionDTO[];
datasetInfo?: DatasetInfoDTO[];
}
File renamed without changes.
9 changes: 7 additions & 2 deletions src/dtos/filelist.ts → src/dtos2/filelist.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export interface DatasetTitle {
language: string;
title: string;
}

export interface FileDescription {
id: string;
internal_name: string;
dataset_id: string;
titles: DatasetTitle[];
}

export interface FileList {
Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions src/dtos2/processedcsv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { DatasetDTO } from './dataset-dto';
import { Error } from './error';

export interface PageInfo {
total_records: number | undefined;
start_record: number | undefined;
end_record: number | undefined;
}

export interface ProcessedCSV {
success: boolean;
dataset: DatasetDTO | undefined;
current_page: number | undefined;
page_info: PageInfo | undefined;
pages: Array<string | number> | undefined;
page_size: number | undefined;
total_pages: number | undefined;
headers: Array<string> | undefined;
data: Array<Array<string>> | undefined;
errors: Array<Error> | undefined;
}
1 change: 0 additions & 1 deletion src/dtos/upload-dto.ts → src/dtos2/upload-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export interface UploadDTO {

export interface UploadErrDTO {
success: boolean;
status: number;
dataset: DatasetDTO | undefined;
errors: Error[];
}
10 changes: 9 additions & 1 deletion src/dtos/view-dto.ts → src/dtos2/view-dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Readable } from 'stream';

import { Error } from './error';
import { DatasetDTO } from './dataset-dto';
import { DatasetDTO, ImportDTO } from './dataset-dto';

export interface PageInfo {
total_records: number | undefined;
Expand All @@ -17,6 +19,7 @@ export interface ViewErrDTO {
export interface ViewDTO {
success: boolean;
dataset: DatasetDTO;
import: ImportDTO;
current_page: number;
page_info: PageInfo;
pages: Array<string | number>;
Expand All @@ -25,3 +28,8 @@ export interface ViewDTO {
headers: Array<string> | undefined;
data: Array<Array<string>>;
}

export interface ViewStream {
success: boolean;
stream: Readable;
}
Loading

0 comments on commit 4621b5e

Please sign in to comment.