Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## Unreleased

- Syncing no longer overwrites product image alt text written in Payload. An image's alt is set to the product title only when it is empty, on first import and on later syncs. Images are matched by URL, so an image whose URL changes in Printify gets the title again. Titles are imported as before.
- Tested: unit tests for a new image getting the title, a curator's alt surviving a resync (also when the title changes), and an emptied alt being refilled. The same flow on a real in-memory MongoDB. Full suite (78 tests), typecheck and leak check pass.
- Not tested: against a live Printify account, or a store whose `images` rows are shaped differently from the plugin's own.
14 changes: 13 additions & 1 deletion src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ export const slugify = (s: string) =>
.replace(/^-+|-+$/g, '')
.slice(0, 80) || 'product'

type StoredImage = { url?: string | null; alt?: string | null }

/**
* Put freshly mapped images onto the stored ones without losing alt text written in Payload.
* An image is matched by its URL. A stored alt that has text is kept; an empty or missing one
* is filled with the title the mapping gave it.
*/
export function mergeImageAlt<T extends { url: string; alt: string }>(images: T[], stored: StoredImage[] | null | undefined): T[] {
const kept = new Map((stored ?? []).filter((s) => s.url && s.alt?.trim()).map((s) => [s.url, s.alt as string]))
return images.map((img) => (kept.has(img.url) ? { ...img, alt: kept.get(img.url)! } : img))
}

/** The parts of a Printify product a store keeps (pure). Prices stay in cents. */
export function mapPrintifyProduct(p: PrintifyProduct) {
const enabled = p.variants.filter((v) => v.is_enabled)
Expand Down Expand Up @@ -108,7 +120,7 @@ export async function syncPrintifyProduct(payload: Payload, opts: ResolvedPrinti
slug,
generateSlug: false,
description: m.description,
images: m.images,
images: mergeImageAlt(m.images, existing?.images as StoredImage[] | undefined),
enableVariants: m.options.length > 0,
variantTypes: typeIds,
[`${price}Enabled`]: m.minPrice != null,
Expand Down
25 changes: 25 additions & 0 deletions test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Config, PayloadRequest } from 'payload'
import { describe, expect, it } from 'vitest'

import { mapPrintifyProduct, printifyPlugin, validPrintifySignature, type PrintifyProduct } from '../src'
import { mergeImageAlt } from '../src/sync'
import { pushVariantPriceHook } from '../src/prices'
import { pushVisibilityHook } from '../src/visibility'
import type { PrintifyPluginOptions, ResolvedPrintifyOptions } from '../src/types'
Expand Down Expand Up @@ -62,6 +63,30 @@ describe('mapping', () => {
})
})

describe('image alt text', () => {
const front = 'https://images-api.printify.com/mockup/front.jpg'
const back = 'https://images-api.printify.com/mockup/back.jpg'
const mapped = () => mapPrintifyProduct(tee()).images

it('gives a new image the product title', () => {
expect(mergeImageAlt(mapped(), undefined).map((i) => i.alt)).toEqual(['Leopard Gecko Tee', 'Leopard Gecko Tee'])
expect(mergeImageAlt(mapped(), [{ url: 'https://example.test/other.jpg', alt: 'Other' }]).map((i) => i.alt)).toEqual(['Leopard Gecko Tee', 'Leopard Gecko Tee'])
})

it('keeps an alt a curator wrote through a resync, even when the title changes', () => {
const stored = [{ url: front, alt: 'A gecko on a black tee, facing left' }, { url: back, alt: 'Leopard Gecko Tee' }]
const out = mergeImageAlt(mapPrintifyProduct(tee({ title: 'Gecko Tee v2' })).images, stored)
expect(out.find((i) => i.url === front)?.alt).toBe('A gecko on a black tee, facing left')
expect(out.find((i) => i.url === back)?.alt).toBe('Leopard Gecko Tee')
})

it('refills an alt that was emptied', () => {
const out = mergeImageAlt(mapped(), [{ url: front, alt: '' }, { url: back, alt: ' ' }])
expect(out.map((i) => i.alt)).toEqual(['Leopard Gecko Tee', 'Leopard Gecko Tee'])
expect(mergeImageAlt(mapped(), [{ url: front, alt: null }]).map((i) => i.alt)).toEqual(['Leopard Gecko Tee', 'Leopard Gecko Tee'])
})
})

describe('webhooks', () => {
it('accepts only correctly signed bodies', () => {
const body = JSON.stringify({ id: 'e1', type: 'product:updated' })
Expand Down
21 changes: 21 additions & 0 deletions test/real-db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,27 @@ async function unknownOrder(s: Store, item: { product: string; variant: string }
return doc
}

describe('image alt text, on a real database', () => {
const withImage = (title: string): PrintifyProduct => ({
...product('pf-alt-1', 4001, title),
images: [{ src: 'https://images-api.printify.com/mockup/alt.jpg', variant_ids: [4001], position: 'front', is_default: true }],
})
const altOf = async (id: string) => ((await one.payload.findByID({ collection: 'products', id, depth: 0, draft: true })) as Doc).images as Doc[]
const setAlt = async (id: string, alt: string) =>
one.payload.update({ collection: 'products', id, data: { images: (await altOf(id)).map((i) => ({ ...i, alt })) } as never, draft: true })

it('fills alt, keeps a curator’s alt through resyncs, and refills it when emptied', async () => {
const { id } = await syncPrintifyProduct(one.payload, one.opts, null, withImage('Alt Mug'))
expect((await altOf(id))[0]!.alt).toBe('Alt Mug')
await setAlt(id, 'A white mug with a gecko')
await syncPrintifyProduct(one.payload, one.opts, null, withImage('Alt Mug 2'))
expect((await altOf(id))[0]!.alt).toBe('A white mug with a gecko')
await setAlt(id, '')
await syncPrintifyProduct(one.payload, one.opts, null, withImage('Alt Mug 2'))
expect((await altOf(id))[0]!.alt).toBe('Alt Mug 2')
})
})

let mongo: MongoMemoryReplSet
let one: Store
let item: { product: string; variant: string }
Expand Down