Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIMSBIOHUB-333: Telemetry Table #1177

Merged
merged 106 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 98 commits
Commits
Show all changes
106 commits
Select commit Hold shift + click to select a range
2d29f9f
got basic UI section and router wired up
al-rosenthal Nov 14, 2023
6bc2c1a
routed to new page
al-rosenthal Nov 14, 2023
155deee
basic list component created
al-rosenthal Nov 15, 2023
84e7a19
built basic list layout
al-rosenthal Nov 16, 2023
d37a3e7
added deployment to survey context, wired up list with deployment api
al-rosenthal Nov 16, 2023
bf5e59f
wired up list
al-rosenthal Nov 16, 2023
86166f9
added skeleton loader component
al-rosenthal Nov 16, 2023
a649b53
clean up
al-rosenthal Nov 16, 2023
df9688e
Merge branch 'dev' into SIMSBIOHUB-333
al-rosenthal Nov 16, 2023
08cd378
updated api for list
al-rosenthal Nov 16, 2023
4576dce
added telemetry form to list
al-rosenthal Nov 17, 2023
d93a659
dialog properly showing
al-rosenthal Nov 17, 2023
99fc1dc
added selector, form is submitting
al-rosenthal Nov 17, 2023
549d47c
updated API, added menu button, added critter selector
al-rosenthal Nov 17, 2023
c8f736b
clean up
al-rosenthal Nov 18, 2023
69a8b75
added formik validation and stubbing out more work flow changes
al-rosenthal Nov 18, 2023
33adcfa
stubbing in menu actions
al-rosenthal Nov 20, 2023
fe9fa60
Merge branch 'dev' into SIMSBIOHUB-333
al-rosenthal Nov 20, 2023
b553c6f
got add deployment working
al-rosenthal Nov 20, 2023
09b5985
refactored form
al-rosenthal Nov 21, 2023
fceafba
updated validation ui
al-rosenthal Nov 21, 2023
a84653d
clean up and nw typing
al-rosenthal Nov 21, 2023
dc78928
delete / create / get proxy endpoints for BCTW manual telemetry created
MacQSL Nov 22, 2023
f2c0317
Merge branch 'SIMSBIOHUB-333' of https://github.com/bcgov/biohubbc in…
MacQSL Nov 22, 2023
4122eaa
added edit flow
al-rosenthal Nov 22, 2023
e558b97
stubbed out more edit code
al-rosenthal Nov 22, 2023
4868f58
updated create structure
MacQSL Nov 22, 2023
832d375
Merge branch 'SIMSBIOHUB-333' of https://github.com/bcgov/biohubbc in…
MacQSL Nov 22, 2023
c01736a
stubbing out more edit changes
al-rosenthal Nov 22, 2023
a0b765e
Merge branch 'SIMSBIOHUB-333' of https://github.com/bcgov/biohubbc in…
al-rosenthal Nov 22, 2023
94b5e29
updating edit workflow
al-rosenthal Nov 23, 2023
4d3a0f0
got basic delete wired up
al-rosenthal Nov 23, 2023
530a3ca
fixed broken tests
al-rosenthal Nov 23, 2023
8a34a5a
updated tests for new bctw proxy endpoints
MacQSL Nov 23, 2023
37be203
Merge branch 'SIMSBIOHUB-333' of https://github.com/bcgov/biohubbc in…
MacQSL Nov 23, 2023
a068c3a
more form enhancements
al-rosenthal Nov 24, 2023
9c3a778
Merge branch 'SIMSBIOHUB-333' of https://github.com/bcgov/biohubbc in…
al-rosenthal Nov 24, 2023
02010f4
updated skeleton lists
al-rosenthal Nov 27, 2023
0f345d9
stubbing out tables
al-rosenthal Nov 27, 2023
cb8fb0c
updating card
al-rosenthal Nov 27, 2023
dc45538
dded label
al-rosenthal Nov 27, 2023
817d3e9
updated select and list ui
al-rosenthal Nov 27, 2023
0e6ec55
stubbing out context for telemetry
al-rosenthal Nov 27, 2023
6d88e9c
Merge branch 'dev' into SIMSBIOHUB-333
al-rosenthal Nov 29, 2023
fa11956
stubbed out more api changes
al-rosenthal Nov 29, 2023
29c07ed
copying over table changes
al-rosenthal Nov 29, 2023
36e59e2
renamed file
al-rosenthal Nov 29, 2023
ec0ce55
oops
al-rosenthal Nov 29, 2023
0a77b37
rename continues
al-rosenthal Nov 29, 2023
2d2728d
SIMSBIOHUB-333: Added i18n consts
curtisupshall Nov 29, 2023
fdf9c30
SIMSBIOHUB-333: Added functionality to telemetryTableContext (WIP)
curtisupshall Nov 29, 2023
c93d2be
Merge branch 'SIMSBIOHUB-333' of github.com:bcgov/biohubbc into SIMSB…
curtisupshall Nov 29, 2023
8af099e
SIMSBIOHUB-333: Fix import
curtisupshall Nov 29, 2023
57d13de
refactored manual telemetry path format and updated openApi schemas
MacQSL Nov 29, 2023
d122325
Merge branch 'SIMSBIOHUB-333' of https://github.com/bcgov/biohubbc in…
MacQSL Nov 29, 2023
25efa0e
updating telemetry api
al-rosenthal Nov 30, 2023
57e5982
added main buttons and dialog to telemetry table
al-rosenthal Nov 30, 2023
ab29a85
added telemetry table columns
al-rosenthal Nov 30, 2023
8f696f0
fixed value setter
al-rosenthal Nov 30, 2023
0759eaf
adding drop down
al-rosenthal Dec 1, 2023
bfb4f24
save is wired up
al-rosenthal Dec 1, 2023
b07f1a2
creation and loading wired up
al-rosenthal Dec 1, 2023
3c90279
added update flow to table buttons
al-rosenthal Dec 1, 2023
7777193
added some stuff
al-rosenthal Dec 1, 2023
d3f6e96
fixed spelling
al-rosenthal Dec 4, 2023
3dedc29
removed observation references
al-rosenthal Dec 4, 2023
e13a870
added required files for processing and parsing csv files
al-rosenthal Dec 4, 2023
aa434ee
updated endpoints
al-rosenthal Dec 4, 2023
c6b2544
got a more consistent load
al-rosenthal Dec 4, 2023
8982fbe
began work on file upload for telemetry
al-rosenthal Dec 4, 2023
0d17ea4
fixed delete endpoint
al-rosenthal Dec 5, 2023
33facc7
added header to telemetry page
al-rosenthal Dec 5, 2023
e97dfec
clean up
al-rosenthal Dec 5, 2023
b4912b0
added snackbar and delete dialog
al-rosenthal Dec 5, 2023
06ca64e
added more snackbars
al-rosenthal Dec 5, 2023
3f0eec3
added proxy endpoint for manual/vendor telemetry combined
MacQSL Dec 5, 2023
934ee67
Merge branch 'SIMSBIOHUB-333' of https://github.com/bcgov/biohubbc in…
MacQSL Dec 5, 2023
41a8ab2
updated tests for getAllTelemetryByDeploymentIds
MacQSL Dec 5, 2023
9dd7597
fleshed out upload process
al-rosenthal Dec 5, 2023
964bca8
Merge branch 'SIMSBIOHUB-333' of https://github.com/bcgov/biohubbc in…
al-rosenthal Dec 5, 2023
a3e8db1
fixed issue with edit/ save
al-rosenthal Dec 5, 2023
70b5d09
updated api to use new endpoint
al-rosenthal Dec 5, 2023
c0663d5
Merge branch 'dev' into SIMSBIOHUB-333
al-rosenthal Dec 5, 2023
89d52b2
resolving code smells
al-rosenthal Dec 5, 2023
022b5b9
more code smell clean up
al-rosenthal Dec 5, 2023
2034536
more smells
al-rosenthal Dec 5, 2023
5119713
ran make fix
al-rosenthal Dec 5, 2023
d04e1fc
fixed bug caused by code smell fix
al-rosenthal Dec 5, 2023
88845c9
updated telemetry count
al-rosenthal Dec 5, 2023
a53796b
ignore-skip
al-rosenthal Dec 5, 2023
399efb7
hiding import button until it is working
al-rosenthal Dec 5, 2023
e0c3313
SIMSBIOHUB-333: Add fullwidth prop to datagrid edit components
curtisupshall Dec 6, 2023
512b98c
SIMSBIOHUB-333: Changed pluralization behaviour for dialog title i18n
curtisupshall Dec 6, 2023
71e972f
Merge branch 'dev' into SIMSBIOHUB-333
curtisupshall Dec 6, 2023
ee8d582
updated skeleton loads to use new styling
al-rosenthal Dec 6, 2023
5caf052
ran make fix
al-rosenthal Dec 6, 2023
10cbe55
fixed code smell
al-rosenthal Dec 6, 2023
b67d003
Merge branch 'dev' into SIMSBIOHUB-333
KjartanE Dec 7, 2023
f8a77cb
clean up
al-rosenthal Dec 8, 2023
5e419d7
Merge branch 'dev' into SIMSBIOHUB-333
al-rosenthal Dec 8, 2023
d149161
Merge branch 'dev' into SIMSBIOHUB-333
al-rosenthal Dec 12, 2023
c856334
resolving git comments
al-rosenthal Dec 12, 2023
764935e
Merge branch 'SIMSBIOHUB-333' of https://github.com/bcgov/biohubbc in…
al-rosenthal Dec 12, 2023
5c1be71
updated try catch
al-rosenthal Dec 12, 2023
f4fee15
ignore-skip
al-rosenthal Dec 12, 2023
5ca08e5
fixed discriminator
al-rosenthal Dec 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
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 { authorizeRequestHandler } from '../../../../../../request-handlers/security/authorization';
import { TelemetryService } from '../../../../../../services/telemetry-service';
import { scanFileForVirus, uploadFileToS3 } from '../../../../../../utils/file-utils';
import { getLogger } from '../../../../../../utils/logger';

