Abo-Spiegel: fehlt ein neues Feld in Appwrite, wird ohne dieses Feld gespiegelt
Schützt Webhooks und den öffentlichen Kündigungs-/Widerrufsknopf, falls das Setup-Skript erst nach dem Deploy läuft; der Fehler steht laut im Log. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dc61649228
commit
2694ef18d6
2 files changed
+71
-1
No files matched your search
@@ -138,7 +138,7 @@ function periodEndOf(sub) {
|
|||||||
return sub.current_period_end || sub.items?.data?.[0]?.current_period_end || null
|
return sub.current_period_end || sub.items?.data?.[0]?.current_period_end || null
|
||||||
}
|
}
|
||||||
|
|
||||||
async function upsertSubscriptionDoc(id, data) {
|
async function writeSubscriptionDoc(id, data) {
|
||||||
const existing = await getDocument(config.collections.subscriptions, id).catch(() => null)
|
const existing = await getDocument(config.collections.subscriptions, id).catch(() => null)
|
||||||
if (existing) return updateDocument(config.collections.subscriptions, id, data)
|
if (existing) return updateDocument(config.collections.subscriptions, id, data)
|
||||||
try {
|
try {
|
||||||
@@ -150,6 +150,25 @@ async function upsertSubscriptionDoc(id, data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function upsertSubscriptionDoc(id, data) {
|
||||||
|
let fields = data
|
||||||
|
for (;;) {
|
||||||
|
try {
|
||||||
|
return await writeSubscriptionDoc(id, fields)
|
||||||
|
} catch (err) {
|
||||||
|
// Neues Feld noch nicht in Appwrite (Setup-Skript nicht gelaufen): ohne das Feld spiegeln,
|
||||||
|
// damit Webhooks und der öffentliche Kündigungs-/Widerrufsknopf weiter funktionieren
|
||||||
|
const missing = err.status === 400 && /Unknown attribute: "(\w+)"/.exec(err.message || '')?.[1]
|
||||||
|
if (!missing || !(missing in fields)) throw err
|
||||||
|
console.error(
|
||||||
|
`[stripe] Appwrite kennt das Feld "${missing}" nicht (scripts/setup-abo-collections.mjs ausführen) – Abo ${id} ohne dieses Feld gespiegelt`
|
||||||
|
)
|
||||||
|
const { [missing]: _dropped, ...rest } = fields
|
||||||
|
fields = rest
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Stripe-Subscription frisch laden und nach Appwrite spiegeln. */
|
/** Stripe-Subscription frisch laden und nach Appwrite spiegeln. */
|
||||||
export async function syncSubscription(subscriptionId) {
|
export async function syncSubscription(subscriptionId) {
|
||||||
const stripe = getStripe()
|
const stripe = getStripe()
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import { test, after } from 'node:test'
|
||||||
|
import assert from 'node:assert/strict'
|
||||||
|
import { startAppwriteMock } from './support/appwriteMock.js'
|
||||||
|
import { startFakeStripe } from './support/fakeStripe.js'
|
||||||
|
import { useTestEnv } from './support/testEnv.js'
|
||||||
|
import { schemaKeys } from './support/schema.js'
|
||||||
|
|
||||||
|
// Produktion, bevor das Setup-Skript gelaufen ist: Appwrite kennt productKey noch nicht
|
||||||
|
const aw = await startAppwriteMock({
|
||||||
|
schema: { subscriptions: schemaKeys('subscriptions').filter((key) => key !== 'productKey') },
|
||||||
|
})
|
||||||
|
const st = await startFakeStripe()
|
||||||
|
after(() => Promise.all([aw.close(), st.close()]))
|
||||||
|
useTestEnv({ appwriteUrl: aw.url, stripeUrl: st.url })
|
||||||
|
const { syncSubscription } = await import('../server/services/stripeBilling.js')
|
||||||
|
|
||||||
|
const sub = (id, cancelAtPeriodEnd) => ({
|
||||||
|
id,
|
||||||
|
object: 'subscription',
|
||||||
|
status: 'active',
|
||||||
|
customer: 'cus_1',
|
||||||
|
start_date: 1790000000,
|
||||||
|
current_period_end: 1792600000,
|
||||||
|
cancel_at_period_end: cancelAtPeriodEnd,
|
||||||
|
metadata: { webklarCustomerId: 'c1', email: 'k@example.invalid', termsVersion: '2026-09-25-v3' },
|
||||||
|
items: {
|
||||||
|
object: 'list',
|
||||||
|
data: [{ id: 'si_w', quantity: 1, price: { id: 'price_w', lookup_key: 'webklar_wartung_monatlich_5000', unit_amount: 5000 } }],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Fehlendes Feld in Appwrite: Abo wird trotzdem gespiegelt (anlegen und ändern)', async () => {
|
||||||
|
const errors = []
|
||||||
|
const original = console.error
|
||||||
|
console.error = (...args) => errors.push(args.join(' '))
|
||||||
|
try {
|
||||||
|
st.subscriptions.set('sub_w', sub('sub_w', false))
|
||||||
|
const created = await syncSubscription('sub_w')
|
||||||
|
assert.equal(created.status, 'active')
|
||||||
|
assert.equal('productKey' in created, false)
|
||||||
|
|
||||||
|
// z. B. Kündigung über den öffentlichen Knopf: Stripe ist schon geändert, der Spiegel muss folgen
|
||||||
|
st.subscriptions.set('sub_w', sub('sub_w', true))
|
||||||
|
const updated = await syncSubscription('sub_w')
|
||||||
|
assert.equal(updated.cancelAtPeriodEnd, true)
|
||||||
|
assert.equal(aw.docs('subscriptions').get('sub_w').cancelAtPeriodEnd, true)
|
||||||
|
} finally {
|
||||||
|
console.error = original
|
||||||
|
}
|
||||||
|
assert.ok(errors.some((line) => line.includes('productKey')), 'Fehler im Log nennt das fehlende Feld')
|
||||||
|
})
|
||||||
Reference in new issue
Block a user