import sequelize from '../database.js';

let hasPaidAmountCentsColumnPromise = null;

function isMissingTableError(error) {
  return error?.original?.code === 'ER_NO_SUCH_TABLE';
}

export async function hasClubPaymentClaimPaidAmountCentsColumn() {
  if (!hasPaidAmountCentsColumnPromise) {
    hasPaidAmountCentsColumnPromise = sequelize.getQueryInterface().describeTable('club_payment_claims')
      .then((description) => Boolean(description?.paid_amount_cents))
      .catch((error) => {
        if (isMissingTableError(error)) {
          return false;
        }
        throw error;
      });
  }

  return hasPaidAmountCentsColumnPromise;
}
