Skip to content

Commit

Permalink
Merge 2dbd878 into 3dacfb0
Browse files Browse the repository at this point in the history
  • Loading branch information
AmandaBirmingham committed Nov 17, 2020
2 parents 3dacfb0 + 2dbd878 commit 8f8095c
Show file tree
Hide file tree
Showing 8 changed files with 608 additions and 0 deletions.
34 changes: 34 additions & 0 deletions microsetta_private_api/admin/admin_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from microsetta_private_api.config_manager import SERVER_CONFIG
from microsetta_private_api.model.log_event import LogEvent
from microsetta_private_api.model.project import Project
from microsetta_private_api.model.daklapack_order import DaklapackOrder, \
ORDER_ID_KEY, SUBMITTER_ACCT_KEY
from microsetta_private_api.exceptions import RepoException
from microsetta_private_api.repo.account_repo import AccountRepo
from microsetta_private_api.repo.event_log_repo import EventLogRepo
Expand Down Expand Up @@ -371,3 +373,35 @@ def query_email_stats(body, token_info):
result['summary'] = 'May Require User Interaction'

return jsonify(results), 200


def create_daklapack_order(body, token_info):
validate_admin_access(token_info)

body = body.copy()
body[ORDER_ID_KEY] = str(uuid.uuid4())

with Transaction() as t:
account_repo = AccountRepo(t)
body[SUBMITTER_ACCT_KEY] = account_repo.find_linked_account(
token_info['iss'], token_info['sub'])

try:
daklapack_order = DaklapackOrder.from_api(**body)
except ValueError as e:
raise RepoException(e)

# write order to db
admin_repo = AdminRepo(t)
order_id = admin_repo.create_daklapack_order(daklapack_order)
t.commit()

# return response to caller
# Note: the response msg is largely here as room to grow on--will need
# to be able to send back more info when daklapack api submission and
# automatic emailing are incorporated
response_msg = {"order_id": order_id}
response = jsonify(response_msg)
response.status_code = 201
response.headers['Location'] = f'/api/admin/daklapack_orders/{order_id}'
return response
69 changes: 69 additions & 0 deletions microsetta_private_api/admin/tests/test_admin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
extract_last_id_from_location_header # noqa "client" IS used, by name
from microsetta_private_api.admin.tests.test_admin_repo import \
FIRST_DAKLAPACK_ARTICLE, delete_test_scan
from microsetta_private_api.model.tests.test_daklapack_order import \
DUMMY_PROJ_ID_LIST, DUMMY_DAK_ARTICLE_CODE, DUMMY_ADDRESSES, \
DUMMY_DAK_ORDER_DESC, DUMMY_HOLD_MSG, DUMMY_FEDEX_REFS

DUMMY_PROJ_NAME = "test project"

Expand Down Expand Up @@ -59,6 +62,22 @@ def setup_test_data():
t.commit()


def delete_test_daklapack_order(new_order_id):
if new_order_id is not None:
with Transaction() as t:
with t.cursor() as cur:
cur.execute("DELETE FROM barcodes.daklapack_order_to_project "
"WHERE "
"dak_order_id = %s",
(new_order_id,))

cur.execute("DELETE FROM barcodes.daklapack_order "
"WHERE "
"dak_order_id = %s",
(new_order_id,))
t.commit()


