Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import 'dotenv/config'
|
import 'dotenv/config'
|
||||||
|
|
||||||
import config from '@payload-config'
|
import config from '@payload-config'
|
||||||
|
import crypto from 'crypto'
|
||||||
import { getPayload, type Payload } from 'payload'
|
import { getPayload, type Payload } from 'payload'
|
||||||
|
|
||||||
import { aboutContent } from '@/spa/aboutContent'
|
import { aboutContent } from '@/spa/aboutContent'
|
||||||
@@ -15,6 +16,23 @@ const mediaByFilename = async (payload: Payload, filename: string) => {
|
|||||||
return result.docs[0]?.id
|
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() {
|
async function main() {
|
||||||
const payload = await getPayload({ config })
|
const payload = await getPayload({ config })
|
||||||
|
|
||||||
@@ -51,6 +69,7 @@ async function main() {
|
|||||||
const { imageFilename: _prizeFilename, ...prizeContent } = aboutContent.prize
|
const { imageFilename: _prizeFilename, ...prizeContent } = aboutContent.prize
|
||||||
const { imageFilename: _mittelstandFilename, ...mittelstandContent } = aboutContent.mittelstand
|
const { imageFilename: _mittelstandFilename, ...mittelstandContent } = aboutContent.mittelstand
|
||||||
const { fallbackImageFilename: _highlightFallbackFilename, ...highlightsContent } = aboutContent.highlights
|
const { fallbackImageFilename: _highlightFallbackFilename, ...highlightsContent } = aboutContent.highlights
|
||||||
|
const testimonials = await testimonialsWithImages(payload)
|
||||||
|
|
||||||
await payload.update({
|
await payload.update({
|
||||||
collection: 'pages',
|
collection: 'pages',
|
||||||
@@ -79,6 +98,7 @@ async function main() {
|
|||||||
fallbackImage: fallbackWinnerImage,
|
fallbackImage: fallbackWinnerImage,
|
||||||
selectedWinners: winnerResult.docs.map((winner) => winner.id),
|
selectedWinners: winnerResult.docs.map((winner) => winner.id),
|
||||||
},
|
},
|
||||||
|
testimonials,
|
||||||
},
|
},
|
||||||
} as never,
|
} as never,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'dotenv/config'
|
import 'dotenv/config'
|
||||||
|
|
||||||
import config from '@payload-config'
|
import config from '@payload-config'
|
||||||
|
import crypto from 'crypto'
|
||||||
import { getPayload, type Payload } from 'payload'
|
import { getPayload, type Payload } from 'payload'
|
||||||
|
|
||||||
import { homeContent } from '@/spa/homeContent'
|
import { homeContent } from '@/spa/homeContent'
|
||||||
@@ -15,6 +16,23 @@ const mediaByFilename = async (payload: Payload, filename: string) => {
|
|||||||
return result.docs[0]?.id
|
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() {
|
async function main() {
|
||||||
const payload = await getPayload({ config })
|
const payload = await getPayload({ config })
|
||||||
|
|
||||||
@@ -46,6 +64,7 @@ async function main() {
|
|||||||
const { fallbackImageFilename: _winnerFallbackFilename, ...winnersContent } = homeContent.winners
|
const { fallbackImageFilename: _winnerFallbackFilename, ...winnersContent } = homeContent.winners
|
||||||
const { imageFilename: _benefitsFilename, ...benefitsContent } = homeContent.benefits
|
const { imageFilename: _benefitsFilename, ...benefitsContent } = homeContent.benefits
|
||||||
const { awardImageFilename: _awardFilename, ...applicationContent } = homeContent.application
|
const { awardImageFilename: _awardFilename, ...applicationContent } = homeContent.application
|
||||||
|
const testimonials = await testimonialsWithImages(payload)
|
||||||
|
|
||||||
const winners = await payload.find({
|
const winners = await payload.find({
|
||||||
collection: 'preistraeger',
|
collection: 'preistraeger',
|
||||||
@@ -80,7 +99,7 @@ async function main() {
|
|||||||
fallbackImage: fallbackWinnerImage,
|
fallbackImage: fallbackWinnerImage,
|
||||||
featured: winners.docs.map((doc) => doc.id),
|
featured: winners.docs.map((doc) => doc.id),
|
||||||
},
|
},
|
||||||
testimonials: homeContent.testimonials,
|
testimonials,
|
||||||
benefits: {
|
benefits: {
|
||||||
...benefitsContent,
|
...benefitsContent,
|
||||||
image: benefitsImage,
|
image: benefitsImage,
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user