Skip to content

Commit

Permalink
fix(locksmith): for robustness, compute ticket object
Browse files Browse the repository at this point in the history
  • Loading branch information
julien51 committed May 17, 2024
1 parent f4a1c77 commit 47231cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 3 additions & 2 deletions locksmith/__tests__/operations/eventOperations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('eventOperations', () => {
})

it('should not override previously set event data', async () => {
expect.assertions(5)
expect.assertions(6)
const eventParams = getEventFixture({
data: {
name: 'my party',
Expand Down Expand Up @@ -115,7 +115,8 @@ describe('eventOperations', () => {
'0x123'
)
expect(sameEvent.slug).toEqual(event.slug)
expect(sameEvent.data.ticket.event_end_date).toEqual('2024-05-22')
expect(sameEvent.name).toEqual('name changed!')
expect(sameEvent.data.ticket.event_end_date).toEqual('2024-05-22') // unchanged
expect(sameEvent.data.ticket.event_address).toEqual(
'Central Park, New York, NY 10014, USA'
)
Expand Down
17 changes: 16 additions & 1 deletion locksmith/src/operations/eventOperations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import dayjs from '../config/dayjs'
import { kebabCase, defaultsDeep } from 'lodash'
import * as metadataOperations from './metadataOperations'
import { PaywallConfig, getLockTypeByMetadata } from '@unlock-protocol/core'
import {
PaywallConfig,
getLockTypeByMetadata,
toFormData,
} from '@unlock-protocol/core'
import { CheckoutConfig, EventData } from '../models'
import { saveCheckoutConfig } from './checkoutConfigOperations'
import { EventBodyType } from '../controllers/v2/eventsController'
Expand Down Expand Up @@ -63,6 +67,11 @@ export const getEventForLock = async (
if (event && !includeProtected) {
event.data = removeProtectedAttributesFromObject(event.data)
}
// Robustness principle: the front-end, as well as mailers expects a ticket object to be present
if (event) {
const ticket = toFormData(event?.data).ticket
event.data.ticket = defaultsDeep(ticket, event.data.ticket)
}
return event
}

Expand Down Expand Up @@ -169,6 +178,12 @@ export const getEventBySlug = async (
slug,
},
})
// Robustness principle: the front-end, as well as mailers expects a ticket object to be present
if (event) {
const ticket = toFormData(event?.data).ticket
event.data.ticket = defaultsDeep(ticket, event.data.ticket)
}

if (event && !includeProtected) {
event.data = removeProtectedAttributesFromObject(event.data)
}
Expand Down

0 comments on commit 47231cd

Please sign in to comment.