Skip to content

Commit

Permalink
add id field to request
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonSharratt committed Oct 9, 2024
1 parent e553033 commit 999debd
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""empty message
Revision ID: 4aaab6597068
Revision ID: 3b039edc1404
Revises: 8d8279a86def
Create Date: 2024-10-07 14:12:40.894440
Create Date: 2024-10-09 13:44:54.558111
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '4aaab6597068'
revision = '3b039edc1404'
down_revision = '8d8279a86def'
branch_labels = None
depends_on = None
Expand All @@ -19,7 +19,8 @@
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('request',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('uuid', sa.Uuid(), nullable=False),
sa.Column('full_name', sa.String(), nullable=False),
sa.Column('email', sa.String(), nullable=False),
sa.Column('birthdate', sa.Date(), nullable=False),
Expand All @@ -38,9 +39,11 @@ def upgrade():
)
with op.batch_alter_table('request', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_request_business_identifier'), ['business_identifier'], unique=False)
batch_op.create_index(batch_op.f('ix_request_uuid'), ['uuid'], unique=False)

op.create_table('request_history',
sa.Column('id', sa.Uuid(), autoincrement=False, nullable=False),
sa.Column('id', sa.Integer(), autoincrement=False, nullable=False),
sa.Column('uuid', sa.Uuid(), autoincrement=False, nullable=False),
sa.Column('full_name', sa.String(), autoincrement=False, nullable=False),
sa.Column('email', sa.String(), autoincrement=False, nullable=False),
sa.Column('birthdate', sa.Date(), autoincrement=False, nullable=False),
Expand All @@ -60,17 +63,21 @@ def upgrade():
)
with op.batch_alter_table('request_history', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_request_history_business_identifier'), ['business_identifier'], unique=False)
batch_op.create_index(batch_op.f('ix_request_history_uuid'), ['uuid'], unique=False)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###

with op.batch_alter_table('request_history', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_request_history_uuid'))
batch_op.drop_index(batch_op.f('ix_request_history_business_identifier'))

op.drop_table('request_history')
with op.batch_alter_table('request', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_request_uuid'))
batch_op.drop_index(batch_op.f('ix_request_business_identifier'))

op.drop_table('request')
Expand Down
12 changes: 8 additions & 4 deletions btr-api/src/btr_api/models/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class Request(Versioned, Base):
"""Stores request (to omit) data."""

__tablename__ = 'request'
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
uuid: Mapped[uuid.UUID] = mapped_column(index=True)
full_name: Mapped[str] = mapped_column(nullable=False)
email: Mapped[str] = mapped_column(nullable=False)
birthdate: Mapped[date] = mapped_column(nullable=False)
Expand All @@ -66,7 +67,7 @@ class Request(Versioned, Base):
updated_at: Mapped[datetime] = mapped_column(nullable=False, default=datetime.now())

def __init__(self, data):
self.id = uuid.uuid4()
self.uuid = uuid.uuid4()
self.full_name = data['fullName']
self.email = data['email']
self.birthdate = data['birthdate']
Expand All @@ -79,15 +80,17 @@ def __init__(self, data):
self.completing_email = data['completingEmail']

@classmethod
def find_by_id(cls, request_id: int) -> Request | None:
def find_by_uuid(cls, request_id: int) -> Request | None:
"""Return the person by id."""
return cls.query.filter_by(id=request_id).one_or_none()
return cls.query.filter_by(uuid=request_id).one_or_none()

@classmethod
def __setitem__(cls, key, value):
match key:
case 'id':
cls.id = value
case 'uuid':
cls.uuid = value
case 'email':
cls.email = value
case 'birthdate':
Expand Down Expand Up @@ -131,6 +134,7 @@ def to_dict(request: Request) -> dict:
"""Return the request class as a dict for response purposes."""
return {
'id': request.id,
'uuid': request.uuid,
'fullName': request.full_name,
'email': request.email,
'birthdate': request.birthdate,
Expand Down
4 changes: 2 additions & 2 deletions btr-api/src/btr_api/resources/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
def get_request(id: uuid.UUID | None = None): # pylint: disable=redefined-builtin
"""Get the request by id."""
# try:
if req := RequestModel.find_by_id(id):
if req := RequestModel.find_by_uuid(id):
account_id = request.headers.get('Account-Id', None)
btr_auth.product_authorizations(request=request, account_id=account_id)
# if btr_auth.get_user_type() == UserType.USER_STAFF:
Expand Down Expand Up @@ -128,7 +128,7 @@ def update_request(req_id: str):
# try:
UserModel.get_or_create_user_by_jwt(g.jwt_oidc_token_info)
account_id = request.headers.get('Account-Id', None)
req = RequestModel.find_by_id(req_id)
req = RequestModel.find_by_uuid(req_id)
if req:
btr_auth.product_authorizations(request, account_id)

Expand Down
4 changes: 2 additions & 2 deletions btr-api/tests/postman/btr-api.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@
"script": {
"exec": [
"var jsonData = pm.response.json();",
"pm.environment.set(\"request_id\", jsonData.id);",
"pm.environment.set(\"request_id\", jsonData.uuid);",
"pm.environment.set(\"request_birthdate\", jsonData.birthate);",
"pm.environment.set(\"request_completingEmail\", jsonData.completingEmail);",
"pm.environment.set(\"request_completingParty\", jsonData.completingParty);",
Expand Down Expand Up @@ -498,7 +498,7 @@
"script": {
"exec": [
"var jsonData = pm.response.json();",
"pm.expect(jsonData.id).to.eql(pm.collectionVariables.get(\"request_id\"));",
"pm.expect(jsonData.uuid).to.eql(pm.collectionVariables.get(\"request_id\"));",
"pm.expect(jsonData.birthate).to.eql(pm.collectionVariables.get(\"request_birthdate\"));",
"pm.expect(jsonData.completingEmail).to.eql(pm.collectionVariables.get(\"request_completingEmail\"));",
"pm.expect(jsonData.completingParty).to.eql(pm.collectionVariables.get(\"request_completingParty\"));",
Expand Down
4 changes: 2 additions & 2 deletions btr-api/tests/unit/models/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
from tests.unit.utils.mock_data import REQUEST_DICT


def test_find_by_id(session):
def test_find_by_uuid(session):
# Prepare data
clear_db(session)
request = Request(REQUEST_DICT)
session.add(request)
session.commit()

# Do test
result = Request.find_by_id(request.id)
result = Request.find_by_uuid(request.uuid)

# Verify result
assert result == request
8 changes: 4 additions & 4 deletions btr-api/tests/unit/resources/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_get(app, client, session, jwt, requests_mock, sample_user, test_name, u
req = RequestModel(REQUEST_DICT)
session.add(req)
session.commit()
id = req.id
id = req.uuid

requests_mock.get(
f"{app.config.get('AUTH_SVC_URL')}/orgs/1/products?include_hidden=true",
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_get_fail_no_auth(app, client, session, jwt, requests_mock, sample_user,
req = RequestModel(REQUEST_DICT)
session.add(req)
session.commit()
id = req.id
id = req.uuid

# Test
rv = client.get(
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_put(app, client, session, jwt, requests_mock, sample_user, test_name, u
f'/requests',
json=REQUEST_DICT,
)
id = rv.json['id']
id = rv.json['uuid']
# Test
rv = client.put(
f'/requests/{id}',
Expand Down Expand Up @@ -176,7 +176,7 @@ def test_put_no_auth(app, client, session, jwt, requests_mock, sample_user, test
f'/requests',
json=REQUEST_DICT,
)
id = rv.json['id']
id = rv.json['uuid']
# Test
rv = client.put(
f'/requests/{id}',
Expand Down

0 comments on commit 999debd

Please sign in to comment.