fix(api): on duplicate password update
This commit is contained in:
		@@ -3,12 +3,12 @@ from enum import Enum
 | 
			
		||||
from typing import Optional
 | 
			
		||||
 | 
			
		||||
from sqlalchemy import insert, select, update
 | 
			
		||||
from sqlalchemy.dialects.mysql import insert as mysql_insert
 | 
			
		||||
from sqlalchemy.ext.asyncio import AsyncConnection
 | 
			
		||||
 | 
			
		||||
from api.db.tables.account import account_keyring_table, KeyStatus, KeyType
 | 
			
		||||
from api.schemas.account.account_keyring import AccountKeyring
 | 
			
		||||
from api.utils.hasher import hasher
 | 
			
		||||
from api.utils.key_id_gen import KeyIdGenerator
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def get_key_by_id(connection: AsyncConnection, key_id: str) -> Optional[AccountKeyring]:
 | 
			
		||||
@@ -73,15 +73,19 @@ async def create_key(connection: AsyncConnection, key: AccountKeyring, key_id: i
 | 
			
		||||
async def create_password_key(connection: AsyncConnection, password: str | None, owner_id: int):
 | 
			
		||||
    if password is None:
 | 
			
		||||
        password = hasher.generate_password()
 | 
			
		||||
    stmt = insert(account_keyring_table).values(
 | 
			
		||||
    hashed_password = hasher.hash_data(password)
 | 
			
		||||
    stmt = mysql_insert(account_keyring_table).values(
 | 
			
		||||
        owner_id=owner_id,
 | 
			
		||||
        key_type=KeyType.PASSWORD.value,
 | 
			
		||||
        key_id=KeyIdGenerator(),
 | 
			
		||||
        key_value=hasher.hash_data(password),
 | 
			
		||||
        key_id="PASSWORD",
 | 
			
		||||
        key_value=hashed_password,
 | 
			
		||||
        created_at=datetime.now(timezone.utc),
 | 
			
		||||
        expiry=datetime.now(timezone.utc) + timedelta(days=365),
 | 
			
		||||
        status=KeyStatus.ACTIVE,
 | 
			
		||||
    )
 | 
			
		||||
    stmt.on_duplicate_key_update(
 | 
			
		||||
        key_value=hashed_password
 | 
			
		||||
    )
 | 
			
		||||
    await connection.execute(stmt)
 | 
			
		||||
    await connection.commit()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user