const defaultLog = getLogger('/api/project/{projectId}/survey/{surveyId}/telemetry/upload');

Check warning on line 11 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L11

Added line #L11 was not covered by tests

export const POST: Operation = [

Check warning on line 13 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L13

Added line #L13 was not covered by tests
authorizeRequestHandler((req) => {
return {

Check warning on line 15 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L15

Added line #L15 was not covered by tests
or: [
{
validProjectPermissions: [PROJECT_PERMISSION.COORDINATOR, PROJECT_PERMISSION.COLLABORATOR],
projectId: Number(req.params.projectId),
discriminator: 'ProjectPermission'
},
{
validSystemRoles: [SYSTEM_ROLE.DATA_ADMINISTRATOR],
discriminator: 'SystemRole'
}
]
};
}),
uploadMedia()
];

POST.apiDoc = {

Check warning on line 32 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L32

Added line #L32 was not covered by tests
description: 'Upload survey telemetry submission file.',
tags: ['telemetry'],
security: [
{
Bearer: []
}
],
parameters: [
{
in: 'path',
name: 'projectId',
required: true
},
{
in: 'path',
name: 'surveyId',
required: true
}
],
requestBody: {
description: 'Survey telemetry submission file to upload',
content: {
'multipart/form-data': {
schema: {
type: 'object',
properties: {
media: {
description: 'A survey telemetry submission file.',
type: 'string',
format: 'binary'
}
}
}
}
}
},
responses: {
200: {
description: 'Upload OK',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
submissionId: {
type: 'number'
}
}
}
}
}
},
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'
}
}
};

