Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dotenv/config'
|
||||
|
||||
import config from '@payload-config'
|
||||
import crypto from 'crypto'
|
||||
import { getPayload, type Payload } from 'payload'
|
||||
|
||||
import { aboutContent } from '@/spa/aboutContent'
|
||||
@@ -15,6 +16,23 @@ const mediaByFilename = async (payload: Payload, filename: string) => {
|
||||
return result.docs[0]?.id
|
||||
}
|
||||
|
||||
const safeRemoteName = (url: string) => {
|
||||
const parsed = new URL(url)
|
||||
const ext = parsed.pathname.split('/').pop()?.includes('.') ? `.${parsed.pathname.split('.').pop()}` : '.jpg'
|
||||
const hash = crypto.createHash('sha1').update(url).digest('hex').slice(0, 10)
|
||||
return `${parsed.hostname.replace(/[^a-z0-9]+/gi, '-')}-${hash}${ext}`.toLowerCase()
|
||||
}
|
||||
|
||||
const testimonialsWithImages = async (payload: Payload) => ({
|
||||
...aboutContent.testimonials,
|
||||
items: await Promise.all(
|
||||
aboutContent.testimonials.items.map(async (item) => ({
|
||||
...item,
|
||||
image: await mediaByFilename(payload, safeRemoteName(item.imageUrl)),
|
||||
})),
|
||||
),
|
||||
})
|
||||
|
||||
async function main() {
|
||||
const payload = await getPayload({ config })
|
||||
|
||||
@@ -51,6 +69,7 @@ async function main() {
|
||||
const { imageFilename: _prizeFilename, ...prizeContent } = aboutContent.prize
|
||||
const { imageFilename: _mittelstandFilename, ...mittelstandContent } = aboutContent.mittelstand
|
||||
const { fallbackImageFilename: _highlightFallbackFilename, ...highlightsContent } = aboutContent.highlights
|
||||
const testimonials = await testimonialsWithImages(payload)
|
||||
|
||||
await payload.update({
|
||||
collection: 'pages',
|
||||
@@ -79,6 +98,7 @@ async function main() {
|
||||
fallbackImage: fallbackWinnerImage,
|
||||
selectedWinners: winnerResult.docs.map((winner) => winner.id),
|
||||
},
|
||||
testimonials,
|
||||
},
|
||||
} as never,
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dotenv/config'
|
||||
|
||||
import config from '@payload-config'
|
||||
import crypto from 'crypto'
|
||||
import { getPayload, type Payload } from 'payload'
|
||||
|
||||
import { homeContent } from '@/spa/homeContent'
|
||||
@@ -15,6 +16,23 @@ const mediaByFilename = async (payload: Payload, filename: string) => {
|
||||
return result.docs[0]?.id
|
||||
}
|
||||
|
||||
const safeRemoteName = (url: string) => {
|
||||
const parsed = new URL(url)
|
||||
const ext = parsed.pathname.split('/').pop()?.includes('.') ? `.${parsed.pathname.split('.').pop()}` : '.jpg'
|
||||
const hash = crypto.createHash('sha1').update(url).digest('hex').slice(0, 10)
|
||||
return `${parsed.hostname.replace(/[^a-z0-9]+/gi, '-')}-${hash}${ext}`.toLowerCase()
|
||||
}
|
||||
|
||||
const testimonialsWithImages = async (payload: Payload) => ({
|
||||
...homeContent.testimonials,
|
||||
items: await Promise.all(
|
||||
homeContent.testimonials.items.map(async (item) => ({
|
||||
...item,
|
||||
image: await mediaByFilename(payload, safeRemoteName(item.imageUrl)),
|
||||
})),
|
||||
),
|
||||
})
|
||||
|
||||
async function main() {
|
||||
const payload = await getPayload({ config })
|
||||
|
||||
@@ -46,6 +64,7 @@ async function main() {
|
||||
const { fallbackImageFilename: _winnerFallbackFilename, ...winnersContent } = homeContent.winners
|
||||
const { imageFilename: _benefitsFilename, ...benefitsContent } = homeContent.benefits
|
||||
const { awardImageFilename: _awardFilename, ...applicationContent } = homeContent.application
|
||||
const testimonials = await testimonialsWithImages(payload)
|
||||
|
||||
const winners = await payload.find({
|
||||
collection: 'preistraeger',
|
||||
@@ -80,7 +99,7 @@ async function main() {
|
||||
fallbackImage: fallbackWinnerImage,
|
||||
featured: winners.docs.map((doc) => doc.id),
|
||||
},
|
||||
testimonials: homeContent.testimonials,
|
||||
testimonials,
|
||||
benefits: {
|
||||
...benefitsContent,
|
||||
image: benefitsImage,
|
||||
|
||||
Reference in New Issue
Block a user