package com.caliverse.admin.global.component.transaction;

import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.UUID;

@Component
public class TransactionIdManager {
    private static final String TRANSACTION_ID_KEY = "TRANSACTION_ID";

    public String getCurrentTransactionId() {
        String currentId = (String) TransactionSynchronizationManager.getResource(TRANSACTION_ID_KEY);
        if (currentId == null) {
            currentId = generateTransactionId();
            TransactionSynchronizationManager.bindResource(TRANSACTION_ID_KEY, currentId);
        }
        return currentId;
    }

    private String generateTransactionId() {
        return UUID.randomUUID().toString();
    }
}