/**
* Uploads a media file to S3 and inserts a matching record in the `survey_telemetry_submission` table.
*
* @return {*} {RequestHandler}
*/
export function uploadMedia(): RequestHandler {
return async (req, res) => {
const rawMediaArray: Express.Multer.File[] = req.files as Express.Multer.File[];

Check warning on line 110 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L109-L110

Added lines #L109 - L110 were not covered by tests

if (!rawMediaArray?.length) {
// no media objects included, skipping media upload step
throw new HTTP400('Missing upload data');

Check warning on line 114 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L114

Added line #L114 was not covered by tests
}

if (rawMediaArray.length !== 1) {
// no media objects included
throw new HTTP400('Too many files uploaded, expected 1');

Check warning on line 119 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L119

Added line #L119 was not covered by tests
}

const connection = getDBConnection(req['keycloak_token']);

Check warning on line 122 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L122

Added line #L122 was not covered by tests

try {
const rawMediaFile = rawMediaArray[0];

Check warning on line 125 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L124-L125

Added lines #L124 - L125 were not covered by tests

if (!rawMediaFile?.originalname.endsWith('.csv')) {
throw new HTTP400('Invalid file type, expected a CSV file.');

Check warning on line 128 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L128

Added line #L128 was not covered by tests
}

await connection.open();

Check warning on line 131 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L131

Added line #L131 was not covered by tests

// Scan file for viruses using ClamAV
const virusScanResult = await scanFileForVirus(rawMediaFile);

Check warning on line 134 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L134

Added line #L134 was not covered by tests

if (!virusScanResult) {
throw new HTTP400('Malicious content detected, upload cancelled');

Check warning on line 137 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L137

Added line #L137 was not covered by tests
}

// Insert a new record in the `survey_telemetry_submission` table
const service = new TelemetryService(connection);
const { submission_id: submissionId, key } = await service.insertSurveyTelemetrySubmission(

Check warning on line 142 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L141-L142

Added lines #L141 - L142 were not covered by tests
rawMediaFile,
Number(req.params.projectId),
Number(req.params.surveyId)
);

// Upload file to S3
const metadata = {

Check warning on line 149 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L149

Added line #L149 was not covered by tests
filename: rawMediaFile.originalname,
username: req['auth_payload']?.preferred_username ?? '',
email: req['auth_payload']?.email ?? ''
};

const result = await uploadFileToS3(rawMediaFile, key, metadata);

Check warning on line 155 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L155

Added line #L155 was not covered by tests

defaultLog.debug({ label: 'uploadMedia', message: 'result', result });

Check warning on line 157 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L157

Added line #L157 was not covered by tests

await connection.commit();

Check warning on line 159 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L159

Added line #L159 was not covered by tests

return res.status(200).json({ submissionId });

Check warning on line 161 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L161

Added line #L161 was not covered by tests
} catch (error) {
defaultLog.error({ label: 'uploadMedia', message: 'error', error });
await connection.rollback();
throw error;

Check warning on line 165 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L163-L165

Added lines #L163 - L165 were not covered by tests
} finally {
connection.release();

Check warning on line 167 in api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/project/{projectId}/survey/{surveyId}/telemetry/upload.ts#L167

Added line #L167 was not covered by tests
}
};
}
48 changes: 48 additions & 0 deletions api/src/paths/telemetry/deployments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expect } from 'chai';
import sinon from 'sinon';
import { BctwService, IManualTelemetry } from '../../services/bctw-service';
import { getRequestHandlerMocks } from '../../__mocks__/db';
import { getAllTelemetryByDeploymentIds } from './deployments';

