From f6cb5ed8fc1c2eaa0d4fc79b7ff11651d6ef7054 Mon Sep 17 00:00:00 2001 From: Ravi Date: Sat, 2 Oct 2021 18:10:33 +0530 Subject: [PATCH 1/6] Added setUserInterestForChapter mutation and used in rsvp submit --- client/src/generated/graphql.tsx | 43 ++++++++++++++++++ .../dashboard/Events/graphql/queries.ts | 6 +++ client/src/modules/events/pages/eventPage.tsx | 12 ++++- .../controllers/UserChapterRole/resolver.ts | 44 +++++++++++++++++++ server/src/controllers/index.ts | 4 +- 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 server/src/controllers/UserChapterRole/resolver.ts diff --git a/client/src/generated/graphql.tsx b/client/src/generated/graphql.tsx index 1fc5a0c1c3..c8bd48496c 100644 --- a/client/src/generated/graphql.tsx +++ b/client/src/generated/graphql.tsx @@ -2292,3 +2292,46 @@ const result: PossibleTypesResultData = { possibleTypes: {}, }; export default result; + +export const setUserInterestForChapterDocument = gql` + mutation setUserInterestForChapter($event_id: Int!) { + setUserInterestForChapter(event_id: $event_id) { + id + } + } +`; +export type setUserInterestForChapterFn = Apollo.MutationFunction< + setUserInterestForChapterMutation, + setUserInterestForChapterMutationVariables +>; + +export type setUserInterestForChapterMutation = { + __typename?: 'Mutation'; + setUserInterestForChapter: { + __typename: 'setUserChapterRole'; + event_id: Scalars['Int']; + }; +}; + +export type setUserInterestForChapterMutationVariables = Exact<{ + event_id: Scalars['Int']; +}>; + +export function useSetUserInterestForChapter( + baseOptions?: Apollo.MutationHookOptions< + setUserInterestForChapterMutation, + setUserInterestForChapterMutationVariables + >, +) { + const options = { ...defaultOptions, ...baseOptions }; + return Apollo.useMutation< + setUserInterestForChapterMutation, + setUserInterestForChapterMutationVariables + >(setUserInterestForChapterDocument, options); +} + +export type setUserInterestForChapterMutationHookResult = ReturnType< + typeof useSetUserInterestForChapter +>; +export type setUserInterestForChapterMutationResult = + Apollo.MutationResult; diff --git a/client/src/modules/dashboard/Events/graphql/queries.ts b/client/src/modules/dashboard/Events/graphql/queries.ts index 71bc9b3476..a5c4663929 100644 --- a/client/src/modules/dashboard/Events/graphql/queries.ts +++ b/client/src/modules/dashboard/Events/graphql/queries.ts @@ -164,3 +164,9 @@ export const sendEventInvite = gql` sendEventInvite(id: $id) } `; + +export const setUserInterestForChapter = gql` + mutation Mutation($event_id: Int!) { + setUserInterestForChapter(event_id: $event_id) + } +`; diff --git a/client/src/modules/events/pages/eventPage.tsx b/client/src/modules/events/pages/eventPage.tsx index bbef7aefd3..1685844d28 100644 --- a/client/src/modules/events/pages/eventPage.tsx +++ b/client/src/modules/events/pages/eventPage.tsx @@ -19,7 +19,11 @@ import React, { useMemo } from 'react'; import { LoginRegisterModal } from '../../../components/LoginRegisterModal'; import { useAuth } from '../../auth/store'; import { EVENT } from '../../dashboard/Events/graphql/queries'; -import { useEventQuery, useRsvpToEventMutation } from 'generated/graphql'; +import { + useEventQuery, + useRsvpToEventMutation, + useSetUserInterestForChapter, +} from 'generated/graphql'; import { useParam } from 'hooks/useParam'; export const EventPage: NextPage = () => { @@ -27,6 +31,7 @@ export const EventPage: NextPage = () => { const { user } = useAuth(); const [rsvpToEvent] = useRsvpToEventMutation(); + const [setUserInterestForChapter] = useSetUserInterestForChapter(); const { loading, error, data } = useEventQuery({ variables: { id: id || -1 }, }); @@ -80,6 +85,11 @@ export const EventPage: NextPage = () => { } : { title: 'You canceled your RSVP 👋', status: 'error' }, ); + if (add) { + await setUserInterestForChapter({ + variables: { event_id: 3 }, + }); + } } catch (err) { toast({ title: 'Something went wrong', status: 'error' }); console.error(err); diff --git a/server/src/controllers/UserChapterRole/resolver.ts b/server/src/controllers/UserChapterRole/resolver.ts new file mode 100644 index 0000000000..0a6dfc827d --- /dev/null +++ b/server/src/controllers/UserChapterRole/resolver.ts @@ -0,0 +1,44 @@ +import { GQLCtx } from 'src/common-types/gql'; +import { Event, UserChapterRole } from 'src/models'; +import { Arg, Ctx, Int, Mutation, Resolver } from 'type-graphql'; + +@Resolver() +export class UserChapterRoleResolver { + @Mutation(() => UserChapterRole) + async setUserInterestForChapter( + @Arg('event_id', () => Int) event_id: number, + @Ctx() ctx: GQLCtx, + ) { + console.log('Inside the resolver'); + + if (!ctx.user) { + throw Error('User must be logged in to update role '); + } + const event = await Event.findOne(event_id, { relations: ['chapter'] }); + console.log(event); + if (!event) { + throw Error('Cannot find the event with id ' + event_id); + } + if (!event.chapter) { + throw Error('Cannot find the chapter of the event with id ' + event_id); + } + + const userChapterRole = await UserChapterRole.findOne({ + where: { user_id: ctx.user.id, chapter_id: event.chapter.id }, + }); + + console.log(userChapterRole); + + if (!userChapterRole) { + console.log('Role Not present so creating it'); + + return await new UserChapterRole({ + userId: ctx.user.id, + chapterId: event.chapter.id, + roleName: 'member', + interested: true, + }).save(); + } + return userChapterRole; + } +} diff --git a/server/src/controllers/index.ts b/server/src/controllers/index.ts index e973724450..61860419e3 100644 --- a/server/src/controllers/index.ts +++ b/server/src/controllers/index.ts @@ -4,14 +4,14 @@ import { EmailResolver } from './Messages/resolver'; import { VenueResolver } from './Venue/resolver'; import { AuthResolver } from 'src/controllers/Auth/resolver'; import { UserResolver } from 'src/models'; - +import { UserChapterRoleResolver } from './UserChapterRole/resolver'; const resolvers = [ ChapterResolver, VenueResolver, EventResolver, EmailResolver, AuthResolver, - + UserChapterRoleResolver, UserResolver, // Somehow extract this somewhere else ] as const; From 8462bbb4e06b9ee14c5af56ee1c1375391e02ae7 Mon Sep 17 00:00:00 2001 From: Ravi Date: Sat, 2 Oct 2021 18:40:38 +0530 Subject: [PATCH 2/6] Added placeholder for unsubscribe in emails --- server/src/controllers/Events/resolver.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/server/src/controllers/Events/resolver.ts b/server/src/controllers/Events/resolver.ts index c162b2c8bf..75e5b638e9 100644 --- a/server/src/controllers/Events/resolver.ts +++ b/server/src/controllers/Events/resolver.ts @@ -6,6 +6,10 @@ import { GQLCtx } from 'src/common-types/gql'; import { Event, Venue, Chapter, Rsvp, UserEventRole } from 'src/models'; import MailerService from 'src/services/MailerService'; +//Place holder for unsubscribe +//TODO: Replace placeholder with actual unsubscribe link +let unsubscribe: string = `
Unsubscribe`; + @Resolver() export class EventResolver { @Query(() => [Event]) @@ -118,6 +122,8 @@ To add this event to your calendar(s) you can use these links: Google
Outlook + +${unsubscribe} `, ).sendEmail(); // TODO: rather than getting all the roles and filtering them, we should @@ -128,7 +134,7 @@ To add this event to your calendar(s) you can use these links: await new MailerService( organizersEmails, `New RSVP for ${event.name}`, - `User ${ctx.user.first_name} ${ctx.user.last_name} has RSVP'd.`, + `User ${ctx.user.first_name} ${ctx.user.last_name} has RSVP'd. ${unsubscribe}`, ).sendEmail(); return rsvp; } @@ -242,6 +248,7 @@ ${venue.street_address ? venue.street_address + '
' : ''} ${venue.city}
${venue.region}
${venue.postal_code}
+${unsubscribe} `; new MailerService(emailList, subject, body).sendEmail(); } @@ -334,6 +341,7 @@ ${venue.postal_code}
// TODO: this needs to include an ical file // TODO: it needs a link to unsubscribe from just this event. See // https://github.com/freeCodeCamp/chapter/issues/276#issuecomment-596913322 + // Update the place holder with actual const body = `When: ${event.start_at} to ${event.ends_at}
` + (event.venue ? `Where: ${event.venue?.name}
` : '') + @@ -345,7 +353,9 @@ ${venue.postal_code}
----------------------------
You received this email because you follow this chapter.

-See the options above to change your notifications.`; +See the options above to change your notifications. +${unsubscribe} +`; await new MailerService(addresses, subject, body).sendEmail(); From 7d13e0ca1fe3a84e4cfef2fa1ad6a5f39886fd4c Mon Sep 17 00:00:00 2001 From: Ravi Date: Sat, 2 Oct 2021 19:06:15 +0530 Subject: [PATCH 3/6] Updated code for linter rules to pass --- server/src/controllers/Events/resolver.ts | 2 +- server/src/controllers/UserChapterRole/resolver.ts | 9 +-------- server/src/controllers/index.ts | 2 +- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/server/src/controllers/Events/resolver.ts b/server/src/controllers/Events/resolver.ts index 75e5b638e9..82863e423c 100644 --- a/server/src/controllers/Events/resolver.ts +++ b/server/src/controllers/Events/resolver.ts @@ -8,7 +8,7 @@ import MailerService from 'src/services/MailerService'; //Place holder for unsubscribe //TODO: Replace placeholder with actual unsubscribe link -let unsubscribe: string = `
Unsubscribe`; +const unsubscribe = `
Unsubscribe`; @Resolver() export class EventResolver { diff --git a/server/src/controllers/UserChapterRole/resolver.ts b/server/src/controllers/UserChapterRole/resolver.ts index 0a6dfc827d..4bbe738f91 100644 --- a/server/src/controllers/UserChapterRole/resolver.ts +++ b/server/src/controllers/UserChapterRole/resolver.ts @@ -1,6 +1,6 @@ +import { Arg, Ctx, Int, Mutation, Resolver } from 'type-graphql'; import { GQLCtx } from 'src/common-types/gql'; import { Event, UserChapterRole } from 'src/models'; -import { Arg, Ctx, Int, Mutation, Resolver } from 'type-graphql'; @Resolver() export class UserChapterRoleResolver { @@ -9,13 +9,10 @@ export class UserChapterRoleResolver { @Arg('event_id', () => Int) event_id: number, @Ctx() ctx: GQLCtx, ) { - console.log('Inside the resolver'); - if (!ctx.user) { throw Error('User must be logged in to update role '); } const event = await Event.findOne(event_id, { relations: ['chapter'] }); - console.log(event); if (!event) { throw Error('Cannot find the event with id ' + event_id); } @@ -27,11 +24,7 @@ export class UserChapterRoleResolver { where: { user_id: ctx.user.id, chapter_id: event.chapter.id }, }); - console.log(userChapterRole); - if (!userChapterRole) { - console.log('Role Not present so creating it'); - return await new UserChapterRole({ userId: ctx.user.id, chapterId: event.chapter.id, diff --git a/server/src/controllers/index.ts b/server/src/controllers/index.ts index 61860419e3..a746e11f05 100644 --- a/server/src/controllers/index.ts +++ b/server/src/controllers/index.ts @@ -1,10 +1,10 @@ import { ChapterResolver } from './Chapter/resolver'; import { EventResolver } from './Events/resolver'; import { EmailResolver } from './Messages/resolver'; +import { UserChapterRoleResolver } from './UserChapterRole/resolver'; import { VenueResolver } from './Venue/resolver'; import { AuthResolver } from 'src/controllers/Auth/resolver'; import { UserResolver } from 'src/models'; -import { UserChapterRoleResolver } from './UserChapterRole/resolver'; const resolvers = [ ChapterResolver, VenueResolver, From 03549ba9fa344a759e539f08927b33f78c93a082 Mon Sep 17 00:00:00 2001 From: Ravi Date: Mon, 4 Oct 2021 19:15:41 +0530 Subject: [PATCH 4/6] Removed await from the return statement --- server/src/controllers/UserChapterRole/resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/controllers/UserChapterRole/resolver.ts b/server/src/controllers/UserChapterRole/resolver.ts index 4bbe738f91..002cb337f8 100644 --- a/server/src/controllers/UserChapterRole/resolver.ts +++ b/server/src/controllers/UserChapterRole/resolver.ts @@ -25,7 +25,7 @@ export class UserChapterRoleResolver { }); if (!userChapterRole) { - return await new UserChapterRole({ + return new UserChapterRole({ userId: ctx.user.id, chapterId: event.chapter.id, roleName: 'member', From 5e40b90db1eedf85b2ef3008ba6ad6f7dbd2a3a1 Mon Sep 17 00:00:00 2001 From: Ravi Date: Mon, 4 Oct 2021 20:14:38 +0530 Subject: [PATCH 5/6] Renamed the mutation from setUserInterestForchapter to initUserInterestForChapter --- client/src/generated/graphql.tsx | 38 +++++++++---------- .../dashboard/Events/graphql/queries.ts | 4 +- client/src/modules/events/pages/eventPage.tsx | 6 +-- .../controllers/UserChapterRole/resolver.ts | 2 +- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/client/src/generated/graphql.tsx b/client/src/generated/graphql.tsx index c8bd48496c..fe0ec0a4bc 100644 --- a/client/src/generated/graphql.tsx +++ b/client/src/generated/graphql.tsx @@ -2293,45 +2293,45 @@ const result: PossibleTypesResultData = { }; export default result; -export const setUserInterestForChapterDocument = gql` - mutation setUserInterestForChapter($event_id: Int!) { - setUserInterestForChapter(event_id: $event_id) { +export const initUserInterestForChapterDocument = gql` + mutation initUserInterestForChapter($event_id: Int!) { + initUserInterestForChapter(event_id: $event_id) { id } } `; -export type setUserInterestForChapterFn = Apollo.MutationFunction< - setUserInterestForChapterMutation, - setUserInterestForChapterMutationVariables +export type initUserInterestForChapterFn = Apollo.MutationFunction< + initUserInterestForChapterMutation, + initUserInterestForChapterMutationVariables >; -export type setUserInterestForChapterMutation = { +export type initUserInterestForChapterMutation = { __typename?: 'Mutation'; - setUserInterestForChapter: { + initUserInterestForChapter: { __typename: 'setUserChapterRole'; event_id: Scalars['Int']; }; }; -export type setUserInterestForChapterMutationVariables = Exact<{ +export type initUserInterestForChapterMutationVariables = Exact<{ event_id: Scalars['Int']; }>; -export function useSetUserInterestForChapter( +export function useinitUserInterestForChapter( baseOptions?: Apollo.MutationHookOptions< - setUserInterestForChapterMutation, - setUserInterestForChapterMutationVariables + initUserInterestForChapterMutation, + initUserInterestForChapterMutationVariables >, ) { const options = { ...defaultOptions, ...baseOptions }; return Apollo.useMutation< - setUserInterestForChapterMutation, - setUserInterestForChapterMutationVariables - >(setUserInterestForChapterDocument, options); + initUserInterestForChapterMutation, + initUserInterestForChapterMutationVariables + >(initUserInterestForChapterDocument, options); } -export type setUserInterestForChapterMutationHookResult = ReturnType< - typeof useSetUserInterestForChapter +export type initUserInterestForChapterMutationHookResult = ReturnType< + typeof useinitUserInterestForChapter >; -export type setUserInterestForChapterMutationResult = - Apollo.MutationResult; +export type initUserInterestForChapterMutationResult = + Apollo.MutationResult; diff --git a/client/src/modules/dashboard/Events/graphql/queries.ts b/client/src/modules/dashboard/Events/graphql/queries.ts index a5c4663929..d4dc680dea 100644 --- a/client/src/modules/dashboard/Events/graphql/queries.ts +++ b/client/src/modules/dashboard/Events/graphql/queries.ts @@ -165,8 +165,8 @@ export const sendEventInvite = gql` } `; -export const setUserInterestForChapter = gql` +export const initUserInterestForChapter = gql` mutation Mutation($event_id: Int!) { - setUserInterestForChapter(event_id: $event_id) + initUserInterestForChapter(event_id: $event_id) } `; diff --git a/client/src/modules/events/pages/eventPage.tsx b/client/src/modules/events/pages/eventPage.tsx index 1685844d28..d3a6287e1e 100644 --- a/client/src/modules/events/pages/eventPage.tsx +++ b/client/src/modules/events/pages/eventPage.tsx @@ -22,7 +22,7 @@ import { EVENT } from '../../dashboard/Events/graphql/queries'; import { useEventQuery, useRsvpToEventMutation, - useSetUserInterestForChapter, + useinitUserInterestForChapter, } from 'generated/graphql'; import { useParam } from 'hooks/useParam'; @@ -31,7 +31,7 @@ export const EventPage: NextPage = () => { const { user } = useAuth(); const [rsvpToEvent] = useRsvpToEventMutation(); - const [setUserInterestForChapter] = useSetUserInterestForChapter(); + const [initUserInterestForChapter] = useinitUserInterestForChapter(); const { loading, error, data } = useEventQuery({ variables: { id: id || -1 }, }); @@ -86,7 +86,7 @@ export const EventPage: NextPage = () => { : { title: 'You canceled your RSVP 👋', status: 'error' }, ); if (add) { - await setUserInterestForChapter({ + await initUserInterestForChapter({ variables: { event_id: 3 }, }); } diff --git a/server/src/controllers/UserChapterRole/resolver.ts b/server/src/controllers/UserChapterRole/resolver.ts index 002cb337f8..7f15b2c524 100644 --- a/server/src/controllers/UserChapterRole/resolver.ts +++ b/server/src/controllers/UserChapterRole/resolver.ts @@ -5,7 +5,7 @@ import { Event, UserChapterRole } from 'src/models'; @Resolver() export class UserChapterRoleResolver { @Mutation(() => UserChapterRole) - async setUserInterestForChapter( + async initUserInterestForChapter( @Arg('event_id', () => Int) event_id: number, @Ctx() ctx: GQLCtx, ) { From cd4a92c5a646fc49bf784d12b79886e475c2ea43 Mon Sep 17 00:00:00 2001 From: Ravi Date: Mon, 4 Oct 2021 20:54:32 +0530 Subject: [PATCH 6/6] changed the hardcoded value to id variable --- client/src/modules/events/pages/eventPage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/modules/events/pages/eventPage.tsx b/client/src/modules/events/pages/eventPage.tsx index d3a6287e1e..e8abd36cb4 100644 --- a/client/src/modules/events/pages/eventPage.tsx +++ b/client/src/modules/events/pages/eventPage.tsx @@ -87,7 +87,7 @@ export const EventPage: NextPage = () => { ); if (add) { await initUserInterestForChapter({ - variables: { event_id: 3 }, + variables: { event_id: id }, }); } } catch (err) {