@pytest.mark.usefixtures("client")
class AdminApiTests(TestCase):
FULL_PROJ_INFO = {"project_name": DUMMY_PROJ_NAME,
Expand Down Expand Up @@ -805,3 +824,53 @@ def test_metadata_qiita_compatible_valid_no_private(self):
self.assertEqual(set(result.keys()), {'000004216', '000004213'})
obs = {c.lower() for c in result['000004216']}
self.assertNotIn('about_yourself_text', obs)

def _test_post_daklapack_orders(self, order_info):
input_json = json.dumps(order_info)

new_order_id = None
try:
# execute articles post
response = self.client.post(
"api/admin/daklapack_orders",
content_type='application/json',
data=input_json,
headers=MOCK_HEADERS
)

# check for successful create response code
self.assertEqual(201, response.status_code)

new_order_id = extract_last_id_from_location_header(response)
finally:
delete_test_daklapack_order(new_order_id)

def test_post_daklapack_orders_fully_specified(self):
# create post input json with a nonsense date field
order_info = {
"project_ids": DUMMY_PROJ_ID_LIST,
"article_code": DUMMY_DAK_ARTICLE_CODE,
"addresses": DUMMY_ADDRESSES,
"description": DUMMY_DAK_ORDER_DESC,
"fedex_ref_1": DUMMY_FEDEX_REFS[0],
"fedex_ref_2": DUMMY_FEDEX_REFS[1],
"fedex_ref_3": DUMMY_FEDEX_REFS[2],
"fulfillment_hold_msg": DUMMY_HOLD_MSG
}

self._test_post_daklapack_orders(order_info)

def test_post_daklapack_orders_wo_optionals(self):
# create post input json with a nonsense date field
order_info = {
"project_ids": DUMMY_PROJ_ID_LIST,
"article_code": DUMMY_DAK_ARTICLE_CODE,
"addresses": DUMMY_ADDRESSES,
"description": None,
"fedex_ref_1": None,
"fedex_ref_2": None,
"fedex_ref_3": None,
"fulfillment_hold_msg": None
}

self._test_post_daklapack_orders(order_info)
127 changes: 127 additions & 0 deletions microsetta_private_api/admin/tests/test_admin_repo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import TestCase
from datetime import date
import datetime
import dateutil.parser
import psycopg2

import microsetta_private_api.model.project as p
Expand All @@ -9,6 +10,7 @@

from microsetta_private_api.model.account import Account
from microsetta_private_api.model.address import Address
from microsetta_private_api.model.daklapack_order import DaklapackOrder
from microsetta_private_api.repo.account_repo import AccountRepo
from microsetta_private_api.repo.admin_repo import AdminRepo
from microsetta_private_api.repo.transaction import Transaction
Expand Down Expand Up @@ -714,3 +716,128 @@ def test_get_daklapack_articles(self):
first_article = articles[0]
first_article.pop("dak_article_id")
self.assertEqual(FIRST_DAKLAPACK_ARTICLE, first_article)

def test_create_daklapack_order(self):
with Transaction() as t:
# need a valid submitter id from the account table to input
with t.dict_cursor() as cur:
cur.execute("SELECT id, first_name, last_name "
"FROM ag.account "
"WHERE account_type = 'admin' "
"ORDER BY id "
"LIMIT 1;")
submitter_record = cur.fetchone()
submitter_id = submitter_record[0]
submitter_name = f"{submitter_record[1]} " \
f"{submitter_record[2]}"

# need real project ids to show can link order to project
cur.execute("SELECT project_id "
"FROM barcodes.project "
"ORDER BY project_id "
"LIMIT 2;")
project_id_records = cur.fetchall()
project_ids = [x[0] for x in project_id_records]

order_struct = {
'orderId': '7ed917ef-0c4d-431a-9aa0-0a1f4f41f44b',
'articles': [
{
'articleCode': 350102,
'addresses': [
{
'firstName': 'Jane',
'lastName': 'Doe',
'address1': '123 Main St',
'insertion': 'Apt 2',
'address2': '',
'postalCode': 92210,
'city': 'San Diego',
'state': 'CA',
'country': 'USA',
'countryCode': 'us',
'phone': '(858) 555-1212',
'creationDate': '2020-10-09T22:43:52.219328Z',
'companyName': submitter_name
},
{
'firstName': 'Tom',
'lastName': 'Thumb',
'address1': '29 Side St',
'insertion': '',
'address2': 'Kew Gardens',
'postalCode': 'KG7-448',
'city': 'Gananoque',
'state': 'Ontario',
'country': 'Canada',
'countryCode': 'ca',
'phone': '(858) 555-1212',
'creationDate': '2020-10-09T22:43:52.219350Z',
'companyName': submitter_name
}
]
}
],
'shippingProvider': 'FedEx',
'shippingType': 'FEDEX_2_DAY',
'shippingProviderMetadata': [
{'key': 'Reference 1',
'value': 'Bill Ted'}
]
}

acct_repo = AccountRepo(t)
submitter_acct = acct_repo.get_account(submitter_id)

input_id = '7ed917ef-0c4d-431a-9aa0-0a1f4f41f44b'
creation_timestamp = dateutil.parser.isoparse(
"2020-10-09T22:43:52.219328Z")
last_polling_timestamp = dateutil.parser.isoparse(
"2020-10-19T12:40:19.219328Z")
desc = "a description"
hold_msg = "hold this order"
last_status = "accepted"

# create dummy daklapack order object
input = DaklapackOrder(input_id, submitter_acct, list(project_ids),
order_struct, desc, hold_msg,
creation_timestamp, last_polling_timestamp,
last_status)

# call create_daklapack_order
admin_repo = AdminRepo(t)
returned_id = admin_repo.create_daklapack_order(input)

self.assertEqual(input_id, returned_id)

expected_record = [input_id,
submitter_id,
desc,
hold_msg,
order_struct,
creation_timestamp,
last_polling_timestamp,
last_status]

# check db to show new records exist
with t.dict_cursor() as cur:
# need real project ids to show can link order to project
cur.execute("SELECT * "
"FROM barcodes.daklapack_order "
"WHERE dak_order_id = %s",
(input_id, ))
curr_records = cur.fetchall()
self.assertEqual(len(curr_records), 1)
self.assertEqual(expected_record, curr_records[0])

cur.execute("SELECT project_id "
"FROM barcodes.daklapack_order_to_project "
"WHERE dak_order_id = %s",
(input_id, ))
curr_proj_records = cur.fetchall()
self.assertEqual(len(curr_proj_records), 2)
for curr_proj_rec in curr_proj_records:
self.assertTrue(curr_proj_rec[0] in project_ids)

# NB: all the above happens within a transaction that we then DO NOT
# commit so the db changes are not permanent
46 changes: 46 additions & 0 deletions microsetta_private_api/api/microsetta_private_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,52 @@ paths:
schema:
type: array

'/admin/daklapack_orders':
post:
operationId: microsetta_private_api.admin.admin_impl.create_daklapack_order
tags:
- Admin
summary: Submit a new order to Daklapack and record to db
description: Submit a new order to Daklapack and record to db
requestBody:
content:
application/json:
schema:
type: object
properties:
project_ids:
type: array
items:
type: integer
article_code:
type: integer
addresses:
type: array
items:
type: object
description:
type: string
nullable: true
fedex_ref_1:
type: string
nullable: true
fedex_ref_2:
type: string
nullable: true
fedex_ref_3:
type: string
nullable: true
fulfillment_hold_msg:
type: string
nullable: true
responses:
'201':
description: Order successfully submitted and recorded
headers:
Location:
schema:
type: string

'/admin/account_email_summary':
post:
operationId: microsetta_private_api.admin.admin_impl.query_email_stats
Expand Down
24 changes: 24 additions & 0 deletions microsetta_private_api/db/patches/0072.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- create tables to record daklapack orders

CREATE TABLE barcodes.daklapack_order
(
dak_order_id uuid DEFAULT uuid_generate_v4() NOT NULL,
submitter_acct_id uuid NOT NULL,
description varchar,
fulfillment_hold_msg varchar,
order_json JSONB NOT NULL,
creation_timestamp timestamptz,
last_polling_timestamp timestamptz,
last_polling_status varchar,
CONSTRAINT dak_order_pkey PRIMARY KEY (dak_order_id),
CONSTRAINT fk_acct_id FOREIGN KEY ( submitter_acct_id ) REFERENCES ag.account( id )
);

CREATE TABLE barcodes.daklapack_order_to_project
(
dak_order_id uuid NOT NULL,
project_id bigint NOT NULL,
CONSTRAINT dak_order_to_project_pkey PRIMARY KEY ( dak_order_id, project_id ),
CONSTRAINT fk_dak_order_id FOREIGN KEY ( dak_order_id ) REFERENCES barcodes.daklapack_order( dak_order_id ),
CONSTRAINT fk_project_id FOREIGN KEY ( project_id ) REFERENCES barcodes.project( project_id )
);

0 comments on commit 8f8095c

Please sign in to comment.