Files
payload/test/plugin-stripe/webhooks/subscriptionCreatedOrUpdated.ts
James Mikrut 51474fa661 feat: allows mongoose schemaOptions to be configured (#7099)
## Description

This PR adds the ability to configure Mongoose's `schemaOptions`, which exposes more control about how Mongoose operates internally. For example, you can now disable `strict` mode in Mongoose to be able to preserve / surface data in MongoDB that is not reflected in Payload schemas.

- [x] I have read and understand the
[CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md)
document in this repository.

## Type of change

- [x] New feature (non-breaking change which adds functionality)

## Checklist:

- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] Existing test suite passes locally with my changes
- [ ] I have made corresponding changes to the documentation
2024-07-16 12:58:55 -04:00

113 lines
3.1 KiB
TypeScript

import { APIError } from '../../../packages/payload/src/exports/errors'
export const subscriptionCreatedOrUpdated = async (args) => {
const {
event,
payload,
// stripe,
// stripeConfig
} = args
const customerStripeID = event.data.object.customer
payload.logger.info(
`🪝 A new subscription was created or updated in Stripe on customer ID: ${customerStripeID}, syncing to Payload...`,
)
const { id: eventID, plan, status: subscriptionStatus } = event.data.object
let payloadProductID
// First lookup the product in Payload
try {
payload.logger.info(`- Looking up existing Payload product with Stripe ID: ${plan.product}...`)
const productQuery = await payload.find({
collection: 'products',
depth: 0,
where: {
stripeID: {
equals: plan.product,
},
},
})
payloadProductID = productQuery.docs?.[0]?.id
if (payloadProductID) {
payload.logger.info(
`- Found existing product with Stripe ID: ${plan.product}. Creating relationship...`,
)
}
} catch (error: any) {
payload.logger.error(`Error finding product ${error?.message}`)
}
// Now look up the customer in Payload
try {
payload.logger.info(
`- Looking up existing Payload customer with Stripe ID: ${customerStripeID}.`,
)
const customerReq: any = await payload.find({
collection: 'customers',
depth: 0,
where: {
stripeID: customerStripeID,
},
})
const foundCustomer = customerReq.docs[0]
if (foundCustomer) {
payload.logger.info(`- Found existing customer, now updating.`)
const subscriptions = foundCustomer.subscriptions || []
const indexOfSubscription = subscriptions.findIndex(
({ stripeSubscriptionID }) => stripeSubscriptionID === eventID,
)
if (indexOfSubscription > -1) {
payload.logger.info(`- Subscription already exists, now updating.`)
// update existing subscription
subscriptions[indexOfSubscription] = {
stripeProductID: plan.product,
product: payloadProductID,
status: subscriptionStatus,
}
} else {
payload.logger.info(`- This is a new subscription, now adding.`)
// create new subscription
subscriptions.push({
stripeSubscriptionID: eventID,
stripeProductID: plan.product,
product: payloadProductID,
status: subscriptionStatus,
})
}
try {
await payload.update({
collection: 'customers',
id: foundCustomer.id,
data: {
subscriptions,
skipSync: true,
},
})
payload.logger.info(`✅ Successfully updated subscription.`)
} catch (error) {
payload.logger.error(`- Error updating subscription: ${error}`)
}
} else {
payload.logger.info(`- No existing customer found, cannot update subscription.`)
}
} catch (error) {
new APIError(
`Error looking up customer with Stripe ID: '${customerStripeID}': ${error?.message}`,
)
}
}