Skip to content

Commit

Permalink
fix(locksmith): making sure we never overwrite eventdata when not exp…
Browse files Browse the repository at this point in the history
…licitly set
  • Loading branch information
julien51 committed May 17, 2024
1 parent d56cc0a commit 8c50ca4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
38 changes: 38 additions & 0 deletions locksmith/__tests__/operations/eventOperations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,44 @@ describe('eventOperations', () => {
expect(sameEvent.slug).toEqual(event.slug)
})

it('should not override previously set event data', async () => {
expect.assertions(3)
const eventParams = getEventFixture({
data: {
name: 'my party',
requiresApproval: true,
emailSender: 'Julien Genestoux',
ticket: {
event_address: '29 Little W 12th St, New York, NY 10014, USA',
event_end_date: '2024-05-22',
event_end_time: '14:00',
event_timezone: 'America/New_York',
event_start_date: '2024-05-22',
event_start_time: '08:30',
},
},
checkoutConfig: { config: { locks: {} } },
})
const [event] = await saveEvent(eventParams, '0x123')
expect(event.data.ticket.event_end_date).toEqual('2024-05-22')

const [sameEvent] = await saveEvent(
{
data: {
slug: event.slug,
name: 'name changed!',
ticket: {
event_address: 'Central Park, New York, NY 10014, USA',
},
},
checkoutConfig: { config: { locks: {} } },
},
'0x123'
)
expect(sameEvent.slug).toEqual(event.slug)
expect(sameEvent.data.ticket.event_end_date).toEqual('2024-05-22')
})

it('should save requiresAppoval when applicable', async () => {
expect.assertions(2)
const eventParams = getEventFixture({
Expand Down
23 changes: 18 additions & 5 deletions locksmith/src/operations/eventOperations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs from '../config/dayjs'
import { kebabCase } from 'lodash'
import { kebabCase, defaultsDeep } from 'lodash'
import * as metadataOperations from './metadataOperations'
import { PaywallConfig, getLockTypeByMetadata } from '@unlock-protocol/core'
import { CheckoutConfig, EventData } from '../models'
Expand Down Expand Up @@ -195,14 +195,27 @@ export const saveEvent = async (
walletAddress: string
): Promise<[EventData, boolean]> => {
const slug = parsed.data.slug || (await createEventSlug(parsed.data.name))

let data = {}
const previousEvent = await EventData.findOne({
where: { slug },
})
if (previousEvent) {
data = defaultsDeep(previousEvent.data, {
...parsed.data,
})
} else {
data = {
...parsed.data,
slug, // Making sure we add the slug to the data as well.
}
}

const [savedEvent, _] = await EventData.upsert(
{
name: parsed.data.name,
slug,
data: {
...parsed.data,
slug, // Making sure we add the slug to the data as well.
},
data,
createdBy: walletAddress,
},
{
Expand Down

0 comments on commit 8c50ca4

Please sign in to comment.