const mockTelemetry = ([
{
telemetry_manual_id: 1
},
{
telemetry_manual_id: 2
}
] as unknown[]) as IManualTelemetry[];

describe('getAllTelemetryByDeploymentIds', () => {
afterEach(() => {
sinon.restore();
});
it('should retrieve both manual and vendor telemetry', async () => {
const mockGetTelemetry = sinon
.stub(BctwService.prototype, 'getAllTelemetryByDeploymentIds')
.resolves(mockTelemetry);

const { mockReq, mockRes, mockNext } = getRequestHandlerMocks();
const requestHandler = getAllTelemetryByDeploymentIds();

await requestHandler(mockReq, mockRes, mockNext);

expect(mockRes.jsonValue).to.eql(mockTelemetry);
expect(mockRes.statusValue).to.equal(200);
expect(mockGetTelemetry).to.have.been.calledOnce;
});
it('should catch error', async () => {
const mockError = new Error('test error');
const mockGetTelemetry = sinon.stub(BctwService.prototype, 'getAllTelemetryByDeploymentIds').rejects(mockError);

const { mockReq, mockRes, mockNext } = getRequestHandlerMocks();
const requestHandler = getAllTelemetryByDeploymentIds();

try {
await requestHandler(mockReq, mockRes, mockNext);
} catch (err) {
expect(err).to.equal(mockError);
expect(mockGetTelemetry).to.have.been.calledOnce;
}
});
});
103 changes: 103 additions & 0 deletions api/src/paths/telemetry/deployments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { RequestHandler } from 'express';
import { Operation } from 'express-openapi';
import { authorizeRequestHandler } from '../../request-handlers/security/authorization';
import { BctwService, getBctwUser } from '../../services/bctw-service';
import { getLogger } from '../../utils/logger';

