Skip to content

Commit

Permalink
requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ncomerci committed Jul 1, 2024
1 parent 934b803 commit a3bbbfd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/models/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ export default class EventModel extends Model<Event> {
}

static async getLatest(filters: EventFilter): Promise<Event[]> {
const { withInterval, event_type, proposal_id } = filters
const { with_interval, event_type, proposal_id } = filters
const query = SQL`
SELECT *
FROM ${table(EventModel)}
WHERE 1=1
${conditional(withInterval !== undefined ? withInterval : true, SQL`AND created_at >= NOW() - INTERVAL '7 day'`)}
${conditional(
with_interval !== undefined ? with_interval : true,
SQL`AND created_at >= NOW() - INTERVAL '7 day'`
)}
${conditional(
!!event_type,
SQL`AND event_type IN (${join(event_type?.map((type) => SQL`${type}`) || [], SQL`, `)})`
Expand Down
2 changes: 1 addition & 1 deletion src/shared/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export enum EventType {
export const EventFilterSchema = z.object({
event_type: z.nativeEnum(EventType).array().optional(),
proposal_id: z.string().uuid().optional(),
withInterval: z.boolean().optional(),
with_interval: z.boolean().optional(),
})

export type EventFilter = z.infer<typeof EventFilterSchema>
Expand Down
4 changes: 2 additions & 2 deletions src/utils/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ export function validateAlchemyWebhookSignature(req: Request) {
}

export function validateEventFilters(req: Request) {
const { event_type, proposal_id, withInterval } = req.query
const { event_type, proposal_id, with_interval } = req.query
const filters: Record<string, unknown> = {}
if (event_type) {
filters.event_type = isArray(event_type) ? event_type : [event_type]
}
if (proposal_id) {
filters.proposal_id = proposal_id.toString()
}
filters.withInterval = withInterval ? stringToBoolean(withInterval.toString()) : undefined
filters.with_interval = with_interval ? stringToBoolean(with_interval.toString()) : undefined
const parsedEventTypes = EventFilterSchema.safeParse(filters)
if (!parsedEventTypes.success) {
throw new Error('Invalid event types: ' + parsedEventTypes.error.message)
Expand Down

0 comments on commit a3bbbfd

Please sign in to comment.