Skip to content

Commit

Permalink
Merge 9e818b5 into 082701a
Browse files Browse the repository at this point in the history
  • Loading branch information
dhakim87 authored Apr 18, 2020
2 parents 082701a + 9e818b5 commit 209607e
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 2 deletions.
16 changes: 16 additions & 0 deletions microsetta_private_api/admin/admin_impl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import flask
from flask import jsonify

from microsetta_private_api.repo.account_repo import AccountRepo
Expand All @@ -17,6 +18,21 @@ def search_barcode(token_info, sample_barcode):
return jsonify(diag), 200


def scan_barcode(token_info, sample_barcode, body):
validate_admin_access(token_info)

with Transaction() as t:
admin_repo = AdminRepo(t)
admin_repo.scan_barcode(sample_barcode, body)
t.commit()

response = flask.Response()
response.status_code = 201
response.headers['Location'] = '/api/admin/search/samples/%s' % \
sample_barcode
return response


def validate_admin_access(token_info):
with Transaction() as t:
account_repo = AccountRepo(t)
Expand Down
41 changes: 40 additions & 1 deletion microsetta_private_api/admin/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from werkzeug.exceptions import Unauthorized
from werkzeug.exceptions import Unauthorized, NotFound

from microsetta_private_api.model.account import Account
from microsetta_private_api.model.address import Address
Expand Down Expand Up @@ -121,3 +121,42 @@ def test_search_barcode(self):
self.assertIsNone(diag['source'])
self.assertIsNone(diag['sample'])
self.assertEqual(len(diag['barcode_info']), 0)

def test_scan_barcode(self):
with Transaction() as t:
# TODO FIXME HACK: Need to build mock barcodes rather than using
# these fixed ones

TEST_BARCODE = '000000001'
TEST_STATUS = "sample-has-inconsistencies"
TEST_NOTES = "THIS IS A UNIT TEST"
admin_repo = AdminRepo(t)

diag = admin_repo.retrieve_diagnostics_by_barcode(TEST_BARCODE)
prestatus = diag['barcode_info'][0]['status']

admin_repo.scan_barcode(
TEST_BARCODE,
{
"sample_status": TEST_STATUS,
"technician_notes": TEST_NOTES
}
)

diag = admin_repo.retrieve_diagnostics_by_barcode(TEST_BARCODE)
self.assertEqual(diag['barcode_info'][0]['technician_notes'],
TEST_NOTES)
self.assertEqual(diag['barcode_info'][0]['sample_status'],
TEST_STATUS)
self.assertEqual(diag['barcode_info'][0]['status'],
prestatus)

with self.assertRaises(NotFound):
admin_repo.scan_barcode(
"THIZIZNOTAREALBARCODEISWARE",
{
"sample_status": "Abc",
"technician_notes": "123"
}
)
self.fail("Shouldn't get here")
37 changes: 36 additions & 1 deletion microsetta_private_api/api/microsetta_private_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ paths:
$ref: '#/components/responses/422UnprocessableEntity'

# START ADMINISTRATOR PATHS
'/admin/search/sample/{sample_barcode}':
'/admin/search/samples/{sample_barcode}':
get:
operationId: microsetta_private_api.admin.admin_impl.search_barcode
tags:
Expand All @@ -835,6 +835,41 @@ paths:
$ref: '#/components/responses/401Unauthorized'
'404':
$ref: '#/components/responses/404NotFound'
'/admin/scan/{sample_barcode}':
post:
# Note: We might want to be able to differentiate system administrator operations
# from technician operations in the future by user accounts and the routes they post to
operationId: microsetta_private_api.admin.admin_impl.scan_barcode
tags:
- Admin
summary: Set sample processing information for a barcode
description: Set sample processing information for a barcode
parameters:
- $ref: '#/components/parameters/sample_barcode'
requestBody:
content:
application/json:
schema:
type: object
properties:
sample_status:
type: string
enum: ["not-received",
"sample-is-valid",
"no-associated-consent",
"no-registered-account",
"sample-has-inconsistencies"]
example: "sample-has-inconsistencies"
technician_notes:
type: string
example: "Sample Processing Complete!"
responses:
'201':
description: Successfully registered new user account
headers:
Location:
schema:
type: string


components:
Expand Down
6 changes: 6 additions & 0 deletions microsetta_private_api/db/patches/0054.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This script adds two columns to the barcodes.barcode table to enable
-- adding information when scanned by a technician

ALTER TABLE barcodes.barcode
ADD COLUMN sample_status VARCHAR(100),
ADD COLUMN technician_notes VARCHAR;
32 changes: 32 additions & 0 deletions microsetta_private_api/repo/admin_repo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from datetime import date

from microsetta_private_api.exceptions import RepoException
from microsetta_private_api.repo.account_repo import AccountRepo
from microsetta_private_api.repo.base_repo import BaseRepo
from microsetta_private_api.repo.sample_repo import SampleRepo
from microsetta_private_api.repo.source_repo import SourceRepo
from werkzeug.exceptions import NotFound


class AdminRepo(BaseRepo):
Expand Down Expand Up @@ -75,3 +79,31 @@ def retrieve_diagnostics_by_barcode(self, sample_barcode):
}

return diagnostic

def scan_barcode(self, sample_barcode, scan_info):
update_args = (
scan_info['sample_status'],
scan_info['technician_notes'],
date.today(), # TODO: Do we need date or datetime here?
sample_barcode
)

with self._transaction.cursor() as cur:
cur.execute(
"UPDATE barcodes.barcode "
"SET "
"sample_status = %s, "
"technician_notes = %s, "
"scan_date = %s "
"WHERE "
"barcode = %s",
update_args
)

if cur.rowcount == 0:
raise NotFound("No such barcode: %s" % sample_barcode)

if cur.rowcount > 1:
# Note: This "can't" happen.
raise RepoException("ERROR: Multiple barcode entries would be "
"updated by scan, failing out")

0 comments on commit 209607e

Please sign in to comment.