MukeshKapoor25's picture
refactor(auth): Migrate from employees to staff module and consolidate constants
cfd9177
"""
Database initialization utilities.
Creates indexes and sets up collections for the SCM microservice.
"""
# from insightfy_utils.logging import get_logger # TODO: Uncomment when package is available
import logging
from app.nosql import get_database
from app.constants.collections import (
SCM_MERCHANTS_COLLECTION,
SCM_staff_COLLECTION,
SCM_ROLES_COLLECTION,
SCM_AUTH_LOGS_COLLECTION,
SCM_SALES_ORDERS_COLLECTION,
SCM_RMA_COLLECTION,
SCM_CREDIT_NOTES_COLLECTION
)
# logger = get_logger(__name__) # TODO: Uncomment when insightfy_utils is available
logger = logging.getLogger(__name__)
async def create_indexes():
"""
Create all required database indexes.
Should be called during application startup.
"""
try:
logger.info("Creating database indexes")
# Merchants collection indexes
await get_database()[SCM_MERCHANTS_COLLECTION].create_index("merchant_id", unique=True)
await get_database()[SCM_MERCHANTS_COLLECTION].create_index("business_name", unique=True)
await get_database()[SCM_MERCHANTS_COLLECTION].create_index("merchant_type")
await get_database()[SCM_MERCHANTS_COLLECTION].create_index("contact_email")
logger.info(f"Created indexes for {SCM_MERCHANTS_COLLECTION}")
# staff collection indexes
await get_database()[SCM_staff_COLLECTION].create_index("associate_id", unique=True)
await get_database()[SCM_staff_COLLECTION].create_index("email", unique=True)
await get_database()[SCM_staff_COLLECTION].create_index("mobile", unique=True)
await get_database()[SCM_staff_COLLECTION].create_index("merchant_id")
await get_database()[SCM_staff_COLLECTION].create_index([
("merchant_id", 1),
("role_id", 1)
])
logger.info(f"Created indexes for {SCM_staff_COLLECTION}")
# Roles collection indexes
await get_database()[SCM_ROLES_COLLECTION].create_index([
("merchant_id", 1),
("role_id", 1)
], unique=True)
await get_database()[SCM_ROLES_COLLECTION].create_index("merchant_id")
logger.info(f"Created indexes for {SCM_ROLES_COLLECTION}")
# Auth logs collection indexes
await get_database()[SCM_AUTH_LOGS_COLLECTION].create_index([
("merchant_id", 1),
("timestamp", -1)
])
await get_database()[SCM_AUTH_LOGS_COLLECTION].create_index([
("associate_id", 1),
("timestamp", -1)
])
await get_database()[SCM_AUTH_LOGS_COLLECTION].create_index("event_type")
# TTL index for 90-day retention (7776000 seconds)
await get_database()[SCM_AUTH_LOGS_COLLECTION].create_index(
"timestamp",
expireAfterSeconds=7776000
)
logger.info(f"Created indexes for {SCM_AUTH_LOGS_COLLECTION}")
# Sales orders collection indexes
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("sales_order_id", unique=True)
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("order_number", unique=True)
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("merchant_id")
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("branch_id")
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("status")
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("order_date")
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("customer.customer_id")
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index([
("merchant_id", 1),
("order_date", -1)
])
logger.info(f"Created indexes for {SCM_SALES_ORDERS_COLLECTION}")
logger.info("All database indexes created successfully")
except Exception as e:
logger.error("Failed to create database indexes", exc_info=e)
raise
async def drop_indexes():
"""
Drop all indexes (useful for testing).
"""
try:
logger.warning("Dropping all database indexes")
await get_database()[SCM_MERCHANTS_COLLECTION].drop_indexes()
await get_database()[SCM_staff_COLLECTION].drop_indexes()
await get_database()[SCM_ROLES_COLLECTION].drop_indexes()
await get_database()[SCM_AUTH_LOGS_COLLECTION].drop_indexes()
await get_database()[SCM_SALES_ORDERS_COLLECTION].drop_indexes()
logger.info("All database indexes dropped")
except Exception as e:
logger.error("Failed to drop database indexes", exc_info=e)
raise