99 lines
3.0 KiB
TypeScript
99 lines
3.0 KiB
TypeScript
import 'dotenv/config'
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
import { fileURLToPath } from 'url'
|
|
import type { Payload } from 'payload'
|
|
|
|
export const PUBLIC_CONTRACT_FILENAME = 'Dogovor-platnie-2026.docx'
|
|
export const PUBLIC_CONTRACT_TITLE = 'Договор на оказание платных медицинских услуг (2026)'
|
|
const CONTRACT_MIME =
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
|
|
|
function contractPath() {
|
|
const here = path.dirname(fileURLToPath(import.meta.url))
|
|
const bundled = path.join(here, 'files', PUBLIC_CONTRACT_FILENAME)
|
|
if (fs.existsSync(bundled)) return bundled
|
|
const icloud = path.join(
|
|
process.env.HOME || '',
|
|
'Library/Mobile Documents/com~apple~CloudDocs/Downloads icloud',
|
|
PUBLIC_CONTRACT_FILENAME,
|
|
)
|
|
if (fs.existsSync(icloud)) return icloud
|
|
return null
|
|
}
|
|
|
|
function copyToWebPublic(source: string) {
|
|
const here = path.dirname(fileURLToPath(import.meta.url))
|
|
const dest = path.resolve(here, '../../../web/public/documents', PUBLIC_CONTRACT_FILENAME)
|
|
fs.mkdirSync(path.dirname(dest), { recursive: true })
|
|
fs.copyFileSync(source, dest)
|
|
}
|
|
|
|
export async function seedPublicDocuments(payload: Payload) {
|
|
const filePath = contractPath()
|
|
if (!filePath) {
|
|
payload.logger.warn('Public contract file not found, skip seed')
|
|
return
|
|
}
|
|
|
|
copyToWebPublic(filePath)
|
|
|
|
const existing = await payload.find({
|
|
collection: 'documents',
|
|
where: {
|
|
and: [{ isPublic: { equals: true } }, { type: { equals: 'contract' } }],
|
|
},
|
|
limit: 50,
|
|
depth: 1,
|
|
})
|
|
|
|
const already = existing.docs.some((doc) => {
|
|
const file = typeof doc.file === 'object' && doc.file ? doc.file : null
|
|
const filename = file && 'filename' in file ? String(file.filename || '') : ''
|
|
return filename === PUBLIC_CONTRACT_FILENAME || String(doc.title || '').includes('платных медицинских услуг')
|
|
})
|
|
if (already) return
|
|
|
|
const buf = fs.readFileSync(filePath)
|
|
const media = await payload.create({
|
|
collection: 'media',
|
|
data: { alt: PUBLIC_CONTRACT_TITLE },
|
|
file: {
|
|
data: buf,
|
|
mimetype: CONTRACT_MIME,
|
|
name: PUBLIC_CONTRACT_FILENAME,
|
|
size: buf.length,
|
|
},
|
|
})
|
|
|
|
await payload.create({
|
|
collection: 'documents',
|
|
locale: 'ru',
|
|
data: {
|
|
title: PUBLIC_CONTRACT_TITLE,
|
|
type: 'contract',
|
|
isPublic: true,
|
|
file: media.id,
|
|
issuedAt: '2026-01-01T00:00:00.000Z',
|
|
},
|
|
})
|
|
payload.logger.info(`Seeded public document ${PUBLIC_CONTRACT_FILENAME}`)
|
|
}
|
|
|
|
async function main() {
|
|
const { getPayload } = await import('payload')
|
|
const config = (await import('../payload.config')).default
|
|
const payload = await getPayload({ config })
|
|
const { ensureDocumentsSchema } = await import('../lib/ensure-documents-schema')
|
|
await ensureDocumentsSchema(payload)
|
|
await seedPublicDocuments(payload)
|
|
process.exit(0)
|
|
}
|
|
|
|
if (process.argv[1]?.includes('public-documents')) {
|
|
main().catch((err) => {
|
|
console.error(err)
|
|
process.exit(1)
|
|
})
|
|
}
|