const defaultLog = getLogger('paths/telemetry/manual');

export const POST: Operation = [
authorizeRequestHandler(() => {
return {

Check warning on line 11 in api/src/paths/telemetry/deployments.ts

View check run for this annotation

Codecov / codecov/patch

api/src/paths/telemetry/deployments.ts#L11

Added line #L11 was not covered by tests
and: [
{
discriminator: 'SystemUser'
}
]
};
}),
getAllTelemetryByDeploymentIds()
];

POST.apiDoc = {
description: 'Get list of manual and vendor telemetry by deployment ids',
tags: ['telemetry'],
security: [
{
Bearer: []
}
],
responses: {
200: {
description: 'Manual and Vendor telemetry response object',
content: {
'application/json': {
schema: {
type: 'array',
items: {
type: 'object',
properties: {
id: { type: 'string' },
deployment_id: { type: 'string', format: 'uuid' },
telemetry_manual_id: { type: 'string', nullable: true },
telemetry_id: { type: 'number', nullable: true },
latitude: { type: 'number' },
longitude: { type: 'number' },
acquisition_date: { type: 'string' },
telemetry_type: { type: 'string' }
}
}
}
}
}
},
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'
}
},

requestBody: {
description: 'Request body',
required: true,
content: {
'application/json': {
schema: {
title: 'BCTW deployment ids',
type: 'array',
minItems: 1,
items: {
title: 'BCTW deployment ids',
type: 'string',
format: 'uuid'
}
}
}
}
}
};

export function getAllTelemetryByDeploymentIds(): RequestHandler {
return async (req, res) => {
const user = getBctwUser(req);
const bctwService = new BctwService(user);
try {
const result = await bctwService.getAllTelemetryByDeploymentIds(req.body);
return res.status(200).json(result);
} catch (error) {
defaultLog.error({ label: 'getAllTelemetryByDeploymentIds', message: 'error', error });
throw error;
}
};
}
46 changes: 46 additions & 0 deletions api/src/paths/telemetry/manual/delete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expect } from 'chai';
import sinon from 'sinon';
import { BctwService, IManualTelemetry } from '../../../services/bctw-service';
import { getRequestHandlerMocks } from '../../../__mocks__/db';
import { deleteManualTelemetry } from './delete';

const mockTelemetry = ([
{
telemetry_manual_id: 1
},
{
telemetry_manual_id: 2
}
] as unknown[]) as IManualTelemetry[];

describe('deleteManualTelemetry', () => {
afterEach(() => {
sinon.restore();
});
it('should retrieve all manual telemetry', async () => {
const mockGetTelemetry = sinon.stub(BctwService.prototype, 'deleteManualTelemetry').resolves(mockTelemetry);

const { mockReq, mockRes, mockNext } = getRequestHandlerMocks();
const requestHandler = deleteManualTelemetry();

await requestHandler(mockReq, mockRes, mockNext);

expect(mockRes.jsonValue).to.eql(mockTelemetry);
expect(mockRes.statusValue).to.equal(200);
expect(mockGetTelemetry).to.have.been.calledOnce;
});
it('should catch error', async () => {
const mockError = new Error('test error');
const mockGetTelemetry = sinon.stub(BctwService.prototype, 'deleteManualTelemetry').rejects(mockError);

const { mockReq, mockRes, mockNext } = getRequestHandlerMocks();
const requestHandler = deleteManualTelemetry();

try {
await requestHandler(mockReq, mockRes, mockNext);
} catch (err) {
expect(err).to.equal(mockError);
expect(mockGetTelemetry).to.have.been.calledOnce;
}
});
});
Loading
Loading