// Seed script: create the "1/3 A4 out-stock form" print template. // Run: node seed-print-template.mjs import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const BASE = process.env.F9_API_BASE || 'http://localhost:5136' const PHONE = '13800000000' const PASSWORD = '123456' const tpl = JSON.parse( fs.readFileSync(path.join(__dirname, 'print-template-outstock.json'), 'utf8') ) async function main() { const loginRes = await fetch(`${BASE}/api/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ phone: PHONE, password: PASSWORD }) }) const login = await loginRes.json() if (login.code !== 0) throw new Error(`Login failed: ${login.message}`) const token = login.data.token const headers = { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' } const list = await fetch(`${BASE}/api/print/templates`, { headers }).then((r) => r.json()) const dup = (list.items || []).find((i) => i.name === tpl.name) if (dup) { console.log(`Template already exists, skip: ${tpl.name} (id=${dup.id})`) return } const res = await fetch(`${BASE}/api/print/templates`, { method: 'POST', headers, body: JSON.stringify({ name: tpl.name, content: JSON.stringify(tpl.content) }) }) const row = await res.json() console.log(`Template created: id=${row.id} name=${row.name}`) } main().catch((e) => { console.error(e) process.exit(1) })