53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import * as dotenv from "dotenv"
|
|
import postgres from "postgres"
|
|
import { normalizeCardBrand } from "../payment/card-brand"
|
|
|
|
dotenv.config({ path: ".env.local" })
|
|
|
|
const sql = postgres(process.env.DATABASE_URL!)
|
|
|
|
async function main() {
|
|
const rows = await sql<{ id: string; card_brand: string | null }[]>`
|
|
SELECT id, card_brand
|
|
FROM channels
|
|
WHERE card_brand IS NOT NULL
|
|
`
|
|
|
|
const unsupported = rows.filter((row) => !normalizeCardBrand(row.card_brand))
|
|
if (unsupported.length > 0) {
|
|
throw new Error(
|
|
`Unsupported card brands: ${unsupported.map((row) => row.card_brand).join(", ")}`
|
|
)
|
|
}
|
|
|
|
let updated = 0
|
|
for (const row of rows) {
|
|
const cardBrand = normalizeCardBrand(row.card_brand)
|
|
if (cardBrand && cardBrand !== row.card_brand) {
|
|
await sql`
|
|
UPDATE channels
|
|
SET card_brand = ${cardBrand}, updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = ${row.id}
|
|
`
|
|
updated += 1
|
|
}
|
|
}
|
|
|
|
const brands = await sql<{ card_brand: string }[]>`
|
|
SELECT DISTINCT card_brand
|
|
FROM channels
|
|
WHERE card_brand IS NOT NULL
|
|
ORDER BY card_brand
|
|
`
|
|
|
|
console.log(`Normalized ${updated} channel card brands.`)
|
|
console.log("Stored card brands:", brands.map((row) => row.card_brand))
|
|
}
|
|
|
|
main()
|
|
.catch((error) => {
|
|
console.error(error)
|
|
process.exitCode = 1
|
|
})
|
|
.finally(() => sql.end())
|