items_API.getPossLocations updated to new schema

This commit is contained in:
Jadowyne Ulve 2025-04-28 07:05:25 -05:00
parent b75376899d
commit 780c43cd73
5 changed files with 60 additions and 6 deletions

View File

@ -170,6 +170,26 @@ def paginateZonesBySku(site: str, payload: tuple, convert=True):
except Exception as error:
raise postsqldb.DatabaseError(error, payload, sql)
def paginateLocationsWithZone(site:str, payload:tuple, convert:bool=True):
recordset, count = (), 0
database_config = config.config()
with open(f"application/items/sql/getLocationsWithZone.sql", "r+") as file:
sql = file.read().replace("%%site_name%%", site)
try:
with psycopg2.connect(**database_config) as conn:
with conn.cursor() as cur:
cur.execute(sql, payload)
rows = cur.fetchall()
if rows and convert:
recordset = [postsqldb.tupleDictionaryFactory(cur.description, row) for row in rows]
elif rows and not convert:
recordset = rows
cur.execute(f"SELECT COUNT(*) FROM {site}_locations;")
count = cur.fetchone()[0]
return recordset, count
except Exception as error:
raise postsqldb.DatabaseError(error, (), sql)
def paginateLocationsBySkuZone(site: str, payload: tuple, convert=True):
database_config = config.config()
locations, count = (), 0
@ -306,6 +326,12 @@ def postUpdateItem(site:str, payload:dict):
raise postsqldb.DatabaseError(error, payload, "MULTICALL!")
def postUpdateItemLink(site: str, payload: dict):
""" POST update to ItemLink
Args:
site (str): _description_
payload (dict): {id, update, old_conv_factor, user_id}
"""
def postUpdateData(conn, table, payload, convert=True):
updated = ()
set_clause, values = postsqldb.updateStringFactory(payload['update'])
@ -359,3 +385,4 @@ def postUpdateItemLink(site: str, payload: dict):
postUpdateData(conn, f"{site}_itemlinks", {'id': payload['id'], 'update': {'conv_factor': payload['update']['conv_factor']}})
postAddTransaction(conn, site, transaction.payload())

View File

@ -379,6 +379,7 @@ def updateItem():
return jsonify({'error': True, 'message': f'method {request.method} is not allowed!'})
@items_api.route('/item/updateItemLink', methods=['POST'])
@login_required
def updateItemLink():
""" UPDATE item link by passing id, conv_factor, barcode, old_conv
---
@ -428,15 +429,36 @@ def updateItemLink():
@items_api.route('/item/getPossibleLocations', methods=["GET"])
@login_required
def getPossibleLocations():
""" GET locations with zones by passing a page and limit
---
parameters:
- in: query
name: page
schema:
type: interger
minimum: 1
default: 1
description: page in the records to GET
- in: query
name: limit
schema:
type: interger
minimum: 1
default: 1
description: number of records to GET
responses:
200:
description: Locations GET successful.
"""
locations, count = (), 0
if request.method == "GET":
page = int(request.args.get('page', 1))
limit = int(request.args.get('limit', 1))
offset = (page-1)*limit
database_config = config()
site_name = session['selected_site']
with psycopg2.connect(**database_config) as conn:
locations, count = db.LocationsTable.paginateLocationsWithZone(conn, site_name, (limit, offset))
return jsonify(locations=locations, end=math.ceil(count/limit))
locations, count = database_items.paginateLocationsWithZone(site_name, (limit, offset))
return jsonify({'locations': locations, 'end':math.ceil(count/limit), 'error':False, 'message': f'Locations received successfully!'})
return jsonify({'locations': locations, 'end':math.ceil(count/limit), 'error':True, 'message': f'method {request.method} not allowed.'})
@items_api.route('/item/getLinkedItem', methods=["GET"])
@login_required

View File

@ -0,0 +1,5 @@
SELECT %%site_name%%_locations.*,
row_to_json(%%site_name%%_zones.*) as zone
FROM %%site_name%%_locations
LEFT JOIN %%site_name%%_zones ON %%site_name%%_zones.id = %%site_name%%_locations.zone_id
LIMIT %s OFFSET %s;