From 8ed3b128326a207a6332a631429ba80274098bbe Mon Sep 17 00:00:00 2001 From: Nick Phura Date: Thu, 14 Apr 2022 10:22:39 -0700 Subject: [PATCH 01/20] Add api response validation --- api/src/app.ts | 59 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/api/src/app.ts b/api/src/app.ts index 143ab99594..23b4bf7eba 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -38,7 +38,11 @@ app.use(function (req: Request, res: Response, next: NextFunction) { // Initialize express-openapi framework const openAPIFramework = initialize({ - apiDoc: rootAPIDoc as OpenAPIV3.Document, // base open api spec + apiDoc: { + ...(rootAPIDoc as OpenAPIV3.Document), // base open api spec + 'x-express-openapi-additional-middleware': [validateAllResponses], + 'x-express-openapi-validation-strict': true + }, app: app, // express app to initialize paths: './src/paths', // base folder for endpoint routes pathsIgnore: new RegExp('.(spec|test)$'), // ignore test files in paths @@ -107,3 +111,56 @@ try { defaultLog.error({ label: 'start api', message: 'error', error }); process.exit(1); } + +/** + * Middleware to apply openapi response validation to all routes. + * + * Note: validates `` sent via `res.status().json()` against the matching openapi response schema + * for ``. + * + * @param {Request} req + * @param {Response} res + * @param {NextFunction} next + */ +function validateAllResponses(req: Request, res: Response, next: NextFunction) { + const isStrictValidation = !!req['apiDoc']['x-express-openapi-validation-strict'] || false; + + if (typeof res['validateResponse'] === 'function') { + const json = res.json; + + res.json = (...args) => { + if (res.get('x-express-openapi-validation-error-for')) { + // Already validated, return + return json.apply(res, args); + } + + const body = args[0]; + + const validationResult: { message: any; errors: any[] } | undefined = res['validateResponse']( + res.statusCode, + body + ); + + let validationMessage; + + if (validationResult?.errors) { + const errorList = Array.from(validationResult.errors) + .map((item: any) => item.message) + .join(','); + + validationMessage = `Invalid response for status code ${res.statusCode}: ${errorList}`; + + // Set to avoid a loop, and to provide the original status code + res.set('x-express-openapi-validation-error-for', res.statusCode.toString()); + } + + if (!isStrictValidation || !validationResult?.errors) { + return json.apply(res, args); + } else { + return res.status(500).json({ error: validationMessage }); + } + }; + } + + next(); +} From 854fb7be7a58bf6186ca6b0e1ffb1fce1ac7ea2e Mon Sep 17 00:00:00 2001 From: Nick Phura Date: Thu, 14 Apr 2022 10:49:12 -0700 Subject: [PATCH 02/20] Updates --- api/src/app.ts | 18 +++++++++++------- api/src/paths/user/self.ts | 5 +++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/api/src/app.ts b/api/src/app.ts index 23b4bf7eba..36088a5f35 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -4,7 +4,7 @@ import multer from 'multer'; import { OpenAPIV3 } from 'openapi-types'; import swaggerUIExperss from 'swagger-ui-express'; import { defaultPoolConfig, initDBPool } from './database/db'; -import { ensureHTTPError } from './errors/custom-error'; +import { ensureHTTPError, HTTPErrorType } from './errors/custom-error'; import { rootAPIDoc } from './openapi/root-api-doc'; import { authenticateRequest } from './request-handlers/security/authentication'; import { getLogger } from './utils/logger'; @@ -141,14 +141,13 @@ function validateAllResponses(req: Request, res: Response, next: NextFunction) { body ); - let validationMessage; + let validationMessage = ''; + let errorList = []; if (validationResult?.errors) { - const errorList = Array.from(validationResult.errors) - .map((item: any) => item.message) - .join(','); + validationMessage = `Invalid response for status code ${res.statusCode}`; - validationMessage = `Invalid response for status code ${res.statusCode}: ${errorList}`; + errorList = Array.from(validationResult.errors); // Set to avoid a loop, and to provide the original status code res.set('x-express-openapi-validation-error-for', res.statusCode.toString()); @@ -157,7 +156,12 @@ function validateAllResponses(req: Request, res: Response, next: NextFunction) { if (!isStrictValidation || !validationResult?.errors) { return json.apply(res, args); } else { - return res.status(500).json({ error: validationMessage }); + return res.status(500).json({ + name: HTTPErrorType.INTERNAL_SERVER_ERROR, + status: 500, + message: validationMessage, + errors: errorList + }); } }; } diff --git a/api/src/paths/user/self.ts b/api/src/paths/user/self.ts index ea2d54925f..5b4f921e25 100644 --- a/api/src/paths/user/self.ts +++ b/api/src/paths/user/self.ts @@ -37,6 +37,7 @@ GET.apiDoc = { schema: { title: 'User Response Object', type: 'object', + required: ['id', 'user_identifier', 'role_ids', 'role_names'], properties: { id: { description: 'user id', @@ -47,9 +48,9 @@ GET.apiDoc = { type: 'string' }, record_end_date: { + oneOf: [{ type: 'object' }, { type: 'string', format: 'date' }], description: 'Determines if the user record has expired', - type: 'string', - format: 'date' + nullable: true }, role_ids: { description: 'list of role ids for the user', From 62953f6f5327eedadb0b2b340250bc0371a14d29 Mon Sep 17 00:00:00 2001 From: Anissa Agahchen Date: Thu, 14 Apr 2022 17:00:36 -0700 Subject: [PATCH 03/20] update reponse for the project attachments list --- api/src/models/project-survey-attachments.ts | 2 +- .../project/{projectId}/attachments/list.ts | 58 +++++++++---------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/api/src/models/project-survey-attachments.ts b/api/src/models/project-survey-attachments.ts index fd47b92a5b..7f2c3831ae 100644 --- a/api/src/models/project-survey-attachments.ts +++ b/api/src/models/project-survey-attachments.ts @@ -21,7 +21,7 @@ export class GetAttachmentsData { id: item.id, fileName: item.file_name, fileType: item.file_type || 'Report', - lastModified: item.update_date || item.create_date, + lastModified: (item.update_date || item.create_date).toString(), size: item.file_size, securityToken: item.security_token }; diff --git a/api/src/paths/project/{projectId}/attachments/list.ts b/api/src/paths/project/{projectId}/attachments/list.ts index 466a93dffb..dc6396d38e 100644 --- a/api/src/paths/project/{projectId}/attachments/list.ts +++ b/api/src/paths/project/{projectId}/attachments/list.ts @@ -49,36 +49,34 @@ GET.apiDoc = { content: { 'application/json': { schema: { - type: 'array', - items: { - type: 'object', - required: ['projectId', 'fileName', 'fileType', 'lastModified', 'size', 'securityToken', 'revisionCount'], - properties: { - projectId: { - type: 'number' - }, - fileName: { - description: 'The file name of the attachment', - type: 'string' - }, - fileType: { - description: 'The file type of the attachment', - type: 'string' - }, - lastModified: { - description: 'The date the object was last modified', - type: 'string' - }, - size: { - description: 'The size of the attachment', - type: 'number' - }, - securityToken: { - description: 'The security token of the attachment', - type: 'string' - }, - revisionCount: { - type: 'number' + type: 'object', + properties: { + attachmentsList: { + type: 'array', + items: { + type: 'object', + required: ['id', 'fileName', 'fileType', 'lastModified', 'securityToken', 'size'], + properties: { + id: { + type: 'number' + }, + fileName: { + type: 'string' + }, + fileType: { + type: 'string' + }, + lastModified: { + type: 'string' + }, + securityToken: { + description: 'The security token of the attachment', + type: 'string' + }, + size: { + type: 'number' + } + } } } } From 4c6efdff4cb4d1ce989eaa67f225685ec26d5751 Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Wed, 20 Apr 2022 17:02:18 -0700 Subject: [PATCH 04/20] BHBC-1689: Add validation to projects --- api/src/app.ts | 3 +++ api/src/paths/project/{projectId}/update.ts | 16 ++++++++++++---- api/src/paths/project/{projectId}/view.ts | 8 ++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/api/src/app.ts b/api/src/app.ts index 36088a5f35..18a39277a3 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -145,6 +145,9 @@ function validateAllResponses(req: Request, res: Response, next: NextFunction) { let errorList = []; if (validationResult?.errors) { + // Temporary log + console.log('Body:', JSON.stringify(body)) + validationMessage = `Invalid response for status code ${res.statusCode}`; errorList = Array.from(validationResult.errors); diff --git a/api/src/paths/project/{projectId}/update.ts b/api/src/paths/project/{projectId}/update.ts index c549cc81c5..9346d6ada8 100644 --- a/api/src/paths/project/{projectId}/update.ts +++ b/api/src/paths/project/{projectId}/update.ts @@ -90,6 +90,7 @@ GET.apiDoc = { 'publish_date', 'revision_count' ], + nullable: true, properties: { project_name: { type: 'string' @@ -126,6 +127,7 @@ GET.apiDoc = { permit: { type: 'object', required: ['permits'], + nullable: true, properties: { permits: { type: 'array', @@ -147,6 +149,7 @@ GET.apiDoc = { coordinator: { title: 'Project coordinator', type: 'object', + nullable: true, required: [ 'first_name', 'last_name', @@ -181,6 +184,7 @@ GET.apiDoc = { description: 'The project objectives and caveats', type: 'object', required: ['objectives', 'caveats'], + nullable: true, properties: { objectives: { type: 'string' @@ -194,6 +198,7 @@ GET.apiDoc = { description: 'The project location object', type: 'object', required: ['location_description', 'geometry'], + nullable: true, properties: { location_description: { type: 'string' @@ -210,6 +215,7 @@ GET.apiDoc = { description: 'The International Union for Conservation of Nature number', type: 'object', required: ['classificationDetails'], + nullable: true, properties: { classificationDetails: { type: 'array', @@ -217,13 +223,13 @@ GET.apiDoc = { type: 'object', properties: { classification: { - type: 'string' + type: 'number' }, subClassification1: { - type: 'string' + type: 'number' }, subClassification2: { - type: 'string' + type: 'number' } } } @@ -234,6 +240,7 @@ GET.apiDoc = { description: 'The project funding details', type: 'object', required: ['fundingSources'], + nullable: true, properties: { fundingSources: { type: 'array', @@ -283,11 +290,12 @@ GET.apiDoc = { description: 'The project partners', type: 'object', required: ['indigenous_partnerships', 'stakeholder_partnerships'], + nullable: true, properties: { indigenous_partnerships: { type: 'array', items: { - type: 'string' + type: 'number' } }, stakeholder_partnerships: { diff --git a/api/src/paths/project/{projectId}/view.ts b/api/src/paths/project/{projectId}/view.ts index 3549122235..d3465e01f0 100644 --- a/api/src/paths/project/{projectId}/view.ts +++ b/api/src/paths/project/{projectId}/view.ts @@ -201,13 +201,13 @@ GET.apiDoc = { type: 'object', properties: { classification: { - type: 'string' + type: 'number' }, subClassification1: { - type: 'string' + type: 'number' }, subClassification2: { - type: 'string' + type: 'number' } } } @@ -271,7 +271,7 @@ GET.apiDoc = { indigenous_partnerships: { type: 'array', items: { - type: 'string' + type: 'number' } }, stakeholder_partnerships: { From 489078e9ab5db02be4c529f8eb4dab9a5041f0b4 Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Mon, 25 Apr 2022 13:21:23 -0700 Subject: [PATCH 05/20] BHBC-1689: Add response validation to surveys + attachments + observations --- .../survey/{surveyId}/attachments/list.ts | 28 +++++++++++-------- .../{surveyId}/attachments/report/upload.ts | 2 +- .../{attachmentId}/metadata/update.ts | 9 +----- .../{surveyId}/observation/submission/get.ts | 2 ++ .../observation/submission/upload.ts | 14 +++++++++- .../submission/{submissionId}/getSignedUrl.ts | 14 +++++++++- .../{surveyId}/summary/submission/get.ts | 1 + .../submission/{summaryId}/getSignedUrl.ts | 14 +++++++++- .../summary/submission/{summaryId}/view.ts | 1 + 9 files changed, 62 insertions(+), 23 deletions(-) diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/list.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/list.ts index 5e3655cb0e..a1f3868f8a 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/list.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/list.ts @@ -57,17 +57,23 @@ GET.apiDoc = { content: { 'application/json': { schema: { - type: 'array', - items: { - type: 'object', - properties: { - fileName: { - description: 'The file name of the attachment', - type: 'string' - }, - lastModified: { - description: 'The date the object was last modified', - type: 'string' + type: 'object', + required: ['attachmentsList'], + properties: { + attachmentsList: { + type: 'array', + items: { + type: 'object', + properties: { + fileName: { + description: 'The file name of the attachment', + type: 'string' + }, + lastModified: { + description: 'The date the object was last modified', + type: 'string' + } + } } } } diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/report/upload.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/report/upload.ts index 847668f24f..6d3be0543c 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/report/upload.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/report/upload.ts @@ -69,7 +69,7 @@ POST.apiDoc = { type: 'string' }, year_published: { - type: 'string' + type: 'number' }, authors: { type: 'array', diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/{attachmentId}/metadata/update.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/{attachmentId}/metadata/update.ts index 8c1ceb4e99..cfa304c74f 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/{attachmentId}/metadata/update.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/{attachmentId}/metadata/update.ts @@ -81,14 +81,7 @@ PUT.apiDoc = { type: 'string' }, year_published: { - oneOf: [ - { - type: 'string' - }, - { - type: 'number' - } - ] + type: 'number' }, authors: { type: 'array', diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/get.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/get.ts index 9399f2feba..a65ddc22f0 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/get.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/get.ts @@ -57,6 +57,7 @@ GET.apiDoc = { 'application/json': { schema: { type: 'object', + nullable: true, properties: { id: { type: 'number' @@ -67,6 +68,7 @@ GET.apiDoc = { }, status: { description: 'The validation status of the submission', + nullable: true, type: 'string' }, messages: { diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/upload.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/upload.ts index 835b58436f..cfe15f1a84 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/upload.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/upload.ts @@ -64,7 +64,19 @@ POST.apiDoc = { }, responses: { 200: { - description: 'Upload OK' + description: 'Upload OK', + content: { + 'application/json': { + schema: { + type: 'object', + properties: { + submissionId: { + type: 'number' + } + } + } + } + } }, 400: { $ref: '#/components/responses/400' diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/{submissionId}/getSignedUrl.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/{submissionId}/getSignedUrl.ts index b6906f9219..4f02afb78c 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/{submissionId}/getSignedUrl.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/{submissionId}/getSignedUrl.ts @@ -58,7 +58,19 @@ GET.apiDoc = { }, required: true } - ] + ], + responses: { + 200: { + description: 'Obsesrvation submission signed URL response.', + content: { + 'application/json': { + schema: { + type: 'string' + } + } + } + } + } }; export function getSingleSubmissionURL(): RequestHandler { diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/get.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/get.ts index 9e3140d8f2..dc95781bc8 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/get.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/get.ts @@ -57,6 +57,7 @@ GET.apiDoc = { 'application/json': { schema: { type: 'object', + nullable: true, properties: { id: { type: 'number' diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/getSignedUrl.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/getSignedUrl.ts index cd7435037d..05a11d1e33 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/getSignedUrl.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/getSignedUrl.ts @@ -56,7 +56,19 @@ GET.apiDoc = { }, required: true } - ] + ], + responses: { + 200: { + description: 'Submission summary signed URL response.', + content: { + 'application/json': { + schema: { + type: 'string' + } + } + } + } + } }; export function getSingleSummarySubmissionURL(): RequestHandler { diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts index 3d5bebc251..f84e262a31 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts @@ -196,6 +196,7 @@ export function getSummarySubmissionCSVForView(): RequestHandler { data.push(dataItem); } + console.log("_DATA:", data) return res.status(200).json({ data }); } catch (error) { From 7b3144b566946f8b35a9eede7a0ab86d274ed4f9 Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Mon, 25 Apr 2022 13:21:57 -0700 Subject: [PATCH 06/20] BHBC-1689: Add response validation to project attachments. --- api/src/models/project-survey-attachments.ts | 8 ++++---- api/src/models/project-view.ts | 2 +- api/src/paths/dwc/view-occurrences.ts | 4 ++-- api/src/paths/project/{projectId}/attachments/list.ts | 3 ++- .../project/{projectId}/attachments/report/upload.ts | 2 +- .../{projectId}/attachments/{attachmentId}/delete.ts | 3 ++- .../attachments/{attachmentId}/metadata/update.ts | 9 +-------- 7 files changed, 13 insertions(+), 18 deletions(-) diff --git a/api/src/models/project-survey-attachments.ts b/api/src/models/project-survey-attachments.ts index 7f2c3831ae..ae253d0059 100644 --- a/api/src/models/project-survey-attachments.ts +++ b/api/src/models/project-survey-attachments.ts @@ -37,13 +37,13 @@ export interface IReportAttachmentAuthor { export class PostReportAttachmentMetadata { title: string; - year_published: string; + year_published: number; authors: IReportAttachmentAuthor[]; description: string; constructor(obj?: any) { this.title = (obj && obj?.title) || null; - this.year_published = (obj && obj?.year_published) || null; + this.year_published = Number((obj && obj?.year_published) || null); this.authors = (obj?.authors?.length && obj.authors) || []; this.description = (obj && obj?.description) || null; } @@ -71,9 +71,9 @@ export class GetReportAttachmentMetadata { constructor(metaObj?: any, authorObj?: any) { this.attachment_id = (metaObj && metaObj?.attachment_id) || null; this.title = (metaObj && metaObj?.title) || null; - this.last_modified = (metaObj && metaObj?.update_date) || null; + this.last_modified = (metaObj && metaObj?.update_date.toString()) || null; this.description = (metaObj && metaObj?.description) || null; - this.year_published = (metaObj && metaObj?.year) || null; + this.year_published = Number((metaObj && metaObj?.year) || null); this.revision_count = (metaObj && metaObj?.revision_count) || null; this.authors = (authorObj && diff --git a/api/src/models/project-view.ts b/api/src/models/project-view.ts index a9128ee0ca..59e0a8d5e9 100644 --- a/api/src/models/project-view.ts +++ b/api/src/models/project-view.ts @@ -54,7 +54,7 @@ export class GetProjectData { moment(projectData.end_date).endOf('day').isBefore(moment()) && COMPLETION_STATUS.COMPLETED) || COMPLETION_STATUS.ACTIVE; - this.publish_date = projectData?.publish_date || ''; + this.publish_date = String(projectData?.publish_date || ''); this.revision_count = projectData?.revision_count ?? null; } } diff --git a/api/src/paths/dwc/view-occurrences.ts b/api/src/paths/dwc/view-occurrences.ts index ee2284d9da..23da6e2ac2 100644 --- a/api/src/paths/dwc/view-occurrences.ts +++ b/api/src/paths/dwc/view-occurrences.ts @@ -61,8 +61,8 @@ POST.apiDoc = { 'application/json': { schema: { title: 'Occurrences spatial and metadata response object, for view purposes', - type: 'object', - properties: {} + type: 'array', + items: {} } } } diff --git a/api/src/paths/project/{projectId}/attachments/list.ts b/api/src/paths/project/{projectId}/attachments/list.ts index dc6396d38e..db16dfca84 100644 --- a/api/src/paths/project/{projectId}/attachments/list.ts +++ b/api/src/paths/project/{projectId}/attachments/list.ts @@ -71,7 +71,8 @@ GET.apiDoc = { }, securityToken: { description: 'The security token of the attachment', - type: 'string' + type: 'string', + nullable: true }, size: { type: 'number' diff --git a/api/src/paths/project/{projectId}/attachments/report/upload.ts b/api/src/paths/project/{projectId}/attachments/report/upload.ts index 648eb05293..32550267e8 100644 --- a/api/src/paths/project/{projectId}/attachments/report/upload.ts +++ b/api/src/paths/project/{projectId}/attachments/report/upload.ts @@ -64,7 +64,7 @@ POST.apiDoc = { type: 'string' }, year_published: { - type: 'string' + type: 'number' }, authors: { type: 'array', diff --git a/api/src/paths/project/{projectId}/attachments/{attachmentId}/delete.ts b/api/src/paths/project/{projectId}/attachments/{attachmentId}/delete.ts index e03677c52d..a8bc75f46b 100644 --- a/api/src/paths/project/{projectId}/attachments/{attachmentId}/delete.ts +++ b/api/src/paths/project/{projectId}/attachments/{attachmentId}/delete.ts @@ -63,7 +63,8 @@ POST.apiDoc = { type: 'string' }, securityToken: { - type: 'string' + type: 'string', + nullable: true } } } diff --git a/api/src/paths/project/{projectId}/attachments/{attachmentId}/metadata/update.ts b/api/src/paths/project/{projectId}/attachments/{attachmentId}/metadata/update.ts index 6fc9037c3f..b0362c7ad4 100644 --- a/api/src/paths/project/{projectId}/attachments/{attachmentId}/metadata/update.ts +++ b/api/src/paths/project/{projectId}/attachments/{attachmentId}/metadata/update.ts @@ -73,14 +73,7 @@ PUT.apiDoc = { type: 'string' }, year_published: { - oneOf: [ - { - type: 'string' - }, - { - type: 'number' - } - ] + type: 'number' }, authors: { type: 'array', From ecf6d87caa5fd78064ca09c67bb343f772060f76 Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Mon, 25 Apr 2022 15:18:38 -0700 Subject: [PATCH 07/20] BHBC-1689: Fix publish_year type for report attachment uploading via POST. --- api/src/paths/project/{projectId}/attachments/report/upload.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/src/paths/project/{projectId}/attachments/report/upload.ts b/api/src/paths/project/{projectId}/attachments/report/upload.ts index 32550267e8..71d12e5aca 100644 --- a/api/src/paths/project/{projectId}/attachments/report/upload.ts +++ b/api/src/paths/project/{projectId}/attachments/report/upload.ts @@ -64,7 +64,8 @@ POST.apiDoc = { type: 'string' }, year_published: { - type: 'number' + type: 'string', + description: "Year the report is published. (Note: Content-Type: multipart/form-data requires all parameters to be strings.)" }, authors: { type: 'array', From b98471444831a2e394ef9dec34cfc5191b1f175e Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Mon, 25 Apr 2022 16:47:05 -0700 Subject: [PATCH 08/20] BHBC-1689: Fix api response validation for user data response. --- api/src/models/public/project.ts | 2 +- api/src/openapi/schemas/administrative-activity.ts | 4 ++-- api/src/openapi/schemas/permit-no-sampling.ts | 2 +- api/src/paths/administrative-activities.ts | 8 +++++--- api/src/paths/permit/list.ts | 3 ++- api/src/paths/user/list.ts | 2 +- api/src/paths/user/{userId}/get.ts | 3 ++- 7 files changed, 14 insertions(+), 10 deletions(-) diff --git a/api/src/models/public/project.ts b/api/src/models/public/project.ts index d3ab161c27..a3b95838c3 100644 --- a/api/src/models/public/project.ts +++ b/api/src/models/public/project.ts @@ -84,7 +84,7 @@ export class GetPublicAttachmentsData { id: item.id, fileName: item.file_name, fileType: item.file_type || 'Report', - lastModified: item.update_date || item.create_date, + lastModified: (item.update_date || item.create_date).toString(), size: item.file_size, securityToken: item.is_secured }; diff --git a/api/src/openapi/schemas/administrative-activity.ts b/api/src/openapi/schemas/administrative-activity.ts index bf87fc9b47..6c3cff4245 100644 --- a/api/src/openapi/schemas/administrative-activity.ts +++ b/api/src/openapi/schemas/administrative-activity.ts @@ -10,8 +10,8 @@ export const administrativeActivityResponseObject = { type: 'number' }, date: { - type: 'string', - description: 'The date this administrative activity was made' + description: 'The date this administrative activity was made', + oneOf: [{ type: 'object' }, { type: 'string', format: 'date' }] } } }; diff --git a/api/src/openapi/schemas/permit-no-sampling.ts b/api/src/openapi/schemas/permit-no-sampling.ts index 8bdbea71a5..e256cdbe93 100644 --- a/api/src/openapi/schemas/permit-no-sampling.ts +++ b/api/src/openapi/schemas/permit-no-sampling.ts @@ -59,7 +59,7 @@ export const permitNoSamplingPostBody = { export const permitNoSamplingResponseBody = { title: 'Permit no sampling Response Object', type: 'object', - required: ['id'], + required: ['ids'], properties: { ids: { type: 'array', diff --git a/api/src/paths/administrative-activities.ts b/api/src/paths/administrative-activities.ts index 4bfaa93f72..3003017bc4 100644 --- a/api/src/paths/administrative-activities.ts +++ b/api/src/paths/administrative-activities.ts @@ -92,10 +92,12 @@ GET.apiDoc = { description: 'Administrative activity status type name' }, description: { - type: 'string' + type: 'string', + nullable: true }, notes: { - type: 'string' + type: 'string', + nullable: true }, data: { type: 'object', @@ -105,7 +107,7 @@ GET.apiDoc = { } }, create_date: { - type: 'string' + oneOf: [{ type: 'object' }, { type: 'string', format: 'date' }], } } } diff --git a/api/src/paths/permit/list.ts b/api/src/paths/permit/list.ts index 1228a79087..46fef06b1c 100644 --- a/api/src/paths/permit/list.ts +++ b/api/src/paths/permit/list.ts @@ -57,7 +57,8 @@ GET.apiDoc = { type: 'string' }, project_name: { - type: 'string' + type: 'string', + nullable: true } } }, diff --git a/api/src/paths/user/list.ts b/api/src/paths/user/list.ts index 84cbb4dc4a..615b691dd4 100644 --- a/api/src/paths/user/list.ts +++ b/api/src/paths/user/list.ts @@ -50,7 +50,7 @@ GET.apiDoc = { role_ids: { type: 'array', items: { - type: 'string' + type: 'number' } }, role_names: { diff --git a/api/src/paths/user/{userId}/get.ts b/api/src/paths/user/{userId}/get.ts index d708b5e10f..f77dacd74d 100644 --- a/api/src/paths/user/{userId}/get.ts +++ b/api/src/paths/user/{userId}/get.ts @@ -59,8 +59,9 @@ GET.apiDoc = { type: 'string' }, record_end_date: { + oneOf: [{ type: 'object' }, { type: 'string', format: 'date' }], + nullable: true, description: 'Determines if the user record has expired', - type: 'string' }, role_ids: { description: 'list of role ids for the user', From 050cc6745e5203399d9a0060367229e1c0dad9b7 Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Mon, 25 Apr 2022 16:53:49 -0700 Subject: [PATCH 09/20] BHBC-1689: Change year_published type from string to number in attachment tests. --- api/src/models/project-survey-attachments.test.ts | 6 +++--- .../attachments/{attachmentId}/metadata/get.test.ts | 2 +- .../attachments/{attachmentId}/metadata/get.test.ts | 2 +- api/src/queries/project/project-attachments-queries.test.ts | 4 ++-- api/src/queries/survey/survey-attachments-queries.test.ts | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/api/src/models/project-survey-attachments.test.ts b/api/src/models/project-survey-attachments.test.ts index 447397f7ed..3fb9fa719f 100644 --- a/api/src/models/project-survey-attachments.test.ts +++ b/api/src/models/project-survey-attachments.test.ts @@ -72,7 +72,7 @@ describe('PostReportAttachmentsMetaData', () => { const input = { title: 'Report 1', - year_published: '2000', + year_published: 2000, authors: [{ first_name: 'John', last_name: 'Smith' }], description: 'abstract of the report' }; @@ -84,7 +84,7 @@ describe('PostReportAttachmentsMetaData', () => { it('sets the report metadata', function () { expect(postReportAttachmentsData).to.eql({ title: 'Report 1', - year_published: '2000', + year_published: 2000, authors: [{ first_name: 'John', last_name: 'Smith' }], description: 'abstract of the report' }); @@ -108,7 +108,7 @@ describe('PutReportAttachmentMetaData', () => { describe('All values provided', () => { const input = { title: 'Report 1', - year_published: '2000', + year_published: 2000, authors: [{ first_name: 'John', last_name: 'Smith' }], description: 'abstract of the report', revision_count: 1 diff --git a/api/src/paths/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts b/api/src/paths/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts index 5b5cd0022f..cba5c9e1b1 100644 --- a/api/src/paths/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts +++ b/api/src/paths/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts @@ -153,7 +153,7 @@ describe('gets metadata for a project report', () => { title: 'My report', last_modified: '2020-10-10', description: 'some description', - year_published: '2020', + year_published: 2020, revision_count: '1', authors: [{ first_name: 'John', last_name: 'Smith' }] }); diff --git a/api/src/paths/public/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts b/api/src/paths/public/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts index 5daafb7c26..53c5528759 100644 --- a/api/src/paths/public/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts +++ b/api/src/paths/public/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts @@ -153,7 +153,7 @@ describe('gets metadata for a project report', () => { title: 'My report', last_modified: '2020-10-10', description: 'some description', - year_published: '2020', + year_published: 2020, revision_count: '1', authors: [{ first_name: 'John', last_name: 'Smith' }] }); diff --git a/api/src/queries/project/project-attachments-queries.test.ts b/api/src/queries/project/project-attachments-queries.test.ts index a81db30158..4d72f8bdc5 100644 --- a/api/src/queries/project/project-attachments-queries.test.ts +++ b/api/src/queries/project/project-attachments-queries.test.ts @@ -23,7 +23,7 @@ import { const post_sample_attachment_meta = { title: 'title', - year_published: '2000', + year_published: 2000, authors: [ { first_name: 'John', @@ -35,7 +35,7 @@ const post_sample_attachment_meta = { const put_sample_attachment_meta = { title: 'title', - year_published: '2000', + year_published: 2000, authors: [ { first_name: 'John', diff --git a/api/src/queries/survey/survey-attachments-queries.test.ts b/api/src/queries/survey/survey-attachments-queries.test.ts index 83275e1277..442c0b41f6 100644 --- a/api/src/queries/survey/survey-attachments-queries.test.ts +++ b/api/src/queries/survey/survey-attachments-queries.test.ts @@ -23,7 +23,7 @@ import { const post_sample_attachment_meta = { title: 'title', - year_published: '2000', + year_published: 2000, authors: [ { first_name: 'John', @@ -35,7 +35,7 @@ const post_sample_attachment_meta = { const put_sample_attachment_meta = { title: 'title', - year_published: '2000', + year_published: 2000, authors: [ { first_name: 'John', From f47184db56d28c59dbd89e9186d44ba6a61865ca Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Tue, 26 Apr 2022 10:11:54 -0700 Subject: [PATCH 10/20] BHBC-1689: Update tests --- api/src/models/project-survey-attachments.test.ts | 8 ++++---- api/src/models/project-survey-attachments.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/api/src/models/project-survey-attachments.test.ts b/api/src/models/project-survey-attachments.test.ts index 3fb9fa719f..98d3222780 100644 --- a/api/src/models/project-survey-attachments.test.ts +++ b/api/src/models/project-survey-attachments.test.ts @@ -63,7 +63,7 @@ describe('PostReportAttachmentsMetaData', () => { }); it('sets attachmentsData', function () { - expect(postReportAttachmentsData).to.eql({ title: null, year_published: null, authors: [], description: null }); + expect(postReportAttachmentsData).to.eql({ title: null, year_published: 0, authors: [], description: null }); }); }); @@ -133,7 +133,7 @@ describe('GetReportAttachmentMetaData', () => { expect(getReportAttachmentData).to.eql({ attachment_id: null, title: null, - year_published: null, + year_published: 0, authors: [], description: null, last_modified: null, @@ -148,7 +148,7 @@ describe('GetReportAttachmentMetaData', () => { title: 'My Report', update_date: '2020-10-10', description: 'abstract of the report', - year: '2020', + year_published: 2020, revision_count: 2, authors: [{ first_name: 'John', last_name: 'Smith' }] }; @@ -157,7 +157,7 @@ describe('GetReportAttachmentMetaData', () => { const getReportAttachmentData = new GetReportAttachmentMetadata(input); expect(getReportAttachmentData.title).to.equal(input.title); - expect(getReportAttachmentData.year_published).to.equal(input.year); + expect(getReportAttachmentData.year_published).to.equal(input.year_published); expect(getReportAttachmentData.description).to.equal(input.description); expect(getReportAttachmentData.last_modified).to.equal(input.update_date); expect(getReportAttachmentData.revision_count).to.equal(input.revision_count); diff --git a/api/src/models/project-survey-attachments.ts b/api/src/models/project-survey-attachments.ts index ae253d0059..2e24a73b45 100644 --- a/api/src/models/project-survey-attachments.ts +++ b/api/src/models/project-survey-attachments.ts @@ -73,7 +73,7 @@ export class GetReportAttachmentMetadata { this.title = (metaObj && metaObj?.title) || null; this.last_modified = (metaObj && metaObj?.update_date.toString()) || null; this.description = (metaObj && metaObj?.description) || null; - this.year_published = Number((metaObj && metaObj?.year) || null); + this.year_published = Number((metaObj && metaObj?.year_published) || null); this.revision_count = (metaObj && metaObj?.revision_count) || null; this.authors = (authorObj && From 6ea5f614d30bf5337483f8d98d2fc105704380b4 Mon Sep 17 00:00:00 2001 From: Nick Phura Date: Tue, 26 Apr 2022 10:43:45 -0700 Subject: [PATCH 11/20] Update occurrence submission response openapi --- .../{surveyId}/observation/submission/{submissionId}/view.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/{submissionId}/view.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/{submissionId}/view.ts index 1910d8d858..402f344c91 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/{submissionId}/view.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/observation/submission/{submissionId}/view.ts @@ -89,7 +89,10 @@ GET.apiDoc = { rows: { type: 'array', items: { - type: 'string' + type: 'array', + items: { + nullable: true + } } } } From cc071bbafe4913e2fa548ce52ce1dccd1ddac9b0 Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Tue, 26 Apr 2022 10:57:33 -0700 Subject: [PATCH 12/20] BHBC-1689: Fix api response validation for uploads --- .../survey/{surveyId}/attachments/report/upload.ts | 3 ++- .../survey/{surveyId}/attachments/upload.ts | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/report/upload.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/report/upload.ts index 6d3be0543c..befdee790a 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/report/upload.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/report/upload.ts @@ -69,7 +69,8 @@ POST.apiDoc = { type: 'string' }, year_published: { - type: 'number' + type: 'string', + description: "Year the report is published. (Note: Content-Type: multipart/form-data requires all parameters to be strings.)" }, authors: { type: 'array', diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/upload.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/upload.ts index 9f9e38e370..2da518b052 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/upload.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/upload.ts @@ -68,8 +68,16 @@ POST.apiDoc = { content: { 'application/json': { schema: { - type: 'string', - description: 'The S3 unique key for this file.' + type: 'object', + required: ['attachmentId', 'revision_count'], + properties: { + attachmentId: { + type: 'number' + }, + revision_count: { + type: 'number' + } + } } } } From aaa5d08277e560448137610f1fb1a8e7f355977e Mon Sep 17 00:00:00 2001 From: Nick Phura Date: Tue, 26 Apr 2022 11:11:13 -0700 Subject: [PATCH 13/20] Update summary submission openapi and parse logic --- .../{surveyId}/summary/submission/upload.ts | 51 +++++++++++++++++-- .../summary/submission/{summaryId}/view.ts | 5 +- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/upload.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/upload.ts index 5ec4e92988..858d3c511f 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/upload.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/upload.ts @@ -11,10 +11,10 @@ import { authorizeRequestHandler } from '../../../../../../../request-handlers/s import { generateS3FileKey, scanFileForVirus, uploadFileToS3 } from '../../../../../../../utils/file-utils'; import { getLogger } from '../../../../../../../utils/logger'; import { ICsvState } from '../../../../../../../utils/media/csv/csv-file'; -import { IMediaState } from '../../../../../../../utils/media/media-file'; +import { IMediaState, MediaFile } from '../../../../../../../utils/media/media-file'; +import { parseUnknownMedia } from '../../../../../../../utils/media/media-utils'; import { ValidationSchemaParser } from '../../../../../../../utils/media/validation/validation-schema-parser'; import { XLSXCSV } from '../../../../../../../utils/media/xlsx/xlsx-file'; -import { prepXLSX } from './../../../../../../../paths/xlsx/validate'; const defaultLog = getLogger('/api/project/{projectId}/survey/{surveyId}/summary/upload'); @@ -79,7 +79,19 @@ POST.apiDoc = { }, responses: { 200: { - description: 'Upload OK' + description: 'Upload OK', + content: { + 'application/json': { + schema: { + type: 'object', + properties: { + summarySubmissionId: { + type: 'number' + } + } + } + } + } }, 400: { $ref: '#/components/responses/400' @@ -208,6 +220,39 @@ export function uploadMedia(): RequestHandler { }; } +export function prepXLSX(): RequestHandler { + return async (req, res, next) => { + defaultLog.debug({ label: 'prepXLSX', message: 's3File' }); + + try { + const s3File = req['s3File']; + + const parsedMedia = parseUnknownMedia(s3File); + + if (!parsedMedia) { + req['parseError'] = 'Failed to parse submission, file was empty'; + + return next(); + } + + if (!(parsedMedia instanceof MediaFile)) { + req['parseError'] = 'Failed to parse submission, not a valid XLSX CSV file'; + + return next(); + } + + const xlsxCsv = new XLSXCSV(parsedMedia); + + req['xlsx'] = xlsxCsv; + + next(); + } catch (error) { + defaultLog.error({ label: 'prepXLSX', message: 'error', error }); + throw error; + } + }; +} + /** * Inserts a new record into the `survey_summary_submission` table. * diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts index f84e262a31..c0edfd6da4 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts @@ -89,7 +89,10 @@ GET.apiDoc = { rows: { type: 'array', items: { - type: 'string' + type: 'array', + items: { + nullable: true + } } } } From e5471d3edd0210608555e20bb696874fe18c81c9 Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Tue, 26 Apr 2022 11:47:12 -0700 Subject: [PATCH 14/20] BHBC-1689: Fix all failing tests for reports --- api/src/models/project-survey-attachments.test.ts | 2 +- .../attachments/{attachmentId}/metadata/get.test.ts | 2 +- .../attachments/{attachmentId}/metadata/get.test.ts | 4 ++-- .../attachments/{attachmentId}/metadata/get.test.ts | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/api/src/models/project-survey-attachments.test.ts b/api/src/models/project-survey-attachments.test.ts index 98d3222780..0c4c9a7aae 100644 --- a/api/src/models/project-survey-attachments.test.ts +++ b/api/src/models/project-survey-attachments.test.ts @@ -98,7 +98,7 @@ describe('PutReportAttachmentMetaData', () => { const putReportAttachmentData = new PutReportAttachmentMetadata(null); expect(putReportAttachmentData.title).to.equal(null); - expect(putReportAttachmentData.year_published).to.equal(null); + expect(putReportAttachmentData.year_published).to.equal(0); expect(putReportAttachmentData.authors).to.eql([]); expect(putReportAttachmentData.description).to.equal(null); expect(putReportAttachmentData.revision_count).to.equal(null); diff --git a/api/src/paths/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts b/api/src/paths/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts index cba5c9e1b1..27ddf30a5a 100644 --- a/api/src/paths/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts +++ b/api/src/paths/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts @@ -126,7 +126,7 @@ describe('gets metadata for a project report', () => { title: 'My report', update_date: '2020-10-10', description: 'some description', - year: '2020', + year_published: 2020, revision_count: '1' } ] diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/{attachmentId}/metadata/get.test.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/{attachmentId}/metadata/get.test.ts index e8fa005e09..908f494f9c 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/{attachmentId}/metadata/get.test.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/{attachmentId}/metadata/get.test.ts @@ -144,7 +144,7 @@ describe('gets metadata for a survey report', () => { title: 'My report', update_date: '2020-10-10', description: 'some description', - year: '2020', + year_published: 2020, revision_count: '1' } ] @@ -171,7 +171,7 @@ describe('gets metadata for a survey report', () => { title: 'My report', last_modified: '2020-10-10', description: 'some description', - year_published: '2020', + year_published: 2020, revision_count: '1', authors: [{ first_name: 'John', last_name: 'Smith' }] }); diff --git a/api/src/paths/public/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts b/api/src/paths/public/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts index 53c5528759..3d31892d88 100644 --- a/api/src/paths/public/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts +++ b/api/src/paths/public/project/{projectId}/attachments/{attachmentId}/metadata/get.test.ts @@ -126,7 +126,7 @@ describe('gets metadata for a project report', () => { title: 'My report', update_date: '2020-10-10', description: 'some description', - year: '2020', + year_published: 2020, revision_count: '1' } ] From c1f49b7d8aaf65434a8a03cc0220f87e1bec437a Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Tue, 26 Apr 2022 11:56:16 -0700 Subject: [PATCH 15/20] BHBC-1689: Make lint-fix and make format-fix. --- api/src/app.ts | 2 +- api/src/paths/administrative-activities.ts | 2 +- api/src/paths/project/{projectId}/attachments/report/upload.ts | 3 ++- .../project/{projectId}/survey/{surveyId}/attachments/list.ts | 2 +- .../{projectId}/survey/{surveyId}/attachments/report/upload.ts | 3 ++- .../survey/{surveyId}/summary/submission/{summaryId}/view.ts | 2 +- api/src/paths/user/{userId}/get.ts | 2 +- 7 files changed, 9 insertions(+), 7 deletions(-) diff --git a/api/src/app.ts b/api/src/app.ts index 18a39277a3..2074f590b9 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -146,7 +146,7 @@ function validateAllResponses(req: Request, res: Response, next: NextFunction) { if (validationResult?.errors) { // Temporary log - console.log('Body:', JSON.stringify(body)) + console.log('Body:', JSON.stringify(body)); validationMessage = `Invalid response for status code ${res.statusCode}`; diff --git a/api/src/paths/administrative-activities.ts b/api/src/paths/administrative-activities.ts index 3003017bc4..9ebfa853da 100644 --- a/api/src/paths/administrative-activities.ts +++ b/api/src/paths/administrative-activities.ts @@ -107,7 +107,7 @@ GET.apiDoc = { } }, create_date: { - oneOf: [{ type: 'object' }, { type: 'string', format: 'date' }], + oneOf: [{ type: 'object' }, { type: 'string', format: 'date' }] } } } diff --git a/api/src/paths/project/{projectId}/attachments/report/upload.ts b/api/src/paths/project/{projectId}/attachments/report/upload.ts index 71d12e5aca..228e0b0a02 100644 --- a/api/src/paths/project/{projectId}/attachments/report/upload.ts +++ b/api/src/paths/project/{projectId}/attachments/report/upload.ts @@ -65,7 +65,8 @@ POST.apiDoc = { }, year_published: { type: 'string', - description: "Year the report is published. (Note: Content-Type: multipart/form-data requires all parameters to be strings.)" + description: + 'Year the report is published. (Note: Content-Type: multipart/form-data requires all parameters to be strings.)' }, authors: { type: 'array', diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/list.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/list.ts index a1f3868f8a..a6e66b8d46 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/list.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/list.ts @@ -57,7 +57,7 @@ GET.apiDoc = { content: { 'application/json': { schema: { - type: 'object', + type: 'object', required: ['attachmentsList'], properties: { attachmentsList: { diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/report/upload.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/report/upload.ts index befdee790a..2e69e31106 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/report/upload.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/attachments/report/upload.ts @@ -70,7 +70,8 @@ POST.apiDoc = { }, year_published: { type: 'string', - description: "Year the report is published. (Note: Content-Type: multipart/form-data requires all parameters to be strings.)" + description: + 'Year the report is published. (Note: Content-Type: multipart/form-data requires all parameters to be strings.)' }, authors: { type: 'array', diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts index c0edfd6da4..345cf7d1aa 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts @@ -199,7 +199,7 @@ export function getSummarySubmissionCSVForView(): RequestHandler { data.push(dataItem); } - console.log("_DATA:", data) + console.log('_DATA:', data); return res.status(200).json({ data }); } catch (error) { diff --git a/api/src/paths/user/{userId}/get.ts b/api/src/paths/user/{userId}/get.ts index f77dacd74d..9704014279 100644 --- a/api/src/paths/user/{userId}/get.ts +++ b/api/src/paths/user/{userId}/get.ts @@ -61,7 +61,7 @@ GET.apiDoc = { record_end_date: { oneOf: [{ type: 'object' }, { type: 'string', format: 'date' }], nullable: true, - description: 'Determines if the user record has expired', + description: 'Determines if the user record has expired' }, role_ids: { description: 'list of role ids for the user', From c9addc05029462e232e7556923a522fef616fd8a Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Tue, 26 Apr 2022 12:29:08 -0700 Subject: [PATCH 16/20] BHBC-1689: Remove console logs used for testing. --- api/src/app.ts | 3 --- .../survey/{surveyId}/summary/submission/{summaryId}/view.ts | 1 - 2 files changed, 4 deletions(-) diff --git a/api/src/app.ts b/api/src/app.ts index 2074f590b9..36088a5f35 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -145,9 +145,6 @@ function validateAllResponses(req: Request, res: Response, next: NextFunction) { let errorList = []; if (validationResult?.errors) { - // Temporary log - console.log('Body:', JSON.stringify(body)); - validationMessage = `Invalid response for status code ${res.statusCode}`; errorList = Array.from(validationResult.errors); diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts index 345cf7d1aa..794b6c90e0 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/summary/submission/{summaryId}/view.ts @@ -199,7 +199,6 @@ export function getSummarySubmissionCSVForView(): RequestHandler { data.push(dataItem); } - console.log('_DATA:', data); return res.status(200).json({ data }); } catch (error) { From b53c60cbb5e027464946759ec644269e9547294c Mon Sep 17 00:00:00 2001 From: Anissa Agahchen Date: Tue, 26 Apr 2022 14:47:02 -0700 Subject: [PATCH 17/20] api validation for view.ts and surveys.ts --- .../{projectId}/survey/{surveyId}/view.ts | 26 +++++++++++----- api/src/paths/project/{projectId}/surveys.ts | 30 +++++++++++++++++-- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/view.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/view.ts index debfa95755..47a0577786 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/view.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/view.ts @@ -100,7 +100,7 @@ GET.apiDoc = { ancillary_species: { type: 'array', items: { - type: 'string' + type: 'number' } }, ancillary_species_names: { @@ -112,7 +112,7 @@ GET.apiDoc = { focal_species: { type: 'array', items: { - type: 'string' + type: 'number' } }, focal_species_names: { @@ -148,20 +148,25 @@ GET.apiDoc = { required: ['agency_name', 'funding_amount', 'funding_start_date', 'funding_end_date'], properties: { pfs_id: { - type: 'number' + type: 'number', + nullable: true }, agency_name: { - type: 'string' + type: 'string', + nullable: true }, funding_amount: { - type: 'number' + type: 'number', + nullable: true }, funding_start_date: { type: 'string', + nullable: true, description: 'ISO 8601 date string' }, funding_end_date: { type: 'string', + nullable: true, description: 'ISO 8601 date string' } } @@ -176,6 +181,7 @@ GET.apiDoc = { occurrence_submission_id: { description: 'A survey occurrence submission ID', type: 'number', + nullable: true, example: 1 }, permit_number: { @@ -185,7 +191,9 @@ GET.apiDoc = { type: 'string' }, publish_date: { - type: 'string' + oneOf: [{ type: 'object' }, { type: 'string', format: 'date' }], + nullable: true, + description: 'Determines if the record has been published' }, revision_count: { type: 'number' @@ -260,10 +268,12 @@ GET.apiDoc = { type: 'string' }, first_nations_id: { - type: 'number' + type: 'number', + nullable: true }, first_nations_name: { - type: 'string' + type: 'string', + nullable: true }, proprietary_data_category: { type: 'number' diff --git a/api/src/paths/project/{projectId}/surveys.ts b/api/src/paths/project/{projectId}/surveys.ts index d2bb8f5a6a..001f4fb2a2 100644 --- a/api/src/paths/project/{projectId}/surveys.ts +++ b/api/src/paths/project/{projectId}/surveys.ts @@ -52,10 +52,34 @@ GET.apiDoc = { items: { title: 'Survey Response Object', type: 'object', - required: ['id'], properties: { - id: { - type: 'number' + survey: { + type: 'object', + properties: { + id: { + type: 'number' + }, + name: { + type: 'string' + }, + publish_status: { + type: 'string' + }, + completion_status: { + type: 'string' + }, + start_date: { + type: 'string', + description: 'ISO 8601 date string' + }, + end_date: { + type: 'string', + description: 'ISO 8601 date string' + } + } + }, + species: { + type: 'object' } } } From 3d49191abe92ac49d59de5c2b064382219e4575c Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Tue, 26 Apr 2022 15:15:13 -0700 Subject: [PATCH 18/20] BHBC-1689: Fix validation for surveys. --- .../project/{projectId}/survey/{surveyId}/update.ts | 10 ++++++---- .../project/{projectId}/survey/{surveyId}/view.ts | 5 +++-- api/src/paths/project/{projectId}/surveys.ts | 3 ++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts index a04c119389..ba72876352 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts @@ -144,13 +144,13 @@ GET.apiDoc = { ancillary_species: { type: 'array', items: { - type: 'string' + type: 'number' } }, focal_species: { type: 'array', items: { - type: 'string' + type: 'number' } }, biologist_first_name: { @@ -227,6 +227,7 @@ GET.apiDoc = { survey_purpose_and_methodology: { description: 'Survey Details', type: 'object', + nullable: true, required: [ 'field_method_id', 'intended_outcome_id', @@ -242,7 +243,8 @@ GET.apiDoc = { type: 'number' }, additional_details: { - type: 'string' + type: 'string', + nullable: true }, intended_outcome_id: { type: 'number' @@ -268,7 +270,7 @@ GET.apiDoc = { survey_proprietor: { description: 'Survey Details', type: 'object', - //Note: do not make any of these fields required as the object can be null + nullable: true, properties: { survey_data_proprietary: { type: 'string' diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/view.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/view.ts index 47a0577786..84b1096ad4 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/view.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/view.ts @@ -227,7 +227,8 @@ GET.apiDoc = { type: 'number' }, additional_details: { - type: 'string' + type: 'string', + nullable: true }, intended_outcome_id: { type: 'number' @@ -253,7 +254,7 @@ GET.apiDoc = { survey_proprietor: { description: 'Survey Details', type: 'object', - //Note: do not make any of these fields required as the object can be null + nullable: true, properties: { survey_data_proprietary: { type: 'string' diff --git a/api/src/paths/project/{projectId}/surveys.ts b/api/src/paths/project/{projectId}/surveys.ts index 001f4fb2a2..d8a926db82 100644 --- a/api/src/paths/project/{projectId}/surveys.ts +++ b/api/src/paths/project/{projectId}/surveys.ts @@ -74,7 +74,8 @@ GET.apiDoc = { }, end_date: { type: 'string', - description: 'ISO 8601 date string' + description: 'ISO 8601 date string', + nullable: true } } }, From 2a28c73d5da89b3d5d5d144bb451672661f1e36f Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Tue, 26 Apr 2022 15:21:38 -0700 Subject: [PATCH 19/20] BHBC-1689: Fix additional survey validation errors re publish_date. --- api/src/models/survey-update.ts | 2 +- api/src/models/survey-view.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api/src/models/survey-update.ts b/api/src/models/survey-update.ts index 76ed4ac7c0..c94bed7e0e 100644 --- a/api/src/models/survey-update.ts +++ b/api/src/models/survey-update.ts @@ -61,7 +61,7 @@ export class GetUpdateSurveyDetailsData { moment(surveyDetailsData.end_date).endOf('day').isBefore(moment()) && COMPLETION_STATUS.COMPLETED) || COMPLETION_STATUS.ACTIVE; - this.publish_date = surveyDetailsData?.publish_date || ''; + this.publish_date = String(surveyDetailsData?.publish_date || ''); } } diff --git a/api/src/models/survey-view.ts b/api/src/models/survey-view.ts index 07af1c4bce..84c2c2ba88 100644 --- a/api/src/models/survey-view.ts +++ b/api/src/models/survey-view.ts @@ -67,7 +67,7 @@ export class GetViewSurveyDetailsData { moment(surveyDetailsData.end_date).endOf('day').isBefore(moment()) && COMPLETION_STATUS.COMPLETED) || COMPLETION_STATUS.ACTIVE; - this.publish_date = surveyDetailsData?.publish_date || ''; + this.publish_date = String(surveyDetailsData?.publish_date || ''); } } From d054b649541d42b3673c3e65d3deda4fcb693a70 Mon Sep 17 00:00:00 2001 From: Curtis Upshall Date: Tue, 26 Apr 2022 15:29:22 -0700 Subject: [PATCH 20/20] BHBC-1689: Fix additional survey validation for purpose_and_methodolgy and proprietary_data. --- api/src/paths/project/{projectId}/survey/{surveyId}/update.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts b/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts index ba72876352..c71d2c56e9 100644 --- a/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts +++ b/api/src/paths/project/{projectId}/survey/{surveyId}/update.ts @@ -118,6 +118,7 @@ GET.apiDoc = { survey_details: { description: 'Survey Details', type: 'object', + nullable: true, required: [ 'id', 'focal_species', @@ -285,7 +286,8 @@ GET.apiDoc = { type: 'string' }, first_nations_id: { - type: 'number' + type: 'number', + nullable: true }, first_nations_name: { type: 'string'