-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into double-standards
- Loading branch information
Showing
36 changed files
with
1,096 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
api/src/paths/project/{projectId}/survey/{surveyId}/critters/markings/import.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import { RequestHandler } from 'express'; | ||
import { Operation } from 'express-openapi'; | ||
import { PROJECT_PERMISSION, SYSTEM_ROLE } from '../../../../../../../constants/roles'; | ||
import { getDBConnection } from '../../../../../../../database/db'; | ||
import { HTTP400 } from '../../../../../../../errors/http-error'; | ||
import { csvFileSchema } from '../../../../../../../openapi/schemas/file'; | ||
import { authorizeRequestHandler } from '../../../../../../../request-handlers/security/authorization'; | ||
import { importCSV } from '../../../../../../../services/import-services/import-csv'; | ||
import { ImportMarkingsStrategy } from '../../../../../../../services/import-services/marking/import-markings-strategy'; | ||
import { scanFileForVirus } from '../../../../../../../utils/file-utils'; | ||
import { getLogger } from '../../../../../../../utils/logger'; | ||
import { parseMulterFile } from '../../../../../../../utils/media/media-utils'; | ||
import { getFileFromRequest } from '../../../../../../../utils/request'; | ||
|
||
const defaultLog = getLogger('/api/project/{projectId}/survey/{surveyId}/markings/import'); | ||
|
||
export const POST: Operation = [ | ||
authorizeRequestHandler((req) => { | ||
return { | ||
or: [ | ||
{ | ||
validProjectPermissions: [PROJECT_PERMISSION.COORDINATOR, PROJECT_PERMISSION.COLLABORATOR], | ||
surveyId: Number(req.params.surveyId), | ||
discriminator: 'ProjectPermission' | ||
}, | ||
{ | ||
validSystemRoles: [SYSTEM_ROLE.DATA_ADMINISTRATOR], | ||
discriminator: 'SystemRole' | ||
} | ||
] | ||
}; | ||
}), | ||
importCsv() | ||
]; | ||
|
||
POST.apiDoc = { | ||
description: 'Upload Critterbase CSV Markings file', | ||
tags: ['observations'], | ||
security: [ | ||
{ | ||
Bearer: [] | ||
} | ||
], | ||
parameters: [ | ||
{ | ||
in: 'path', | ||
description: 'SIMS survey id', | ||
name: 'projectId', | ||
required: true, | ||
schema: { | ||
type: 'integer', | ||
minimum: 1 | ||
} | ||
}, | ||
{ | ||
in: 'path', | ||
description: 'SIMS survey id', | ||
name: 'surveyId', | ||
required: true, | ||
schema: { | ||
type: 'integer', | ||
minimum: 1 | ||
} | ||
} | ||
], | ||
requestBody: { | ||
description: 'Critterbase Markings CSV import file.', | ||
content: { | ||
'multipart/form-data': { | ||
schema: { | ||
type: 'object', | ||
additionalProperties: false, | ||
required: ['media'], | ||
properties: { | ||
media: { | ||
description: 'Critterbase Markings CSV import file.', | ||
type: 'array', | ||
minItems: 1, | ||
maxItems: 1, | ||
items: csvFileSchema | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
responses: { | ||
201: { | ||
description: 'Marking import success.', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
markingsCreated: { | ||
description: 'Number of Critterbase markings created.', | ||
type: 'integer' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
400: { | ||
$ref: '#/components/responses/400' | ||
}, | ||
401: { | ||
$ref: '#/components/responses/401' | ||
}, | ||
403: { | ||
$ref: '#/components/responses/403' | ||
}, | ||
500: { | ||
$ref: '#/components/responses/500' | ||
}, | ||
default: { | ||
$ref: '#/components/responses/default' | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Imports a `Critterbase Marking CSV` which bulk adds markings to Critterbase. | ||
* | ||
* @return {*} {RequestHandler} | ||
*/ | ||
export function importCsv(): RequestHandler { | ||
return async (req, res) => { | ||
const surveyId = Number(req.params.surveyId); | ||
const rawFile = getFileFromRequest(req); | ||
|
||
const connection = getDBConnection(req.keycloak_token); | ||
|
||
try { | ||
await connection.open(); | ||
|
||
// Check for viruses / malware | ||
const virusScanResult = await scanFileForVirus(rawFile); | ||
|
||
if (!virusScanResult) { | ||
throw new HTTP400('Malicious content detected, import cancelled.'); | ||
} | ||
|
||
const importCsvMarkingsStrategy = new ImportMarkingsStrategy(connection, surveyId); | ||
|
||
// Pass CSV file and importer as dependencies | ||
const markingsCreated = await importCSV(parseMulterFile(rawFile), importCsvMarkingsStrategy); | ||
|
||
await connection.commit(); | ||
|
||
return res.status(201).json({ markingsCreated }); | ||
} catch (error) { | ||
defaultLog.error({ label: 'importMarkingsCSV', message: 'error', error }); | ||
await connection.rollback(); | ||
throw error; | ||
} finally { | ||
connection.release(); | ||
} | ||
}; | ||
} |
Oops, something went wrong.