diff --git a/api/src/graphql/events.sdl.ts b/api/src/graphql/events.sdl.ts index fac37cf..acbaa6d 100644 --- a/api/src/graphql/events.sdl.ts +++ b/api/src/graphql/events.sdl.ts @@ -19,6 +19,14 @@ export const schema = gql` capacity: Int createdAt: DateTime! updatedAt: DateTime! + + formattedStartAt: String + formattedEndAt: String + formattedRegistrationStartAt: String + formattedRegistrationEndAt: String + formattedCreatedAt: String + formattedUpdatedAt: String + sessions: [EventSession] speakers: [EventSpeaker] registrants: [EventRegistrant] diff --git a/api/src/lib/formatters.ts b/api/src/lib/formatters.ts new file mode 100644 index 0000000..d4968ef --- /dev/null +++ b/api/src/lib/formatters.ts @@ -0,0 +1,7 @@ +import { utcToZonedTime, format } from 'date-fns-tz'; + +export function formatDateTime(dt: Date, tz?: string): string { + return format(utcToZonedTime(dt, tz), 'MMM d, yyyy hh:mmaaa zzz', { + timeZone: tz || 'Etc/UTC', + }); +} diff --git a/api/src/services/events/events.ts b/api/src/services/events/events.ts index a35666b..a1add99 100644 --- a/api/src/services/events/events.ts +++ b/api/src/services/events/events.ts @@ -1,6 +1,11 @@ -import type { QueryResolvers, MutationResolvers } from 'types/graphql'; +import type { + QueryResolvers, + MutationResolvers, + EventRelationResolvers, +} from 'types/graphql'; import { db } from 'src/lib/db'; +import { formatDateTime } from 'src/lib/formatters'; export const events: QueryResolvers['events'] = () => { return db.event.findMany(); @@ -34,7 +39,25 @@ export const deleteEvent: MutationResolvers['deleteEvent'] = ({ id }) => { }); }; -export const Event = { +export const Event: EventRelationResolvers = { + formattedStartAt: (_obj, { root }) => { + return formatDateTime(root.startAt, root.timezone); + }, + formattedEndAt: (_obj, { root }) => { + return formatDateTime(root.endAt, root.timezone); + }, + formattedRegistrationStartAt: (_obj, { root }) => { + return formatDateTime(root.registrationStartAt, root.timezone); + }, + formattedRegistrationEndAt: (_obj, { root }) => { + return formatDateTime(root.registrationEndAt, root.timezone); + }, + formattedCreatedAt: (_obj, { root }) => { + return formatDateTime(root.createdAt, root.timezone); + }, + formattedUpdatedAt: (_obj, { root }) => { + return formatDateTime(root.updatedAt, root.timezone); + }, sessions: (_obj, { root }) => { return db.event.findUnique({ where: { id: root?.id } }).sessions(); },